-
-
Save CharlieSu/2ea685b0b07857330955 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'optparse' | |
require 'pp' | |
require 'ostruct' | |
class ShApplicationServer | |
AMI_32_BIT="ami-6936fb00" | |
AMI_64_BIT="ami-1136fb78" | |
ZONES = %w(us-east-1a us-east-1b us-east-1c us-east-1d) | |
ENVIRONMENTS= %w(production feature training qa beta beta2 develop demo) | |
INSTANCE_TYPES = %w(c1.xlarge m1.large m1.small) | |
attr_reader :options | |
def initialize(args) | |
@options = OpenStruct.new | |
@options.build_number = nil | |
@options.environment = nil | |
@options.ssh_key = nil | |
@options.ami = AMI_64_BIT | |
@options.instance_type = 'm1.large' | |
@options.run_list = ["recipe[sledgehammer_app]"] | |
@options.security_groups = ["application_layer", "chef_client"] | |
@options.zone = 'us-east-1a' | |
opts = OptionParser.new do |opts| | |
opts.banner = "Usage: \t #{File.basename(__FILE__)} -b BUILD_NUMBER -e ENVIRONMENT [-i INSTANCE_TYPE]" | |
opts.on('-b', '--build BUILD_NUMBER', "Jenkins build number") do |b| | |
@options.build_number = b | |
end | |
opts.on('-e', '--environment ENVIRONMENT', ENVIRONMENTS, "Options: #{ENVIRONMENTS.join(', ')}") do |e| | |
@options.environment = e | |
case @options.environment | |
when 'production', 'beta', 'beta2' | |
@options.ssh_key = 'production' | |
@options.run_list << "recipe[zabbix::client]" | |
@options.run_list << "recipe[splunk::light_forwarder]" | |
@options.security_groups << "production" | |
when 'feature', 'training', 'qa', 'develop', 'demo' | |
@options.ssh_key = 'developers' | |
@options.security_groups << "qa" | |
end | |
end | |
opts.on('-i', '--instance INSTANCE_TYPE', INSTANCE_TYPES, "Default: #{@options.instance_type}" , "Options: #{INSTANCE_TYPES.join(', ')}") do |i| | |
@options.instance_type = i | |
@options.ami = AMI_32_BIT if @options.instance_type == "m1.small" | |
end | |
opts.on('-z', '--zone ZONE', ZONES, "Default: #{@options.zone}", "Options: #{ZONES.join(', ')}") do |z| | |
@options.zone = z | |
end | |
end.parse! | |
end | |
def validate | |
throw :missing_environment if not @options.environment | |
throw :missing_ssh_key if not @options.ssh_key | |
throw :missing_build_number if not @options.build_number | |
end | |
def knife_string | |
validate | |
command = <<EOS | |
BUILD=#{@options.build_number} knife ec2 server create \ | |
-I #{@options.ami} \ | |
-f #{@options.instance_type} \ | |
-S #{@options.ssh_key} \ | |
-E #{@options.environment} \ | |
-G #{@options.security_groups.join(',')} \ | |
-x ubuntu \ | |
-Z #{@options.zone} \ | |
--template-file #{ENV['CHEF_HOME']}/templates/sledgehammer_app.erb \ | |
-i #{ENV['HOME']}/.ssh/#{@options.ssh_key}.pem \ | |
-r #{@options.run_list.join(',')} | |
EOS | |
puts command | |
return command | |
end | |
end | |
runner = ShApplicationServer.new(ARGV) | |
puts | |
`#{runner.knife_string}` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment