Class: Automan::Beanstalk::Deployer

Inherits:
Automan::Base show all
Includes:
Mixins::Utils
Defined in:
lib/automan/beanstalk/deployer.rb

Constant Summary

Constants included from Mixins::AwsCaller

Mixins::AwsCaller::S3_PROTO

Instance Attribute Summary

Attributes inherited from Automan::Base

#logger, #wait

Attributes included from Mixins::AwsCaller

#as, #cfn, #eb, #ec, #ec2, #elb, #r53, #rds, #s3

Instance Method Summary collapse

Methods included from Mixins::Utils

#region_from_az

Methods inherited from Automan::Base

add_option, #log_options, #wait_until

Methods included from Mixins::AwsCaller

#account, #configure_aws, #looks_like_s3_path?, #parse_s3_path, #s3_object_exists?, #s3_read

Constructor Details

#initialize(options = {}) ⇒ Deployer

Returns a new instance of Deployer.



21
22
23
24
# File 'lib/automan/beanstalk/deployer.rb', line 21

def initialize(options={})
  @number_to_keep = 0
  super
end

Instance Method Details

#create_environmentObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/automan/beanstalk/deployer.rb', line 120

def create_environment
  logger.info "creating environment #{eb_environment_name} for #{name} with version #{version_label}"

  opts = {
    application_name: name,
    environment_name: eb_environment_name,
    version_label:    version_label,
    template_name:    configuration_template,
    cname_prefix:     eb_environment_name
  }

  response = eb.create_environment opts

  unless response.successful?
    logger.error "create_environment failed: #{response.error}"
    exit 1
  end

end

#create_or_update_environmentObject



161
162
163
164
165
166
167
168
169
170
# File 'lib/automan/beanstalk/deployer.rb', line 161

def create_or_update_environment
  case environment_status
  when nil, "Terminated"
    create_environment
  when "Ready"
    update_environment
  else
    raise InvalidEnvironmentStatusError, "Could not update environment as it's status is #{environment_status}"
  end
end

#deployObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/automan/beanstalk/deployer.rb', line 172

def deploy
  logger.info "Deploying service."

  if !manifest.nil?
    read_manifest
  end

  log_options

  unless package_exists?
    raise MissingPackageFileError, "package #{package} does not exist."
  end

  ensure_version_exists

  create_or_update_environment

  logger.info "Finished deploying service."
end

#eb_environment_nameObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/automan/beanstalk/deployer.rb', line 51

def eb_environment_name
  # Constraint: Must be from 4 to 23 characters in length.
  # The name can contain only letters, numbers, and hyphens.
  # It cannot start or end with a hyphen.
  env_name = "#{name}-#{environment}"
  unless beanstalk_name.nil?
    env_name = beanstalk_name.dup
  end

  if env_name.length > 23
    env_name = env_name[0,23]
  end

  env_name.gsub!(/[^A-Za-z0-9\-]/, '-')

  env_name.gsub!(/(^\-+)|(\-+$)/, '')

  if env_name.length < 4
    env_name += "-Env"
  end

  env_name
end

#ensure_version_existsObject



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/automan/beanstalk/deployer.rb', line 147

def ensure_version_exists
  version = get_version()

  if version.exists?
    logger.warn "version #{version_label} for #{name} already exists. Not creating."
  else
    version.create(package)

    if number_to_keep > 0
      version.cull_versions(number_to_keep)
    end
  end
end

#environment_statusObject

Possible status states: Launching Updating Ready Terminating Terminated



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/automan/beanstalk/deployer.rb', line 81

def environment_status
  opts = {
    environment_names: [eb_environment_name]
  }

  response = eb.describe_environments opts

  unless response.successful?
    logger.error "describe_environments failed: #{response.error}"
    exit 1
  end

  status = nil
  response.data[:environments].each do |env|
    if env[:environment_name] == eb_environment_name
      status = env[:status]
      break
    end
  end
  status
end

#get_versionObject



140
141
142
143
144
145
# File 'lib/automan/beanstalk/deployer.rb', line 140

def get_version
  v = Automan::Beanstalk::Version.new
  v.application = name
  v.label = version_label
  v
end

#manifest_exists?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/automan/beanstalk/deployer.rb', line 26

def manifest_exists?
  s3_object_exists? manifest
end

#package_exists?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/automan/beanstalk/deployer.rb', line 47

def package_exists?
  s3_object_exists? package
end

#read_manifestObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/automan/beanstalk/deployer.rb', line 30

def read_manifest

  if !manifest_exists?
    raise MissingManifestError, "Manifest #{manifest} does not exist"
  end

  # read manifest from s3 location
  json = s3_read(manifest)

  # parse it from json
  data = JSON.parse json

  # set version_label and package
  self.version_label = data['version_label']
  self.package       = data['package']
end

#update_environmentObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/automan/beanstalk/deployer.rb', line 103

def update_environment
  logger.info "updating environment #{eb_environment_name} with version #{version_label}"

  opts = {
    environment_name: eb_environment_name,
    version_label:    version_label
  }

  response = eb.update_environment opts

  unless response.successful?
    logger.error "update_environment failed: #{response.error}"
    exit 1
  end

end