Module: Stem::Family

Extended by:
Family
Includes:
Util
Included in:
Family
Defined in:
lib/stem/family.rb,
lib/stem/family/member.rb

Defined Under Namespace

Classes: Member

Instance Method Summary collapse

Methods included from Util

#get_filter_opts, #swirl, #tags_to_filter, #tagset_to_hash

Instance Method Details

#ami_for(family, release, architecture = "x86_64") ⇒ Object



7
8
9
10
11
12
13
# File 'lib/stem/family.rb', line 7

def ami_for(family, release, architecture = "x86_64")
  amis = Stem::Image::tagged(:family => family,
                             :release => release, 
                             :architecture => architecture)
  throw "More than one AMI matched release." if amis.length > 1
  amis[0]
end

#build_image(family, config, userdata) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/stem/family.rb', line 44

def build_image(family, config, userdata)
  log = lambda { |msg|
    puts "[#{family}|#{Time.now.to_s}] #{msg}"
  }

  aggregate_hash_options_for_ami!(config)
  sha1 = image_hash(config, userdata)

  log.call "Beginning to build image for #{family}"
  log.call "Config:\n------\n#{ config.inspect }\n-------"
  instance_id = Stem::Instance.launch(config, userdata)

  log.call "Booting #{instance_id} to produce your prototype instance"
  wait_for_stopped instance_id, log

  timestamp = Time.now.utc.iso8601
  image_id = Stem::Image.create("#{family}-#{timestamp.gsub(':', '_')}",
                                instance_id,
                                {
                                  :created => timestamp,
                                  :family => family,
                                  :sha1 => sha1,
                                  :source_ami => config["ami"]
                                })
  log.call "Image ID is #{image_id}"

  wait_for_available(image_id, log)

  log.call "Terminating #{instance_id} now that the image is captured"
  Stem::Instance::destroy(instance_id)
  image_id
end

#describe_members(family, architecture = nil) ⇒ Object



31
32
33
34
# File 'lib/stem/family.rb', line 31

def describe_members family, architecture = nil
  opts = architecture ? { "architecture" => architecture } : {}
  Stem::Image.describe_tagged(opts.merge("family" => family))
end

#image_already_built?(family, config, userdata) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/stem/family.rb', line 77

def image_already_built?(family, config, userdata)
  aggregate_hash_options_for_ami!(config)
  sha1 = image_hash(config, userdata)
  !Stem::Image.tagged(:family => family, :sha1 => sha1).empty?
end

#image_hash(config, userdata) ⇒ Object



83
84
85
# File 'lib/stem/family.rb', line 83

def image_hash(config, userdata)
  Digest::SHA1::hexdigest([config.to_s, userdata].join(' '))
end

#member?(family, ami) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/stem/family.rb', line 20

def member? family, ami
  desc = Stem::Image::describe(ami)
  throw "AMI #{ami} does not exist" if desc.nil?
  tagset_to_hash(desc["tagSet"])["family"] == family
end

#members(family, architecture = nil) ⇒ Object



26
27
28
29
# File 'lib/stem/family.rb', line 26

def members family, architecture = nil
  opts = architecture ? { "architecture" => architecture } : {}
  Stem::Image.tagged(opts.merge("family" => family))
end

#release(family, release_name, *amis) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/stem/family.rb', line 36

def release family, release_name, *amis
  amis.each do |ami|
    throw "#{ami} not part of #{family}" unless member?(family, ami)
  end
  unrelease family, release_name
  amis.each { |ami| Stem::Tag::create(ami, :release => release_name) }
end

#unrelease(family, release_name) ⇒ Object



15
16
17
18
# File 'lib/stem/family.rb', line 15

def unrelease family, release_name
  prev = Stem::Image::tagged(:family => family, :release => release_name)
  prev.each { |ami| Stem::Tag::destroy(ami, :release => release_name) }
end