build-cloud 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,62 @@
1
+
2
+ class BuildCloud::DbParameterGroup
3
+
4
+ include ::BuildCloud::Component
5
+
6
+ @@objects = []
7
+
8
+ def initialize ( fog_interfaces, log, options = {} )
9
+
10
+ @rds = fog_interfaces[:rds]
11
+ @log = log
12
+ @options = options
13
+
14
+ @log.debug( options.inspect )
15
+
16
+ required_options(:family, :description, :id, :params)
17
+
18
+ end
19
+
20
+ def create
21
+
22
+ return if exists?
23
+
24
+ @log.info( "Creating DB Parameter Group #{@options[:id]}" )
25
+
26
+ options = @options.dup
27
+
28
+ param_group = @rds.create_db_parameter_group(options[:id], options[:family], options[:description])
29
+
30
+ @log.debug( param_group.inspect )
31
+
32
+ params = @rds.modify_db_parameter_group options[:id], options[:params].collect! { |c|
33
+ {
34
+ 'ParameterName' => c[:param_name],
35
+ 'ParameterValue' => c[:param_value],
36
+ 'ApplyMethod' => c[:apply_method],
37
+ }
38
+ }
39
+
40
+ @log.debug( params.inspect )
41
+
42
+ end
43
+
44
+ def read
45
+ @rds.parameter_groups.select { |g| g.id == "#{@options[:id]}".downcase }.first
46
+ end
47
+
48
+ alias_method :fog_object, :read
49
+
50
+ def delete
51
+
52
+ return unless exists?
53
+
54
+ @log.info( "Deleting DB Parameter Group #{@options[:id]}" )
55
+
56
+ puts fog_object.inspect
57
+ fog_object.destroy
58
+
59
+ end
60
+
61
+ end
62
+
@@ -0,0 +1,52 @@
1
+
2
+ class BuildCloud::DbParameterGroup
3
+
4
+ include ::BuildCloud::Component
5
+
6
+ @@objects = []
7
+
8
+ def initialize ( fog_interfaces, log, options = {} )
9
+
10
+ @rds = fog_interfaces[:rds]
11
+ @log = log
12
+ @options = options
13
+
14
+ @log.debug( options.inspect )
15
+
16
+ required_options(:family, :description, :id)
17
+
18
+ end
19
+
20
+ def create
21
+
22
+ return if exists?
23
+
24
+ @log.info( "Creating DB Parameter Group #{@options[:id]}" )
25
+
26
+ options = @options.dup
27
+
28
+ param_group = @rds.create_db_parameter_group(options[:id], options[:family], options[:description])
29
+
30
+ @log.debug( param_group.inspect )
31
+
32
+ end
33
+
34
+ def read
35
+ @rds.parameter_groups.select { |g| g.id == "#{@options[:name]}" }.first
36
+ end
37
+
38
+ alias_method :fog_object, :read
39
+
40
+ def delete
41
+
42
+ return unless exists?
43
+
44
+ @log.info( "Deleting DB Parameter Group #{@options[:name]}" )
45
+
46
+ puts fog_object.inspect
47
+ fog_object.destroy
48
+
49
+ end
50
+
51
+ end
52
+
@@ -0,0 +1,64 @@
1
+
2
+ class BuildCloud::DbSubnetGroup
3
+
4
+ include ::BuildCloud::Component
5
+
6
+ @@objects = []
7
+
8
+ def initialize ( fog_interfaces, log, options = {} )
9
+
10
+ @rds = fog_interfaces[:rds]
11
+ @log = log
12
+ @options = options
13
+
14
+ @log.debug( options.inspect )
15
+
16
+ required_options(:name)
17
+ require_one_of(:subnet_ids, :subnet_names)
18
+
19
+ end
20
+
21
+ def create
22
+
23
+ return if exists?
24
+
25
+ @log.info( "Creating DB Subnet Group #{@options[:id]}" )
26
+
27
+ options = @options.dup
28
+
29
+ unless options[:subnet_ids]
30
+
31
+ options[:subnet_ids] = []
32
+
33
+ options[:subnet_names].each do |sn|
34
+ options[:subnet_ids] << BuildCloud::Subnet.get_id_by_name( sn )
35
+ end
36
+
37
+ options.delete(:subnet_names)
38
+
39
+ end
40
+
41
+ group = @rds.create_db_subnet_group( options[:name], options[:subnet_ids] )
42
+
43
+ @log.debug( group.inspect )
44
+
45
+ end
46
+
47
+ def read
48
+ @rds.subnet_groups.select { |g| g.id == @options[:name] }.first
49
+ end
50
+
51
+ alias_method :fog_object, :read
52
+
53
+ def delete
54
+
55
+ return unless exists?
56
+
57
+ @log.info( "Deleting DB Subnet Group #{@options[:name]}" )
58
+
59
+ fog_object.destroy
60
+
61
+ end
62
+
63
+ end
64
+
@@ -0,0 +1,84 @@
1
+ class BuildCloud::IAMRole
2
+
3
+ require 'json'
4
+
5
+ include ::BuildCloud::Component
6
+
7
+ @@objects = []
8
+
9
+ def initialize ( fog_interfaces, log, options = {} )
10
+
11
+ @iam = fog_interfaces[:iam]
12
+ @log = log
13
+ @options = options
14
+
15
+ @log.debug( options.inspect )
16
+
17
+ required_options(:rolename, :assume_role_policy_document)
18
+
19
+ end
20
+
21
+ def create
22
+
23
+ return if exists?
24
+
25
+ @log.info( "Creating new IAM role for #{@options[:rolename]}" )
26
+
27
+ policies = @options.delete(:policies)
28
+
29
+ # Genuinely don't think I've understood the data model with
30
+ # this stuff. In particular how roles, instance profiles etc. relate
31
+ #
32
+ # It does what we need right now though, and can be revisited if necessary
33
+
34
+ role = @iam.roles.new( @options )
35
+ role.save
36
+
37
+ @log.debug( role.inspect )
38
+
39
+ policies.each do |policy|
40
+
41
+ @log.debug( "Adding policy #{policy}" )
42
+
43
+ policy_document = JSON.parse( policy[:policy_document] )
44
+
45
+ @iam.put_role_policy( @options[:rolename], policy[:policy_name],
46
+ policy_document )
47
+
48
+ @iam.create_instance_profile( @options[:rolename] )
49
+ @iam.add_role_to_instance_profile( @options[:rolename], @options[:rolename] )
50
+
51
+ end
52
+
53
+ end
54
+
55
+ def read
56
+ @iam.roles.select { |r| r.rolename == @options[:rolename] }.first
57
+ end
58
+
59
+ alias_method :fog_object, :read
60
+
61
+ def delete
62
+
63
+ return unless exists?
64
+
65
+ @log.info( "Deleting IAM role for #{@options[:rolename]}" )
66
+
67
+ instance_profiles = @iam.list_instance_profiles_for_role( @options[:rolename] ).body['InstanceProfiles'].map { |k| k['InstanceProfileName'] }
68
+ instance_profiles.each do |ip|
69
+ @iam.delete_instance_profile( ip )
70
+ @iam.remove_role_from_instance_profile( @options[:rolename], ip )
71
+ end
72
+
73
+ policies = @iam.list_role_policies( @options[:rolename] ).body['PolicyNames']
74
+ policies.each do |policy|
75
+ @iam.delete_role_policy( @options[:rolename], policy )
76
+ end
77
+
78
+
79
+ fog_object.destroy
80
+
81
+ end
82
+
83
+ end
84
+
@@ -0,0 +1,140 @@
1
+ class BuildCloud::Instance
2
+
3
+ include ::BuildCloud::Component
4
+
5
+ @@objects = []
6
+
7
+ def self.get_id_by_name( name )
8
+
9
+ instance = self.search( :name => name ).first
10
+
11
+ unless instance
12
+ raise "Couldn't get an instance object for #{name} - is it defined?"
13
+ end
14
+
15
+ instance_fog = instance.read
16
+
17
+ unless instance_fog
18
+ raise "Couldn't get an instance fog object for #{name} - is it created?"
19
+ end
20
+
21
+ instance_fog.id
22
+
23
+ end
24
+
25
+ def initialize ( fog_interfaces, log, options = {} )
26
+
27
+ @ec2 = fog_interfaces[:compute]
28
+ @log = log
29
+ @options = options
30
+
31
+ @log.debug( options.inspect )
32
+
33
+ required_options(:image_id, :flavor_id, :private_ip_address, :name)
34
+ require_one_of(:security_group_ids, :security_group_names, :network_interfaces)
35
+ require_one_of(:subnet_id, :subnet_name, :network_interfaces)
36
+ #require_one_of(:network_interfaces, :private_ip_address)
37
+ require_one_of(:vpc_id, :vpc_name)
38
+
39
+ end
40
+
41
+ def ready_timeout
42
+ 5 * 60 # some instances (eg big EBS root vols) can take a while
43
+ end
44
+
45
+ def create
46
+
47
+ return if exists?
48
+
49
+ @log.info( "Creating instance #{@options[:name]}" )
50
+
51
+ options = @options.dup
52
+
53
+ if options[:security_group_names] or options[:security_group_ids]
54
+ unless options[:security_group_ids]
55
+
56
+ options[:security_group_ids] = []
57
+
58
+ options[:security_group_names].each do |sg|
59
+ options[:security_group_ids] << BuildCloud::SecurityGroup.get_id_by_name( sg )
60
+ end
61
+
62
+ options.delete(:security_group_names)
63
+
64
+ end
65
+ end
66
+
67
+ if options[:subnet_id] or options[:subnet_name]
68
+ unless options[:subnet_id]
69
+
70
+ options[:subnet_id] = BuildCloud::Subnet.get_id_by_name( options[:subnet_name] )
71
+ options.delete(:subnet_name)
72
+
73
+ end
74
+ end
75
+
76
+ unless options[:vpc_id]
77
+
78
+ options[:vpc_id] = BuildCloud::VPC.get_id_by_name( options[:vpc_name] )
79
+ options.delete(:vpc_name)
80
+
81
+ end
82
+
83
+ if options[:private_ip_address] and options[:network_interfaces]
84
+ puts "WARNING: InvalidParameterCombination => Network interfaces and an instance-level private IP address should not be specified on the same request"
85
+ puts "Using Network interface"
86
+ options.delete(:private_ip_address)
87
+ end
88
+
89
+ if options[:subnet_id] and options[:network_interfaces]
90
+ puts "WARNING: InvalidParameterCombination => Network interfaces and subnet_ids should not be specified on the same request"
91
+ puts "Using Network interface"
92
+ options.delete(:subnet_id)
93
+ end
94
+
95
+ options[:user_data] = JSON.generate( @options[:user_data] )
96
+
97
+ options[:network_interfaces].each { |iface|
98
+ if ! iface[:network_interface_name].nil?
99
+ interface_id = BuildCloud::NetworkInterface.get_id_by_name( iface[:network_interface_name] )
100
+ iface['NetworkInterfaceId'] = interface_id
101
+ iface.delete(:network_interface_name)
102
+ end
103
+ } unless options[:network_interfaces].nil?
104
+
105
+ @log.debug( options.inspect )
106
+
107
+ instance = @ec2.servers.new( options )
108
+ instance.save
109
+
110
+ options[:tags].each do | tag |
111
+ attributes = {}
112
+ attributes[:resource_id] = instance.id.to_s
113
+ attributes[:key] = tag[:key]
114
+ attributes[:value] = tag[:value]
115
+ new_tag = @ec2.tags.new( attributes )
116
+ new_tag.save
117
+ end unless options[:tags].empty? or options[:tags].nil?
118
+
119
+ @log.debug( instance.inspect )
120
+
121
+ end
122
+
123
+ def read
124
+ instances = @ec2.servers.select{ |l| l.tags['Name'] == @options[:name]}
125
+ instances.select{ |i| i.state =~ /(running|pending)/ }.first
126
+ end
127
+
128
+ alias_method :fog_object, :read
129
+
130
+ def delete
131
+
132
+ return unless exists?
133
+
134
+ @log.info( "Deleting instance #{@options[:name]}" )
135
+
136
+ fog_object.destroy
137
+
138
+ end
139
+
140
+ end
@@ -0,0 +1,86 @@
1
+ class BuildCloud::InternetGateway
2
+
3
+ include ::BuildCloud::Component
4
+
5
+ @@objects = []
6
+
7
+ def self.get_id_by_name( name )
8
+
9
+ internet_gateway = self.search( :name => name ).first
10
+
11
+ unless internet_gateway
12
+ raise "Couldn't get an InternetGateway object for #{name} - is it defined?"
13
+ end
14
+
15
+ internet_gateway_fog = internet_gateway.read
16
+
17
+ unless internet_gateway_fog
18
+ raise "Couldn't get an InternetGateway fog object for #{name} - is it created?"
19
+ end
20
+
21
+ internet_gateway_fog.internet_gateway_id
22
+
23
+ end
24
+
25
+ def initialize ( fog_interfaces, log, options = {} )
26
+
27
+ @compute = fog_interfaces[:compute]
28
+ @log = log
29
+ @options = options
30
+
31
+ @log.debug( options.inspect )
32
+
33
+ required_options(:name)
34
+ require_one_of(:vpc_id, :vpc_name)
35
+
36
+ end
37
+
38
+ def create
39
+
40
+ return if exists?
41
+
42
+ @log.info("Creating Internet Gateway #{@options[:name]}")
43
+
44
+ options = @options.dup
45
+
46
+ unless options[:vpc_id]
47
+
48
+ options[:vpc_id] = BuildCloud::VPC.get_id_by_name( options[:vpc_name] )
49
+ options.delete(:vpc_name)
50
+
51
+ end
52
+
53
+ options[:tags] = { 'Name' => options.delete(:name) }
54
+
55
+ ig = @compute.internet_gateways.new()
56
+ ig.save
57
+
58
+ @log.debug(ig.inspect)
59
+
60
+ @compute.create_tags( ig.id, options[:tags] )
61
+
62
+ ig.attach(options[:vpc_id])
63
+
64
+ end
65
+
66
+ def read
67
+ @compute.internet_gateways.select { |r| r.tag_set['Name'] == @options[:name] }.first
68
+ end
69
+
70
+ alias_method :fog_object, :read
71
+
72
+ def delete
73
+
74
+ return unless exists?
75
+
76
+ @log.info("Deleting Internet Gateway #{@options[:name]}")
77
+
78
+ read.detach(read.attachment_set['vpcId'])
79
+
80
+ fog_object.destroy
81
+
82
+ end
83
+
84
+ end
85
+
86
+
@@ -0,0 +1,68 @@
1
+ require 'json'
2
+
3
+ class BuildCloud::LaunchConfiguration
4
+
5
+ include ::BuildCloud::Component
6
+
7
+ @@objects = []
8
+
9
+ def initialize ( fog_interfaces, log, options = {} )
10
+
11
+ @as = fog_interfaces[:as]
12
+ @log = log
13
+ @options = options
14
+
15
+ @log.debug( options.inspect )
16
+
17
+ required_options(:id, :image_id, :key_name, :user_data, :instance_type)
18
+ require_one_of(:security_groups, :security_group_names)
19
+
20
+ end
21
+
22
+ def create
23
+
24
+ return if exists?
25
+
26
+ @log.info( "Creating launch configuration #{@options[:id]}" )
27
+
28
+ options = @options.dup
29
+
30
+ unless options[:security_groups]
31
+
32
+ options[:security_groups] = []
33
+
34
+ options[:security_group_names].each do |sg|
35
+ options[:security_groups] << BuildCloud::SecurityGroup.get_id_by_name( sg )
36
+ end
37
+
38
+ options.delete(:security_group_names)
39
+
40
+ end
41
+
42
+ options[:user_data] = JSON.generate( @options[:user_data] )
43
+
44
+ launch_config = @as.configurations.new( options )
45
+ launch_config.save
46
+
47
+ @log.debug( launch_config.inspect )
48
+
49
+ end
50
+
51
+ def read
52
+ @as.configurations.select { |lc| lc.id == @options[:id] }.first
53
+ end
54
+
55
+ alias_method :fog_object, :read
56
+
57
+ def delete
58
+
59
+ return unless exists?
60
+
61
+ @log.info( "Deleting Launch Configuration #{@options[:id]}" )
62
+
63
+ fog_object.destroy
64
+
65
+ end
66
+
67
+ end
68
+
@@ -0,0 +1,119 @@
1
+ class BuildCloud::LoadBalancer
2
+
3
+ include ::BuildCloud::Component
4
+
5
+ @@objects = []
6
+
7
+ def initialize ( fog_interfaces, log, options = {} )
8
+
9
+ @elb = fog_interfaces[:elb]
10
+ @log = log
11
+ @options = options
12
+
13
+ @log.debug( options.inspect )
14
+
15
+ required_options(:id, :listeners)
16
+ require_one_of(:security_groups, :security_group_names)
17
+ require_one_of(:subnet_ids, :subnet_names)
18
+ require_one_of(:vpc_id, :vpc_name)
19
+
20
+ end
21
+
22
+ def create
23
+
24
+ return if exists?
25
+
26
+ @log.info( "Creating load balancer #{@options[:id]}" )
27
+
28
+ options = @options.dup
29
+
30
+ unless options[:security_groups]
31
+
32
+ options[:security_groups] = []
33
+
34
+ options[:security_group_names].each do |sg|
35
+ options[:security_groups] << BuildCloud::SecurityGroup.get_id_by_name( sg )
36
+ end
37
+
38
+ options.delete(:security_group_names)
39
+
40
+ end
41
+
42
+ unless options[:subnet_ids]
43
+
44
+ options[:subnet_ids] = []
45
+
46
+ options[:subnet_names].each do |sn|
47
+ options[:subnet_ids] << BuildCloud::Subnet.get_id_by_name( sn )
48
+ end
49
+
50
+ options.delete(:subnet_names)
51
+
52
+ end
53
+
54
+ unless options[:vpc_id]
55
+
56
+ options[:vpc_id] = BuildCloud::VPC.get_id_by_name( options[:vpc_name] )
57
+ options.delete(:vpc_name)
58
+
59
+ end
60
+
61
+ options.delete(:listeners)
62
+
63
+ elb = @elb.load_balancers.new( options )
64
+ elb.save
65
+
66
+ # Remove first port 80 listener - we can add it back if we need
67
+ elb.listeners.select { |l| l.instance_port == 80 }.first.destroy
68
+
69
+ @options[:listeners].each do |listener_options|
70
+
71
+ [:instance_port, :instance_protocol, :lb_port, :protocol].each do |o|
72
+ raise "Listeners need #{o.to_s}" unless listener_options.has_key?(o)
73
+ end
74
+
75
+ elb.listeners.new( listener_options ).save
76
+
77
+ end
78
+
79
+ unless @options[:instance_names].nil?
80
+
81
+ options[:instance_ids] = []
82
+
83
+ options[:instance_names].each do |i|
84
+ options[:instance_ids] << BuildCloud::Instance.get_id_by_name( i )
85
+ end
86
+
87
+ @log.info( options[:instance_ids] )
88
+ @log.info( "#{options[:instance_ids].inspect}" )
89
+
90
+ options.delete(options[:instance_names])
91
+
92
+ elb.register_instances(options[:instance_ids])
93
+
94
+ end
95
+
96
+ @elb.configure_health_check( elb.id, options[:health_check] )
97
+
98
+ @log.debug( elb.inspect )
99
+
100
+ end
101
+
102
+ def read
103
+ @elb.load_balancers.select { |l| l.id == @options[:id] }.first
104
+ end
105
+
106
+ alias_method :fog_object, :read
107
+
108
+ def delete
109
+
110
+ return unless exists?
111
+
112
+ @log.info( "Deleting load balancer #{@options[:id]}" )
113
+
114
+ fog_object.destroy
115
+
116
+ end
117
+
118
+ end
119
+