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,121 @@
1
+ class BuildCloud::NetworkInterface
2
+
3
+ include ::BuildCloud::Component
4
+
5
+ @@objects = []
6
+
7
+ def self.get_id_by_name( name )
8
+
9
+ interface = self.search( :name => name ).first
10
+
11
+ unless interface
12
+ raise "Couldn't get an NetworkInterface object for #{name} - is it defined?"
13
+ end
14
+
15
+ interface_fog = interface.read
16
+
17
+ unless interface_fog
18
+ raise "Couldn't get a NetworkInterface fog object for #{name} - is it created?"
19
+ end
20
+
21
+ interface_fog.network_interface_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, :private_ip_address)
34
+ require_one_of(:subnet_id, :subnet_name)
35
+ require_one_of(:security_groups, :security_group_names)
36
+
37
+ end
38
+
39
+ def create
40
+
41
+ return if exists?
42
+
43
+ @log.info( "Creating network interface #{@options[:private_ip_address]}" )
44
+
45
+ options = @options.dup
46
+
47
+ unless options[:subnet_id]
48
+
49
+ options[:subnet_id] = BuildCloud::Subnet.get_id_by_name( options[:subnet_name] )
50
+ options.delete(:subnet_name)
51
+
52
+ end
53
+
54
+ unless options[:security_groups]
55
+
56
+ options[:group_set] = []
57
+
58
+ options[:security_group_names].each do |sg|
59
+ options[:group_set] << BuildCloud::SecurityGroup.get_id_by_name( sg )
60
+ end
61
+
62
+ options.delete(:security_group_names)
63
+
64
+ end
65
+
66
+ options[:description] = options[:name]
67
+ options.delete(:name)
68
+
69
+ interface = @compute.network_interfaces.new(options)
70
+ interface.save
71
+
72
+ attributes = {}
73
+ attributes[:resource_id] = interface.network_interface_id
74
+ attributes[:key] = 'Name'
75
+ attributes[:value] = options[:description]
76
+ interface_tag = @compute.tags.new( attributes )
77
+ interface_tag.save
78
+
79
+ if options[:assign_new_public_ip] and ! options[:existing_public_ip].nil?
80
+ raise "Cannot specifiy both new and existing IP addresses"
81
+ end
82
+
83
+ if options[:assign_new_public_ip]
84
+ ip = @compute.addresses.create
85
+ public_ip = ip.public_ip
86
+ allocation_id = ip.allocation_id
87
+ @compute.associate_address(nil, nil, interface.network_interface_id, allocation_id )
88
+ end
89
+
90
+ unless options[:existing_public_ip].nil?
91
+ ip = @compute.addresses.get(options[:existing_public_ip])
92
+ public_ip = ip.public_ip
93
+ allocation_id = ip.allocation_id
94
+ @compute.associate_address(nil, nil, interface.network_interface_id, allocation_id )
95
+ end
96
+
97
+ @log.debug( interface.inspect )
98
+ @log.debug( interface_tag.inspect )
99
+ @log.debug( ip.inspect ) unless ! options[:assign_new_public_ip]
100
+ @log.debug( ip.inspect ) unless options[:existing_public_ip].nil?
101
+
102
+ end
103
+
104
+ def read
105
+ @compute.network_interfaces.select { |ni| ni.private_ip_address == @options[:private_ip_address]}.first
106
+ end
107
+
108
+ alias_method :fog_object, :read
109
+
110
+ def delete
111
+
112
+ return unless exists?
113
+
114
+ @log.info( "Deleting network interface with IP address #{@options[:private_ip_address]}" )
115
+
116
+ fog_object.destroy
117
+
118
+ end
119
+
120
+ end
121
+
@@ -0,0 +1,110 @@
1
+ class BuildCloud::R53RecordSet
2
+
3
+ include ::BuildCloud::Component
4
+
5
+ @@objects = []
6
+
7
+ def initialize ( fog_interfaces, log, options = {} )
8
+
9
+ @log = log
10
+ @options = options
11
+
12
+ @log.debug( options.inspect )
13
+
14
+ required_options(:name, :type, :zone)
15
+
16
+ @zone_name = options.delete(:zone)
17
+ end
18
+
19
+ def create
20
+
21
+ return if exists?
22
+
23
+ @log.info( "Creating record set #{@options[:name]}" )
24
+
25
+ options = @options.dup
26
+
27
+ if options.has_key?(:alias_target)
28
+
29
+ unless options[:alias_target][:dns_name] and options[:alias_target][:hosted_zone_id]
30
+
31
+ elb_name = options[:alias_target].delete(:elb)
32
+ elb = BuildCloud::LoadBalancer.search( :id => elb_name ).first
33
+
34
+ unless elb
35
+ raise "Can't find ELB object for #{elb_name}"
36
+ end
37
+
38
+ options[:alias_target][:dns_name] = elb.read.dns_name
39
+ options[:alias_target][:hosted_zone_id] = elb.read.hosted_zone_name_id
40
+
41
+ end
42
+
43
+ end
44
+
45
+ if rds_server = options.delete(:rds_server)
46
+
47
+ rds = BuildCloud::RDSServer.search( :id => rds_server ).first
48
+
49
+ unless rds
50
+ raise "Can't find RDS Server for #{rds_server}"
51
+ end
52
+
53
+ options[:value] = [ rds.read.endpoint["Address"] ]
54
+ end
55
+
56
+ if cache_cluster = options.delete(:cache_cluster)
57
+
58
+ cache = BuildCloud::CacheCluster.search( :id => cache_cluster ).first
59
+
60
+ unless cache
61
+ raise "Can't find cache cluster for #{cache_cluster}"
62
+ end
63
+
64
+ options[:value] = [ cache.read.nodes.first["Address"] ]
65
+
66
+ end
67
+
68
+
69
+ record = zone.records.create( options )
70
+
71
+ @log.debug(record.inspect)
72
+
73
+ end
74
+
75
+ def read
76
+ if zone
77
+ return zone.records.select { |r| r.name == @options[:name] }.first
78
+ end
79
+ nil
80
+ end
81
+
82
+ alias_method :fog_object, :read
83
+
84
+ def delete
85
+
86
+ return unless exists?
87
+
88
+ @log.info( "Deleting record #{@options[:name]}" )
89
+
90
+ # Fog errors unless ttl is set:
91
+ fog_object.ttl = 1
92
+ fog_object.destroy
93
+
94
+ end
95
+
96
+ def wait_until_ready
97
+ @log.debug("Can't wait on r53 record set creation")
98
+ end
99
+
100
+ private
101
+
102
+ def zone
103
+
104
+ BuildCloud::Zone.search( :domain => @zone_name ).first.fog_object
105
+
106
+ end
107
+
108
+
109
+ end
110
+
@@ -0,0 +1,73 @@
1
+
2
+ class BuildCloud::RDSServer
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(:id, :engine, :allocated_storage, :backup_retention_period,
17
+ :flavor_id, :db_name, :master_username, :password, :vpc_security_group_names)
18
+
19
+ end
20
+
21
+ def create
22
+
23
+ return if exists?
24
+
25
+ @log.info( "Creating RDS Server #{@options[:id]}" )
26
+
27
+ options = @options.dup
28
+
29
+ options[:db_security_groups] = []
30
+
31
+ unless options[:vpc_security_groups]
32
+
33
+ options[:vpc_security_groups] = []
34
+
35
+ options[:vpc_security_group_names].each do |sg|
36
+ options[:vpc_security_groups] << BuildCloud::SecurityGroup.get_id_by_name( sg )
37
+ end
38
+
39
+ options.delete(:vpc_security_group_names)
40
+
41
+ end
42
+
43
+ @log.debug( options.inspect)
44
+
45
+ rds_server = @rds.servers.new( options )
46
+ rds_server.save
47
+
48
+ @log.debug( rds_server.inspect )
49
+
50
+ end
51
+
52
+ def ready_timeout
53
+ 20 * 60 # RDS instances take a while
54
+ end
55
+
56
+ def read
57
+ @rds.servers.select { |r| r.id == @options[:id] }.first
58
+ end
59
+
60
+ alias_method :fog_object, :read
61
+
62
+ def delete
63
+
64
+ return unless exists?
65
+
66
+ @log.info( "Deleting RDS Server #{@options[:id]}" )
67
+
68
+ fog_object.destroy
69
+
70
+ end
71
+
72
+ end
73
+
@@ -0,0 +1,110 @@
1
+ class BuildCloud::Route
2
+
3
+ include ::BuildCloud::Component
4
+
5
+ @@objects = []
6
+
7
+ def initialize ( fog_interfaces, log, options = {} )
8
+
9
+ @compute = fog_interfaces[:compute]
10
+ @log = log
11
+ @options = options
12
+
13
+ @log.debug( options.inspect )
14
+
15
+ required_options(:name, :route_table_name, :destination_cidr_block)
16
+ require_one_of(:internet_gateway_name, :network_interface_name, :internet_gateway_id, :network_interface_id)
17
+ require_one_of(:route_table_id, :route_table_name)
18
+
19
+ end
20
+
21
+ def create
22
+
23
+ return if exists?
24
+
25
+ @log.info("Creating route #{@options[:name]}")
26
+
27
+ options = @options.dup
28
+
29
+ options[:tags] = { 'Name' => options.delete(:name) }
30
+
31
+ unless options[:network_interface_name]
32
+ options[:network_interface_id] = BuildCloud::NetworkInterface.get_id_by_name( options[:network_interface_name] )
33
+ options.delete(:network_interface_name)
34
+ end
35
+
36
+ unless options[:internet_gateway_name]
37
+ options[:internet_gateway_id] = BuildCloud::InternetGateway.get_id_by_name( options[:internet_gateway_name] )
38
+ options.delete(:internet_gateway_name)
39
+ end
40
+
41
+ unless options[:route_table_name]
42
+ options[:route_table_id] = BuildCloud::RouteTable.get_id_by_name( options[:route_table_name] )
43
+ options.delete(:route_table_name)
44
+ end
45
+
46
+ route_table_id = options[:route_table_id]
47
+ destination_cidr_block = options[:destination_cidr_block]
48
+ internet_gateway_id = options[:internet_gateway_id]
49
+ network_interface_id = options[:network_interface_id]
50
+
51
+ # Using requests instead of model here, because the model
52
+ # doesn't support associations.
53
+
54
+ begin
55
+
56
+ if create_route(route_table_id, destination_cidr_block, internet_gateway_id, nil, network_interface_id)
57
+ @log.debug("route created successfully")
58
+ else
59
+ @log.debug("failed to create route")
60
+ end
61
+
62
+ rescue Exception => e
63
+ @log.error( "An exception - #{e} - occured")
64
+ end
65
+
66
+ end
67
+
68
+ def read
69
+ rt = @compute.route_tables.select { |rt| rt.tags['Name'] == @options[:name] }.first
70
+ @log.info( rt.inspect )
71
+ @log.info( rt.routes ) unless rt.routes.empty?
72
+
73
+ route = rt.routes.select { |t| t['destinationCidrBlock'] == @options[:destination_cidr_block]}
74
+ @log.info( route.inspect )
75
+
76
+ end
77
+
78
+ alias_method :fog_object, :read
79
+
80
+ def delete
81
+
82
+ return unless exists?
83
+
84
+ @log.info("Deleting route #{@options[:name]}")
85
+
86
+ unless options[:route_table_name]
87
+ options[:route_table_id] = BuildCloud::RouteTable.get_id_by_name( options[:route_table_name] )
88
+ options.delete(:route_table_name)
89
+ end
90
+
91
+ route_table_id = options[:route_table_id]
92
+ destination_cidr_block = options[:destination_cidr_block]
93
+
94
+ begin
95
+
96
+ if delete_route(route_table_id, destination_cidr_block)
97
+ @log.debug("route deleted successfully")
98
+ else
99
+ @log.debug("failed to delet route")
100
+ end
101
+
102
+ rescue Exception => e
103
+ @log.error( "An exception - #{e} - occured")
104
+ end
105
+
106
+ end
107
+
108
+ end
109
+
110
+
@@ -0,0 +1,106 @@
1
+ class BuildCloud::RouteTable
2
+
3
+ include ::BuildCloud::Component
4
+
5
+ @@objects = []
6
+
7
+ def self.get_id_by_name( name )
8
+
9
+ route_table = self.search( :name => name ).first
10
+
11
+ unless route_table
12
+ raise "Couldn't get a RouteTable object for #{name} - is it defined?"
13
+ end
14
+
15
+ route_table_fog = route_table.read
16
+
17
+ unless route_table_fog
18
+ raise "Couldn't get a RouteTable fog object for #{name} - is it created?"
19
+ end
20
+
21
+ route_table_fog.route_table_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
+ require_one_of(:subnet_ids, :subnet_names)
36
+
37
+
38
+ end
39
+
40
+ def create
41
+
42
+ return if exists?
43
+
44
+ @log.info("Creating route table #{@options[:name]}")
45
+
46
+ options = @options.dup
47
+
48
+ unless options[:subnet_ids]
49
+
50
+ options[:subnet_ids] = []
51
+
52
+ options[:subnet_names].each do |sn|
53
+ options[:subnet_ids] << BuildCloud::Subnet.get_id_by_name( sn )
54
+ end
55
+
56
+ options.delete(:subnet_names)
57
+
58
+ end
59
+
60
+ unless options[:vpc_id]
61
+
62
+ options[:vpc_id] = BuildCloud::VPC.get_id_by_name( options[:vpc_name] )
63
+ options.delete(:vpc_name)
64
+
65
+ end
66
+
67
+ options[:tags] = { 'Name' => options.delete(:name) }
68
+
69
+ # Using requests instead of model here, because the model
70
+ # doesn't support associations.
71
+
72
+ rt = @compute.route_tables.new ( options )
73
+ rt.save
74
+ @log.debug(rt.inspect)
75
+
76
+ @compute.create_tags( rt.id, options[:tags] )
77
+
78
+ options[:subnet_ids].each do |s|
79
+ @compute.associate_route_table( rt.id, s )
80
+ end
81
+
82
+ end
83
+
84
+ def read
85
+ @compute.route_tables.select { |r| r.tags['Name'] == @options[:name] }.first
86
+ end
87
+
88
+ alias_method :fog_object, :read
89
+
90
+ def delete
91
+
92
+ return unless exists?
93
+
94
+ @log.info("Deleting route table #{@options[:name]}")
95
+
96
+ read.associations.each do |ra|
97
+ @compute.disassociate_route_table( ra['routeTableAssociationId'] )
98
+ end
99
+
100
+ fog_object.destroy
101
+
102
+ end
103
+
104
+ end
105
+
106
+
@@ -0,0 +1,49 @@
1
+ class BuildCloud::S3Bucket
2
+
3
+ include ::BuildCloud::Component
4
+
5
+ @@objects = []
6
+
7
+ def initialize ( fog_interfaces, log, options = {} )
8
+
9
+ @s3 = fog_interfaces[:s3]
10
+ @log = log
11
+ @options = options
12
+
13
+ @log.debug( options.inspect )
14
+
15
+ required_options(:key, :location)
16
+
17
+ end
18
+
19
+ def create
20
+
21
+ return if exists?
22
+
23
+ @log.info( "Creating new S3 bucket #{@options[:key]}" )
24
+
25
+ bucket = @s3.directories.new( @options )
26
+ bucket.save
27
+
28
+ @log.debug( bucket.inspect )
29
+
30
+ end
31
+
32
+ def read
33
+ @s3.directories.select { |d| d.key == @options[:key] }.first
34
+ end
35
+
36
+ alias_method :fog_object, :read
37
+
38
+ def delete
39
+
40
+ return unless exists?
41
+
42
+ @log.info( "Deleting S3 bucket #{@options[:key]}" )
43
+
44
+ fog_object.destroy
45
+
46
+ end
47
+
48
+ end
49
+
@@ -0,0 +1,91 @@
1
+ class BuildCloud::SecurityGroup
2
+
3
+ include ::BuildCloud::Component
4
+
5
+ @@objects = []
6
+
7
+ def self.get_id_by_name( name )
8
+
9
+ sg = self.search( :name => name ).first
10
+
11
+ unless sg
12
+ raise "Couldn't get a SecurityGroup object for #{name} - is it defined?"
13
+ end
14
+
15
+ sg_fog = sg.read
16
+
17
+ unless sg_fog
18
+ raise "Couldn't get a SecurityGroup fog object for #{name} - is it created?"
19
+ end
20
+
21
+ sg_fog.group_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, :description)
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 security group #{@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
+ authorized_ranges = []
54
+ if options[:authorized_ranges]
55
+ authorized_ranges = options[:authorized_ranges]
56
+ options.delete(:authorized_ranges)
57
+ end
58
+
59
+ security_group = @compute.security_groups.new( options )
60
+ security_group.save
61
+
62
+ authorized_ranges.each do |r|
63
+
64
+ security_group.authorize_port_range(
65
+ r.delete(:min_port)..r.delete(:max_port), r
66
+ )
67
+
68
+ end
69
+
70
+ @log.debug( security_group.inspect )
71
+
72
+ end
73
+
74
+ def read
75
+ @compute.security_groups.select { |sg| sg.name == @options[:name] }.first
76
+ end
77
+
78
+ alias_method :fog_object, :read
79
+
80
+ def delete
81
+
82
+ return unless exists?
83
+
84
+ @log.info( "Deleting security group #{@options[:name]}" )
85
+
86
+ fog_object.destroy
87
+
88
+ end
89
+
90
+ end
91
+