solutious-rudy 0.8.5 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. data/CHANGES.txt +55 -14
  2. data/README.rdoc +26 -34
  3. data/Rudyfile +27 -32
  4. data/bin/rudy +47 -27
  5. data/bin/rudy-ec2 +2 -16
  6. data/bin/rudy-s3 +0 -10
  7. data/bin/rudy-sdb +11 -12
  8. data/lib/rudy.rb +49 -89
  9. data/lib/rudy/aws.rb +2 -43
  10. data/lib/rudy/aws/ec2.rb +57 -20
  11. data/lib/rudy/aws/ec2/address.rb +10 -11
  12. data/lib/rudy/aws/ec2/group.rb +10 -9
  13. data/lib/rudy/aws/ec2/image.rb +8 -8
  14. data/lib/rudy/aws/ec2/instance.rb +15 -16
  15. data/lib/rudy/aws/ec2/keypair.rb +14 -19
  16. data/lib/rudy/aws/ec2/snapshot.rb +16 -9
  17. data/lib/rudy/aws/ec2/volume.rb +37 -24
  18. data/lib/rudy/aws/ec2/zone.rb +5 -4
  19. data/lib/rudy/aws/s3.rb +2 -1
  20. data/lib/rudy/aws/sdb.rb +35 -86
  21. data/lib/rudy/backups.rb +24 -0
  22. data/lib/rudy/cli.rb +5 -131
  23. data/lib/rudy/cli/aws/ec2/addresses.rb +19 -27
  24. data/lib/rudy/cli/aws/ec2/candy.rb +32 -7
  25. data/lib/rudy/cli/aws/ec2/groups.rb +9 -13
  26. data/lib/rudy/cli/aws/ec2/images.rb +5 -133
  27. data/lib/rudy/cli/aws/ec2/instances.rb +25 -25
  28. data/lib/rudy/cli/aws/ec2/keypairs.rb +7 -11
  29. data/lib/rudy/cli/aws/ec2/snapshots.rb +5 -9
  30. data/lib/rudy/cli/aws/ec2/volumes.rb +22 -23
  31. data/lib/rudy/cli/aws/ec2/zones.rb +2 -3
  32. data/lib/rudy/cli/aws/sdb/domains.rb +5 -6
  33. data/lib/rudy/cli/aws/sdb/objects.rb +33 -0
  34. data/lib/rudy/cli/aws/sdb/select.rb +23 -0
  35. data/lib/rudy/cli/backups.rb +38 -0
  36. data/lib/rudy/cli/base.rb +98 -0
  37. data/lib/rudy/cli/candy.rb +1 -2
  38. data/lib/rudy/cli/config.rb +7 -4
  39. data/lib/rudy/cli/disks.rb +6 -8
  40. data/lib/rudy/cli/execbase.rb +53 -0
  41. data/lib/rudy/cli/machines.rb +14 -18
  42. data/lib/rudy/cli/metadata.rb +24 -10
  43. data/lib/rudy/cli/routines.rb +31 -5
  44. data/lib/rudy/config.rb +17 -22
  45. data/lib/rudy/config/objects.rb +44 -30
  46. data/lib/rudy/disks.rb +25 -0
  47. data/lib/rudy/exceptions.rb +95 -0
  48. data/lib/rudy/global.rb +46 -23
  49. data/lib/rudy/guidelines.rb +3 -2
  50. data/lib/rudy/huxtable.rb +66 -57
  51. data/lib/rudy/machines.rb +41 -263
  52. data/lib/rudy/metadata.rb +212 -38
  53. data/lib/rudy/metadata/backup.rb +123 -78
  54. data/lib/rudy/metadata/disk.rb +152 -170
  55. data/lib/rudy/metadata/machine.rb +179 -0
  56. data/lib/rudy/mixins.rb +2 -1
  57. data/lib/rudy/mixins/hash.rb +3 -1
  58. data/lib/rudy/mixins/symbol.rb +8 -0
  59. data/lib/rudy/routines.rb +127 -344
  60. data/lib/rudy/routines/base.rb +226 -0
  61. data/lib/rudy/routines/handlers/base.rb +48 -0
  62. data/lib/rudy/routines/handlers/depends.rb +49 -0
  63. data/lib/rudy/routines/handlers/disks.rb +239 -0
  64. data/lib/rudy/routines/handlers/group.rb +42 -0
  65. data/lib/rudy/routines/handlers/host.rb +72 -0
  66. data/lib/rudy/routines/handlers/keypair.rb +70 -0
  67. data/lib/rudy/routines/handlers/machines.rb +15 -0
  68. data/lib/rudy/routines/handlers/script.rb +85 -0
  69. data/lib/rudy/routines/handlers/user.rb +41 -0
  70. data/lib/rudy/routines/passthrough.rb +19 -23
  71. data/lib/rudy/routines/reboot.rb +82 -51
  72. data/lib/rudy/routines/shutdown.rb +61 -14
  73. data/lib/rudy/routines/startup.rb +111 -17
  74. data/lib/rudy/utils.rb +12 -59
  75. data/rudy.gemspec +85 -25
  76. data/tryouts/01_mixins/01_hash_tryouts.rb +20 -0
  77. data/tryouts/10_require_time/10_rudy_tryouts.rb +33 -0
  78. data/tryouts/10_require_time/15_global_tryouts.rb +58 -0
  79. data/tryouts/12_config/10_load_config_tryouts.rb +43 -0
  80. data/tryouts/12_config/20_defaults_tryouts.rb +16 -0
  81. data/tryouts/12_config/30_accounts_tryouts.rb +17 -0
  82. data/tryouts/12_config/40_machines_tryouts.rb +53 -0
  83. data/tryouts/12_config/50_commands_tryouts.rb +17 -0
  84. data/tryouts/12_config/60_routines_tryouts.rb +16 -0
  85. data/tryouts/15_huxtable/10_huxtable_tryouts.rb +47 -0
  86. data/tryouts/15_huxtable/20_user_tryouts.rb +47 -0
  87. data/tryouts/20_simpledb/10_domains_tryouts.rb +36 -0
  88. data/tryouts/20_simpledb/20_objects_tryouts.rb +56 -0
  89. data/tryouts/25_ec2/10_keypairs_tryouts.rb +54 -0
  90. data/tryouts/25_ec2/20_groups_tryouts.rb +56 -0
  91. data/tryouts/25_ec2/21_groups_authorize_address_tryouts.rb +53 -0
  92. data/tryouts/25_ec2/22_groups_authorize_account_tryouts.rb +54 -0
  93. data/tryouts/25_ec2/30_addresses_tryouts.rb +42 -0
  94. data/tryouts/25_ec2/40_volumes_tryouts.rb +53 -0
  95. data/tryouts/25_ec2/50_snapshots_tryouts.rb +75 -0
  96. data/tryouts/26_ec2_instances/10_instance_tryouts.rb +107 -0
  97. data/tryouts/26_ec2_instances/50_images_tryouts.rb +7 -0
  98. data/tryouts/30_metadata/10_include_tryouts.rb +45 -0
  99. data/tryouts/30_metadata/13_object_tryouts.rb +19 -0
  100. data/tryouts/30_metadata/50_disk_tryouts.rb +115 -0
  101. data/tryouts/30_metadata/51_disk_digest_tryouts.rb +24 -0
  102. data/tryouts/30_metadata/53_disk_list_tryouts.rb +35 -0
  103. data/tryouts/30_metadata/56_disk_volume_tryouts.rb +68 -0
  104. data/tryouts/30_metadata/60_backup_tryouts.rb +101 -0
  105. data/tryouts/30_metadata/63_backup_list_tryouts.rb +38 -0
  106. data/tryouts/30_metadata/64_backup_disk_tryouts.rb +65 -0
  107. data/tryouts/30_metadata/66_backup_snapshot_tryouts.rb +76 -0
  108. data/tryouts/30_metadata/70_machine_tryouts.rb +85 -0
  109. data/tryouts/30_metadata/73_machine_list_tryouts.rb +58 -0
  110. data/tryouts/30_metadata/76_machine_instance_tryouts.rb +64 -0
  111. data/tryouts/30_metadata/77_machines_tryouts.rb +45 -0
  112. data/tryouts/40_routines/10_keypair_handler_tryouts.rb +52 -0
  113. data/tryouts/40_routines/11_group_handler_tryouts.rb +36 -0
  114. data/tryouts/80_cli/10_rudyec2_tryouts.rb +8 -0
  115. data/tryouts/80_cli/60_rudy_tryouts.rb +41 -0
  116. data/tryouts/failer +6 -0
  117. data/tryouts/misc/console_tryout.rb +91 -0
  118. data/tryouts/misc/disks_tryout.rb +48 -0
  119. data/tryouts/misc/drydock_tryout.rb +48 -0
  120. data/tryouts/misc/nested_methods.rb +103 -0
  121. data/tryouts/misc/session_tryout.rb +46 -0
  122. data/tryouts/misc/tryouts.rb +33 -0
  123. data/tryouts/misc/usage_tryout.rb +23 -0
  124. metadata +119 -31
  125. data/bin/ird +0 -153
  126. data/lib/rudy/metadata/backups.rb +0 -67
  127. data/lib/rudy/metadata/debug.rb +0 -38
  128. data/lib/rudy/metadata/disks.rb +0 -67
  129. data/lib/rudy/metadata/objectbase.rb +0 -108
  130. data/lib/rudy/routines/helper.rb +0 -76
  131. data/lib/rudy/routines/helpers/dependshelper.rb +0 -34
  132. data/lib/rudy/routines/helpers/diskhelper.rb +0 -403
  133. data/lib/rudy/routines/helpers/scripthelper.rb +0 -197
  134. data/lib/rudy/routines/helpers/userhelper.rb +0 -37
  135. data/support/rudy-ec2-startup +0 -200
@@ -1,67 +0,0 @@
1
-
2
-
3
- module Rudy
4
- class Backups
5
- include Rudy::MetaData
6
-
7
- def init
8
- now = Time.now.utc
9
- datetime = Rudy::MetaData::Backup.format_timestamp(now).split(Rudy::DELIM)
10
- @created = now.to_i
11
- @date, @time, @second = datetime
12
- end
13
-
14
- #def create(&each_mach)
15
-
16
- #end
17
-
18
-
19
- def destroy(&each_mach)
20
- list do |backup|
21
- puts "Destroying #{backup.name}"
22
- backup.destroy
23
- end
24
- end
25
-
26
- def list(more=[], less=[], local={}, &each_backup)
27
- backups = list_as_hash(more, less, local, &each_backup)
28
- backups &&= backups.values
29
- backups
30
- end
31
-
32
- def list_as_hash(more=[], less=[], local={}, &each_backup)
33
- more ||= []
34
- more += [:rtype, 'back']
35
- query = to_select(more, less, local)
36
- list = @sdb.select(query) || {}
37
- backups = {}
38
- list.each_pair do |n,d|
39
- backups[n] = Rudy::MetaData::Backup.from_hash(d)
40
- end
41
- backups.each_pair { |n,backup| each_backup.call(backup) } if each_backup
42
- backups = nil if backups.empty?
43
- backups
44
- end
45
-
46
- def get(rname=nil)
47
- dhash = @sdb.get(Rudy::DOMAIN, rname)
48
- return nil if dhash.nil? || dhash.empty?
49
- d = Rudy::MetaData::Backup.from_hash(dhash)
50
- d.update if d
51
- d
52
- end
53
-
54
-
55
- def running?
56
- !list.nil?
57
- # TODO: add logic that checks whether the instances are running.
58
- end
59
-
60
- def to_select(*args)
61
- query = super(*args)
62
- query << " and created != '0' order by created desc"
63
- end
64
-
65
- end
66
- end
67
-
@@ -1,38 +0,0 @@
1
-
2
-
3
- module Rudy; module MetaData
4
- class Debug
5
- include Rudy::MetaData
6
-
7
- def init
8
- end
9
-
10
- def list(more=[], less=[], local={}, &block)
11
- objects = list_as_hash(more, less, local, &block)
12
- objects &&= objects.values
13
- objects
14
- end
15
-
16
- def list_as_hash(more=[], less=[], local={}, &block)
17
- query = to_select(more, less, local)
18
- list = @sdb.select(query) || {}
19
- objects = {}
20
- list.each_pair do |n,d|
21
- objects[n] = d
22
- end
23
- objects.each_pair { |n,obj| block.call(obj) } if block
24
- objects = nil if objects.empty?
25
- objects
26
- end
27
-
28
-
29
- def to_select(more, less, local)
30
- query = super(more, less, local)
31
- # query << " order by created desc"
32
- puts query if @@global.verbose > 0
33
- query
34
- end
35
-
36
- end
37
- end; end
38
-
@@ -1,67 +0,0 @@
1
-
2
-
3
- module Rudy
4
- class Disks
5
- include Rudy::MetaData
6
-
7
-
8
- def init
9
- @rback = Rudy::Backups.new
10
- end
11
-
12
- #def create(&each_mach)
13
-
14
- #end
15
-
16
-
17
- def destroy(&each_mach)
18
- #raise MachineGroupNotRunning, current_machine_group unless running?
19
- #raise MachineGroupNotDefined, current_machine_group unless known_machine_group?
20
- list do |disk|
21
- puts "Destroying #{disk.name}"
22
- disk.destroy
23
- end
24
- end
25
-
26
- def backups
27
- @rback.list()
28
- end
29
-
30
- def list(more=[], less=[], &each_disk)
31
- disks = list_as_hash(&each_disk)
32
- disks &&= disks.values
33
- disks
34
- end
35
-
36
- def list_as_hash(more=[], less=[], &each_disk)
37
- query = to_select([:rtype, 'disk'], less)
38
- list = @sdb.select(query) || {}
39
- disks = {}
40
- list.each_pair do |n,d|
41
- disks[n] = Rudy::MetaData::Disk.from_hash(d)
42
- end
43
- disks.each_pair { |n,disk| each_disk.call(disk) } if each_disk
44
- disks = nil if disks.empty?
45
- disks
46
- end
47
-
48
- def get(rname=nil)
49
- dhash = @sdb.get(Rudy::DOMAIN, rname)
50
- return nil if dhash.nil? || dhash.empty?
51
- d = Rudy::MetaData::Disk.from_hash(dhash)
52
- d.update if d
53
- d
54
- end
55
-
56
-
57
- def running?
58
- !list.nil?
59
- # TODO: add logic that checks whether the instances are running.
60
- end
61
-
62
-
63
-
64
-
65
- end
66
- end
67
-
@@ -1,108 +0,0 @@
1
-
2
-
3
- module Rudy
4
- module MetaData
5
- module ObjectBase
6
- include Rudy::Huxtable
7
-
8
- attr_accessor :sdb_domain
9
-
10
- def initialize(*args)
11
- a, s, r = @@global.accesskey, @@global.secretkey, @@global.region
12
- @sdb = Rudy::AWS::SDB.new(a, s, r)
13
- @ec2inst = Rudy::AWS::EC2::Instances.new(a, s, r)
14
- @rvol = Rudy::AWS::EC2::Volumes.new(a, s, r)
15
- @radd = Rudy::AWS::EC2::Addresses.new(a, s, r)
16
- @rsnap = Rudy::AWS::EC2::Snapshots.new(a, s, r)
17
- @sdb_domain = Rudy::DOMAIN
18
- init(*args)
19
- end
20
-
21
- def init(*args); raise "Must override init"; end
22
-
23
- def valid?; raise "#{self.class} must override 'valid?'"; end
24
-
25
- def to_query(more=[], less=[])
26
- Rudy::AWS::SDB.generate_query build_criteria(more, less)
27
- end
28
-
29
- def to_select(more=[], less=[])
30
- s = Rudy::AWS::SDB.generate_select ['*'], @sdb_domain, build_criteria(more, less)
31
- s
32
- end
33
-
34
- def name(identifier, zon, env, rol, pos, *other)
35
- pos = pos.to_s.rjust 2, '0'
36
- [identifier, zon, env, rol, pos, *other].flatten.compact.join(Rudy::DELIM)
37
- end
38
-
39
- def save(replace=true)
40
- replace = true if replace.nil?
41
- @sdb ||= Rudy::AWS::SDB.new(@@global.accesskey, @@global.secretkey, @@global.region)
42
- @sdb.put(@sdb_domain, name, self.to_hash, replace) # Always returns nil
43
- true
44
- end
45
-
46
- def destroy
47
- @sdb ||= Rudy::AWS::SDB.new(@@global.accesskey, @@global.secretkey, @@global.region)
48
- @sdb.destroy(@sdb_domain, name)
49
- true
50
- end
51
-
52
- def refresh
53
- @sdb ||= Rudy::AWS::SDB.new(@@global.accesskey, @@global.secretkey, @@global.region)
54
- h = @sdb.get(@sdb_domain, name) || {}
55
- from_hash(h)
56
- end
57
-
58
- def ==(other)
59
- return false unless other.is_a?(self.class)
60
- self.name == other.name
61
- end
62
-
63
- # A generic default
64
- def to_s
65
- str = ""
66
- field_names.each do |key|
67
- str << sprintf(" %22s: %s#{$/}", key, self.send(key.to_sym))
68
- end
69
- str
70
- end
71
-
72
- def liner_note
73
- info = @awsid && !@awsid.empty? ? @awsid : "[no aws object]"
74
- "%s %s" % [self.name.bright, info]
75
- end
76
-
77
- def inspect
78
- lines = []
79
- lines << liner_note
80
- field_names.each do |key|
81
- next unless self.respond_to?(key)
82
- val = self.send(key)
83
- lines << sprintf(" %22s: %s", key, (val.is_a?(Array) ? val.join(', ') : val))
84
- end
85
- lines.join($/)
86
- end
87
-
88
- protected
89
-
90
- # Builds a zipped Array from a list of criteria.
91
- # The list of criteria is made up of metadata object attributes.
92
- # The list is constructed by taking the adding +more+, and
93
- # subtracting +less+ from <tt>:rtype, :region, :zone, :environment, :role, :position</tt>
94
- #
95
- # Returns [[:rtype, value], [:zone, value], ...]
96
- def build_criteria(more=[], less=[])
97
- criteria = [:rtype, :region, :zone, :environment, :role, :position, *more].compact
98
- criteria -= [*less].flatten.uniq.compact
99
- values = criteria.collect do |n|
100
- self.send(n.to_sym)
101
- end
102
- criteria.zip(values)
103
- end
104
-
105
- end
106
- end
107
- end
108
-
@@ -1,76 +0,0 @@
1
-
2
-
3
- module Rudy
4
- module Routines
5
- module HelperBase
6
- include Rudy::Huxtable
7
-
8
- def trap_rbox_errors(ret=nil, &command)
9
- begin
10
- ret = command.call if command
11
- return unless ret.is_a?(Rye::Rap)
12
- puts ' ' << ret.stdout.join("#{$/} ") if !ret.stdout.empty?
13
- print_response(ret)
14
- rescue IOError => ex
15
- STDERR.puts " Connection Error (#{ex.message})".color(:red)
16
- choice = Annoy.get_user_input('(S)kip (R)etry (A)bort: ') || ''
17
- if choice.match(/\AS/i)
18
- return
19
- elsif choice.match(/\AR/i)
20
- retry
21
- else
22
- exit 12
23
- end
24
- rescue Rye::CommandError => ex
25
- print_response(ex)
26
- choice = Annoy.get_user_input('(S)kip (R)etry (A)bort: ') || ''
27
- if choice.match(/\AS/i)
28
- return
29
- elsif choice.match(/\AR/i)
30
- retry
31
- else
32
- exit 12
33
- end
34
- rescue Rye::CommandNotFound => ex
35
- STDERR.puts " CommandNotFound: #{ex.message}".color(:red)
36
- STDERR.puts ex.backtrace if Rudy.debug?
37
- choice = Annoy.get_user_input('(S)kip (R)etry (A)bort: ') || ''
38
- if choice.match(/\AS/i)
39
- return
40
- elsif choice.match(/\AR/i)
41
- retry
42
- else
43
- exit 12
44
- end
45
- end
46
-
47
- ret
48
- end
49
-
50
- def keep_going?
51
- Annoy.pose_question(" Keep going?\a ", /yes|y|ya|sure|you bet!/i, STDERR)
52
- end
53
-
54
- # Returns a formatted string for printing command info
55
- def command_separator(cmd, user, host)
56
- cmd ||= ""
57
- cmd, user = cmd.to_s, user.to_s
58
- prompt = user == "root" ? "#" : "$"
59
- ("%s@%s%s %s" % [user, host, prompt, cmd.bright])
60
- end
61
-
62
- private
63
- def print_response(rap)
64
- colour = rap.exit_code != 0 ? :red : :normal
65
- [:stderr].each do |sumpin|
66
- next if rap.send(sumpin).empty?
67
- STDERR.puts
68
- STDERR.puts((" #{sumpin.to_s.upcase} " << '-'*38).color(colour).bright)
69
- STDERR.puts " " << rap.send(sumpin).join("#{$/} ").color(colour)
70
- end
71
- STDERR.puts " Exit code: #{rap.exit_code}".color(colour) if rap.exit_code != 0
72
- end
73
-
74
- end
75
- end
76
- end
@@ -1,34 +0,0 @@
1
-
2
- module Rudy; module Routines;
3
- module DependsHelper
4
- include Rudy::Routines::HelperBase # TODO: use trap_rbox_errors
5
- extend self
6
-
7
- def has_depends?(timing, routine)
8
- (!routine.is_a?(Caesars::Hash) || routine[timing].is_a?(Caesars::Hash))
9
- end
10
-
11
- # Returns an Array of the dependent routines for the given +timing+ (before/after)
12
- def get(timing, routine)
13
- return if !(routine.is_a?(Caesars::Hash) || routine[timing].is_a?(Caesars::Hash))
14
-
15
- # This will produce an Array containing the routines to run. The
16
- # elements are the valid routine names.
17
- # NOTE: The "timing" elements are removed from the routines hash.
18
- dependencies = []
19
- routine[timing].each_pair do |n,v|
20
- next unless v.nil? # this skips all "script" blocks
21
- raise "#{timing}: #{n} is not a known routine" unless valid_routine?(n)
22
- routine[timing].delete(n)
23
- dependencies << n
24
- end
25
-
26
- # We need to return only the keys b/c the values are nil
27
- dependencies = nil if dependencies.empty?
28
- dependencies
29
- end
30
-
31
-
32
- end
33
-
34
- end; end
@@ -1,403 +0,0 @@
1
-
2
-
3
- module Rudy; module Routines;
4
- module DiskHelper
5
- include Rudy::Routines::HelperBase # TODO: use trap_rbox_errors
6
- extend self
7
-
8
- def disks?(routine)
9
- (routine.is_a?(Caesars::Hash) && routine.disks &&
10
- routine.disks.is_a?(Caesars::Hash) && !routine.disks.empty?) ? true : false
11
- end
12
-
13
- def paths(routine)
14
- return nil unless disks?(routine)
15
- routine.disks.values.collect { |d| d.keys }.flatten
16
- end
17
-
18
- def execute(routine, machine, rbox)
19
- return unless routine
20
- raise "Not a Rudy::Machine" unless machine.is_a?(Rudy::Machine)
21
- raise "Not a Rye::Box" unless rbox.is_a?(Rye::Box)
22
-
23
- @machine = machine
24
- @rbox = rbox
25
-
26
- # We need to add mkfs since it's not enabled by default.
27
- # We add it only to this instance we're using.
28
- # We give it a funny name so we can delete it.
29
- def @rbox.rudy_mkfs(*args); cmd('mkfs', args); end
30
-
31
- unless disks?(routine)
32
- STDERR.puts "[nothing to do]"
33
- return
34
- end
35
-
36
- modified = []
37
- routine.disks.each_pair do |action, disks|
38
- unless DiskHelper.respond_to?(action)
39
- STDERR.puts %Q(DiskHelper: unknown action "#{action}")
40
- next
41
- end
42
- send(action, disks) # create, copy, destroy, ...
43
- modified << disks
44
- end
45
-
46
- # TODO: remove rudy_mkfs method
47
-
48
- end
49
-
50
- def snapshot(disks)
51
- rdisk = Rudy::Disks.new
52
- rback = Rudy::Backups.new
53
-
54
- disks.each_pair do |path, props|
55
- adisk = Rudy::MetaData::Disk.new(path, props[:size], props[:device], @machine.position)
56
- disk = rdisk.get(adisk.name)
57
- if disk == nil
58
- puts "Not found: #{adisk.name}".color(:red)
59
- return
60
- end
61
- back = disk.backup
62
- puts "Created backup: #{back.name}"
63
- end
64
- end
65
-
66
- def restore(disks)
67
- rdisk = Rudy::Disks.new
68
- rback = Rudy::Backups.new
69
-
70
- disks.each_pair do |path, props|
71
- disk = Rudy::MetaData::Disk.new(path, props[:size], props[:device], @machine.position)
72
-
73
- olddisk = rdisk.get(disk.name)
74
- back = nil
75
- if olddisk && olddisk.exists?
76
- olddisk.update
77
- puts "Disk found: #{olddisk.name}. Skipping...".color(:red)
78
- return
79
- else
80
- disk.fstype = props[:fstype] || 'ext3'
81
- more = [:environment, props[:environment]] if props[:environment]
82
- more += [:role, props[:role]] if props[:role]
83
- back = (rback.list(more, nil) || []).first
84
- raise "No backup found" unless back
85
- puts "Found backup #{back.name} "
86
- end
87
-
88
- unless disk.exists? # Checks the EBS volume
89
- msg = "Creating volume from snapshot (#{back.awsid})... "
90
- disk.create(back.size, @@global.zone, back.awsid)
91
- Rudy::Utils.waiter(2, 60, STDOUT, msg) {
92
- disk.available?
93
- }
94
- end
95
-
96
- msg = "Attaching #{disk.awsid} to #{@machine.awsid}... "
97
- disk.attach(@machine.awsid)
98
- Rudy::Utils.waiter(2, 10, STDOUT, msg) {
99
- disk.attached?
100
- }
101
-
102
- sleep 2
103
-
104
- begin
105
- @rbox.mkdir(:p, disk.path)
106
-
107
- print "Mounting at #{disk.path}... "
108
-
109
- ret = @rbox.mount(:t, disk.fstype, disk.device, disk.path)
110
- print_response ret
111
- if ret.exit_code > 0
112
- STDERR.puts "Error creating disk".color(:red)
113
- return
114
- else
115
- puts "done"
116
- end
117
- disk.mounted = true
118
- disk.save
119
-
120
- rescue Net::SSH::AuthenticationFailed, Net::SSH::HostKeyMismatch => ex
121
- STDERR.puts "Error creating disk".color(:red)
122
- STDERR.puts ex.message.color(:red)
123
- rescue Rye::CommandNotFound => ex
124
- puts " CommandNotFound: #{ex.message}".color(:red)
125
-
126
- rescue
127
- STDERR.puts "Error creating disk" .color(:red)
128
- Rudy::Utils.bug
129
- end
130
-
131
- end
132
- end
133
-
134
-
135
- def create(disks)
136
- rdisk = Rudy::Disks.new
137
-
138
- disks.each_pair do |path, props|
139
- disk = Rudy::MetaData::Disk.new(path, props[:size], props[:device], @machine.position)
140
- olddisk = rdisk.get(disk.name)
141
- if olddisk && olddisk.exists?
142
- olddisk.update
143
- puts "Disk found: #{olddisk.name}"
144
- if olddisk.attached?
145
- puts "In use. Skipping...".color(:red)
146
- return
147
- else
148
- disk = olddisk
149
- end
150
- else
151
- puts "Creating #{disk.name} "
152
- disk.fstype = props[:fstype] || 'ext3'
153
- end
154
-
155
- unless disk.exists? # Checks the EBS volume
156
- msg = "Creating volume... "
157
- disk.create
158
- Rudy::Utils.waiter(2, 60, STDOUT, msg) {
159
- disk.available?
160
- }
161
- end
162
-
163
- msg = "Attaching #{disk.awsid} to #{@machine.awsid}... "
164
- disk.attach(@machine.awsid)
165
- Rudy::Utils.waiter(2, 10, STDOUT, msg) {
166
- disk.attached?
167
- }
168
-
169
- # The device needs some time.
170
- # Otherwise mkfs returns:
171
- # "No such file or directory while trying to determine filesystem size"
172
- sleep 2
173
-
174
- # TODO: Cleanup. See ScriptHelper
175
- begin
176
- if disk.raw == true
177
- print "Creating #{disk.fstype} filesystem for #{disk.device}... "
178
- @rbox.rudy_mkfs(:t, disk.fstype, :F, disk.device)
179
- disk.raw = false
180
- disk.save
181
- puts "done"
182
- end
183
-
184
- @rbox.mkdir(:p, disk.path)
185
-
186
- print "Mounting at #{disk.path}... "
187
-
188
- ret = @rbox.mount(:t, disk.fstype, disk.device, disk.path)
189
- print_response ret
190
- if ret.exit_code > 0
191
- STDERR.puts "Error creating disk".color(:red)
192
- return
193
- else
194
- puts "done"
195
- end
196
- disk.mounted = true
197
- disk.save
198
-
199
- rescue Net::SSH::AuthenticationFailed, Net::SSH::HostKeyMismatch => ex
200
- STDERR.puts "Error creating disk".color(:red)
201
- STDERR.puts ex.message.color(:red)
202
- rescue Rye::CommandNotFound => ex
203
- puts " CommandNotFound: #{ex.message}".color(:red)
204
-
205
- rescue
206
- STDERR.puts "Error creating disk" .color(:red)
207
- Rudy::Utils.bug
208
- end
209
-
210
- end
211
- end
212
-
213
-
214
-
215
-
216
- def attach(disks)
217
- rdisk = Rudy::Disks.new
218
-
219
- disks.each_pair do |path, props|
220
- disk = Rudy::MetaData::Disk.new(path, props[:size], props[:device], @machine.position)
221
- olddisk = rdisk.get(disk.name)
222
- if olddisk && olddisk.exists?
223
- disk.update
224
- puts "Disk found: #{olddisk.name}"
225
- if disk.attached?
226
- puts "In use. Skipping...".color(:red)
227
- return
228
- else
229
- disk = olddisk
230
- end
231
- else
232
- puts "Creating #{disk.name} "
233
- end
234
- p disk
235
- p [disk.exists?, disk.available?, olddisk.exists?, olddisk.available?]
236
- disk.save
237
-
238
- unless disk.exists? # Checks the EBS volume
239
- msg = "Creating volume... "
240
- disk.create
241
- Rudy::Utils.waiter(2, 60, STDOUT, msg) {
242
- disk.available?
243
- }
244
- end
245
-
246
- unless disk.attached?
247
- msg = "Attaching #{disk.awsid} to #{@machine.awsid}... "
248
- disk.attach(@machine.awsid)
249
- Rudy::Utils.waiter(2, 10, STDOUT, msg) {
250
- disk.attached?
251
- }
252
- end
253
- end
254
- end
255
-
256
- def detach(disks, destroy=false)
257
- rdisk = Rudy::Disks.new
258
- disks.each_pair do |path, props|
259
- adisk = Rudy::MetaData::Disk.new(path, props[:size], props[:device], @machine.position)
260
- disk = rdisk.get adisk.name
261
-
262
- if disk == nil
263
- puts "Not found: #{adisk.name}".color(:red)
264
- return
265
- end
266
-
267
- if disk.attached?
268
- msg = "Detaching #{disk.awsid}..."
269
- disk.detach
270
- Rudy::Utils.waiter(2, 60, STDOUT, msg) {
271
- disk.available?
272
- }
273
- end
274
-
275
- if destroy
276
- puts "Destroying volume and metadata... "
277
- disk.destroy
278
- end
279
-
280
- end
281
- end
282
-
283
- def mount(disks)
284
- rdisk = Rudy::Disks.new
285
- disks.each_pair do |path, props|
286
- adisk = Rudy::MetaData::Disk.new(path, props[:size], props[:device], @machine.position)
287
- disk = rdisk.get(adisk.name)
288
- if disk == nil
289
- puts "Not found: #{adisk.name}".color(:red)
290
- return
291
- end
292
-
293
- unless disk.attached?
294
- msg = "Attaching #{disk.awsid} to #{@machine.awsid}... "
295
- disk.attach(@machine.awsid)
296
- Rudy::Utils.waiter(2, 10, STDOUT, msg) {
297
- disk.attached?
298
- }
299
-
300
- sleep 2
301
- end
302
-
303
- begin
304
- @rbox.mkdir(:p, disk.path)
305
-
306
- print "Mounting at #{disk.path}... "
307
-
308
- ret = @rbox.mount(:t, disk.fstype, disk.device, disk.path)
309
- print_response ret
310
- if ret.exit_code > 0
311
- STDERR.puts "Error creating disk".color(:red)
312
- return
313
- else
314
- puts "done"
315
- end
316
- disk.mounted = true
317
- disk.save
318
-
319
- rescue Net::SSH::AuthenticationFailed, Net::SSH::HostKeyMismatch => ex
320
- STDERR.puts "Error creating disk".color(:red)
321
- STDERR.puts ex.message.color(:red)
322
- rescue Rye::CommandNotFound => ex
323
- puts " CommandNotFound: #{ex.message}".color(:red)
324
-
325
- rescue
326
- STDERR.puts "Error creating disk" .color(:red)
327
- Rudy::Utils.bug
328
- end
329
-
330
- end
331
- end
332
-
333
-
334
- def umount(disks)
335
- rdisk = Rudy::Disks.new
336
- disks.each_pair do |path, props|
337
- adisk = Rudy::MetaData::Disk.new(path, props[:size], props[:device], @machine.position)
338
- disk = rdisk.get(adisk.name)
339
- if disk == nil
340
- puts "Not found: #{adisk.name}".color(:red)
341
- return
342
- end
343
-
344
- if disk.mounted?
345
- print "Unmounting #{disk.path}..."
346
- trap_rbox_errors { @rbox.umount(disk.path) }
347
- puts " done"
348
- sleep 0.5
349
- end
350
-
351
- sleep 2
352
-
353
- if disk.attached?
354
- msg = "Detaching #{disk.awsid}..."
355
- disk.detach
356
- Rudy::Utils.waiter(2, 60, STDOUT, msg) {
357
- disk.available?
358
- }
359
- sleep 0.5
360
- end
361
-
362
-
363
- end
364
- end
365
- alias_method :unmount, :umount
366
-
367
- def destroy(disks)
368
- rdisk = Rudy::Disks.new
369
-
370
- disks.each_pair do |path, props|
371
- adisk = Rudy::MetaData::Disk.new(path, props[:size], props[:device], @machine.position)
372
- disk = rdisk.get(adisk.name)
373
- if disk == nil
374
- puts "Not found: #{adisk.name}".color(:red)
375
- return
376
- end
377
-
378
- puts "Destroying #{disk.name}"
379
-
380
- if disk.mounted?
381
- print "Unmounting #{disk.path}..."
382
- trap_rbox_errors { @rbox.umount(disk.path) }
383
- puts " done"
384
- sleep 0.5
385
- end
386
-
387
- if disk.attached?
388
- msg = "Detaching #{disk.awsid}..."
389
- disk.detach
390
- Rudy::Utils.waiter(2, 60, STDOUT, msg) {
391
- disk.available?
392
- }
393
- sleep 0.5
394
- end
395
-
396
- puts "Destroying volume and metadata... "
397
- disk.destroy
398
-
399
- end
400
- end
401
-
402
- end
403
- end;end