Class: S3Deployer

Inherits:
Object
  • Object
show all
Defined in:
lib/s3_deployer.rb,
lib/s3_deployer/color.rb,
lib/s3_deployer/config.rb,
lib/s3_deployer/version.rb

Defined Under Namespace

Classes: Color, Config, Stream

Constant Summary collapse

DATE_FORMAT =
"%Y%m%d%H%M%S"
CURRENT_REVISION =
"CURRENT_REVISION"
RETRY_TIMES =
[1, 3, 8].freeze
VERSION =
"0.7.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



22
23
24
# File 'lib/s3_deployer.rb', line 22

def config
  @config
end

Class Method Details

.changes(from, to) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/s3_deployer.rb', line 116

def changes(from, to)
  from_sha = sha_of_revision(from)
  to_sha = sha_of_revision(to)
  if from_sha && to_sha
    `git log --oneline --reverse #{from_sha}...#{to_sha}`.split("\n").map(&:strip)
  else
    []
  end
end

.configure(&block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/s3_deployer.rb', line 24

def configure(&block)
  @config = Config.new
  @config.instance_eval(&block)
  @config.apply_environment_settings!

  Aws.config.update({
    region: config.region,
    credentials: Aws::Credentials.new(config.access_key_id, config.secret_access_key),
  })
end

.currentObject



84
85
86
87
88
89
90
91
# File 'lib/s3_deployer.rb', line 84

def current
  current_revision = get_current_revision
  if current_revision
    puts "Current revision: #{current_revision} - #{get_datetime_from_revision(current_revision)}"
  else
    puts "There is no information about the current revision"
  end
end

.deploy!Object



40
41
42
43
44
45
46
# File 'lib/s3_deployer.rb', line 40

def deploy!
  revision = config.revision || time_zone.now.strftime(DATE_FORMAT)
  config.before_deploy[revision] if config.before_deploy
  stage!(revision)
  switch!(revision)
  config.after_deploy[revision] if config.after_deploy
end

.execute(cmd) ⇒ Object



35
36
37
38
# File 'lib/s3_deployer.rb', line 35

def execute(cmd)
  puts "Running '#{cmd}'"
  system(cmd, out: $stdout, err: :out)
end

.listObject



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/s3_deployer.rb', line 104

def list
  puts "Getting the list of deployed revisions..."
  current_revision = get_current_revision
  get_list_of_revisions.each do |rev|
    datetime = get_datetime_from_revision(rev)
    sha = shas_by_revisions[rev]
    title = sha ? `git show -s --format=%s #{sha}`.strip : nil
    string = "#{rev} - #{datetime} #{sha ? " - #{sha[0..7]}" : ""} #{title ? "(#{title})" : ""} #{" <= current" if rev == current_revision}"
    puts string
  end
end

.normalize_revision(revision) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/s3_deployer.rb', line 93

def normalize_revision(revision)
  if revision && !revision.empty?
    datetime = get_datetime_from_revision(revision)
    if datetime
      revision
    else
      shas_by_revisions.detect { |k, v| v.start_with?(revision) }.first
    end
  end
end

.sha_of_revision(revision) ⇒ Object



126
127
128
# File 'lib/s3_deployer.rb', line 126

def sha_of_revision(revision)
  shas_by_revisions[revision]
end

.stage!(revision = (config.revision || time_zone.now.strftime(DATE_FORMAT))) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/s3_deployer.rb', line 48

def stage!(revision = (config.revision || time_zone.now.strftime(DATE_FORMAT)))
  puts "Staging #{colorize(:green, revision)}"
  config.before_stage[revision] if config.before_stage
  copy_files_to_s3(revision)
  store_git_hash(revision)
  config.after_stage[revision] if config.after_stage
end

.switch!(revision = config.revision) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/s3_deployer.rb', line 56

def switch!(revision = config.revision)
  current_revision = get_current_revision
  current_sha = sha_of_revision(current_revision)
  sha = sha_of_revision(revision)
  puts "Switching from #{colorize(:green, current_revision)} (#{colorize(:yellow, current_sha && current_sha[0..7])}) " +
    "to #{colorize(:green, revision)} (#{colorize(:yellow, sha && sha[0..7])})"
  if !revision || revision.strip.empty?
    warn "You must specify the revision by REVISION env variable"
    exit(1)
  end
  config.before_switch[current_revision, revision] if config.before_switch
  prefix = config.app_path.empty? ? revision : File.join(revisions_path, revision)
  list_of_objects = []
  Aws::S3::Resource.new.bucket(config.bucket).objects(prefix: prefix).each do |object_summary|
    list_of_objects << object_summary
  end
  Parallel.each(list_of_objects, in_threads: 20) do |object_summary|
    object = object_summary.object
    target_path = config.app_path.empty? ? @config.current_path : File.join(config.app_path, @config.current_path)
    path = object.key.gsub(prefix, target_path)
    value = object.get.body.read
    value = object.content_encoding == "gzip" ? decompress(value) : value
    store_value(path, value)
  end
  store_current_revision(revision)
  config.after_switch[current_revision, revision] if config.after_switch
end