Top Level Namespace

Defined Under Namespace

Classes: Ami, Cloud, Definition, Instance, Sproutr

Instance Method Summary collapse

Instance Method Details

#ami_done?(ami) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
# File 'lib/sproutr/utilities.rb', line 75

def ami_done?(ami)
  ami_list = @ec2.call "DescribeImages", "Owner" => "self"
  images = ami_list["imagesSet"].select { |img| img["name"] } rescue nil
  images = images.map { |image| image["imageId"] if image["imageState"] == "available" }
  (images.include? ami) ? true : false
end

#clone_ami_config(config, new_ami) ⇒ Object



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
# File 'lib/sproutr/utilities.rb', line 49

def clone_ami_config(config, new_ami)
  throw "No config provided" unless config

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

  devices = []
  sizes = []
  config["blockDeviceMapping"].each do |block_device|
    volume = @ec2.call "DescribeVolumes", "VolumeId" => block_device["ebs"]["volumeId"]
    next if volume["volumeSet"][0]["attachmentSet"][0]["device"] == "/dev/sda1"
    say "Adding volume #{volume["volumeSet"][0]["attachmentSet"][0]["device"]} with a size of #{volume["volumeSet"][0]["size"]}", :green
    devices << volume["volumeSet"][0]["attachmentSet"][0]["device"]
    sizes << volume["volumeSet"][0]["size"]
  end

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

end

#demand(question) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/sproutr/utilities.rb', line 103

def demand(question)
  answer = nil
  while answer.nil? or answer.strip == ""
    answer = ask question, :green
  end
  answer
end

#invoke_launch(config) ⇒ Object



19
20
21
22
# File 'lib/sproutr/utilities.rb', line 19

def invoke_launch(config)
  response = @ec2.call "RunInstances", config
  response["instancesSet"].first["instanceId"]
end

#load_configObject



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/sproutr/utilities.rb', line 1

def load_config
   = "default"
  swirl_config = "#{ENV["HOME"]}/.swirl"
   = .to_sym

  if File.exists?(swirl_config)
    data = YAML.load_file(swirl_config)
  else
    abort("You are required to write a proper YAML config file called .swirl file in your home directory")
  end

  if data.key?()
    data[]
  else
    abort("I don't see the account you're looking for")
  end
end

#parse_tags(tags) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/sproutr/utilities.rb', line 82

def parse_tags(tags)
  if tags && !tags.empty?
    if tags.is_a? Hash
      {"Tag.#.Key" => tags.keys.map(&:to_s),
       "Tag.#.Value" => tags.values.map(&:to_s)}
    elsif tags.is_a? Array
      {
          "Tag.#.Key" => tags.map(&:to_s),
          "Tag.#.Value" => (1..tags.size).map { '' }
      }
    else
      {"Tag.1.Key" => tags.to_s, "Tag.1.Value" => ''}
    end
  end
end

#tag_instance(instance_id, tags) ⇒ Object



98
99
100
101
# File 'lib/sproutr/utilities.rb', line 98

def tag_instance(instance_id, tags)
  instance_id = [instance_id] unless instance_id.is_a? Array
  @ec2.call("CreateTags", parse_tags(tags).merge("ResourceId" => instance_id))
end

#validate_launch_config(config, availability_zone = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sproutr/utilities.rb', line 24

def validate_launch_config(config, availability_zone = nil)
  instance_options = {
      "SecurityGroup.#" => config["groups"] || [],
      "MinCount" => "1",
      "MaxCount" => "1",
      "KeyName" => config["key_name"] || "Production",
      "InstanceType" => config["instance_type"] || "m1.small",
      "ImageId" => config["ami"],
      "AvailabilityZone" => availability_zone || config["placement"]["availabilityZone"]
  }

  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

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

end