solutious-rudy 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. data/CHANGES.txt +19 -1
  2. data/README.rdoc +66 -35
  3. data/Rakefile +1 -0
  4. data/Rudyfile +97 -6
  5. data/bin/ird +7 -4
  6. data/bin/rudy +120 -212
  7. data/bin/rudy-ec2 +240 -0
  8. data/bin/rudy-s3 +76 -0
  9. data/bin/rudy-sdb +67 -0
  10. data/lib/annoy.rb +61 -18
  11. data/lib/console.rb +10 -5
  12. data/lib/rudy/aws/ec2/address.rb +76 -40
  13. data/lib/rudy/aws/ec2/group.rb +138 -78
  14. data/lib/rudy/aws/ec2/image.rb +134 -12
  15. data/lib/rudy/aws/ec2/instance.rb +116 -89
  16. data/lib/rudy/aws/ec2/keypair.rb +26 -14
  17. data/lib/rudy/aws/ec2/snapshot.rb +23 -12
  18. data/lib/rudy/aws/ec2/volume.rb +198 -202
  19. data/lib/rudy/aws/ec2/zone.rb +77 -0
  20. data/lib/rudy/aws/ec2.rb +56 -25
  21. data/lib/rudy/aws/s3.rb +54 -0
  22. data/lib/rudy/aws/sdb/error.rb +46 -0
  23. data/lib/rudy/aws/sdb.rb +298 -0
  24. data/lib/rudy/aws.rb +29 -57
  25. data/lib/rudy/{metadata/backup.rb → backup.rb} +8 -8
  26. data/lib/rudy/cli/aws/ec2/addresses.rb +105 -0
  27. data/lib/rudy/cli/aws/ec2/candy.rb +208 -0
  28. data/lib/rudy/cli/aws/ec2/groups.rb +121 -0
  29. data/lib/rudy/cli/aws/ec2/images.rb +196 -0
  30. data/lib/rudy/cli/aws/ec2/instances.rb +194 -0
  31. data/lib/rudy/cli/aws/ec2/keypairs.rb +53 -0
  32. data/lib/rudy/cli/aws/ec2/snapshots.rb +49 -0
  33. data/lib/rudy/cli/aws/ec2/volumes.rb +104 -0
  34. data/lib/rudy/cli/aws/ec2/zones.rb +22 -0
  35. data/lib/rudy/cli/aws/s3/buckets.rb +50 -0
  36. data/lib/rudy/cli/aws/s3/store.rb +22 -0
  37. data/lib/rudy/cli/aws/sdb/domains.rb +41 -0
  38. data/lib/rudy/cli/{deploy.rb → candy.rb} +2 -6
  39. data/lib/rudy/cli/config.rb +25 -20
  40. data/lib/rudy/cli/disks.rb +18 -108
  41. data/lib/rudy/cli/machines.rb +94 -0
  42. data/lib/rudy/cli/routines.rb +47 -70
  43. data/lib/rudy/cli.rb +104 -89
  44. data/lib/rudy/config/objects.rb +5 -43
  45. data/lib/rudy/config.rb +8 -24
  46. data/lib/rudy/disks.rb +248 -0
  47. data/lib/rudy/global.rb +121 -0
  48. data/lib/rudy/huxtable.rb +232 -147
  49. data/lib/rudy/machines.rb +245 -0
  50. data/lib/rudy/metadata.rb +136 -0
  51. data/lib/rudy/routines/helpers/diskhelper.rb +101 -0
  52. data/lib/rudy/routines/helpers/scripthelper.rb +91 -0
  53. data/lib/rudy/routines/release.rb +27 -8
  54. data/lib/rudy/routines/shutdown.rb +47 -32
  55. data/lib/rudy/routines/startup.rb +47 -37
  56. data/lib/rudy/routines.rb +30 -37
  57. data/lib/rudy/scm/svn.rb +1 -1
  58. data/lib/rudy/utils.rb +262 -4
  59. data/lib/rudy.rb +76 -248
  60. data/lib/storable.rb +19 -16
  61. data/lib/sysinfo.rb +1 -1
  62. data/rudy.gemspec +88 -68
  63. data/support/rudy-ec2-startup +5 -5
  64. data/test/05_config/00_setup_test.rb +3 -7
  65. data/test/20_sdb/00_setup_test.rb +2 -17
  66. data/test/20_sdb/10_domains_test.rb +18 -16
  67. data/test/25_ec2/00_setup_test.rb +5 -10
  68. data/test/25_ec2/10_keypairs_test.rb +13 -5
  69. data/test/25_ec2/20_groups_test.rb +48 -56
  70. data/test/25_ec2/30_addresses_test.rb +13 -10
  71. data/test/25_ec2/40_volumes_test.rb +11 -8
  72. data/test/25_ec2/50_snapshots_test.rb +17 -12
  73. data/test/26_ec2_instances/00_setup_test.rb +3 -8
  74. data/test/26_ec2_instances/10_instances_test.rb +21 -19
  75. data/test/30_sdb_metadata/00_setup_test.rb +2 -9
  76. data/test/30_sdb_metadata/10_disks_test.rb +47 -37
  77. data/test/30_sdb_metadata/20_backups_test.rb +9 -9
  78. data/test/helper.rb +5 -3
  79. data/vendor/highline-1.5.1/CHANGELOG +222 -0
  80. data/vendor/highline-1.5.1/INSTALL +35 -0
  81. data/vendor/highline-1.5.1/LICENSE +7 -0
  82. data/vendor/highline-1.5.1/README +63 -0
  83. data/vendor/highline-1.5.1/Rakefile +82 -0
  84. data/vendor/highline-1.5.1/TODO +6 -0
  85. data/vendor/highline-1.5.1/examples/ansi_colors.rb +38 -0
  86. data/vendor/highline-1.5.1/examples/asking_for_arrays.rb +18 -0
  87. data/vendor/highline-1.5.1/examples/basic_usage.rb +75 -0
  88. data/vendor/highline-1.5.1/examples/color_scheme.rb +32 -0
  89. data/vendor/highline-1.5.1/examples/limit.rb +12 -0
  90. data/vendor/highline-1.5.1/examples/menus.rb +65 -0
  91. data/vendor/highline-1.5.1/examples/overwrite.rb +19 -0
  92. data/vendor/highline-1.5.1/examples/page_and_wrap.rb +322 -0
  93. data/vendor/highline-1.5.1/examples/password.rb +7 -0
  94. data/vendor/highline-1.5.1/examples/trapping_eof.rb +22 -0
  95. data/vendor/highline-1.5.1/examples/using_readline.rb +17 -0
  96. data/vendor/highline-1.5.1/lib/highline/color_scheme.rb +120 -0
  97. data/vendor/highline-1.5.1/lib/highline/compatibility.rb +17 -0
  98. data/vendor/highline-1.5.1/lib/highline/import.rb +43 -0
  99. data/vendor/highline-1.5.1/lib/highline/menu.rb +395 -0
  100. data/vendor/highline-1.5.1/lib/highline/question.rb +463 -0
  101. data/vendor/highline-1.5.1/lib/highline/system_extensions.rb +193 -0
  102. data/vendor/highline-1.5.1/lib/highline.rb +758 -0
  103. data/vendor/highline-1.5.1/setup.rb +1360 -0
  104. data/vendor/highline-1.5.1/test/tc_color_scheme.rb +56 -0
  105. data/vendor/highline-1.5.1/test/tc_highline.rb +823 -0
  106. data/vendor/highline-1.5.1/test/tc_import.rb +54 -0
  107. data/vendor/highline-1.5.1/test/tc_menu.rb +429 -0
  108. data/vendor/highline-1.5.1/test/ts_all.rb +15 -0
  109. metadata +81 -69
  110. data/lib/aws_sdb/error.rb +0 -42
  111. data/lib/aws_sdb/service.rb +0 -215
  112. data/lib/aws_sdb.rb +0 -3
  113. data/lib/rudy/aws/simpledb.rb +0 -71
  114. data/lib/rudy/cli/addresses.rb +0 -85
  115. data/lib/rudy/cli/backups.rb +0 -175
  116. data/lib/rudy/cli/domains.rb +0 -17
  117. data/lib/rudy/cli/groups.rb +0 -77
  118. data/lib/rudy/cli/images.rb +0 -111
  119. data/lib/rudy/cli/instances.rb +0 -142
  120. data/lib/rudy/cli/keypairs.rb +0 -47
  121. data/lib/rudy/cli/manager.rb +0 -51
  122. data/lib/rudy/cli/release.rb +0 -174
  123. data/lib/rudy/cli/volumes.rb +0 -121
  124. data/lib/rudy/command/addresses.rb +0 -69
  125. data/lib/rudy/command/backups.rb +0 -65
  126. data/lib/rudy/command/disks-old.rb +0 -322
  127. data/lib/rudy/command/disks.rb +0 -9
  128. data/lib/rudy/command/domains.rb +0 -34
  129. data/lib/rudy/command/groups.rb +0 -118
  130. data/lib/rudy/command/instances.rb +0 -278
  131. data/lib/rudy/command/keypairs.rb +0 -149
  132. data/lib/rudy/command/manager.rb +0 -65
  133. data/lib/rudy/command/volumes.rb +0 -127
  134. data/lib/rudy/metadata/disk.rb +0 -149
  135. data/lib/rudy/metadata/machine.rb +0 -34
  136. data/lib/rudy/routines/disk_handler.rb +0 -190
  137. data/lib/rudy/routines/script_runner.rb +0 -65
  138. data/test/50_commands/00_setup_test.rb +0 -11
  139. data/test/50_commands/10_keypairs_test.rb +0 -79
  140. data/test/50_commands/20_groups_test.rb +0 -77
  141. data/test/50_commands/40_volumes_test.rb +0 -55
  142. data/test/50_commands/50_instances_test.rb +0 -110
  143. data/tryouts/console_tryout.rb +0 -91
  144. data/tryouts/disks.rb +0 -55
  145. data/tryouts/nested_methods.rb +0 -36
  146. data/tryouts/session_tryout.rb +0 -46
data/lib/rudy/huxtable.rb CHANGED
@@ -2,96 +2,76 @@
2
2
 
3
3
 
4
4
  module Rudy
5
+
6
+ # = Rudy::Huxtable
7
+ #
8
+ # Huxtable gives access to instances for config, global, and logger to any
9
+ # class that includes it.
10
+ #
11
+ # class Rudy::Hello
12
+ # include Rudy::Huxtable
13
+ #
14
+ # def print_config
15
+ # p self.config.defaults
16
+ # end
17
+ #
18
+ # end
19
+ #
5
20
  module Huxtable
6
- include Rudy::AWS
7
21
 
8
22
  # TODO: investigate @@debug bug. When this is true, Caesars.debug? returns true
9
- # too. It's probably some include thing.
23
+ # too. It's possible this is intentional but probably not.
10
24
  @@debug = false
11
-
12
- attr_accessor :config
13
- attr_accessor :global
14
- attr_accessor :logger
15
25
 
16
- # An instance of Rye::Box for the local machine (running Rudy)
17
- attr_reader :rbox
26
+ @@config = Rudy::Config.new
27
+ @@global = Rudy::Global.new
28
+ @@logger = StringIO.new # BUG: memory-leak for long-running apps
18
29
 
19
- def initialize(opts={})
20
- opts = { :config => nil, :logger => STDERR, :global => OpenStruct.new }.merge(opts)
21
-
22
- # Set instance variables
23
- opts.each_pair { |n,v| self.send("#{n}=", v) if self.respond_to?("#{n}=") }
24
-
25
- unless @config
26
- @config = Rudy::Config.new
27
- @config.look_and_load(@global.config)
28
- end
29
-
30
- init_globals
31
-
32
- @rbox = Rye::Box.new('localhost')
33
-
34
- raise Rudy::NoConfig unless has_keys?
35
- Rudy::AWS.set_access_identifiers(@global.accesskey, @global.secretkey, @logger)
36
-
30
+ @@sacred_params = [:accesskey, :secretkey, :cert, :privatekey]
31
+
32
+ # NOTE: These methods conflict with Drydock::Command classes. It's
33
+ # probably a good idea to not expose these anyway since it can be
34
+ # done via Rudy::Huxtable.update_global etc...
35
+ #def config; @@config; end
36
+ #def global; @@global; end
37
+ #def logger; @@logger; end
38
+
39
+ def self.update_config(path=nil)
40
+ # nil or otherwise bad paths send to look_and_load are ignored
41
+ @@config.look_and_load(path || nil)
42
+ @@global.apply_config(@@config)
37
43
  end
38
44
 
45
+ update_config
39
46
 
47
+ def self.update_global(ghash={})
48
+ @@global.update(ghash)
49
+ end
40
50
 
41
- def init_globals
42
- @global.verbose ||= 0
43
-
44
- @global.cert = File.expand_path(@global.cert || '')
45
- @global.privatekey = File.expand_path(@global.privatekey || '')
46
-
47
- # ATROCIOUS!
48
-
49
- if @config.defaults
50
- @global.region ||= @config.defaults.region
51
- @global.zone ||= @config.defaults.zone
52
- @global.environment ||= @config.defaults.environment
53
- @global.role ||= @config.defaults.role
54
- @global.position ||= @config.defaults.position
55
- @global.user ||= @config.defaults.user
56
- @global.nocolor = @config.defaults.nocolor
57
- @global.quiet = @config.defaults.quiet
58
- end
59
-
60
- @global.region ||= DEFAULT_REGION
61
- @global.zone ||= DEFAULT_ZONE
62
- @global.environment ||= DEFAULT_ENVIRONMENT
63
- @global.role ||= DEFAULT_ROLE
64
- @global.position ||= DEFAULT_POSITION
65
- @global.user ||= DEFAULT_USER
66
- @global.nocolor = false
67
- @global.quiet = false
68
-
69
- @global.position &&= @global.position.to_s.rjust(2, '0')
70
-
71
- if @config.accounts && @config.accounts.aws
72
- @global.accesskey ||= @config.accounts.aws.accesskey
73
- @global.secretkey ||= @config.accounts.aws.secretkey
74
- @global.account ||= @config.accounts.aws.accountnum
75
-
76
- @global.cert ||= @config.accounts.aws.cert
77
- @global.privatekey ||= @config.accounts.aws.privatekey
78
- end
79
-
80
- @global.accesskey ||= ENV['AWS_ACCESS_KEY']
81
- @global.secretkey ||= ENV['AWS_SECRET_KEY'] || ENV['AWS_SECRET_ACCESS_KEY']
82
- @global.account ||= ENV['AWS_ACCOUNT_NUMBER']
83
-
84
- @global.cert ||= ENV['EC2_CERT']
85
- @global.privatekey ||= ENV['EC2_PRIVATE_KEY']
86
-
87
- @global.local_user = ENV['USER'] || :rudy
88
- @global.local_hostname = Socket.gethostname || :localhost
89
-
90
-
91
- String.disable_color if @global.nocolor
92
- Rudy.enable_quiet if @global.quiet
51
+ def self.update_logger(logger)
52
+ @@logger = logger
53
+ end
54
+
55
+ def self.create_domain
56
+ @sdb = Rudy::AWS::SDB.new(@@global.accesskey, @@global.secretkey, @@global.region)
57
+ @sdb.create_domain Rudy::DOMAIN
58
+ end
59
+
60
+ def self.domain_exists?
61
+ @sdb = Rudy::AWS::SDB.new(@@global.accesskey, @@global.secretkey, @@global.region)
62
+ (@sdb.list_domains || []).member? Rudy::DOMAIN
63
+ end
64
+
65
+ def self.domain
66
+ Rudy::DOMAIN
93
67
  end
94
68
 
69
+ def self.change_zone(v); @@global.zone = v; end
70
+ def self.change_role(v); @@global.role = v; end
71
+ def self.change_region(v); @@global.region = v; end
72
+ def self.change_environment(v); @@global.environment = v; end
73
+ def self.change_position(v); @@global.position = v; end
74
+
95
75
  def debug?; @@debug == true; end
96
76
 
97
77
  def check_keys
@@ -101,17 +81,17 @@ module Rudy
101
81
  end
102
82
 
103
83
  def has_pem_keys?
104
- (@global.cert && File.exists?(@global.cert) &&
105
- @global.privatekey && File.exists?(@global.privatekey))
84
+ (@@global.cert && File.exists?(@@global.cert) &&
85
+ @@global.privatekey && File.exists?(@@global.privatekey))
106
86
  end
107
87
 
108
88
  def has_keys?
109
- (@global.accesskey && !@global.accesskey.empty? && @global.secretkey && !@global.secretkey.empty?)
89
+ (@@global.accesskey && !@@global.accesskey.empty? && @@global.secretkey && !@@global.secretkey.empty?)
110
90
  end
111
91
 
112
92
  def config_dirname
113
- raise "No config paths defined" unless @config.is_a?(Rudy::Config) && @config.paths.is_a?(Array)
114
- base_dir = File.dirname @config.paths.first
93
+ raise "No config paths defined" unless @@config.is_a?(Rudy::Config) && @@config.paths.is_a?(Array)
94
+ base_dir = File.dirname @@config.paths.first
115
95
  raise "Config directory doesn't exist #{base_dir}" unless File.exists?(base_dir)
116
96
  base_dir
117
97
  end
@@ -121,41 +101,56 @@ module Rudy
121
101
  (!kp.nil? && File.exists?(kp))
122
102
  end
123
103
 
104
+ # Returns the name of the current keypair for the given user.
105
+ # If there's a private key path in the config this will return
106
+ # the basename (it's assumed the Amazon KeyPair has the same
107
+ # name as the file). Otherwise this returns the Rudy style
108
+ # name: <tt>key-ENV-ROLE-USER</tt>. Or if this the user is
109
+ # root: <tt>key-ENV-ROLE</tt>
124
110
  def user_keypairname(user)
125
111
  kp = user_keypairpath(user)
126
- return unless kp
127
- KeyPairs.path_to_name(kp)
112
+ if kp
113
+ kp = Huxtable.keypair_path_to_name(kp)
114
+ else
115
+ n = (user.to_s == 'root') ? '' : "-#{user}"
116
+ "key-%s%s" % [current_machine_group, n]
117
+ end
118
+ end
119
+ def root_keypairname
120
+ user_keypairname :root
128
121
  end
129
122
 
130
-
131
123
 
132
124
  def user_keypairpath(name)
133
125
  raise "No user provided" unless name
134
- zon, env, rol = @global.zone, @global.environment, @global.role
126
+ raise "No configuration" unless @@config
127
+ raise "No machines configuration" unless @@config.machines
128
+ zon, env, rol = @@global.zone, @@global.environment, @@global.role
135
129
  #Caesars.enable_debug
136
- kp = @config.machines.find_deferred(zon, env, rol, [:users, name, :keypair])
137
- kp ||= @config.machines.find_deferred(env, rol, [:users, name, :keypair])
138
- kp ||= @config.machines.find_deferred(rol, [:users, name, :keypair])
130
+ path = @@config.machines.find_deferred(zon, env, rol, [:users, name, :keypair])
131
+ path ||= @@config.machines.find_deferred(env, rol, [:users, name, :keypair])
132
+ path ||= @@config.machines.find_deferred(rol, [:users, name, :keypair])
139
133
 
140
134
  # EC2 Keypairs that were created are intended for starting the machine instances.
141
135
  # These are used as the root SSH keys. If we can find a user defined key, we'll
142
136
  # check the config path for a generated one.
143
- if !kp && name.to_s == 'root'
137
+ if !path && name.to_s == 'root'
144
138
  path = File.join(self.config_dirname, "key-#{current_machine_group}")
145
- kp = path if File.exists?(path)
146
139
  end
147
-
148
- kp &&= File.expand_path(kp)
149
- kp
140
+ path = File.expand_path(path) if path && File.exists?(path)
141
+ path
150
142
  end
151
-
143
+ def root_keypairpath
144
+ user_keypairpath :root
145
+ end
146
+
152
147
  def has_root_keypair?
153
148
  path = user_keypairpath(:root)
154
149
  (!path.nil? && !path.empty?)
155
150
  end
156
151
 
157
152
  def current_user
158
- @global.user
153
+ @@global.user
159
154
  end
160
155
  def current_user_keypairpath
161
156
  user_keypairpath(current_user)
@@ -166,90 +161,180 @@ module Rudy
166
161
  end
167
162
 
168
163
  def current_machine_group
169
- [@global.environment, @global.role].join(RUDY_DELIM)
164
+ [@@global.environment, @@global.role].join(Rudy::DELIM)
165
+ end
166
+
167
+ def current_group_name
168
+ "g-#{current_machine_group}"
169
+ end
170
+
171
+ def current_machine_count
172
+ fetch_machine_param(:positions) || 1
170
173
  end
171
174
 
172
175
  def current_machine_image
173
- zon, env, rol = @global.zone, @global.environment, @global.role
174
- ami = @config.machines.find_deferred(zon, env, rol, :ami)
175
- ami ||= @config.machines.find_deferred(env, rol, :ami)
176
- ami ||= @config.machines.find_deferred(rol, :ami)
177
- raise Rudy::NoMachineImage, current_machine_group unless ami
178
- ami
176
+ fetch_machine_param(:ami)
177
+ #zon, env, rol = @@global.zone, @@global.environment, @@global.role
178
+ #ami = @@config.machines.find_deferred([zon, env, rol]) || {}
179
+ #ami.merge!(@@config.machines.find_deferred(env, rol, :ami))
180
+ #ami.merge!(@@config.machines.find_deferred(rol, :ami))
181
+ ## I commented this out while cleaning (start of 0.6 branch) . It
182
+ ## seems like a bad idea. I don't want Huxtables throwing exceptions.
183
+ ##raise Rudy::NoMachineImage, current_machine_group unless ami
184
+ #ami
185
+ end
186
+
187
+ def current_machine_size
188
+ fetch_machine_param(:size) || 'm1.small'
179
189
  end
180
190
 
181
191
  def current_machine_address
182
- @config.machines.find_deferred(@global.environment, @global.role, :address)
192
+ raise "No configuration" unless @@config
193
+ raise "No machines configuration" unless @@config.machines
194
+ @@config.machines.find_deferred(@@global.environment, @@global.role, :address)
183
195
  end
184
196
 
185
197
  # TODO: fix machine_group to include zone
186
198
  def current_machine_name
187
- [@global.zone, current_machine_group, @global.position].join(RUDY_DELIM)
199
+ [@@global.zone, current_machine_group, @@global.position].join(Rudy::DELIM)
188
200
  end
189
201
 
190
-
191
-
192
202
  # +name+ the name of the remote user to use for the remainder of the command
193
203
  # (or until switched again). If no name is provided, the user will be revert
194
204
  # to whatever it was before the previous switch.
195
205
  # TODO: deprecate
196
206
  def switch_user(name=nil)
197
207
  if name == nil && @switch_user_previous
198
- @global.user = @switch_user_previous
199
- elsif @global.user != name
208
+ @@global.user = @switch_user_previous
209
+ elsif @@global.user != name
200
210
  raise "No root keypair defined for #{name}!" unless has_keypair?(name)
201
- @logger.puts "Remote commands will be run as #{name} user"
202
- @switch_user_previous = @global.user
203
- @global.user = name
211
+ @@logger.puts "Remote commands will be run as #{name} user"
212
+ @switch_user_previous = @@global.user
213
+ @@global.user = name
204
214
  end
205
215
  end
206
216
 
207
- # Returns a hash of info for the requested machine. If the requested machine
208
- # is not running, it will raise an exception.
209
- def current_machine
210
- find_machine(current_machine_group)
217
+ def group_metadata(env=@@global.environment, role=@@global.role)
218
+ query = "['environment' = '#{env}'] intersection ['role' = '#{role}']"
219
+ @sdb.query_with_attributes(Rudy::DOMAIN, query)
211
220
  end
212
-
213
- def find_machine(group)
214
- machine_list = @ec2.instances.list(group)
215
- machine = machine_list.values.first # NOTE: Only one machine per group, for now...
216
- raise "There's no machine running in #{group}" unless machine
217
- raise "The primary machine in #{group} is not in a running state" unless machine[:aws_state] == 'running'
218
- machine
221
+
222
+ def self.keypair_path_to_name(kp)
223
+ return nil unless kp
224
+ name = File.basename kp
225
+ #name.gsub(/key-/, '') # We keep the key- now
219
226
  end
220
227
 
221
-
228
+ private
222
229
 
223
- def group_metadata(env=@global.environment, role=@global.role)
224
- query = "['environment' = '#{env}'] intersection ['role' = '#{role}']"
225
- @sdb.query_with_attributes(RUDY_DOMAIN, query)
226
- end
227
230
 
228
- # * +opts+
229
- # :recursive => false, :preserve => false, :chunk_size => 16384
230
- def scp(task, host, user, keypairpath, paths, dest, opts)
231
- opts = {
232
- :recursive => false, :preserve => false, :chunk_size => 16384
233
- }.merge(opts)
231
+ # We grab the appropriate routines config and check the paths
232
+ # against those defined for the matching machine group.
233
+ # Disks that appear in a routine but not in a machine will be
234
+ # removed and a warning printed. Otherwise, the routines config
235
+ # is merged on top of the machine config and that's what we return.
236
+ #
237
+ # This means that all the disk info is returned so we know what
238
+ # size they are and stuff.
239
+ # Return a hash:
240
+ #
241
+ # :after:
242
+ # - :root: pwd
243
+ # - :rudy: pwd
244
+ # :disks:
245
+ # :create:
246
+ # /rudy/example1:
247
+ # :device: /dev/sdr
248
+ # :size: 2
249
+ # /rudy/example2:
250
+ # :device: /dev/sdm
251
+ # :size: 1
252
+ #
253
+ def fetch_routine_config(action)
254
+ raise "No configuration" unless @@config
255
+ raise "No routines configuration" unless @@config.routines
256
+ raise "No globals" unless @@global
257
+
258
+ zon, env, rol = @@global.zone, @@global.environment, @@global.role
234
259
 
235
- Net::SCP.start(host, user, :keys => [keypairpath]) do |scp|
236
- paths.each do |path|
237
- prev_path = nil
238
- scp.send("#{task}!", path, dest, opts) do |ch, name, sent, total|
239
- msg = ((prev_path == name) ? "\r" : "\n") # new line for new file
240
- msg << "#{name}: #{sent}/#{total}" # otherwise, update the same line
241
- @logger.print msg
242
- @logger.flush # update the screen every cycle
243
- prev_path = name
260
+ disk_defs = fetch_machine_param(:disks)
261
+
262
+ routine = @@config.routines.find(@@global.environment, @@global.role, action)
263
+ return nil unless routine
264
+
265
+ routine.disks.each_pair do |raction,disks|
266
+
267
+ disks.each_pair do |path, props|
268
+ unless disk_defs.has_key?(path)
269
+ @logger.puts "#{path} is not defined. Check your #{action} routines config.".color(:red)
270
+ routine.disks[raction].delete(path)
271
+ next
244
272
  end
245
- @logger.puts unless prev_path == path
273
+
274
+ routine.disks[raction][path] = disk_defs[path].merge(props)
275
+
246
276
  end
247
277
  end
278
+
279
+ routine
248
280
  end
249
281
 
282
+ # Looks for ENV-ROLE configuration in machines. There must be
283
+ # at least one definition in the config for this to return true
284
+ # That's how Rudy knows the current group is defined.
285
+ def known_machine_group?
286
+ return false if !@@config && !@@global
287
+ zon, env, rol = @@global.zone, @@global.environment, @@global.role
288
+ conf = @@config.machines.find_deferred(@@global.region, zon, [env, rol])
289
+ conf ||= @@config.machines.find_deferred(zon, [env, rol])
290
+ !conf.nil?
291
+ end
250
292
 
293
+ def fetch_machine_param(parameter)
294
+ raise "No configuration" unless @@config
295
+ raise "No machines configuration" unless @@config.machines
296
+ raise "No globals" unless @@global
297
+ top_level = @@config.machines.find(parameter)
298
+ mc = fetch_machine_config
299
+ mc[parameter] || top_level || nil
300
+ end
301
+
302
+ def fetch_machine_config
303
+ raise "No configuration" unless @@config
304
+ raise "No machines configuration" unless @@config.machines
305
+ raise "No globals" unless @@global
306
+ zon, env, rol = @@global.zone, @@global.environment, @@global.role
307
+ hashes = []
308
+ hashes << @@config.machines.find(env, rol)
309
+ hashes << @@config.machines.find(zon, env, rol)
310
+ hashes << @@config.machines.find(zon, [env, rol])
311
+ hashes << @@config.machines.find(zon, env)
312
+ hashes << @@config.machines.find(env)
313
+ hashes << @@config.machines.find(zon)
314
+ compilation = {}
315
+ hashes.reverse.each do |conf|
316
+ compilation.merge! conf if conf
317
+ end
318
+ compilation = nil if compilation.empty?
319
+ compilation
320
+ end
321
+
322
+ # Returns the appropriate config block from the machines config.
323
+ # Also adds the following unless otherwise specified:
324
+ # :region, :zone, :environment, :role, :position
325
+ def fetch_script_config
326
+ sconf = fetch_machine_param :config
327
+ extras = {
328
+ :region => @@global.region,
329
+ :zone => @@global.zone,
330
+ :environment => @@global.environment,
331
+ :role => @@global.role,
332
+ :position => @@global.position
333
+ }
334
+ sconf.merge! extras
335
+ sconf
336
+ end
251
337
 
252
- private
253
338
 
254
339
  end
255
- end
340
+ end