Class Rudy::Machine

  1. lib/rudy/metadata/machine.rb
Parent: Storable

Included modules

  1. Rudy::Metadata
  2. Gibbler::Complex

Attributes

available [W] An ephemeral value which is set after checking whether the SSH daemon is running. By default this will be set to false but can be set to true to avoid checking again. See available?
instance [R]

Public class methods

new (position='01', opts={})
  • position
  • opts is a hash of machine options.

Valid options are:

  • :position (overridden by position arg)
  • :size
  • :os
  • :ami
  • :group
  • :keypair
  • :address
[show source]
    # File lib/rudy/metadata/machine.rb, line 57
57:     def initialize(position='01', opts={})
58:       
59:       opts = {
60:         :size => current_machine_size,
61:         :os => current_machine_os,
62:         :ami => current_machine_image,
63:         :group => current_group_name,
64:         :keypair => root_keypairname
65:       }.merge opts
66:       
67:       opts[:address] = current_machine_address opts[:position] || position
68:       
69:       super Rudy::Machines::RTYPE, opts  # Rudy::Metadata#initialize
70:       
71:       @position = position
72:       
73:       # Defaults:
74:       @created = Time.now.utc
75:       @available = false
76:       postprocess
77:       
78:     end

Public instance methods

attached_volumes ()
[show source]
     # File lib/rudy/metadata/machine.rb, line 181
181:     def attached_volumes
182:       volumes = []
183:       return volumes if @instid.nil?
184:       Rudy::AWS::EC2::Volumes.list_by_instance( @instid) || []
185:     end
available? ()

See available attribute

[show source]
     # File lib/rudy/metadata/machine.rb, line 228
228:     def available?; @available; end
create ()
[show source]
     # File lib/rudy/metadata/machine.rb, line 141
141:     def create
142:       raise "#{name} is already running" if instance_running?
143:       
144:       # Options for Rudy::AWS::EC2::Instances#create
145:       opts = {
146:         :min  => 1,
147:         :size => @size,
148:         :ami => @ami,
149:         :group => @group,
150:         :keypair => @keypair, 
151:         :zone => @zone,
152:         :machine_data => self.generate_machine_data.to_yaml
153:       }
154:       
155:       ld "OPTS: #{opts.inspect}"
156:       
157:       Rudy::AWS::EC2::Instances.create(opts) do |inst|
158:         @instid = inst.awsid
159:         @created = @started = Time.now
160:         @state = inst.state
161:         # We need to be safe when creating machines because if an exception is
162:         # raised, instances will have been created but the calling class won't know. 
163:       end
164:       
165:       self.save
166:       
167:       sleep 1 # Eventual consistency in SimpleDB
168:       
169:       self
170:     end
create_mock ()
[show source]
     # File lib/rudy/metadata/machine.rb, line 131
131:     def create_mock
132:       refresh!
133:       @dns_public = @dns_private = nil
134:       inst = Rudy::AWS::EC2::Instances.list(:running).first
135:       @instid = inst.awsid
136:       self.save :replace
137:       sleep 1
138:       self
139:     end
default_device ()
[show source]
     # File lib/rudy/metadata/machine.rb, line 214
214:     def default_device
215:       windows? ? Rudy::DEFAULT_WINDOWS_DEVICE : Rudy::DEFAULT_LINUX_DEVICE
216:     end
default_fstype ()
[show source]
     # File lib/rudy/metadata/machine.rb, line 210
210:     def default_fstype
211:       windows? ? Rudy::DEFAULT_WINDOWS_FS : Rudy::DEFAULT_LINUX_FS
212:     end
destroy ()
[show source]
     # File lib/rudy/metadata/machine.rb, line 172
172:     def destroy
173:       Rudy::AWS::EC2::Instances.destroy(@instid) if instance_running?
174:       super
175:     end
disks ()
[show source]
    # File lib/rudy/metadata/machine.rb, line 93
93:     def disks
94:       Rudy::Disks.list
95:     end
dns_private? ()
[show source]
     # File lib/rudy/metadata/machine.rb, line 225
225:     def dns_private?; !@dns_private.nil? && !@dns_private.empty?; end
dns_public? ()
[show source]
     # File lib/rudy/metadata/machine.rb, line 224
224:     def dns_public?;  !@dns_public.nil? && !@dns_public.empty?;   end
generate_machine_data ()
[show source]
     # File lib/rudy/metadata/machine.rb, line 202
202:     def generate_machine_data
203:       d = {}
204:       [:region, :zone, :environment, :role, :position].each do |k|
205:         d[k] = self.send k
206:       end
207:       d
208:     end
get_console ()
[show source]
     # File lib/rudy/metadata/machine.rb, line 102
102:     def get_console
103:       raise "Instance not running" unless instance_running?
104:       raw = Rudy::AWS::EC2::Instances.console @instid
105:       console = Base64.decode64(raw)
106:       # The linux console can include ANSI escape codes for color, 
107:       # clear screen etc... We strip them out to get rid of the 
108:       # clear specifically. Otherwise the display is messed!
109:       console &&= console.noansi if console.respond_to? :noansi
110:       console
111:     end
get_instance ()
[show source]
     # File lib/rudy/metadata/machine.rb, line 98
 98:     def get_instance
 99:       Rudy::AWS::EC2::Instances.get @instid
100:     end
get_password ()
[show source]
     # File lib/rudy/metadata/machine.rb, line 113
113:     def get_password
114:       unless windows?
115:         raise "Password support is Windows only (this is #{@os})" 
116:       end
117:       console = get_console
118:       
119:       raise "Console output not yet available. Please wait." if console.nil?
120:       
121:       unless console.match(/<Password>(.+)<\/Password>/m)  
122:         # /m, match multiple lines
123:         raise "Password not yet available. Is this a custom AMI?"
124:       end  
125:       
126:       encrtypted_text = ($1 || '').strip
127:       k = Rye::Key.from_file root_keypairpath
128:       k.decrypt encrtypted_text
129:     end
linux? ()
[show source]
     # File lib/rudy/metadata/machine.rb, line 221
221:     def linux?; os? 'linux'; end
os? (v)
[show source]
     # File lib/rudy/metadata/machine.rb, line 219
219:     def os?(v); @os.to_s == v.to_s; end
postprocess ()
[show source]
    # File lib/rudy/metadata/machine.rb, line 80
80:     def postprocess
81:       @position &&= @position.to_s.rjust(2, '0')
82:       @os &&= @os.to_sym
83:     end
rbox ()
[show source]
    # File lib/rudy/metadata/machine.rb, line 89
89:     def rbox
90:       r = Rudy::Routines::Handlers::RyeTools.create_box self
91:     end
refresh! (metadata=true)
[show source]
     # File lib/rudy/metadata/machine.rb, line 187
187:     def refresh!(metadata=true)
188:       ## Updating the metadata isn't necessary
189:       super() if metadata # update metadata
190:       @instance = get_instance
191:       if @instance.is_a?(Rudy::AWS::EC2::Instance)
192:         @dns_public, @dns_private = @instance.dns_public, @instance.dns_private
193:         @state = @instance.state
194:         save :replace
195:       elsif @instance.nil?
196:         @awsid = @dns_public = @dns_private = nil
197:         @state = 'rogue'
198:         # Don't save it b/c it's possible the EC2 server is just down. 
199:       end
200:     end
restart ()
[show source]
     # File lib/rudy/metadata/machine.rb, line 177
177:     def restart
178:       Rudy::AWS::EC2::Instances.restart(@instid) if instance_running?
179:     end
solaris? ()
[show source]
     # File lib/rudy/metadata/machine.rb, line 222
222:     def solaris?; os? 'solaris'; end
to_s (*args)
[show source]
    # File lib/rudy/metadata/machine.rb, line 85
85:     def to_s(*args)
86:       [self.name.bright, self.instid, self.dns_public].join '; '
87:     end
windows? ()
[show source]
     # File lib/rudy/metadata/machine.rb, line 220
220:     def windows?; os? 'windows'; end