Class: AWSCarb::Services::Ec2

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/aws-carb/services/ec2.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



9
10
11
# File 'lib/aws-carb/services/ec2.rb', line 9

def instance
  @instance
end

Instance Method Details

#client(config) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/aws-carb/services/ec2.rb', line 11

def client(config)
  @config = config
  @instance = nil

  ShellSpinner "# configuring ec2 session", false do
    begin
      @client = AWS::EC2.new(config[:ec2])
      @client.regions[@config.find_with_context(:region, :ec2)]
      puts
    rescue => e
      puts "error: failed to create ec2 session, check that you're using a valid region!"
      die e
    end
  end
end

#create_instanceObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/aws-carb/services/ec2.rb', line 27

def create_instance

  instance = nil

  ShellSpinner "# creating instance", false do

    # FIXME: this is naff

    begin
      allowed_ec2_parameters = [
        :count,
        :iam_instance_profile,
        :block_device_mappings,
        :virtual_name,
        :device_name,
        :ebs,
        :snapshot_id,
        :volume_size,
        :delete_on_termination,
        :volume_type,
        :iops,
        :no_device,
        :monitoring_enabled,
        :availability_zone,
        :image_id,
        :key_name,
        :key_pair,
        :security_groups,
        :security_group_ids,
        :user_data,
        :instance_type,
        :kernel_id,
        :ramdisk_id,
        :disable_api_termination,
        :instance_initiated_shutdown_behavior,
        :subnet,
        :private_ip_address,
        :dedicated_tenancy,
        :ebs_optimized,
        :associate_public_ip_address,
      ]

      ec2_config = {}

      allowed_ec2_parameters.each do |param|
        ec2_config[param] = @config[:ec2][param] if @config[:ec2][param]
      end

      @instance = @client.instances.create(ec2_config)
    rescue => e
      puts "# failed to create new ec2 instance:"
      die e
    end
  end

  puts

  ShellSpinner "# awaiting build completion", false do
    sleep 1 until @instance.status != :pending
  end

  puts

  ShellSpinner "# awaiting running state", false do
    sleep 1 until @instance.status == :running
  end

  puts
end