Skip to content

Commit

Permalink
Disk routines are fully functional (again, finally)!
Browse files Browse the repository at this point in the history
  • Loading branch information
delano committed Jul 29, 2009
1 parent ce98d9a commit 49a4bd9
Show file tree
Hide file tree
Showing 18 changed files with 320 additions and 622 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ RUDY, CHANGES
* TODO: look for config file in ./.rudy, then ../.rudy, etc... (git-like)
* TODO: FIX: When shutting down, warns about disks even when they don't exist (CLI::Routines)
* TODO: Investigate SSH daemon on Windows
* TODO: Allow colons in place of dashes (rudy publish:gem)

#### 0.9.0 (2009-07-XX) ###############################

Expand Down
1 change: 1 addition & 0 deletions bin/rudy
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class RudyCLI < Rudy::CLI::Base
global :b, :bucket, String, "An S3 bucket name (used when creating images)"
global :t, :testrun, "Test run. Don't execute action (EXPERIMENTAL)."
global :P, :parallel, "Execute remote commands in parallel (EXPERIMENTAL)."
global :F, :force, "Force an action despite warnings"

# ------------------------------------------ RUDY OBJECTS --------
# ------------------------------------------------------------------
Expand Down
69 changes: 1 addition & 68 deletions lib/rudy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
require 'rye'





# = Rudy
#
#
Expand Down Expand Up @@ -115,73 +112,9 @@ def Rudy.disable_debug; @@debug = false; end
def Rudy.sysinfo; @@sysinfo; end
def sysinfo; @@sysinfo; end

class Error < RuntimeError
def initialize(obj=nil); @obj = obj; end
def message; "#{self.class}: #{@obj}"; end
end
class InsecureKeyPermissions < Rudy::Error
def message
lines = ["Insecure file permissions for #{@obj}"]
lines << "Try: chmod 600 #{@obj}"
lines.join($/)
end
end

#--
# TODO: Update exception Syntax based on:
# http://blog.rubybestpractices.com/posts/gregory/anonymous_class_hacks.html
#++
class NoConfig < Rudy::Error
def message; "No configuration found!"; end
end
class NoGlobal < Rudy::Error
def message; "No globals defined!"; end
end
class NoMachinesConfig < Rudy::Error
def message; "No machines configuration. Check your configs!"; end
end
class NoRoutinesConfig < Rudy::Error
def message; "No routines configuration. Check your configs!"; end
end
class ServiceUnavailable < Rudy::Error
def message; "#{@obj} is not available. Check your internets!"; end
end
class MachineGroupAlreadyRunning < Rudy::Error
def message; "Machine group #{@obj} is already running."; end
end
class MachineGroupNotRunning < Rudy::Error
def message; "Machine group #{@obj} is not running."; end
end
class MachineGroupMetadataExists < Rudy::Error
def message; "Machine group #{@obj} has existing metadata."; end
end
class MachineAlreadyRunning < Rudy::Error
def message; "Machine #{@obj} is already running."; end
end
class MachineNotRunning < Rudy::Error
def message; "Machine #{@obj} is not running."; end
end
class NoMachines < Rudy::Error;
def message; "Specified remote machine(s) not running"; end
end
class MachineGroupNotDefined < Rudy::Error
def message; "#{@obj} is not defined in machines config."; end
end
class PrivateKeyFileExists < Rudy::Error
def message; "Private key #{@obj} already exists."; end
end
class PrivateKeyNotFound < Rudy::Error
def message; "Private key file #{@obj} not found."; end
end
class UnsupportedOS < Rudy::Error; end
class UnknownRecordType < Rudy::Error
def message; "Unknown record type: #{@obj}"; end
end
class UnknownObject < Rudy::Error
def message; "Unknown object: #{@obj}"; end
end
end

require 'rudy/exceptions'
require 'rudy/utils' # The
require 'rudy/global' # order
require 'rudy/config' # of
Expand Down
23 changes: 9 additions & 14 deletions lib/rudy/cli/aws/ec2/volumes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,17 @@ def volumes_attach_valid?
end
def volumes_attach
@option.device ||= "/dev/sdh"
raise "TODO"
rvol = Rudy::AWS::EC2::Volumes.new(@@global.accesskey, @@global.secretkey, @@global.region)
rinst = Rudy::AWS::EC2::Instances.new(@@global.accesskey, @@global.secretkey, @@global.region)
raise "Volume #{@argv.volid} does not exist" unless rvol.exists?(@argv.volid)
raise "Volume #{@argv.volid} is already attached" if rvol.attached?(@argv.volid)
raise "Instance #{@option.instance} does not exist" unless rinst.exists?(@option.instance)
raise "Volume #{@argv.volid} does not exist" unless Rudy::AWS::EC2::Volumes.exists?(@argv.volid)
raise "Volume #{@argv.volid} is already attached" if Rudy::AWS::EC2::Volumes.attached?(@argv.volid)
raise "Instance #{@option.instance} does not exist" unless Rudy::AWS::EC2::Instances.exists?(@option.instance)

puts "Attaching #{@argv.volid} to #{@option.instance} on #{@option.device}"
execute_check(:low)
execute_action("Attach Failed") {
rvol.attach(@argv.volid, @option.instance, @option.device)
Rudy::AWS::EC2::Volumes.attach(@argv.volid, @option.instance, @option.device)
}

vol = rvol.get(@argv.volid)
vol = Rudy::AWS::EC2::Volumes.get(@argv.volid)
puts @global.verbose > 1 ? vol.inspect : vol.dump(@@global.format)
end

Expand All @@ -79,17 +76,15 @@ def volumes_detach_valid?
end

def volumes_detach
rvol = Rudy::AWS::EC2::Volumes.new(@@global.accesskey, @@global.secretkey, @@global.region)
raise "Volume #{@argv.volid} does not exist" unless rvol.exists?(@argv.volid)
raise "TODO"
vol = rvol.get(@argv.volid)
raise "Volume #{@argv.volid} does not exist" unless Rudy::AWS::EC2::Volumes.exists?(@argv.volid)
vol = Rudy::AWS::EC2::Volumes.get(@argv.volid)
raise "Volume #{vol.awsid} is not attached" unless vol.attached?

puts "Detaching #{vol.awsid} from #{vol.instid}"
execute_check(:medium)
execute_action("Detach Failed") { rvol.detach(vol.awsid) }
execute_action("Detach Failed") { Rudy::AWS::EC2::Volumes.detach(vol.awsid) }

vol = rvol.get(vol.awsid)
vol = Rudy::AWS::EC2::Volumes.get(vol.awsid)
puts @global.verbose > 1 ? vol.inspect : vol.dump(@@global.format)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/rudy/cli/disks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def disks
more, less = {}, []
less = [:environment, :role] if @option.all
# We first get the disk metadata
disk_list = Rudy::Disk.list(more, less) || []
disk_list = Rudy::Disks.list(more, less) || []
# If there are no disks currently, there could be backups
# so we grab those to create a list of disks.
if @option.backups
Expand All @@ -34,7 +34,7 @@ def disks
end

def disks_wash
dirt = (Rudy::Disk.list || []).select { |d| d.volume_exists? }
dirt = (Rudy::Disks.list || []).select { |d| d.volume_exists? }
if dirt.empty?
puts "Nothing to wash in #{rdisk.current_machine_group}"
return
Expand Down
2 changes: 1 addition & 1 deletion lib/rudy/cli/execbase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Rudy::CLI
class Base
extend Drydock

debug :on
debug :off

before do |obj|
# Don't print Rudy header unless requested to
Expand Down
2 changes: 1 addition & 1 deletion lib/rudy/cli/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def metadata

def metadata_delete_valid?
raise "Must supply object ID" unless @argv.oid
raise UnknownObject, @argv.oid unless Rudy::Metadata.exists? @argv.oid
raise Rudy::Metadata::UnknownObject, @argv.oid unless Rudy::Metadata.exists? @argv.oid
true
end

Expand Down
1 change: 1 addition & 0 deletions lib/rudy/config/objects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class Routines < Caesars
forced_hash :detach
forced_hash :snapshot
forced_hash :restore
forced_hash :format

# Passthrough routines
forced_hash :local # Force hash b/c we want to
Expand Down
90 changes: 90 additions & 0 deletions lib/rudy/exceptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
module Rudy

class Error < RuntimeError
def initialize(obj=nil); @obj = obj; end
def message; "#{self.class}: #{@obj}"; end
end
class InsecureKeyPermissions < Rudy::Error
def message
lines = ["Insecure file permissions for #{@obj}"]
lines << "Try: chmod 600 #{@obj}"
lines.join($/)
end
end

#--
# TODO: Update exception Syntax based on:
# http://blog.rubybestpractices.com/posts/gregory/anonymous_class_hacks.html
#++

class NoConfig < Rudy::Error
def message; "No configuration found!"; end
end
class NoGlobal < Rudy::Error
def message; "No globals defined!"; end
end
class NoMachinesConfig < Rudy::Error
def message; "No machines configuration. Check your configs!"; end
end
class NoRoutinesConfig < Rudy::Error
def message; "No routines configuration. Check your configs!"; end
end
class ServiceUnavailable < Rudy::Error
def message; "#{@obj} is not available. Check your internets!"; end
end
class MachineGroupAlreadyRunning < Rudy::Error
def message; "Machine group #{@obj} is already running."; end
end
class MachineGroupNotRunning < Rudy::Error
def message; "Machine group #{@obj} is not running."; end
end
class MachineGroupMetadataExists < Rudy::Error
def message; "Machine group #{@obj} has existing metadata."; end
end
class MachineAlreadyRunning < Rudy::Error
def message; "Machine #{@obj} is already running."; end
end
class MachineNotRunning < Rudy::Error
def message; "Machine #{@obj} is not running."; end
end
class NoMachines < Rudy::Error;
def message; "Specified remote machine(s) not running"; end
end
class MachineGroupNotDefined < Rudy::Error
def message; "#{@obj} is not defined in machines config."; end
end
class PrivateKeyFileExists < Rudy::Error
def message; "Private key #{@obj} already exists."; end
end
class PrivateKeyNotFound < Rudy::Error
def message; "Private key file #{@obj} not found."; end
end
class UnsupportedOS < Rudy::Error; end


class NotImplemented < Rudy::Error; end


module Metadata
class UnknownRecordType < Rudy::Error
def message; "Unknown record type: #{@obj}"; end
end
class UnknownObject < Rudy::Error
def message; "Unknown object: #{@obj}"; end
end
# Raised when trying to save a record with a key that already exists
class DuplicateRecord < Rudy::Error; end

end

module Disks
class NotAttached < Rudy::Error; end
class NotFormatted < Rudy::Error; end
class AlreadyFormatted < Rudy::Error; end
class AlreadyMounted < Rudy::Error; end
class AlreadyAttached < Rudy::Error; end
class NotMounted < Rudy::Error; end
class InUse < Rudy::Error; end
end

end
4 changes: 3 additions & 1 deletion lib/rudy/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Global < Storable
field :format
field :print_header
field :yes
field :force

field :accesskey
field :secretkey
Expand Down Expand Up @@ -50,6 +51,7 @@ def initialize
@nocolor = true unless @nocolor == "false" || @nocolor == false
@quiet ||= false
@parallel ||= false
@force ||= false
@format ||= :string # as in, to_s
@print_header = true if @print_header == nil
end
Expand All @@ -68,7 +70,7 @@ def apply_config(config)
# WARNING: Don't add bucket either or any machines configuration param
# TODO: investigate removing this apply_config method
%w[region zone environment role position
localhost nocolor quiet yes parallel].each do |name|
localhost nocolor quiet yes force parallel].each do |name|
curval, defval = self.send(name), config.defaults.send(name)
self.send("#{name}=", defval) if curval.nil? && !defval.nil?
end
Expand Down
14 changes: 2 additions & 12 deletions lib/rudy/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@ module Rudy
module Metadata
include Rudy::Huxtable

# Raised when trying to save a record with a key that already exists
class DuplicateRecord < Rudy::Error; end
class UnknownRecord < Rudy::Error; end

COMMON_FIELDS = [:region, :zone, :environment, :role].freeze

@@rsdb = nil
@@rvol = nil
@@rinst = nil
@@radd = nil
@@rkey = nil
@@rgrp = nil
@@domain = Rudy::DOMAIN

#
Expand Down Expand Up @@ -152,7 +143,6 @@ def postprocess; raise "implement postprocess"; end
end

def self.included(obj)
obj.extend Rudy::Metadata::ClassMethods
obj.send :include, Rudy::Metadata::InstanceMethods

# Add common storable fields.
Expand Down Expand Up @@ -192,15 +182,15 @@ def save(replace=false)
end

def destroy(force=false)
raise UnknownRecord, self.name unless self.exists?
raise UnknownObject, self.name unless self.exists?
Rudy::Metadata.destroy self.name
true
end

# Refresh the metadata object from SimpleDB. If the record doesn't
# exist it will raise an UnknownRecord error
def refresh!
raise UnknownRecord, self.name unless self.exists?
raise UnknownObject, self.name unless self.exists?
h = Rudy::Metadata.get self.name
return false if h.nil? || h.empty?
obj = self.from_hash(h)
Expand Down
16 changes: 12 additions & 4 deletions lib/rudy/metadata/disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def initialize(position=nil, path=nil, opts={})
# sdb values are stored as strings. Some quick conversion.
def postprocess
@size &&= @size.to_i
@raw = true if @raw == "true" unless @raw.is_a?(TrueClass)
@mounted = (@mounted == "true") unless @mounted.is_a?(TrueClass)
end

Expand Down Expand Up @@ -113,15 +114,22 @@ def valid?

def volume_attach(instid)
raise Rudy::Error, "No volume id" unless volume_exists?
vol = @rvol.attach(@volid, instid, @device)
vol = Rudy::AWS::EC2::Volumes.attach(@volid, instid, @device)
end

def volume_detach
raise Rudy::Error, "No volume id" unless volume_exists?
vol = @rvol.detach(@volid)
vol = Rudy::AWS::EC2::Volumes.detach(@volid)
end



def raw?
@raw == true
end

def mounted?
@mounted == true
end

# Create volume_*? methods
%w[exists? deleting? available? attached? in_use?].each do |state|
define_method("volume_#{state}") do
Expand Down
2 changes: 1 addition & 1 deletion lib/rudy/routines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def self.rescue(ret=nil, &bloc_party)
ret = bloc_party.call
rescue => ex
unless Rudy::Huxtable.global.parallel
STDERR.puts " #{ex.class}: #{ex.message}".color(:red)
STDERR.puts " #{ex.message}".color(:red)
STDERR.puts ex.backtrace if Rudy.debug?
choice = Annoy.get_user_input('(S)kip (A)bort: ', nil, 3600) || ''
if choice.match(/\AS/i)
Expand Down
Loading

0 comments on commit 49a4bd9

Please sign in to comment.