Module: Stem::Instance

Extended by:
Instance
Includes:
Util
Included in:
Instance
Defined in:
lib/stem/instance.rb,
lib/stem/instance_types.rb

Constant Summary collapse

Types =
{"m1.small" => {
  "architecture" => "i386"},
 "m1.large" => {
  "architecture" => "x86_64"},
 "m1.xlarge" => {
  "architecture" => "x86_64"},
 "t1.micro" => {
  "architecture" => "i386"},
 "m2.xlarge" => {
  "architecture" => "x86_64"},
 "m2.2xlarge" => {
  "architecture" => "x86_64"},
 "m2.4xlarge" => {
  "architecture" => "x86_64"},
 "c1.medium" => {
  "architecture" => "i386"},
 "c1.xlarge" => {
  "architecture" => "x86_64"},
 "cc1.4xlarge" => {
  "architecture" => "x86_64"},
 "cg1.4xlarge" => {
  "architecture" => "x86_64"}
}

Instance Method Summary collapse

Methods included from Util

#get_filter_opts, #swirl, #tags_to_filter, #tagset_to_hash

Instance Method Details

#console_output(instance) ⇒ Object



68
69
70
# File 'lib/stem/instance.rb', line 68

def console_output instance
  swirl.call("GetConsoleOutput", "InstanceId" => instance)
end

#describe(instance) ⇒ Object



63
64
65
66
# File 'lib/stem/instance.rb', line 63

def describe instance
  throw "You must provide an instance ID to describe" unless instance
  swirl.call("DescribeInstances", "InstanceId" => instance)["reservationSet"][0]["instancesSet"][0]
end

#destroy(instance_id) ⇒ Object



51
52
53
# File 'lib/stem/instance.rb', line 51

def destroy instance_id
  swirl.call "TerminateInstances", "InstanceId" => instance_id
end

#launch(config, userdata = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/stem/instance.rb', line 6

def launch config, userdata = nil
  throw "No config provided" unless config
  if config['tags'] && config['tags'] != {}
    raise Stem::Deprecated.new "Tags should be created separately due to AWS eventual consistency issues"
  end

  config = aggregate_hash_options_for_ami!(config)
  ami = config["ami"]

  opt = {
    "SecurityGroup.#" => config["groups"] || [],
    "MinCount"        => "1",
    "MaxCount"        => "1",
    "KeyName"         => config["key_name"] || "default",
    "InstanceType"    => config["instance_type"] || "m1.small",
    "ImageId"         => ami
  }

  opt.merge! "Placement.AvailabilityZone" => config["availability_zone"] if config["availability_zone"]

  if config["volumes"]
    devices = []
    sizes = []
    config["volumes"].each do |v|
      puts "Adding a volume of #{v["size"]} to be mounted at #{v["device"]}."
      devices << v["device"]
      sizes << v["size"].to_s
    end

    opt.merge! "BlockDeviceMapping.#.Ebs.VolumeSize" => sizes,
               "BlockDeviceMapping.#.DeviceName" => devices
  end

  if userdata
    puts "Userdata provided, encoded and sent to the instance."
    opt.merge!({ "UserData" => Base64.encode64(userdata)})
  end

  swirl.call("RunInstances", opt)["instancesSet"].first["instanceId"]
end

#listObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/stem/instance.rb', line 72

def list
  instances = swirl.call("DescribeInstances")

  lookup = {}
  instances["reservationSet"].each do |r|
    r["instancesSet"].each do |i|
      lookup[i["imageId"]] = nil
    end
  end
  amis = swirl.call("DescribeImages", "ImageId" => lookup.keys)["imagesSet"]

  amis.each do |ami|
    name = ami["name"] || ami["imageId"]
    if !ami["description"] || ami["description"][0..1] != "%%"
      # only truncate ugly names from other people (never truncate ours)
      name.gsub!(/^(.{8}).+(.{8})/) { $1 + "..." + $2 }
      name = "(foreign) " + name
    end
    lookup[ami["imageId"]] = name
  end

  reservations = instances["reservationSet"]
  unless reservations.nil? or reservations.empty?
    puts "------------------------------------------"
    puts "Instances"
    puts "------------------------------------------"
    reservations.each do |r|
      groups = r["groupSet"].map { |g| g["groupId"] }.join(",")
      r["instancesSet"].each do |i|
          name = lookup[i["imageId"]]
          puts "%-15s %-15s %-15s %-20s %s" % [ i["instanceId"], i["ipAddress"] || "no ip", i["instanceState"]["name"], groups, name ]
      end
    end
  end

  result = swirl.call "DescribeImages", "Owner" => "self"
  images = result["imagesSet"].select { |img| img["name"] }
  unless images.nil? or images.empty?
    puts "------------------------------------------"
    puts "AMIs"
    puts "------------------------------------------"
    iwidth = images.map { |img| img["name"].length }.max + 1
    images.each do |img|
      puts "%-#{iwidth}s %s" % [ img["name"], img["imageId"] ]
    end
  end
end

#restart(instance_id) ⇒ Object



47
48
49
# File 'lib/stem/instance.rb', line 47

def restart instance_id
  swirl.call "RebootInstances", "InstanceId" => instance_id
end

#start(instance_id) ⇒ Object



59
60
61
# File 'lib/stem/instance.rb', line 59

def start instance_id
  swirl.call "StartInstances", "InstanceId" => instance_id
end

#stop(instance_id, force = false) ⇒ Object



55
56
57
# File 'lib/stem/instance.rb', line 55

def stop instance_id, force = false
  swirl.call "StopInstances", "InstanceId" => instance_id, "Force" => force.to_s
end

#tagged(*tags) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/stem/instance.rb', line 120

def tagged *tags
  return if tags.empty?
  opts = { "tag-key" => tags.map {|t| t.to_s } }
  instances = swirl.call "DescribeInstances", get_filter_opts(opts)

  ids = []
  instances["reservationSet"].each do |r|
    r["instancesSet"].each do |i|
      ids << i["instanceId"]
    end
  end
  ids
end