aws-ami 0.0.3 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,6 @@
3
3
  require "rubygems"
4
4
  require File.join(File.dirname(__FILE__), '..', 'lib', 'aws_ami')
5
5
 
6
-
7
6
  require 'optparse'
8
7
 
9
8
  options = {}
@@ -38,6 +37,10 @@ OptionParser.new do |opts|
38
37
  options[:publish_to_account] = v
39
38
  end
40
39
 
40
+ opts.on("-t", "--timeout [TIMEOUT]", "Change default timeout of waiting install script run finished, default is 600 seconds") do |t|
41
+ options[:timeout] = t
42
+ end
43
+
41
44
  opts.on('-h', '--help') do
42
45
  puts opts
43
46
  exit(0)
@@ -59,16 +62,5 @@ if options[:dry]
59
62
  exit
60
63
  end
61
64
 
62
- [:name, :region, :install_script, :key_name, :base_ami].each do |n|
63
- raise "Must provide #{n}" unless options[n]
64
- end
65
-
66
- # ENV variables:
67
- # AWS_ACCESS_KEY_ID
68
- # AWS_SECRET_ACCESS_KEY
69
-
70
65
  ami = AWS::AMI.new(options)
71
- ami.build(options[:name],
72
- 'KeyName' => options[:key_name],
73
- 'InstallScript' => File.read(options[:install_script]),
74
- "BaseAMI" => YAML.load(File.read(options[:base_ami]))[options[:region]])
66
+ ami.build
@@ -3,31 +3,47 @@ require 'logger'
3
3
 
4
4
  module AWS
5
5
  class AMI
6
+ # name: AMI name
6
7
  # region: aws region of the new AMI
8
+ # key_name: AWS EC2 ssh key name for accessing ec2 instance for debuging problems
9
+ # install_script: a script file can be run on the ec2 instance that is launched from base ami to install packages
10
+ # base_ami: a yml file describes base AMI used for building new AMI for each region
11
+ # timeout: Timeout for running install script, default 600
12
+ # publish_to_account: publish the AMI to this account if need
7
13
  # assume_yes: true for deleting stack when creating image failed
8
14
  def initialize(options={})
9
- @assume_yes = options[:assume_yes]
10
- @region = options[:region]
15
+ @name = options[:name] || raise("Must have a name for the AMI")
16
+ @region = options[:region] || raise("Must specify aws region")
17
+ @key_name = options[:key_name] || raise("Please specify aws ssh key name, so that you can debug problem when something goes wrong")
18
+ @install_script = File.read(options[:install_script]) || raise("Must provide install packages script file")
19
+
20
+ @base_ami = YAML.load(File.read(options[:base_ami]))[@region] || raise("Must provide base ami yml file for each region")
21
+
22
+ @timeout = options[:timeout] || '600'
23
+ @assume_yes = options[:assume_yes] || false
11
24
  @publish_to_account = options[:publish_to_account]
12
25
  @test = options[:test]
13
26
  end
14
27
 
15
- # name: new AMI name, for example: mingle-saas-base
16
- # parameters:
17
- # BaseAMI: the base AMI id for the new AMI, for example:
18
- # "ami-0d153248" for the "ubuntu/images/ebs/ubuntu-precise-12.04-amd64-server-20121001" in us-west-1 region
19
- # KeyName: the ssh key name for accessing the ec2 instance while building the AMI, this is only used when you need to debug problems
20
- # InstallScript: the script installs everything need for the AMI, from 2048 to 16k bytes depending on the base ami provided
21
- def build(name, parameters)
22
- stack = cloudformation.stacks.create("build-#{name}-ami",
28
+ def build
29
+ stack = cloudformation.stacks.create("build-#{@name}-ami",
23
30
  load_formation,
24
31
  :disable_rollback => true,
25
- :parameters => parameters)
32
+ :parameters => {
33
+ 'Timeout' => @timeout,
34
+ 'KeyName' => @key_name,
35
+ 'InstallScript' => @install_script,
36
+ "BaseAMI" => @base_ami
37
+ })
26
38
  logger.info "creating stack"
27
39
  wait_until_created(stack)
28
40
  begin
29
41
  instance_id = stack.resources['EC2Instance'].physical_resource_id
42
+ instance = ec2.instances[instance_id]
30
43
  if @test
44
+ puts "Build AMI Stack created"
45
+ puts "EC2 instance dns name: #{instance.dns_name}, ip address: #{instance.ip_address}"
46
+ puts "continue to create AMI Image? [y/n]"
31
47
  unless gets.strip == 'y'
32
48
  logger.info "delete stack and stop"
33
49
  stack.delete
@@ -36,7 +52,7 @@ module AWS
36
52
  logger.info "continue to create image"
37
53
  end
38
54
  logger.info "creating image"
39
- image = ec2.instances[instance_id].create_image(name, :description => "Created at #{Time.now}")
55
+ image = instance.create_image(@name, :description => "Created at #{Time.now}")
40
56
  sleep 2 until image.exists?
41
57
  logger.info "image #{image.id} state: #{image.state}"
42
58
  sleep 5 until image.state != :pending
@@ -63,7 +79,7 @@ module AWS
63
79
  logger.info "add permissions for #{@publish_to_account}"
64
80
  image.permissions.add(@publish_to_account.gsub(/-/, ''))
65
81
  end
66
- logger.info "Image #{name}[#{image.id}] created"
82
+ logger.info "Image #{@name}[#{image.id}] created"
67
83
  end
68
84
 
69
85
  private
@@ -13,6 +13,11 @@
13
13
  "BaseAMI": {
14
14
  "Description": "Base AMI for the new AMI",
15
15
  "Type": "String"
16
+ },
17
+ "Timeout": {
18
+ "Description": "Timeout for waiting EC2 instance complete the install script",
19
+ "Type": "Number",
20
+ "Default": "600"
16
21
  }
17
22
  },
18
23
 
@@ -56,7 +61,7 @@
56
61
  "Type" : "AWS::CloudFormation::WaitCondition",
57
62
  "Properties" : {
58
63
  "Handle" : { "Ref" : "myWaitHandle" },
59
- "Timeout" : "300"
64
+ "Timeout" : { "Ref" : "Timeout" }
60
65
  }
61
66
  }
62
67
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-ami
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -42,7 +42,7 @@ files:
42
42
  homepage: http://github.com/ThoughtWorksStudios/aws-ami
43
43
  licenses:
44
44
  - MIT
45
- post_install_message: run aws-ami -h for details how to use
45
+ post_install_message: run 'aws-ami -h' for how to use
46
46
  rdoc_options: []
47
47
  require_paths:
48
48
  - lib
@@ -63,6 +63,6 @@ rubyforge_project:
63
63
  rubygems_version: 1.8.24
64
64
  signing_key:
65
65
  specification_version: 3
66
- summary: AWS AMI toolsets for Ruby
66
+ summary: A tool for creating AWS AMI from a base AMI and an install packages script
67
67
  test_files: []
68
68
  has_rdoc: