internuity-awsum 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/ec2/image.rb ADDED
@@ -0,0 +1,161 @@
1
+ module Awsum
2
+ class Ec2
3
+ class Image
4
+ attr_reader :id, :location, :state, :owner, :public, :architecture, :type, :kernel_id, :ramdisk_id, :platform, :product_codes
5
+
6
+ def initialize(ec2, id, location, state, owner, public, architecture, type, kernel_id, ram_disk_id, platform, product_codes) #:nodoc:
7
+ @ec2 = ec2
8
+ @id = id
9
+ @location = location
10
+ @state = state
11
+ @owner = owner
12
+ @public = public
13
+ @architecture = architecture
14
+ @type = type
15
+ @kernel_id = kernel_id
16
+ @ramdisk_id = ram_disk_id
17
+ @platform = platform
18
+ @product_codes = product_codes
19
+ end
20
+
21
+ def public?
22
+ @public
23
+ end
24
+
25
+ # Deregister this Image
26
+ def deregister
27
+ @ec2.deregister_image @id
28
+ end
29
+
30
+ # Reregister this image
31
+ #
32
+ # Will both deregister and then register the Image again
33
+ def reregister
34
+ @ec2.deregister_image @id
35
+ new_id = @ec2.register_image @location
36
+ @id = new_id
37
+ self
38
+ end
39
+
40
+ # launches instances of this image
41
+ #
42
+ # ===Options:
43
+ # * <tt>:min</tt> - The minimum number of instances to launch. Default: 1
44
+ # * <tt>:max</tt> - The maximum number of instances to launch. Default: 1
45
+ # * <tt>:key_name</tt> - The name of the key pair with which to launch instances
46
+ # * <tt>:security_groups</tt> - The names of security groups to associate launched instances with
47
+ # * <tt>:user_data</tt> - User data made available to instances (Note: Must be 16K or less, will be base64 encoded by Awsum)
48
+ # * <tt>:instance_type</tt> - The size of the instances to launch, can be one of [m1.small, m1.large, m1.xlarge, c1.medium, c1.xlarge], default is m1.small
49
+ # * <tt>:availability_zone</tt> - The name of the availability zone to launch this instance in
50
+ # * <tt>:kernel_id</tt> - The ID of the kernel with which to launch instances
51
+ # * <tt>:ramdisk_id</tt> - The ID of the RAM disk with which to launch instances
52
+ # * <tt>:block_device_map</tt> - A 'hash' of mappings. E.g. {'instancestore0' => 'sdb'}
53
+ def run(options = {})
54
+ @ec2.run_instances(id, options)
55
+ end
56
+ alias_method :launch, :run
57
+ end
58
+
59
+ class ImageParser < Awsum::Parser #:nodoc:
60
+ def initialize(ec2)
61
+ @ec2 = ec2
62
+ @images = []
63
+ @text = nil
64
+ @stack = []
65
+ end
66
+
67
+ def tag_start(tag, attributes)
68
+ case tag
69
+ when 'imagesSet'
70
+ @stack << 'imagesSet'
71
+ when 'item'
72
+ case @stack[-1]
73
+ when 'imagesSet'
74
+ @current = {}
75
+ @text = ''
76
+ when 'productCodes'
77
+ @product_codes = []
78
+ @text = ''
79
+ end
80
+ when 'productCodes'
81
+ @stack << 'productCodes'
82
+ end
83
+ end
84
+
85
+ def text(text)
86
+ @text << text unless @text.nil?
87
+ end
88
+
89
+ def tag_end(tag)
90
+ case tag
91
+ when 'DescribeImagesResponse', 'requestId'
92
+ #no-op
93
+ when 'imagesSet', 'productCodes'
94
+ @stack.pop
95
+ when 'item'
96
+ case @stack[-1]
97
+ when 'imagesSet'
98
+ @images << Image.new(
99
+ @ec2,
100
+ @current['imageId'],
101
+ @current['imageLocation'],
102
+ @current['imageState'],
103
+ @current['imageOwnerId'],
104
+ @current['isPublic'] == 'true',
105
+ @current['architecture'],
106
+ @current['imageType'],
107
+ @current['kernelId'],
108
+ @current['ramdiskId'],
109
+ @current['platform'],
110
+ @product_codes || []
111
+ )
112
+ @text = ''
113
+ end
114
+ when 'productCode'
115
+ @product_codes << @text.strip
116
+ else
117
+ unless @text.nil?
118
+ text = @text.strip
119
+ @current[tag] = (text == '' ? nil : text)
120
+ @text = ''
121
+ end
122
+ end
123
+ end
124
+
125
+ def result
126
+ @images
127
+ end
128
+ end
129
+
130
+ class RegisterImageParser < Awsum::Parser #:nodoc:
131
+ def initialize(ec2)
132
+ @ec2 = ec2
133
+ @image = nil
134
+ @text = nil
135
+ end
136
+
137
+ def tag_start(tag, attributes)
138
+ case tag
139
+ when 'imageId'
140
+ @text = ''
141
+ end
142
+ end
143
+
144
+ def text(text)
145
+ @text << text unless @text.nil?
146
+ end
147
+
148
+ def tag_end(tag)
149
+ case tag
150
+ when 'imageId'
151
+ @image = @text
152
+ @text = nil
153
+ end
154
+ end
155
+
156
+ def result
157
+ @image
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,144 @@
1
+ require 'time'
2
+
3
+ module Awsum
4
+ class Ec2
5
+ class Instance
6
+ attr_reader :id, :image_id, :type, :state, :dns_name, :private_dns_name, :key_name, :kernel_id, :launch_time, :availability_zone, :product_codes, :ramdisk_id, :reason, :launch_index
7
+
8
+ def initialize(ec2, id, image_id, type, state, dns_name, private_dns_name, key_name, kernel_id, launch_time, availability_zone, product_codes, ramdisk_id, reason, launch_index) #:nodoc:
9
+ @ec2 = ec2
10
+ @id = id
11
+ @image_id = image_id
12
+ @type = type
13
+ @state = state
14
+ @dns_name = dns_name
15
+ @private_dns_name = private_dns_name
16
+ @key_name = key_name
17
+ @kernel_id = kernel_id
18
+ @launch_time = launch_time
19
+ @availability_zone = availability_zone
20
+ @product_codes = product_codes
21
+ @ramdisk_id = ramdisk_id
22
+ @reason = reason
23
+ @launch_index = launch_index
24
+ end
25
+
26
+ # Terminates this instance
27
+ def terminate
28
+ @ec2.terminate_instances(id)
29
+ end
30
+
31
+ # Lists all the Volume(s) attached to this Instance
32
+ def volumes
33
+ volumes = @ec2.volumes
34
+ volumes.delete_if {|v| v.instance_id != id}
35
+ end
36
+
37
+ # Will create and attach a Volume to this Instance
38
+ # You must specify a size or a snapshot_id
39
+ def create_volume(size = nil, snapshot_id = nil, device = '/dev/sdh')
40
+ raise ArgumentError.new('You must specify a size if not creating a volume from a snapshot') if size.blank? && snapshot_id.blank?
41
+ raise ArgumentError.new('You must specify a device to attach the volume to') unless device
42
+
43
+ volume = @ec2.create_volume availability_zone, :size => size, :snapshot_id => snapshot_id
44
+ while volume.status != 'available'
45
+ volume.reload
46
+ end
47
+ attach volume, device
48
+ volume
49
+ end
50
+
51
+ # Will attach a Volume to this Instance
52
+ def attach(volume, device = '/dev/sdh')
53
+ @ec2.attach_volume volume.id, id, device
54
+ end
55
+ end
56
+
57
+ class InstanceParser < Awsum::Parser #:nodoc:
58
+ def initialize(ec2)
59
+ @ec2 = ec2
60
+ @instances = []
61
+ @text = nil
62
+ @stack = []
63
+ @placement = nil
64
+ @state = nil
65
+ end
66
+
67
+ def tag_start(tag, attributes)
68
+ case tag
69
+ when 'reservationSet'
70
+ @stack << 'reservationSet'
71
+ when 'instancesSet'
72
+ @stack << 'instancesSet'
73
+ when 'productCodes'
74
+ @stack << 'productCodes'
75
+ when 'instanceState'
76
+ @stack << 'instanceState'
77
+ when 'placement'
78
+ @stack << 'placement'
79
+ when 'item'
80
+ case @stack[-1]
81
+ when 'reservationSet'
82
+ when 'instancesSet'
83
+ @current = {}
84
+ @state = {}
85
+ when 'productCodes'
86
+ @product_codes = []
87
+ end
88
+ end
89
+ @text = ''
90
+ end
91
+
92
+ def text(text)
93
+ @text << text unless @text.nil?
94
+ end
95
+
96
+ def tag_end(tag)
97
+ case tag
98
+ when 'DescribeInstancesResponse', 'requestId', 'reservationId'
99
+ #no-op
100
+ when 'reservationSet', 'instancesSet', 'productCodes', 'instanceState', 'placement'
101
+ @stack.pop
102
+ when 'item'
103
+ case @stack[-1]
104
+ when 'instancesSet'
105
+ @instances << Instance.new(
106
+ @ec2,
107
+ @current['instanceId'],
108
+ @current['imageId'],
109
+ @current['instanceType'],
110
+ @state,
111
+ @current['dnsName'],
112
+ @current['privateDnsName'],
113
+ @current['keyName'],
114
+ @current['kernalId'],
115
+ Time.parse(@current['launchTime']),
116
+ @placement,
117
+ @product_codes || [],
118
+ @current['ramdisk_id'],
119
+ @current['reason'],
120
+ @current['amiLaunchIndex'].to_i
121
+ )
122
+ end
123
+ when 'productCode'
124
+ @product_codes << @text.strip
125
+ when 'availabilityZone'
126
+ @placement = @text.strip
127
+ when 'code'
128
+ @state[:code] = @text.strip.to_i if @stack[-1] == 'instanceState'
129
+ when 'name'
130
+ @state[:name] = @text.strip if @stack[-1] == 'instanceState'
131
+ else
132
+ unless @text.nil? || @current.nil?
133
+ text = @text.strip
134
+ @current[tag] = (text == '' ? nil : text)
135
+ end
136
+ end
137
+ end
138
+
139
+ def result
140
+ @instances
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,82 @@
1
+ module Awsum
2
+ class Ec2
3
+ class Snapshot
4
+ attr_reader :id, :volume_id, :status, :start_time, :progress
5
+
6
+ def initialize(ec2, id, volume_id, status, start_time, progress) #:nodoc:
7
+ @ec2 = ec2
8
+ @id = id
9
+ @volume_id = volume_id
10
+ @status = status
11
+ @start_time = start_time
12
+ @progress = progress
13
+ end
14
+
15
+ # Delete this Snapshot
16
+ def delete
17
+ @ec2.delete_snapshot id
18
+ end
19
+ end
20
+
21
+ class SnapshotParser < Awsum::Parser #:nodoc:
22
+ def initialize(ec2)
23
+ @ec2 = ec2
24
+ @snapshots = []
25
+ @text = nil
26
+ @stack = []
27
+ end
28
+
29
+ def tag_start(tag, attributes)
30
+ #Quick hack so we can use the same parser for CreateSnapshot which doesn't use the item tag to wrap the snapshot information
31
+ if tag == 'CreateSnapshotResponse'
32
+ @stack << 'snapshotSet'
33
+ end
34
+
35
+ case tag
36
+ when 'snapshotSet'
37
+ @stack << 'snapshotSet'
38
+ when 'item', 'CreateSnapshotResponse'
39
+ case @stack[-1]
40
+ when 'snapshotSet'
41
+ @current = {}
42
+ end
43
+ end
44
+ @text = ''
45
+ end
46
+
47
+ def text(text)
48
+ @text << text unless @text.nil?
49
+ end
50
+
51
+ def tag_end(tag)
52
+ case tag
53
+ when 'DescribeSnapshotsResponts'
54
+ #no-op
55
+ when 'snapshotSet'
56
+ @stack.pop
57
+ when 'item', 'CreateSnapshotResponse'
58
+ case @stack[-1]
59
+ when 'snapshotSet'
60
+ @snapshots << Snapshot.new(
61
+ @ec2,
62
+ @current['snapshotId'],
63
+ @current['volumeId'],
64
+ @current['status'],
65
+ @current['startTime'].blank? ? nil :Time.parse(@current['startTime']),
66
+ @current['progress']
67
+ )
68
+ end
69
+ else
70
+ unless @text.nil? || @current.nil?
71
+ text = @text.strip
72
+ @current[tag] = (text == '' ? nil : text)
73
+ end
74
+ end
75
+ end
76
+
77
+ def result
78
+ @snapshots
79
+ end
80
+ end
81
+ end
82
+ end
data/lib/ec2/volume.rb ADDED
@@ -0,0 +1,137 @@
1
+ module Awsum
2
+ class Ec2
3
+ class Volume
4
+ attr_reader :id, :instance_id, :size, :status, :create_time, :snapshot_id, :device, :attach_time, :availability_zone, :attachment_status
5
+
6
+ def initialize(ec2, id, instance_id, size, status, create_time, snapshot_id, device, attach_time, availability_zone, attachment_status) #:nodoc:
7
+ @ec2 = ec2
8
+ @id = id
9
+ @instance_id = instance_id
10
+ @size = size
11
+ @status = status
12
+ @create_time = create_time
13
+ @snapshot_id = snapshot_id
14
+ @device = device
15
+ @attach_time = attach_time
16
+ @availability_zone = availability_zone
17
+ @attachment_status = attachment_status
18
+ end
19
+
20
+ # Will reload the information about this Volume
21
+ #
22
+ # Useful for when a volume has just been created but is not yet available
23
+ # while volume.status != 'available'
24
+ # volume.reload
25
+ # end
26
+ def reload
27
+ reloaded_volume = @ec2.volume id
28
+
29
+ @id = id
30
+ @instance_id = reloaded_volume.instance_id
31
+ @size = reloaded_volume.size
32
+ @status = reloaded_volume.status
33
+ @create_time = reloaded_volume.create_time
34
+ @snapshot_id = reloaded_volume.snapshot_id
35
+ @device = reloaded_volume.device
36
+ @attach_time = reloaded_volume.attach_time
37
+ @availability_zone = reloaded_volume.availability_zone
38
+ @attachment_status = reloaded_volume.attachment_status
39
+ end
40
+
41
+ # Detach this volume
42
+ def detach(force = false)
43
+ @ec2.detach_volume id, :force => force
44
+ end
45
+
46
+ # Delete this volume
47
+ def delete
48
+ @ec2.delete_volume id
49
+ end
50
+
51
+ # Creates a Snapshot of this Volume
52
+ def create_snapshot
53
+ @ec2.create_snapshot id
54
+ end
55
+
56
+ # Lists the Snapshot(s) of this Volume
57
+ def snapshots
58
+ snapshots = @ec2.snapshots
59
+ snapshots.delete_if {|s| s.volume_id != id}
60
+ end
61
+ end
62
+
63
+ class VolumeParser < Awsum::Parser #:nodoc:
64
+ def initialize(ec2)
65
+ @ec2 = ec2
66
+ @volumes = []
67
+ @text = nil
68
+ @stack = []
69
+ end
70
+
71
+ def tag_start(tag, attributes)
72
+ #Quick hack so we can use the same parser for CreateVolume which doesn't use the item tag to wrap the volume information
73
+ if tag == 'CreateVolumeResponse'
74
+ @stack << 'volumeSet'
75
+ end
76
+
77
+ case tag
78
+ when 'volumeSet'
79
+ @stack << 'volumeSet'
80
+ when 'attachmentSet'
81
+ @stack << 'attachmentSet'
82
+ when 'item', 'CreateVolumeResponse'
83
+ case @stack[-1]
84
+ when 'volumeSet'
85
+ @current = {}
86
+ when 'attachmentSet'
87
+ end
88
+ end
89
+ @text = ''
90
+ end
91
+
92
+ def text(text)
93
+ @text << text unless @text.nil?
94
+ end
95
+
96
+ def tag_end(tag)
97
+ case tag
98
+ when 'DescribeVolumesResponse'
99
+ #no-op
100
+ when 'volumeSet', 'attachmentSet'
101
+ @stack.pop
102
+ when 'item', 'CreateVolumeResponse'
103
+ case @stack[-1]
104
+ when 'volumeSet'
105
+ @volumes << Volume.new(
106
+ @ec2,
107
+ @current['volumeId'],
108
+ @current['instanceId'],
109
+ @current['size'].to_i,
110
+ @current['status'],
111
+ @current['createTime'].blank? ? nil :Time.parse(@current['createTime']),
112
+ @current['snapshotId'],
113
+ @current['device'],
114
+ @current['attachTime'].blank? ? nil :Time.parse(@current['attachTime']),
115
+ @current['availabilityZone'],
116
+ @current['attachment_status']
117
+ )
118
+ end
119
+ else
120
+ unless @text.nil? || @current.nil?
121
+ text = @text.strip
122
+ #Handle special case for attachmentSet/status
123
+ if @stack[-1] == 'attachmentSet' && tag == 'status'
124
+ @current['attachment_status'] = (text == '' ? nil : text)
125
+ else
126
+ @current[tag] = (text == '' ? nil : text)
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ def result
133
+ @volumes
134
+ end
135
+ end
136
+ end
137
+ end