rudy 0.4.0 → 0.6.0

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.
Files changed (135) hide show
  1. data/CHANGES.txt +54 -30
  2. data/README.rdoc +100 -12
  3. data/Rakefile +103 -8
  4. data/Rudyfile +119 -0
  5. data/bin/ird +175 -0
  6. data/bin/rudy +259 -156
  7. data/bin/rudy-ec2 +228 -95
  8. data/bin/rudy-s3 +76 -0
  9. data/bin/rudy-sdb +67 -0
  10. data/lib/annoy.rb +270 -0
  11. data/lib/console.rb +30 -9
  12. data/lib/escape.rb +305 -0
  13. data/lib/rudy.rb +151 -182
  14. data/lib/rudy/aws.rb +56 -49
  15. data/lib/rudy/aws/ec2.rb +47 -292
  16. data/lib/rudy/aws/ec2/address.rb +157 -0
  17. data/lib/rudy/aws/ec2/group.rb +301 -0
  18. data/lib/rudy/aws/ec2/image.rb +168 -0
  19. data/lib/rudy/aws/ec2/instance.rb +434 -0
  20. data/lib/rudy/aws/ec2/keypair.rb +104 -0
  21. data/lib/rudy/aws/ec2/snapshot.rb +98 -0
  22. data/lib/rudy/aws/ec2/volume.rb +230 -0
  23. data/lib/rudy/aws/ec2/zone.rb +77 -0
  24. data/lib/rudy/aws/s3.rb +54 -0
  25. data/lib/rudy/aws/sdb.rb +298 -0
  26. data/lib/rudy/aws/sdb/error.rb +46 -0
  27. data/lib/rudy/{metadata/backup.rb → backup.rb} +26 -51
  28. data/lib/rudy/cli.rb +157 -0
  29. data/lib/rudy/cli/aws/ec2/addresses.rb +105 -0
  30. data/lib/rudy/cli/aws/ec2/candy.rb +208 -0
  31. data/lib/rudy/cli/aws/ec2/groups.rb +121 -0
  32. data/lib/rudy/cli/aws/ec2/images.rb +196 -0
  33. data/lib/rudy/cli/aws/ec2/instances.rb +194 -0
  34. data/lib/rudy/cli/aws/ec2/keypairs.rb +53 -0
  35. data/lib/rudy/cli/aws/ec2/snapshots.rb +49 -0
  36. data/lib/rudy/cli/aws/ec2/volumes.rb +104 -0
  37. data/lib/rudy/cli/aws/ec2/zones.rb +22 -0
  38. data/lib/rudy/cli/aws/s3/buckets.rb +50 -0
  39. data/lib/rudy/cli/aws/s3/store.rb +22 -0
  40. data/lib/rudy/cli/aws/sdb/domains.rb +41 -0
  41. data/lib/rudy/cli/candy.rb +8 -0
  42. data/lib/rudy/{command → cli}/config.rb +34 -24
  43. data/lib/rudy/cli/disks.rb +35 -0
  44. data/lib/rudy/cli/machines.rb +94 -0
  45. data/lib/rudy/cli/routines.rb +57 -0
  46. data/lib/rudy/config.rb +77 -72
  47. data/lib/rudy/config/objects.rb +29 -0
  48. data/lib/rudy/disks.rb +248 -0
  49. data/lib/rudy/global.rb +121 -0
  50. data/lib/rudy/huxtable.rb +340 -0
  51. data/lib/rudy/machines.rb +245 -0
  52. data/lib/rudy/metadata.rb +123 -13
  53. data/lib/rudy/routines.rb +47 -0
  54. data/lib/rudy/routines/helpers/diskhelper.rb +101 -0
  55. data/lib/rudy/routines/helpers/scripthelper.rb +91 -0
  56. data/lib/rudy/routines/release.rb +34 -0
  57. data/lib/rudy/routines/shutdown.rb +57 -0
  58. data/lib/rudy/routines/startup.rb +58 -0
  59. data/lib/rudy/scm/svn.rb +1 -1
  60. data/lib/rudy/utils.rb +322 -4
  61. data/lib/storable.rb +26 -17
  62. data/lib/sysinfo.rb +274 -0
  63. data/lib/tryouts.rb +6 -13
  64. data/rudy.gemspec +128 -42
  65. data/support/randomize-root-password +45 -0
  66. data/support/rudy-ec2-startup +9 -9
  67. data/support/update-ec2-ami-tools +20 -0
  68. data/test/05_config/00_setup_test.rb +20 -0
  69. data/test/05_config/30_machines_test.rb +69 -0
  70. data/test/20_sdb/00_setup_test.rb +16 -0
  71. data/test/20_sdb/10_domains_test.rb +115 -0
  72. data/test/25_ec2/00_setup_test.rb +29 -0
  73. data/test/25_ec2/10_keypairs_test.rb +41 -0
  74. data/test/25_ec2/20_groups_test.rb +131 -0
  75. data/test/25_ec2/30_addresses_test.rb +38 -0
  76. data/test/25_ec2/40_volumes_test.rb +49 -0
  77. data/test/25_ec2/50_snapshots_test.rb +74 -0
  78. data/test/26_ec2_instances/00_setup_test.rb +28 -0
  79. data/test/26_ec2_instances/10_instances_test.rb +83 -0
  80. data/test/26_ec2_instances/50_images_test.rb +13 -0
  81. data/test/30_sdb_metadata/00_setup_test.rb +21 -0
  82. data/test/30_sdb_metadata/10_disks_test.rb +109 -0
  83. data/test/30_sdb_metadata/20_backups_test.rb +102 -0
  84. data/test/coverage.txt +51 -0
  85. data/test/helper.rb +36 -0
  86. data/vendor/highline-1.5.1/CHANGELOG +222 -0
  87. data/vendor/highline-1.5.1/INSTALL +35 -0
  88. data/vendor/highline-1.5.1/LICENSE +7 -0
  89. data/vendor/highline-1.5.1/README +63 -0
  90. data/vendor/highline-1.5.1/Rakefile +82 -0
  91. data/vendor/highline-1.5.1/TODO +6 -0
  92. data/vendor/highline-1.5.1/examples/ansi_colors.rb +38 -0
  93. data/vendor/highline-1.5.1/examples/asking_for_arrays.rb +18 -0
  94. data/vendor/highline-1.5.1/examples/basic_usage.rb +75 -0
  95. data/vendor/highline-1.5.1/examples/color_scheme.rb +32 -0
  96. data/vendor/highline-1.5.1/examples/limit.rb +12 -0
  97. data/vendor/highline-1.5.1/examples/menus.rb +65 -0
  98. data/vendor/highline-1.5.1/examples/overwrite.rb +19 -0
  99. data/vendor/highline-1.5.1/examples/page_and_wrap.rb +322 -0
  100. data/vendor/highline-1.5.1/examples/password.rb +7 -0
  101. data/vendor/highline-1.5.1/examples/trapping_eof.rb +22 -0
  102. data/vendor/highline-1.5.1/examples/using_readline.rb +17 -0
  103. data/vendor/highline-1.5.1/lib/highline.rb +758 -0
  104. data/vendor/highline-1.5.1/lib/highline/color_scheme.rb +120 -0
  105. data/vendor/highline-1.5.1/lib/highline/compatibility.rb +17 -0
  106. data/vendor/highline-1.5.1/lib/highline/import.rb +43 -0
  107. data/vendor/highline-1.5.1/lib/highline/menu.rb +395 -0
  108. data/vendor/highline-1.5.1/lib/highline/question.rb +463 -0
  109. data/vendor/highline-1.5.1/lib/highline/system_extensions.rb +193 -0
  110. data/vendor/highline-1.5.1/setup.rb +1360 -0
  111. data/vendor/highline-1.5.1/test/tc_color_scheme.rb +56 -0
  112. data/vendor/highline-1.5.1/test/tc_highline.rb +823 -0
  113. data/vendor/highline-1.5.1/test/tc_import.rb +54 -0
  114. data/vendor/highline-1.5.1/test/tc_menu.rb +429 -0
  115. data/vendor/highline-1.5.1/test/ts_all.rb +15 -0
  116. metadata +141 -38
  117. data/lib/aws_sdb.rb +0 -3
  118. data/lib/aws_sdb/error.rb +0 -42
  119. data/lib/aws_sdb/service.rb +0 -215
  120. data/lib/rudy/aws/simpledb.rb +0 -53
  121. data/lib/rudy/command/addresses.rb +0 -46
  122. data/lib/rudy/command/backups.rb +0 -175
  123. data/lib/rudy/command/base.rb +0 -841
  124. data/lib/rudy/command/deploy.rb +0 -12
  125. data/lib/rudy/command/disks.rb +0 -213
  126. data/lib/rudy/command/environment.rb +0 -73
  127. data/lib/rudy/command/groups.rb +0 -61
  128. data/lib/rudy/command/images.rb +0 -91
  129. data/lib/rudy/command/instances.rb +0 -85
  130. data/lib/rudy/command/machines.rb +0 -161
  131. data/lib/rudy/command/metadata.rb +0 -41
  132. data/lib/rudy/command/release.rb +0 -174
  133. data/lib/rudy/command/volumes.rb +0 -66
  134. data/lib/rudy/metadata/disk.rb +0 -138
  135. data/tryouts/console_tryout.rb +0 -91
@@ -0,0 +1,245 @@
1
+
2
+
3
+
4
+ module Rudy
5
+ class Machine < Storable
6
+ include Rudy::MetaData::ObjectBase
7
+
8
+ field :rtype
9
+ field :awsid
10
+
11
+ field :region
12
+ field :zone
13
+ field :environment
14
+ field :role
15
+ field :position
16
+
17
+ field :created => Time
18
+ field :started => Time
19
+
20
+ field :dns_public
21
+ field :dns_private
22
+ field :state
23
+
24
+ attr_reader :instance
25
+
26
+ def init
27
+ #@created =
28
+ @rtype = 'm'
29
+ @region = @@global.region
30
+ @zone = @@global.zone
31
+ @environment = @@global.environment
32
+ @role = @@global.role
33
+ @position = find_next_position || '01'
34
+ @state = 'no-instance'
35
+ end
36
+
37
+ def liner_note
38
+ update #if !dns_public? && @awsid
39
+ info = !@dns_public.nil? && !@dns_public.empty? ? @dns_public : "#{@awsid}:#{@state}"
40
+ "%s %s" % [self.name.bright, info]
41
+ end
42
+
43
+ def to_s(with_title=false)
44
+ lines = []
45
+ lines << liner_note
46
+ #if self.running?
47
+ # k, g = @keyname || 'no-keypair', self.groups.join(', ')
48
+ # lines << @@sformat % %w{zone size ami keyname groups} if with_title
49
+ # lines << @@sformat % [@zone, @size, @ami, k, g]
50
+ #end
51
+ lines.join($/)
52
+ end
53
+
54
+ def inspect
55
+ update #if !dns_public? && @awsid
56
+ lines = []
57
+ lines << liner_note
58
+ field_names.each do |key|
59
+ next unless self.respond_to?(key)
60
+ val = self.send(key)
61
+ lines << sprintf(" %22s: %s", key, (val.is_a?(Array) ? val.join(', ') : val))
62
+ end
63
+ lines.join($/)
64
+ end
65
+
66
+ def find_next_position
67
+ list = @sdb.select(self.to_select(nil, [:position])) || []
68
+ pos = list.size + 1
69
+ pos.to_s.rjust(2, '0')
70
+ end
71
+
72
+ def name
73
+ Machine.generate_name(@zone, @environment, @role, @position)
74
+ end
75
+
76
+ def Machine.generate_name(zon, env, rol, pos)
77
+ pos = pos.to_s.rjust 2, '0'
78
+ ["m", zon, env, rol, pos].join(Rudy::DELIM)
79
+ end
80
+
81
+ def get_instance
82
+ @ec2inst.get(@awsid)
83
+ end
84
+
85
+ def update
86
+ return false unless @awsid
87
+ @instance = get_instance
88
+ if @instance.is_a?(Rudy::AWS::EC2::Instance)
89
+ @dns_public = @instance.dns_public
90
+ @dns_private = @instance.dns_private
91
+ @state = @instance.state
92
+ save
93
+ elsif @instance.nil?
94
+ @dns_public = @dns_private = nil
95
+ @state = 'unknown'
96
+ end
97
+ end
98
+
99
+ def dns_public?
100
+ !@dns_public.nil? && !@dns_public.empty?
101
+ end
102
+ def dns_private?
103
+ !@dns_private.nil? && !@dns_private.empty?
104
+ end
105
+
106
+ def start(opts={})
107
+ raise "#{name} is already running" if running?
108
+
109
+ opts = {
110
+ :min => 1,
111
+ :size => current_machine_size,
112
+ :ami => current_machine_image,
113
+ :group => current_group_name,
114
+ :keypair => root_keypairname,
115
+ :zone => @@global.zone.to_s,
116
+ :address => current_machine_address,
117
+ :machine_data => Machine.generate_machine_data.to_yaml
118
+ }.merge(opts)
119
+
120
+ @ec2inst.create(opts) do |inst|
121
+ @awsid = inst.awsid
122
+ @created = @starts = Time.now
123
+ @state = inst.state
124
+ end
125
+
126
+ self.save
127
+
128
+ self
129
+ end
130
+
131
+ def destroy
132
+ @ec2inst.destroy(@awsid) if running?
133
+ super
134
+ end
135
+
136
+
137
+ def Machine.generate_machine_data
138
+ data = { # Give the machine an identity
139
+ :region => @@global.region.to_s,
140
+ :zone => @@global.zone.to_s,
141
+ :environment => @@global.environment.to_s,
142
+ :role => @@global.role.to_s,
143
+ :position => @@global.position.to_s,
144
+ :hosts => { # Add hosts to the /etc/hosts file
145
+ :dbmaster => "127.0.0.1",
146
+ }
147
+ }
148
+ data
149
+ end
150
+
151
+ def running?
152
+ return false if @awsid.nil? || @awsid.empty?
153
+ @ec2inst.running?(@awsid)
154
+ end
155
+
156
+ end
157
+
158
+
159
+
160
+ class Machines
161
+ include Rudy::MetaData
162
+
163
+ def create(&each_mach)
164
+ raise MachineGroupAlreadyRunning, current_machine_group if running?
165
+ raise MachineGroupNotDefined, current_machine_group unless known_machine_group?
166
+
167
+ unless (1..MAX_INSTANCES).member?(current_machine_count)
168
+ raise "Instance count must be more than 0, less than #{MAX_INSTANCES}"
169
+ end
170
+
171
+ unless @rgrp.exists?(current_group_name)
172
+ puts "Creating group: #{current_group_name}"
173
+ @rgrp.create(current_group_name)
174
+ end
175
+
176
+ unless @rkey.exists?(root_keypairname)
177
+ kp_file = File.join(Rudy::CONFIG_DIR, root_keypairname)
178
+ raise PrivateKeyFileExists, kp_file if File.exists?(kp_file)
179
+ puts "Creating keypair: #{root_keypairname}"
180
+ kp = @rkey.create(root_keypairname)
181
+ puts "Saving #{kp_file}"
182
+ Rudy::Utils.write_to_file(kp_file, kp.private_key, 'w', 0600)
183
+ else
184
+ kp_file = root_keypairpath
185
+ # This means no keypair file can be found
186
+ raise PrivateKeyNotFound, root_keypairname if kp_file.nil?
187
+ # This means we found a keypair in the config but we cannot find the private key file.
188
+ raise PrivateKeyNotFound, kp_file if !File.exists?(kp_file)
189
+ end
190
+
191
+ machines = []
192
+ current_machine_count.times do |i|
193
+ machine = Rudy::Machine.new
194
+ puts "Starting %s" % machine.name
195
+ machine.start
196
+ machines << machine
197
+ end
198
+ machines.each { |m| each_mach.call(m) } if each_mach
199
+ machines
200
+ end
201
+
202
+
203
+ def destroy(&each_mach)
204
+ raise MachineGroupNotRunning, current_machine_group unless running?
205
+ raise MachineGroupNotDefined, current_machine_group unless known_machine_group?
206
+ list.each { |m| each_mach.call(m); } if each_mach
207
+ list do |mach|
208
+ puts "Destroying #{mach.name}"
209
+ mach.destroy
210
+ end
211
+ end
212
+
213
+ def list(more=[], less=[], &each_mach)
214
+ machines = list_as_hash(&each_mach)
215
+ machines &&= machines.values
216
+ machines
217
+ end
218
+
219
+ def list_as_hash(more=[], less=[], &each_mach)
220
+ query = to_select([:rtype, 'm'], less)
221
+ list = @sdb.select(query) || {}
222
+ machines = {}
223
+ list.each_pair do |n,m|
224
+ machines[n] = Rudy::Machine.from_hash(m)
225
+ end
226
+ machines.each_pair { |n,mach| each_mach.call(mach) } if each_mach
227
+ machines = nil if machines.empty?
228
+ machines
229
+ end
230
+
231
+ def get(rname=nil)
232
+ Rudy::Machine.from_hash(@sdb.get(Rudy::DOMAIN, rname)) # Returns nil if empty
233
+ end
234
+
235
+
236
+ def running?
237
+ !list.nil?
238
+ # TODO: add logic that checks whether the instances are running.
239
+ end
240
+
241
+
242
+
243
+ end
244
+
245
+ end
@@ -1,26 +1,136 @@
1
1
 
2
- require 'aws_sdb'
3
2
 
4
3
  module Rudy
5
4
  module MetaData
6
- attr_accessor :sdb
7
- attr_accessor :ec2
5
+ module ObjectBase
6
+ include Rudy::Huxtable
7
+
8
+ def initialize(*args)
9
+ a, s, r = @@global.accesskey, @@global.secretkey, @@global.region
10
+ @sdb = Rudy::AWS::SDB.new(a, s, r)
11
+ @ec2inst = Rudy::AWS::EC2::Instances.new(a, s, r)
12
+ @rvol = Rudy::AWS::EC2::Volumes.new(a, s, r)
13
+ init(*args)
14
+ end
15
+
16
+ def init(*args); raise "Must override init"; end
17
+
18
+ def valid?; raise "#{self.class} must override 'valid?'"; end
19
+
20
+ def to_query(more=[], less=[])
21
+ Rudy::AWS::SDB.generate_query build_criteria(more, less)
22
+ end
8
23
 
9
- def initalize(sdb, ec2)
10
- @sdb = sdb
11
- @ec2 = ec2
12
- end
24
+ def to_select(more=[], less=[])
25
+ Rudy::AWS::SDB.generate_select ['*'], Rudy::DOMAIN, build_criteria(more, less)
26
+ end
27
+
28
+ def name(identifier, zon, env, rol, pos, *other)
29
+ pos = pos.to_s.rjust 2, '0'
30
+ [identifier, zon, env, rol, pos, *other].flatten.compact.join(Rudy::DELIM)
31
+ end
32
+
33
+ def save(replace=true)
34
+ replace = true if replace.nil?
35
+ @sdb ||= Rudy::AWS::SDB.new(@@global.accesskey, @@global.secretkey, @@global.region)
36
+ @sdb.put(Rudy::DOMAIN, name, self.to_hash, replace) # Always returns nil
37
+ true
38
+ end
13
39
 
14
- end
15
-
16
- module MetaData
40
+ def destroy
41
+ @sdb ||= Rudy::AWS::SDB.new(@@global.accesskey, @@global.secretkey, @@global.region)
42
+ @sdb.destroy(Rudy::DOMAIN, name)
43
+ true
44
+ end
17
45
 
18
- module ObjectBase
19
- extend self
46
+ def refresh
47
+ @sdb ||= Rudy::AWS::SDB.new(@@global.accesskey, @@global.secretkey, @@global.region)
48
+ h = @sdb.get(Rudy::DOMAIN, name) || {}
49
+ from_hash(h)
50
+ end
20
51
 
52
+ def ==(other)
53
+ return false unless other.is_a?(self.class)
54
+ self.name == other.name
55
+ end
56
+
57
+ def to_s
58
+ str = ""
59
+ field_names.each do |key|
60
+ str << sprintf(" %22s: %s#{$/}", key, self.send(key.to_sym))
61
+ end
62
+ str
63
+ end
21
64
 
22
65
 
66
+ protected
67
+
68
+ # Builds a zipped Array from a list of criteria.
69
+ # The list of criteria is made up of metadata object attributes.
70
+ # The list is constructed by taking the adding +more+, and
71
+ # subtracting +less+ from <tt>:rtype, :region, :zone, :environment, :role, :position</tt>
72
+ #
73
+ # Returns [[:rtype, value], [:zone, value], ...]
74
+ def build_criteria(more=[], less=[])
75
+ criteria = [:rtype, :region, :zone, :environment, :role, :position, *more].compact
76
+ criteria -= [*less].flatten.uniq.compact
77
+ values = criteria.collect do |n|
78
+ self.send(n.to_sym)
79
+ end
80
+ criteria.zip(values)
81
+ end
82
+
83
+ end
84
+ end
85
+ end
86
+
87
+
88
+
89
+ module Rudy
90
+ module MetaData
91
+ include Rudy::Huxtable
92
+
93
+ def initialize(*args)
94
+ a, s, r = @@global.accesskey, @@global.secretkey, @@global.region
95
+ @sdb = Rudy::AWS::SDB.new(a, s, r)
96
+ @rinst = Rudy::AWS::EC2::Instances.new(a, s, r)
97
+ @rgrp = Rudy::AWS::EC2::Groups.new(a, s, r)
98
+ @rkey = Rudy::AWS::EC2::KeyPairs.new(a, s, r)
99
+ init(*args)
100
+ end
101
+
102
+ def init(*args)
103
+ end
104
+
105
+ # 20090224-1813-36
106
+ def format_timestamp(dat)
107
+ mon, day, hour, min, sec = [dat.mon, dat.day, dat.hour, dat.min, dat.sec].collect { |v| v.to_s.rjust(2, "0") }
108
+ [dat.year, mon, day, Rudy::DELIM, hour, min, Rudy::DELIM, sec].join
109
+ end
110
+
111
+ private
112
+
113
+ # Returns a generic zipped Array of metadata
114
+ # (There is region, zone, environment, role, but no rtype)
115
+ def build_criteria(more=[], less=[])
116
+ # TODO: This build_criteria treats "more" differently than the
117
+ # ObjectBase one. Sort it out! (This way is better)
118
+ names = [:region, :zone, :environment, :role].compact
119
+ names -= [*less].flatten.uniq.compact
120
+ values = names.collect do |n|
121
+ @@global.send(n.to_sym)
122
+ end
123
+ names.zip(values) + more
124
+ end
125
+
126
+ def to_query(more=[], less=[])
127
+ Rudy::AWS::SDB.generate_query build_criteria(more, less)
128
+ end
129
+
130
+ def to_select(more=[], less=[])
131
+ Rudy::AWS::SDB.generate_select ['*'], Rudy::DOMAIN, build_criteria(more, less)
23
132
  end
24
133
 
25
134
  end
26
- end
135
+ end
136
+
@@ -0,0 +1,47 @@
1
+
2
+
3
+ module Rudy
4
+ module Routines
5
+ class Base
6
+ include Rudy::Huxtable
7
+
8
+ def initialize
9
+ a, s, r = @@global.accesskey, @@global.secretkey, @@global.region
10
+ @sdb = Rudy::AWS::SDB.new(a, s, r)
11
+ @rinst = Rudy::AWS::EC2::Instances.new(a, s, r)
12
+ @rgrp = Rudy::AWS::EC2::Groups.new(a, s, r)
13
+ @rkey = Rudy::AWS::EC2::KeyPairs.new(a, s, r)
14
+ @rvol = Rudy::AWS::EC2::Volumes.new(a, s, r)
15
+ @rsnap = Rudy::AWS::EC2::Snapshots.new(a, s, r)
16
+ init
17
+ end
18
+
19
+ def init
20
+ end
21
+
22
+
23
+ def execute
24
+ raise "Override execute method"
25
+ end
26
+
27
+ def task_separator(title)
28
+ dashes = 52 - title.size #
29
+ dashes = 0 if dashes < 1
30
+ ("%s--- %s %s" % [$/, title, '-'*dashes]).bright
31
+ end
32
+
33
+ def machine_separator(title)
34
+ dashes = 52 - title.size #
35
+ dashes = 0 if dashes < 1
36
+ ("%s--- %s %s" % [$/, title.bright, '-'*dashes]).bright.color(:blue)
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+ end
43
+
44
+ Rudy::Utils.require_glob(RUDY_LIB, 'rudy', 'routines', 'helpers', '*.rb')
45
+ Rudy::Utils.require_glob(RUDY_LIB, 'rudy', 'routines', '*.rb')
46
+
47
+
@@ -0,0 +1,101 @@
1
+
2
+
3
+ module Rudy; module Routines;
4
+ module DiskHelper
5
+ extend self
6
+
7
+ def execute(routine, machine, rbox)
8
+ return unless routine
9
+ raise "Not a Rudy::Machine" unless machine.is_a?(Rudy::Machine)
10
+ raise "Not a Rye::Box" unless rbox.is_a?(Rye::Box)
11
+
12
+ @machine = machine
13
+ @rbox = rbox
14
+
15
+ (routine.disks || {}).each_pair do |action, disks|
16
+ unless DiskHelper.respond_to?(action)
17
+ STDERR.puts %Q(DiskHelper: unknown action "#{action}")
18
+ next
19
+ end
20
+ send(action, disks) # create, copy, destroy, ...
21
+ end
22
+ end
23
+
24
+ def create(disks)
25
+ rdisk = Rudy::Disks.new
26
+
27
+ disks.each_pair do |path, props|
28
+ disk = Rudy::Disk.new(path, props[:size], props[:device], @machine.position)
29
+ #disk = rdisk.get(disk.name)
30
+ puts "Creating #{disk.name} "
31
+
32
+ print "Creating volume... "
33
+ disk.create
34
+ Rudy::Utils.waiter(2, 60, STDOUT, "done", nil) {
35
+ disk.available?
36
+ }
37
+
38
+ print "Attaching #{disk.awsid} to #{@machine.awsid}... "
39
+ disk.attach(@machine.awsid)
40
+ Rudy::Utils.waiter(2, 60, STDOUT, "done", nil) {
41
+ disk.attached?
42
+ }
43
+
44
+ # The device needs some time.
45
+ # Otherwise mkfs returns:
46
+ # "No such file or directory while trying to determine filesystem size"
47
+ sleep 2
48
+
49
+ print "Creating ext3 filesystem for #{disk.device}... "
50
+ @rbox.mkfs(:t, "ext3", :F, disk.device)
51
+ @rbox.mkdir(:p, disk.path)
52
+ puts "done"
53
+
54
+ print "Mounting at #{disk.path}... "
55
+
56
+ @rbox.mount(:t, 'ext3', disk.device, disk.path)
57
+ disk.mounted = true
58
+ disk.save
59
+ puts "done"
60
+
61
+ end
62
+ end
63
+
64
+ def destroy(disks)
65
+ rdisk = Rudy::Disks.new
66
+
67
+ disks.each_pair do |path, props|
68
+ adisk = Rudy::Disk.new(path, props[:size], props[:device], @machine.position)
69
+ disk = rdisk.get(adisk.name)
70
+ if disk == nil
71
+ puts "Not found: #{adisk.name}".color(:red)
72
+ return
73
+ end
74
+
75
+ puts "Destroying #{disk.name}"
76
+
77
+ if disk.mounted?
78
+ print "Unmounting #{disk.path}... "
79
+ @rbox.umount(disk.path)
80
+ sleep 0.5
81
+ puts "done"
82
+ end
83
+
84
+ if disk.attached?
85
+ print "Detaching #{disk.awsid}... "
86
+ disk.detach
87
+ Rudy::Utils.waiter(2, 60, STDOUT, 'done', nil) {
88
+ disk.available?
89
+ }
90
+ sleep 0.5
91
+ end
92
+
93
+ print "Destroying metadata... "
94
+ disk.destroy
95
+ puts "done"
96
+
97
+ end
98
+ end
99
+
100
+ end
101
+ end;end