Class: Jara::Releaser

Inherits:
Object
  • Object
show all
Defined in:
lib/jara/releaser.rb

Defined Under Namespace

Classes: Archiver, Shell, Tarchiver

Instance Method Summary collapse

Constructor Details

#initialize(environment, bucket_name = nil, options = {}) ⇒ Releaser

Returns a new instance of Releaser.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jara/releaser.rb', line 18

def initialize(environment, bucket_name=nil, options={})
  @environment = environment
  @bucket_name = bucket_name
  @re_release = options.fetch(:re_release, false)
  @extra_metadata = options[:metadata] || {}
  @build_command = options[:build_command]
  @app_name = options[:app_name]
  @shell = options[:shell] || Shell.new
  @archiver = create_archiver(options[:archiver])
  @archiver_options = options[:archiver_options] || {}
  @file_system = options[:file_system] || FileUtils
  @s3 = options[:s3]
  @logger = options[:logger] || IoLogger.new($stderr)
  if @environment == 'production'
    if options[:branch]
      raise JaraError, 'Production must be released from master'
    end
    @branch = 'master'
  else
    @branch = options[:branch] || @environment
  end
end

Instance Method Details

#build_artifactObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/jara/releaser.rb', line 41

def build_artifact
  if @environment.nil?
    archive_name = "#{app_name}.#{@archiver.extension}"
    Dir.chdir(project_dir) do
      @archiver.create(@archiver_options.merge(archive_name: archive_name))
    end
    @logger.info('Created test artifact')
    File.join(project_dir, 'build', archive_name)
  elsif (artifact_path = find_local_artifact)
    @logger.warn('An artifact for %s already exists: %s' % [branch_sha[0, 8], File.basename(artifact_path)])
    artifact_path
  else
    date_stamp = Time.now.utc.strftime('%Y%m%d%H%M%S')
    destination_dir = File.join(project_dir, 'build', @environment)
    archive_name = [app_name, @environment, date_stamp, branch_sha[0, 8]].join('-') << '.' << @archiver.extension
    Dir.mktmpdir do |path|
      @shell.exec('git archive --format=tar --prefix=%s/ %s | (cd %s/ && tar xf -)' % [File.basename(path), branch_sha, File.dirname(path)])
      Dir.chdir(path) do
        @logger.info('Checked out %s from branch %s' % [branch_sha[0, 8], @branch])
        if @build_command
          if @build_command.respond_to?(:call)
            @logger.info('Running build command')
            @build_command.call
          else
            @logger.info('Running build command: %s' % @build_command)
            @shell.exec(@build_command)
          end
        end
        @archiver.create(@archiver_options.merge(archive_name: archive_name))
        @file_system.mkdir_p(destination_dir)
        @file_system.cp("build/#{archive_name}", destination_dir)
        @logger.info('Created artifact %s' % archive_name)
      end
    end
    File.join(destination_dir, archive_name)
  end
end

#releaseObject

Raises:



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jara/releaser.rb', line 79

def release
  raise JaraError, 'No environment set' unless @environment
  raise JaraError, 'No bucket name set' unless @bucket_name
  if !@re_release && (obj = find_remote_artifact)
    s3_uri = 's3://%s/%s' % [@bucket_name, obj.key]
    @logger.warn('An artifact for %s already exists: %s' % [branch_sha[0, 8], s3_uri])
    s3_uri
  else
    local_path = find_local_artifact || build_artifact
    upload_artifact(local_path)
  end
end