Module: EbDeployer

Defined in:
lib/eb_deployer.rb,
lib/eb_deployer/utils.rb,
lib/eb_deployer/package.rb,
lib/eb_deployer/version.rb,
lib/eb_deployer/component.rb,
lib/eb_deployer/aws_driver.rb,
lib/eb_deployer/smoke_test.rb,
lib/eb_deployer/application.rb,
lib/eb_deployer/environment.rb,
lib/eb_deployer/event_poller.rb,
lib/eb_deployer/config_loader.rb,
lib/eb_deployer/default_config.rb,
lib/eb_deployer/eb_environment.rb,
lib/eb_deployer/cf_event_source.rb,
lib/eb_deployer/eb_event_source.rb,
lib/eb_deployer/resource_stacks.rb,
lib/eb_deployer/version_cleaner.rb,
lib/eb_deployer/default_component.rb,
lib/eb_deployer/deployment_strategy.rb,
lib/eb_deployer/throttling_handling.rb,
lib/eb_deployer/aws_driver/beanstalk.rb,
lib/eb_deployer/aws_driver/s3_driver.rb,
lib/eb_deployer/cloud_formation_provisioner.rb,
lib/eb_deployer/deployment_strategy/blue_only.rb,
lib/eb_deployer/deployment_strategy/blue_green.rb,
lib/eb_deployer/aws_driver/cloud_formation_driver.rb,
lib/eb_deployer/deployment_strategy/inplace_update.rb,
lib/generators/eb_deployer/install/install_generator.rb

Defined Under Namespace

Modules: AWSDriver, DeploymentStrategy, Generators, Utils Classes: Application, CfEventSource, CloudFormationProvisioner, Component, ConfigLoader, DefaultComponent, DefaultConfig, EbEnvironment, EbEventSource, Environment, EventPoller, Package, ResourceNotInReadyState, ResourceStacks, SmokeTest, ThrottlingHandling, VersionCleaner

Constant Summary collapse

VERSION =
"0.7.0"

Class Method Summary collapse

Class Method Details

.cliObject



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/eb_deployer.rb', line 251

def self.cli
  options = {
    :action => :deploy,
    :environment => 'dev',
    :config_file => 'config/eb_deployer.yml'
  }

  parser = cli_parser(options)
  parser.parse!
  action = options.delete(:action)

  if File.exists?(options[:config_file])
    puts "Found configuration at #{options[:config_file]}."
  else
    puts "Generated default configuration at #{options[:config_file]}."
    DefaultConfig.new(File.basename(Dir.pwd)).write_to(options[:config_file])
    exit(2)
  end

  if !options[:package] && action == :deploy
    puts "Missing options: -p (--package)"
    puts "'eb_deploy --help' for details"
    puts parser
    exit(-1)
  end

  self.send(action, ConfigLoader.new.load(options))
end

.deploy(opts) ⇒ Object

Deploy a package to specified environments on elastic beanstalk

packages will be stored. Note that the string “.packages” will be added as a suffix to your bucket. So, if “thoughtworks.simple” is passed as the bucket name, the actual s3 bucket name will be thoughtworks.simple.packages.

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :application (Symbol)

    required Application name, this used for isolate packages and contribute to your elastic beanstalk cname for environments

  • :environment (Symbol)

    required Environment for same application, e.g. testing, staging, production. This will map to 2 elastic beanstalk environments (env-a-xxx, env-b-xxx) if blue-green deployment strategy specified

  • :package (Symbol)

    required package for the application which should be suitable for elastic beanstalk deploying. For example, a war file should be provided for java solution stacks and a ZIP file should be provided for Rails or Sinatra stack.

  • :option_settings (Symbol) — default: optional

    Elastic Beanstalk settings that will apply to the environments you deploying. Value should be array of hash with format such as:

    [{
       :namespace => 'aws:autoscaling:launchconfiguration',
       :option_name => 'InstanceType',
       :value => 'm1.small' }]
    

    When there are many, using an external yaml file to hold those configuration is recommended. Such as:

    YAML.load(File.read("my_settings_file.yml"))
    

    For all available options take a look at docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-options.html

  • :accepted_healthy_states (Symbol) — default: ['Green']

    If :accepted_healthy_states is specified, EBDeployer will accept provided values when checking health of an environment instead of default value ‘Green’. You can use it to specify additional healthy states, for example: [‘Green’, “Yellow”]

  • :phoenix_mode (Symbol) — default: false

    If phoenix mode is turn on, it will terminate the old elastic beanstalk environment and recreate on deploy. For blue-green deployment it terminate the inactive environment first then recreate it. This is useful to avoiding configuration drift and accumulating state on the EC2 instances. Also it has the benefit of keeping your EC2 instance system package upto date, because every time EC2 instance boot up from AMI it does a system update.

  • :region (Symbol)

    set the region for application deployment (e.g. “us-west-2”, “us-east-1”). See available zones at aws.amazon.com/elasticbeanstalk/faqs/#regions

  • :resources (Symbol)

    If :resources specified, EBDeployer will use the CloudFormation template you provide to create a default CloudFormation stack with name <application_name>-<env-name> for the environment current deploying. Value of resources need to be hash with following keys:

    :template => CloudFormation template file with JSON format
    :policy => CloudFormation policy file with JSON format
    :override_policy => (false) If override_policy is true and a policy file is provided then the
    

    policy will temporarily override any existing policy on the resource stack during this update, otherwise the provided policy will replace any existing policy on the resource stack

    :parameters (or :inputs) => A Hash, input values for the CloudFormation template
    :transforms => A Hash with key map to your CloudFormation
    

    template outputs and value as lambda that return a single or array of elastic beanstalk settings.

    :capabilities => An array. You need set it to ['CAPABILITY_IAM']
    

    if you want to provision IAM Instance Profile.

  • :settings (Symbol)

    See ‘option_settings`

  • :package_bucket (Symbol)

    Name of s3 bucket where uploaded application

  • :smoke_test (Symbol)

    Value should be a proc or a lambda which accept single argument that will passed in as environment DNS name. Smoke test proc or lambda will be called at the end of the deployment for inplace-update deployment strategy. For blue-green deployment it will run after inactive environment update finish and before switching. Defining a smoke test is high recommended for serious usage. The simplest one could just be checking the server is up using curl, e.g.

    :smoke_test => lambda { |host|
      curl_http_code = "curl -k -s -o /dev/null -w \"%{http_code}\" https://#{host}"
      Timeout.timeout(600) do
        while `#{curl_http_code}`.strip != '200'
          sleep 5
        end
      end
    }
    
  • :strategy (Symbol) — default: :blue-green

    There are two options: blue-green or inplace-update. Blue green keep two elastic beanstalk environments and always deploy to inactive one, to achieve zero downtime. inplace-update strategy will only keep one environment, and update the version inplace on deploy. this will save resources but will have downtime.

  • :solution_stack_name (Symbol) — default: "64bit Amazon Linux 2013.09 running Tomcat 7 Java 7"

    The elastic beanstalk solution stack you want to deploy on top of.

  • :tier (Symbol) — default: "WebServer"

    The environment tier. Either “WebServer” or “Worker”

  • :version_label (Symbol)

    required. Version label give the package uploaded a unique identifier. Should use something related to pipeline counter if you have build pipeline setup to build the installer. For the convenience of dev we recommend use md5 digest of the installer so that every time you upload new installer it forms a new version. e.g.

    :version_label => ENV['MY_PIPELINE_COUNTER']
                     || "dev-" + Digest::MD5.file(my_package).hexdigest
    
  • :keep_latest. (Symbol)

    Specifies the maximum number of versions to keep. Older versions are removed and deleted from the S3 source bucket as well. If specified as zero or not specified, all versions will be kept. If a version_prefix is given, only removes version starting with the prefix.

  • :template_name. (Symbol)

    Specifies the environement template you wish to use to build your environment.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/eb_deployer.rb', line 190

def self.deploy(opts)
  if region = opts[:region]
    Aws.config.update(:region => region)
  end

  bs = opts[:bs_driver] || AWSDriver::Beanstalk.new
  bs = ThrottlingHandling.new(bs, Aws::ElasticBeanstalk::Errors::Throttling)
  s3 = opts[:s3_driver] || AWSDriver::S3Driver.new
  cf = opts[:cf_driver] || AWSDriver::CloudFormationDriver.new

  app_name = opts[:application]
  env_name = opts[:environment]
  version_prefix = opts[:version_prefix].to_s.strip
  version_label = "#{version_prefix}#{opts[:version_label].to_s.strip}"

  application = Application.new(app_name, bs, s3, opts[:package_bucket])
  resource_stacks = ResourceStacks.new(opts[:resources],
                                       cf,
                                       opts[:skip_resource_stack_update])

  stack_name = opts[:stack_name] || "#{app_name}-#{env_name}"

  environment = Environment.new(application, env_name, stack_name, bs) do |env|
    env.resource_stacks = resource_stacks
    env.settings = opts[:option_settings] || opts[:settings] || []
    env.inactive_settings = opts[:inactive_settings] || []
    env.creation_opts = {
      :template_name => opts[:template_name],
      :solution_stack => opts[:solution_stack_name],
      :cname_prefix =>  opts[:cname_prefix],
      :smoke_test => opts[:smoke_test],
      :phoenix_mode => opts[:phoenix_mode],
      :accepted_healthy_states => opts[:accepted_healthy_states],
      :blue_green_terminate_inactive => opts[:blue_green_terminate_inactive] || false,
      :blue_green_terminate_inactive_wait => opts[:blue_green_terminate_inactive_wait] || 600,
      :blue_green_terminate_inactive_sleep => opts[:blue_green_terminate_inactive_sleep] || 15,
      :tags => opts[:tags],
      :tier => opts[:tier]
    }
    env.strategy_name = opts[:strategy] || :blue_green
    env.components = opts[:components]
    env.component_under_deploy = opts[:component]
  end

  application.create_version(version_label, opts[:package])
  environment.deploy(version_label)
  application.clean_versions(version_prefix, opts[:keep_latest].to_i || 0)
end

.destroy(opts) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
# File 'lib/eb_deployer.rb', line 239

def self.destroy(opts)
  if region = opts[:region]
    Aws.config.update(:region => region)
  end

  app = opts[:application]
  bs = opts[:bs_driver] || AWSDriver::Beanstalk.new
  s3 = opts[:s3_driver] || AWSDriver::S3Driver.new

  Application.new(app, bs, s3).delete(opts[:environment])
end

.query_resource_output(key, opts) ⇒ Object

Query ouput value of the cloud formation stack

Parameters:

  • key (String)

    CloudFormation output key

  • opts (Hash)

Options Hash (opts):

  • :application (Symbol)

    application name

  • :environment (Symbol)

    environment name (e.g. staging, production)

  • :region (Symbol)

    AWS Region (e.g. “us-west-2”, “us-east-1”)



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/eb_deployer.rb', line 45

def self.query_resource_output(key, opts)
  if region = opts[:region]
    Aws.config.update({
      region: region
    })
  end
  app = opts[:application]
  env_name = opts[:environment]
  cf = opts[:cf_driver] || AWSDriver::CloudFormationDriver.new
  stack_name = opts[:stack_name] || "#{app}-#{env_name}"
  provisioner = CloudFormationProvisioner.new(stack_name, cf)
  provisioner.output(key)
end