Skip to content

Commit

Permalink
More progress on Rudy::Routines
Browse files Browse the repository at this point in the history
  • Loading branch information
delano committed Apr 17, 2009
1 parent cd1da0b commit fc35ee9
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Rudyfile
Expand Up @@ -50,7 +50,7 @@ machines do
ami "ami-11111111"

role :app do
positions 2
positions 1

# We've given the stage-app machines two disks.
disks do
Expand Down
6 changes: 0 additions & 6 deletions bin/rudy-ec2
Expand Up @@ -216,12 +216,6 @@ module RudyEC2CLI
default :instances
debug :off

before do |obj|
# Don't print Rudy header unless requested to
obj.global.print_header = false if (obj.global.verbose == 0)
@start = Time.now
end

end

#puts Rudy::Utils.banner("THIS IS RUBY #{RUBY_VERSION}") if Drydock.debug?
Expand Down
9 changes: 6 additions & 3 deletions lib/rudy.rb
Expand Up @@ -95,7 +95,7 @@ def self.to_f; self.to_s.to_f; end
DEFAULT_EC2_HOST = "ec2.amazonaws.com"
DEFAULT_EC2_PORT = 443

MAX_INSTANCES = 2.freeze
MAX_INSTANCES = 3.freeze

SUPPORTED_SCM_NAMES = [:svn, :git].freeze

Expand Down Expand Up @@ -151,12 +151,15 @@ def message; "No AWS credentials. Check your configs!"; end
class ServiceUnavailable < Rudy::Error
def message; "#{@obj} is not available. Check your internets!"; end
end
class MachineGroupAlreadyRunning < Rudy::Error;
class MachineGroupAlreadyRunning < Rudy::Error
def message; "Machine group #{@obj} is already running."; end
end
class MachineGroupNotDefined < Rudy::Error;
class MachineGroupNotDefined < Rudy::Error
def message; "Machine group #{@obj} is not defined."; end
end
class PrivateKeyFileExists < Rudy::Error
def message; "Private key #{obj} already exists."; end
end
end

require 'rudy/utils' # The
Expand Down
6 changes: 3 additions & 3 deletions lib/rudy/aws/ec2/instance.rb
Expand Up @@ -78,7 +78,6 @@ class Instances
#
# * +:ami+
# * +:group+
# * +:user+
# * +:size+
# * +:keypair+
# * +:address+
Expand All @@ -104,13 +103,14 @@ def create(opts={}, &each_inst)
:max_count => opts[:max] || opts[:min],
:key_name => (opts[:keypair] || '').to_s,
:group_id => [opts[:group]].flatten.compact,
:user_data => opts[:machine_data].to_s,
#:user_data => opts[:machine_data], # Error: Invalid BASE64 encoding of user data ??
:availability_zone => opts[:zone].to_s,
:addressing_type => opts[:private] ? 'private' : 'public',
:instance_type => opts[:size].to_s,
:kernel_id => nil
}

#p opts[:machine_data]
#exit
response = execute_request({}) { @ec2.run_instances(old_opts) }

return nil unless response['instancesSet'].is_a?(Hash)
Expand Down
2 changes: 2 additions & 0 deletions lib/rudy/cli.rb
Expand Up @@ -101,6 +101,8 @@ class Base
extend Drydock

before do |obj|
# Don't print Rudy header unless requested to
obj.global.print_header = false if (obj.global.verbose == 0)
@start = Time.now
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rudy/cli/machines.rb
Expand Up @@ -14,7 +14,7 @@ def machines

def machines_wash
rmach = Rudy::Machines.new
dirt = rmach.list.select { |m| !m.running? }
dirt = (rmach.list || []).select { |m| !m.running? }
if dirt.empty?
puts "Nothing to wash in #{rmach.current_machine_group}"
return
Expand Down
2 changes: 1 addition & 1 deletion lib/rudy/cli/routine/startup.rb
Expand Up @@ -17,7 +17,7 @@ def startup

rmach = Rudy::Machines.new
rmach.list do |machine|
puts machines.to_s
puts machine.to_s
end

end
Expand Down
12 changes: 8 additions & 4 deletions lib/rudy/huxtable.rb
Expand Up @@ -112,11 +112,11 @@ def user_keypairname(user)
else
n = user.to_s == 'root' ? '' : "-#{user}"
"key-%s%s" % [current_machine_group, n]
end

end
end

def root_keypairname
user_keypairname :poop
user_keypairname :root
end

def user_keypairpath(name)
Expand Down Expand Up @@ -159,8 +159,12 @@ def current_machine_group
[@@global.environment, @@global.role].join(Rudy::DELIM)
end

def current_group_name
"g-#{current_machine_group}"
end

def current_machine_count
fetch_machine_param(:positions)
fetch_machine_param(:positions) || 1
end

def current_machine_image
Expand Down
103 changes: 57 additions & 46 deletions lib/rudy/machine.rb
Expand Up @@ -17,8 +17,9 @@ class Machine < Storable
field :created => Time
field :started => Time

attr_reader :dns_public
attr_reader :dns_private
field :dns_public
field :dns_private

attr_reader :instance

def init
Expand Down Expand Up @@ -87,47 +88,47 @@ def update_dns
def start(opts={})
raise "#{name} is already running" if running?

#opts = { :ami => current_machine_image,
# :zone => @@global.zone.to_s,
# :group => current_machine_group,
# :user => current_user,
# :size => current_machine_size,
# :keypair => KeyPairs.path_to_name(user_keypairpath(:root)), # Must be a root key
# :address => current_machine_address,
# :machine_data => generate_machine_data.to_yaml }.merge(opts)
#
#raise "NoKeyPair" unless opts[:keypair]
#
#inst = @@ec2.instances.create(opts)
#
#self.awsid = inst.first.awsid
#save
#self
opts = {
:min => 1,
:size => current_machine_size,
:ami => current_machine_image,
:group => current_group_name,
:keypair => root_keypairname,
:zone => @@global.zone.to_s,
:address => current_machine_address,
:machine_data => Machine.generate_machine_data.to_yaml
}.merge(opts)

@ec2inst.create(opts) do |inst|
@awsid = inst.awsid
@created = @starts = Time.now
@instance = inst
end

self.save

self
end

def generate_machine_data
Machine.generate_machine_data
end

def Machine.generate_machine_data
data = {
# Give the machine an identity
:zone => @@global.zone,
:environment => @@global.environment,
:role => @@global.role,
:position => @@global.position,
data = { # Give the machine an identity
:region => @@global.region.to_s,
:zone => @@global.zone.to_s,
:environment => @@global.environment.to_s,
:role => @@global.role.to_s,
:position => @@global.position.to_s,

# Add hosts to the /etc/hosts file
:hosts => {
:hosts => { # Add hosts to the /etc/hosts file
:dbmaster => "127.0.0.1",
}
}
data.to_hash
data
end

def running?(doublecheck=false)
return (!@awsid && !@awsid.empty?) unless doublecheck
raise "TODO: support doublecheck"
def running?
return false if @awsid.nil? || !@awsid.empty?
@ec2inst.running?(@awsid)
end

end
Expand All @@ -148,28 +149,38 @@ def create(&each_mach)
raise MachineGroupAlreadyRunning, current_machine_group if running?
raise MachineGroupNotDefined, current_machine_group unless known_machine_group?

opts = {
:min => current_machine_count,
:size => current_machine_size,
:ami => current_machine_image,
:group => current_machine_group
}

unless (1..MAX_INSTANCES).member?(opts[:max] || opts[:min])
unless (1..MAX_INSTANCES).member?(current_machine_count)
raise "Instance count must be more than 0, less than #{MAX_INSTANCES}"
end

unless @rgrp.exists?(current_machine_group)
puts "Creating group: #{current_machine_group}"
unless @rgrp.exists?(current_group_name)
puts "Creating group: #{current_group_name}"
@rgrp.create(current_group_name)
end

unless @rkey.exists?(root_keypairname)
kp_file = File.join(Rudy::CONFIG_DIR, root_keypairname)
raise PrivateKeyFileExists, kp_file if File.exists?(kp_file)
puts "Creating keypair: #{root_keypairname}"
kp = @rkey.create(root_keypairname)
puts "Saving #{kp_file}"
Rudy::Utils.write_to_file(kp_file, kp.private_key, 'w', 0600)
end

#@rinst.create(opts) do |instance|
# puts instance.awsid
#end
return
current_machine_count.times do |i|
machine = Rudy::Machine.new
puts "Starting %s" % machine.name
machine.start
end

end


def destroy(&each_mach)
#raise MachineGroupAlreadyRunning, current_machine_group if running?
#raise MachineGroupNotDefined, current_machine_group unless known_machine_group?

end

def list(more=[], less=[], &each_mach)
Expand Down

0 comments on commit fc35ee9

Please sign in to comment.