Sha256: 5bf85d827ec7beea2cb1115ee72f32bcb64856ccc98d59dae8334c9227ee982d

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

module AppBuilder
  class Uploader < Base
    attr_accessor :builder

    class << self
      def upload(conf)
        new(conf).upload
      end
    end

    def initialize(conf = nil)
      case conf
      when Archiver
        @builder = Builder.new(conf)
        conf = conf.config
      when Builder
        @builder = conf
        conf = conf.config
      end
      super(conf)
      @builder ||= Builder.new(config)
    end

    def upload
      upload_proc = s3? ? method(:upload_to_s3) : method(:upload_to_server)
      builder.build
      execute_with_hooks(:upload) do
        upload_proc.call(builded_src_path, remote_src_file)
        generate_manifest
        upload_proc.call(builded_manifest_path, remote_manifest_file)
      end
    end

    def upload_to_s3(local, remote)
      s3_client.put_object(
        bucket: upload_id,
        key:    remote,
        body:   File.open(local),
      )
      log(:info, "Uploaded #{local} to #{config.uploaded_url(remote)}")
    end

    def upload_to_server(local, remote)
      resource_server.upload(local, File.join(resource_document_root, remote))
      log(:info, "Uploaded #{local} to #{config.uploaded_url(remote)}")
    end

    def generate_manifest
      checksum = `openssl sha256 #{builded_src_path} | awk -F"=" '{ print $2 }'`.strip
      manifest = ERB.new(File.read(manifest_template_path)).result(binding)
      File.open(builded_manifest_path, "w") { |f| f.write(manifest) }
    end

    private

      def s3?
        upload_type == :s3
      end

      def s3_client
        @s3_client ||= Aws::S3::Client.new(
          region:        region,
          access_key_id: access_key_id,
          secret_access_key: secret_access_key
        )
      end

      def resource_server
        @resource_server ||= Server.new(
          resource_host,
          user:    resource_user,
          options: resource_ssh_options,
          logger:  logger
        )
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
app_builder-0.1.10 lib/app_builder/uploader.rb
app_builder-0.1.9 lib/app_builder/uploader.rb