class Beanstalk::Deploy

Public Instance Methods

__print_version() click to toggle source
# File lib/beanstalk.rb, line 11
def __print_version
  puts '1.1.4'
end
authenticate_user(profile_name, region) click to toggle source

Authenticates the user & sets up a new ElasticBeanstalk client.

Parameters

profile_name<String>

Name of the profile to use for authentication

region<String>

Name of the AWS region to authenticate against

# File lib/beanstalk.rb, line 55
def authenticate_user(profile_name, region)

  if !ENV['AWS_ACCESS_KEY_ID'].nil? || !ENV['AWS_SECRET_ACCESS_KEY'].nil?
    puts 'Using environment variables for authentication '
    creds = Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'], session_token = nil)
  else
    puts 'Using profile for authentication'
    creds = Aws::SharedCredentials.new(profile_name: profile_name)
  end

  puts 'Setting up ElasticBeanstalk client'
  @eb = Aws::ElasticBeanstalk::Client.new(region: region, credentials: creds)
  puts 'ElasticBeanstalk client setup complete'

end
check_applications(app_name) click to toggle source

Checks if the application exists & creates a new one if it doest.

Parameters

app_name<String>

Name of the application to be used

# File lib/beanstalk.rb, line 77
def check_applications(app_name)
  puts 'Checking applications'
  apps = @eb.describe_applications({
                                       application_names: ["#{app_name}"]
                                   })

  if apps.applications.empty?
    puts "Application does not exist, creating #{app_name}."
    app =@eb.create_application({
                                    application_name: "#{app_name}",
                                    description: "#{app_name}"
                                })
    puts 'Application creating completed'
  end

end
deploy() click to toggle source
# File lib/beanstalk.rb, line 24
def deploy
  app_name = options[:app_name]
  source_dir = options[:source_dir]
  profile_name = options[:profile]
  region = options[:region]
  tags = options[:tags]

  if options[:env_name].size < 40
    env_name = options[:env_name]
  else
    puts 'Environment name too long, truncating...'
    env_name = options[:env_name][0..39].gsub(/\s\w+$/,'...')
  end

  # Authenticates the user & sets up a new ElasticBeanstalk client
  authenticate_user(profile_name, region)

  # Checks if the application exists & creates a new one if it doest
  app = check_applications(app_name)

  # Check if the environment exists then creates or deploys application as appropriate
  deploy_environment(app_name, env_name, source_dir, tags)

end
deploy_environment(app_name, env_name, source_dir, tags) click to toggle source

Check if the environment exists then creates or deploys application as appropriate.

Parameters

eb<Class>

ElasticBeanstalk client

app_name<String>

Name of the application to be used

env_name<String>

Name of the environment to be used

source_dir<String>

Name of the environment to be used

tags<Hash>

Hash of tags to be added to environment

# File lib/beanstalk.rb, line 104
def deploy_environment(app_name, env_name, source_dir, tags)
  puts 'Describing environments '
  envs = @eb.describe_environments({
                                       environment_names: ["#{env_name}"]
                                   })

  environment_available = false
  envs.environments.each do |environemt|
    if environemt.status != 'Terminated'
      environment_available = true
    end
  end

  if (envs.environments.empty?) or (!environment_available)
    puts "CreateEnvironment is starting for #{env_name}."
    system ("cd #{source_dir} && eb create #{env_name} --timeout 60") or raise "Something went wrong"
    # Tag the environment
    unless tags.nil?
      tag_environment(env_name, tags)
    end
  else
    puts "Updating #{env_name}."
    system ("cd #{source_dir} && eb deploy #{env_name} --timeout 60") or raise "Something went wrong"
  end

end
tag_environment(env_name, tags) click to toggle source

Check if the environment exists then creates or deploys application as appropriate. Only called if the environment is being created.

Parameters

env_name<String>

Name of the environment to be used

tags<Hash>

Hash of tags to be added to environment

# File lib/beanstalk.rb, line 139
def tag_environment(env_name, tags)

  puts "Tagging #{env_name}."
  tags.each do |key, value|
    puts "Adding #{key.to_s}=#{value.to_s}"
    system "eb tags #{env_name} -a #{key.to_s}=#{value.to_s}"
  end

end