Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Removed old metadata and routines

  • Loading branch information...
commit 815fa3649a7b79b7251750436c867d789f34dfc1 1 parent 7af47c1
@delano delano authored
View
100 old-metadata/50_disk_old_tryouts.rb
@@ -1,100 +0,0 @@
-__END__
-rudy_lib_path = File.expand_path(File.join(GYMNASIUM_HOME, '..', 'lib'))
-library :rudy, rudy_lib_path
-
-group "Metadata"
-
-test_domain = 'test_' #<< Rudy::Utils.strand
-test_env = 'env_' << Rudy::Utils.strand
-
-tryout "Old Disk API" do
-
- setup do
- Rudy::Huxtable.update_config
- global = Rudy::Huxtable.global
- akey, skey, region = global.accesskey, global.secretkey, global.region
- @@sdb = Rudy::AWS::SDB.new(akey, skey, region)
- def new_disk(path, env)
- disk = Rudy::Metadata::Disk.new(path, 1, '/dev/sds')
- disk.environment = env
- disk
- end
- end
-
- xdrill "can create test domain (#{test_domain})" do
- @@sdb.create_domain test_domain
- end
-
- drill "can set test domain" do
- disk = new_disk '/', test_env
- disk.sdb_domain = test_domain
- disk.sdb_domain
- end
-
- drill "create disk instance" do
- disk = new_disk '/sergeant/disk', test_env
- tmp = [Rudy::Huxtable.global.zone, test_env]
- tmp += [Rudy::Huxtable.global.role, Rudy::Huxtable.global.position]
- # disk-us-east-1b-env_z6253g-app-01-rudy-disk
- disk_name = ['disk', tmp, 'sergeant', 'disk'].join(Rudy::DELIM)
- stash :expected, disk_name
- stash :actual, disk.name
- [disk_name == disk.name, disk.valid?]
- end
-
- drill "save disk" do
- new_disk('/sergeant/disk', test_env).save
- end
-
- xdrill "won't save over a disk with the same name" do
- new_disk('/sergeant/disk', test_env).save
- end
-
- drill "create disk instance with volume" do
- disk = new_disk('/sergeant/disk', test_env)
- disk.create
- stash :awsid, disk.awsid
- end
-
- drill "refresh disk" do
- disk = new_disk('/sergeant/disk', test_env)
- disk.refresh
- disk.awsid
- end
-
- drill "knows about the state of the volume" do
- disk = new_disk('/sergeant/disk', test_env)
- disk.refresh
- [disk.exists?, disk.attached?, disk.in_use?]
- end
-
- drill "destroy disk with volume" do
- disk = new_disk('/sergeant/disk', test_env)
- disk.update
- disk.destroy
- end
-
- xdrill "destroy a domain (#{test_domain})" do
- @@sdb.destroy_domain test_domain
- end
-
-end
-
-tryout "Disk API" do
- dream "can create test domain (#{test_domain})", true
- dream "can set test domain", test_domain
- dream "create disk instance", [true, true]
- dream "save disk", true
- dream "won't save over a disk with the same name" do
- rcode 1
- output false
- end
- dream "refresh disk", String, :class
- dream "knows about the state of the volume", ''
- dream "create disk instance with volume", String, :class
- dream "knows about the state of the volume", [true, false, false]
- dream "destroy disk with volume", true
- dream "destroy a domain (#{test_domain})", true
-end
-
-
View
113 old-metadata/backup.rb
@@ -1,113 +0,0 @@
-module Rudy::Metadata
- class Backup < Storable
- include Rudy::Metadata::ObjectBase
-
- field :rtype
- field :awsid
-
- field :region
- field :zone
- field :environment
- field :role
- field :position
- field :path
-
- field :date
- field :time
- field :second
-
- field :created => Integer
-
- field :size
- field :fstype
- field :volume
-
- require 'date'
-
-
- def init(volume=nil, path=nil, position=nil)
- # NOTE: Arguments must be optional or Storable will raise an exception!
- @volume= volume
- @path = path
- @rtype = 'back'
- @region = @@global.region
- @zone = @@global.zone
- @environment = @@global.environment
- @role = @@global.role
- @position = position || @@global.position
-
-
- now = Time.now.utc
- datetime = Backup.format_timestamp(now).split(Rudy::DELIM)
- @created = now.to_i
- @date, @time, @second = datetime
-
- postprocess
- end
-
-
- def name
- sep=File::SEPARATOR
- dirs = @path.split sep if @path && !@path.empty?
- dirs.shift while dirs && (dirs[0].nil? || dirs[0].empty?)
- args = [dirs, @date, @time, @second].flatten
- super("back", @zone, @environment, @role, @position, *args)
- end
-
- def nice_time
- return "" unless @date && @time
- t = @date.scan(/(\d\d\d\d)(\d\d)(\d\d)/).join('-')
- t << " " << @time.scan(/(\d\d)(\d\d)/).join(':')
- t
- end
-
- def to_s(*args)
- str = ""
- field_names.each do |key|
- str << sprintf(" %22s: %s#{$/}", key, self.send(key.to_sym))
- end
- str
- end
-
- def disk
- rdisk = Rudy::Disks.new
- disk = Rudy::Metadata::Disk.new(@path, nil, nil, @position)
- disk.region, disk.zone = @region, @zone
- disk.environment, disk.role, disk.position = @environment, @role, @position
- diskobj = rdisk.get(disk.name)
- diskobj || disk
- end
-
- def create(volume=nil)
- volume ||= @volume
- vol = @rsnap.create(volume)
- @awsid = vol.awsid
- self.save
- self
- end
-
- # 20090224-1813-36
- def Backup.format_timestamp(dat)
- mon, day, hour, min, sec = [dat.mon, dat.day, dat.hour, dat.min, dat.sec].collect { |v| v.to_s.rjust(2, "0") }
- [dat.year, mon, day, Rudy::DELIM, hour, min, Rudy::DELIM, sec].join
- end
-
- def to_query(more=[], less=[])
- query = super([:path, :date, :time, :second, *more], less)
- end
-
- def to_select(more=[], less=[])
- query = super([:path, :date, :time, :second, *more], less)
- end
-
- # Does this disk have enough info to be saved or used?
- # The test is based on the same criteria for building
- # SimpleDB queries.
- def valid?
- criteria = build_criteria([:path]).flatten
- criteria.size == criteria.compact.size
- end
-
- end
-end
-
View
67 old-metadata/backups.rb
@@ -1,67 +0,0 @@
-
-
-module Rudy
-class Backups
- include Rudy::Metadata
-
- def init
- now = Time.now.utc
- datetime = Rudy::Metadata::Backup.format_timestamp(now).split(Rudy::DELIM)
- @created = now.to_i
- @date, @time, @second = datetime
- end
-
- #def create(&each_mach)
-
- #end
-
-
- def destroy(&each_mach)
- list do |backup|
- puts "Destroying #{backup.name}"
- backup.destroy
- end
- end
-
- def list(more=[], less=[], local={}, &each_backup)
- backups = list_as_hash(more, less, local, &each_backup)
- backups &&= backups.values
- backups
- end
-
- def list_as_hash(more=[], less=[], local={}, &each_backup)
- more ||= []
- more += [:rtype, 'back']
- query = to_select(more, less, local)
- list = @sdb.select(query) || {}
- backups = {}
- list.each_pair do |n,d|
- backups[n] = Rudy::Metadata::Backup.from_hash(d)
- end
- backups.each_pair { |n,backup| each_backup.call(backup) } if each_backup
- backups = nil if backups.empty?
- backups
- end
-
- def get(rname=nil)
- dhash = @sdb.get(Rudy::DOMAIN, rname)
- return nil if dhash.nil? || dhash.empty?
- d = Rudy::Metadata::Backup.from_hash(dhash)
- d.update if d
- d
- end
-
-
- def running?
- !list.nil?
- # TODO: add logic that checks whether the instances are running.
- end
-
- def to_select(*args)
- query = super(*args)
- query << " and created != '0' order by created desc"
- end
-
-end
-end
-
View
38 old-metadata/debug.rb
@@ -1,38 +0,0 @@
-
-
-module Rudy; module Metadata
-class Debug
- include Rudy::Metadata
-
- def init
- end
-
- def list(more=[], less=[], local={}, &block)
- objects = list_as_hash(more, less, local, &block)
- objects &&= objects.values
- objects
- end
-
- def list_as_hash(more=[], less=[], local={}, &block)
- query = to_select(more, less, local)
- list = @sdb.select(query) || {}
- objects = {}
- list.each_pair do |n,d|
- objects[n] = d
- end
- objects.each_pair { |n,obj| block.call(obj) } if block
- objects = nil if objects.empty?
- objects
- end
-
-
- def to_select(more, less, local)
- query = super(more, less, local)
-# query << " order by created desc"
- puts query if @@global.verbose > 0
- query
- end
-
-end
-end; end
-
View
177 old-metadata/disk.rb
@@ -1,177 +0,0 @@
-module Rudy::Metadata
-class Disk < Storable
- include Rudy::Metadata::ObjectBase
-
-
- field :rtype
- field :awsid
- field :status
- field :instid
-
- field :region
- field :zone
- field :environment
- field :role
- field :position
- field :path
-
- field :device
- field :size
- #field :backups => Array
-
- # Is the associated volume formatted? One of: true, false, [empty].
- # [empty] means we don't know and it's the default.
- field :raw
-
- field :fstype
- field :mounted
-
- field :created
-
- def init(path=nil, size=1, device='/dev/sdh', position=nil)
- @path, @size, @device = path, size, device
- @rtype = 'disk'
- @region = @@global.region
- @zone = @@global.zone
- @environment = @@global.environment
- @role = @@global.role
- @position = position || @@global.position
- @mounted = false
-
- now = Time.now.utc
- datetime = Backup.format_timestamp(now).split(Rudy::DELIM)
- @created = now.to_i
-
- postprocess
- end
-
- def postprocess
- @size &&= @size.to_i
- @mounted = true if @mounted == "true"
- end
-
- def to_s(with_titles=true)
- update
- mtd = @mounted == true ? "mounted" : @status
- if @size && @device
- "%s; %3sGB; %s; %s" % [liner_note, @size, @device, mtd]
- else
- liner_note
- end
- end
-
- def inspect
- lines = []
- lines << liner_note
- field_names.each do |key|
- next unless self.respond_to?(key)
- val = self.send(key)
- lines << sprintf(" %22s: %s", key, (val.is_a?(Array) ? val.join(', ') : val))
- end
- lines.join($/)
- end
-
- def name
- sep=File::SEPARATOR
- dirs = @path.split sep if @path && !@path.empty?
- dirs.shift while dirs && (dirs[0].nil? || dirs[0].empty?)
- super("disk", @zone, @environment, @role, @position, *dirs)
- end
-
- def create(size=nil, zone=nil, snapshot=nil)
- raise "#{name} already exists" if exists?
- vol = @rvol.create(size || @size, zone || @zone, snapshot)
- @awsid = vol.awsid
- @raw = true
- self.save
- self
- end
-
- def backup
- raise "No volume to backup" unless @awsid
- bup = Rudy::Metadata::Backup.new(@awsid, @path, @position)
- bup.size = @size || 1
- bup.fstype = @fstype || 'ext3'
- bup.create
- end
-
- def list_backups
- rback = Rudy::Backups.new
- props = {
- :environment => @environment,
- :role => @role,
- :zone => @zone,
- :region => @region,
- :position => @position
- }
- rback.list([], [], props)
- end
-
- def attach(instid)
- raise "No volume id" unless exists?
- vol = @rvol.attach(@awsid, instid, @device)
- end
-
- def detach
- raise "No volume id" unless exists?
- vol = @rvol.detach(@awsid)
- end
-
- def destroy(force=false)
- if @awsid && !deleting?
- if !force
- raise Rudy::AWS::EC2::VolumeNotAvailable, @awsid if attached?
- else
- detach if exists? && attached?
- sleep 0.1
- end
- raise Rudy::AWS::EC2::VolumeNotAvailable, @awsid if in_use?
- @rvol.destroy(@awsid) if exists? && available?
- end
- super() # quotes, otherwise Ruby will send this method's args
- end
-
- def update
- @awsid = nil if @awsid && @awsid.empty?
- @volume = @rvol.get(@awsid) if @awsid
- if @volume.is_a?(Rudy::AWS::EC2::Volume)
- @status = @volume.status
- @instid = @volume.instid
- save
- else
- @awsid, @status, @instid = nil, nil, nil
- @mounted = false # I don't like having to set this
- # Don't save it b/c it's possible the EC2 server is just down
- end
- end
-
- def to_query(more=[], less=[])
- super([:path, *more], less) # Add path to the default fields
- end
-
- def to_select(more=[], less=[])
- super([:path, *more], less)
- end
-
- # Does this disk have enough info to be saved or used?
- # The test is based on the same criteria for building
- # SimpleDB queries.
- def valid?
- criteria = build_criteria([:path]).flatten
- criteria.size == criteria.compact.size
- end
-
- def mounted?
- @mounted && @mounted == true
- end
-
-
- %w[exists? deleting? available? attached? in_use?].each do |state|
- define_method(state) do
- return false if @awsid.nil? || @awsid.empty?
- @rvol.send(state, @awsid) rescue false # deleting?, available?, etc...
- end
- end
-
-end
-end
View
67 old-metadata/disks.rb
@@ -1,67 +0,0 @@
-
-
-module Rudy
-class Disks
- include Rudy::Metadata
-
-
- def init
- @rback = Rudy::Backups.new
- end
-
- #def create(&each_mach)
-
- #end
-
-
- def destroy(&each_mach)
- #raise MachineGroupNotRunning, current_machine_group unless running?
- #raise MachineGroupNotDefined, current_machine_group unless known_machine_group?
- list do |disk|
- puts "Destroying #{disk.name}"
- disk.destroy
- end
- end
-
- def backups
- @rback.list()
- end
-
- def list(more=[], less=[], &each_disk)
- disks = list_as_hash(&each_disk)
- disks &&= disks.values
- disks
- end
-
- def list_as_hash(more=[], less=[], &each_disk)
- query = to_select([:rtype, 'disk'], less)
- list = @sdb.select(query) || {}
- disks = {}
- list.each_pair do |n,d|
- disks[n] = Rudy::Metadata::Disk.from_hash(d)
- end
- disks.each_pair { |n,disk| each_disk.call(disk) } if each_disk
- disks = nil if disks.empty?
- disks
- end
-
- def get(rname=nil)
- dhash = @sdb.get(Rudy::DOMAIN, rname)
- return nil if dhash.nil? || dhash.empty?
- d = Rudy::Metadata::Disk.from_hash(dhash)
- d.update if d
- d
- end
-
-
- def running?
- !list.nil?
- # TODO: add logic that checks whether the instances are running.
- end
-
-
-
-
-end
-end
-
View
193 old-metadata/machine.rb
@@ -1,193 +0,0 @@
-
-## CONSIDER: http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20090713.pdf
-
-module Rudy
- class Machine < Storable
- include Rudy::Metadata::ObjectBase
-
- field :rtype
- field :awsid
-
- field :region
- field :zone
- field :environment
- field :role
- field :position
-
- field :created => Time
- field :started => Time
-
- field :dns_public
- field :dns_private
- field :state
-
- field :os
-
- attr_reader :instance
-
- # 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?
- attr_accessor :available
-
- def init
- #@created =
- @rtype = 'm'
- @region = @@global.region
- @zone = @@global.zone
- @environment = @@global.environment
- @role = @@global.role
- @position = find_next_position || '01'
- @state = 'no-instance'
- @os = 'unknown'
- @available = false
- end
-
- # See +available+ attribute
- def available?; @available; end
-
- def liner_note
- update #if !dns_public? && @awsid
- info = !@dns_public.nil? && !@dns_public.empty? ? @dns_public : "#{@awsid}:#{@state}"
- "%s %s" % [self.name.bright, info]
- end
-
- def to_s(with_title=false)
- lines = []
- lines << liner_note
- #if self.running?
- # k, g = @keyname || 'no-keypair', self.groups.join(', ')
- # lines << @@sformat % %w{zone size ami keyname groups} if with_title
- # lines << @@sformat % [@zone, @size, @ami, k, g]
- #end
- lines.join($/)
- end
-
- def inspect
- update #if !dns_public? && @awsid
- lines = []
- field_names.each do |key|
- next unless self.respond_to?(key)
- val = self.send(key)
- lines << sprintf("%s=%s", key, (val.is_a?(Array) ? val.join(', ') : val))
- end
- "<Rudy::Machine#%s %s>" % [name, lines.join(' ')]
- end
-
- def find_next_position
- list = @sdb.select(self.to_select(nil, [:position])) || []
- pos = list.size + 1
- pos.to_s.rjust(2, '0')
- end
-
- def name
- Machine.generate_name(@zone, @environment, @role, @position)
- end
-
- def Machine.generate_name(zon, env, rol, pos)
- pos = pos.to_s.rjust 2, '0'
- ["m", zon, env, rol, pos].join(Rudy::DELIM)
- end
-
- def get_instance
- @ec2inst.get(@awsid) rescue nil
- end
-
- def update
- return false unless @awsid
- @instance = get_instance
- if @instance.is_a?(Rudy::AWS::EC2::Instance)
- @dns_public = @instance.dns_public
- @dns_private = @instance.dns_private
- @state = @instance.state
- save
- elsif @instance.nil?
- @awsid = @dns_public = @dns_private = nil
- @state = 'rogue'
- # Don't save it b/c it's possible the EC2 server is just down.
- end
- end
-
- def dns_public?
- !@dns_public.nil? && !@dns_public.empty?
- end
- def dns_private?
- !@dns_private.nil? && !@dns_private.empty?
- end
-
- def start(opts={})
- raise "#{name} is already running" if running?
-
- opts = {
- :min => 1,
- :size => current_machine_size,
- :ami => current_machine_image,
- :group => current_group_name,
- :keypair => root_keypairname,
- :zone => @@global.zone.to_s,
- :machine_data => Machine.generate_machine_data.to_yaml
- }.merge(opts)
-
- @os = current_machine_os
-
- @ec2inst.create(opts) do |inst|
- @awsid = inst.awsid
- @created = @starts = Time.now
- @state = inst.state
- # We need to be safe when creating machines because if an exception is
- # raised, instances will have been creating but the calling class won't know.
- begin
- address = current_machine_address(@position)
- # Assign IP address only if we have one for that position
- if address
- # Make sure the address is associated to the current account
- if @radd.exists?(address)
- puts "Associating #{address} to #{inst.awsid}"
- @radd.associate(address, inst.awsid)
- else
- STDERR.puts "Unknown address: #{address}"
- end
- end
- rescue => ex
- STDERR.puts "Error: #{ex.message}"
- STDERR.puts ex.backtrace if Rudy.debug?
- end
- end
-
- self.save
-
- self
- end
-
- def destroy
- @ec2inst.destroy(@awsid) if running?
- super
- end
-
- def restart
- @ec2inst.restart(@awsid) if running?
- end
-
- def Machine.generate_machine_data
- 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 || '01').to_s,
- :hosts => { # Add hosts to the /etc/hosts file
- #:dbmaster => "127.0.0.1",
- }
- }
- data
- end
-
- def running?
- return false if @awsid.nil? || @awsid.empty?
- @ec2inst.running?(@awsid) rescue nil
- end
-
- end
-
-end
View
113 old-metadata/machines.rb
@@ -1,113 +0,0 @@
-
-
-module Rudy
-
- class Machines
- include Rudy::MetaData
-
- def create(&each_mach)
- raise MachineGroupAlreadyRunning, current_machine_group if running?
- raise MachineGroupNotDefined, current_machine_group unless known_machine_group?
-
- 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_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)
- else
- kp_file = root_keypairpath
- # This means no keypair file can be found
- raise PrivateKeyNotFound, root_keypairname if kp_file.nil?
- # This means we found a keypair in the config but we cannot find the private key file.
- raise PrivateKeyNotFound, kp_file if !File.exists?(kp_file)
- end
-
- machines = []
- current_machine_count.times do |i|
- machine = Rudy::Machine.new
-
- #puts "Starting %s" % machine.name
-
- machine.start
- machines << machine
- end
- machines.each { |m| each_mach.call(m) } if each_mach
- machines
- end
-
-
- def destroy(&each_mach)
- raise MachineGroupNotDefined, current_machine_group unless known_machine_group?
- raise MachineGroupNotRunning, current_machine_group unless running?
- list.each { |m| each_mach.call(m); } if each_mach
- list do |mach|
- #puts "Destroying #{mach.name}"
- mach.destroy
- end
- end
-
-
- def restart(&each_mach)
- raise MachineGroupNotDefined, current_machine_group unless known_machine_group?
- raise MachineGroupNotRunning, current_machine_group unless running?
- machines = list
- machines.each do |mach|
- each_mach.call(mach) if each_mach
- puts "Restarting #{mach.name}"
- mach.restart
- end
- machines
- end
-
- def list(more=[], less=[], &each_mach)
- machines = list_as_hash(more, less, &each_mach)
- machines &&= machines.values
- machines
- end
-
- def list_as_hash(more=[], less=[], &each_mach)
- query = to_select([:rtype, 'm'], less)
- list = @sdb.select(query) || {}
- machines = {}
- list.each_pair do |n,m|
- machines[n] = Rudy::Machine.from_hash(m)
- end
- machines.each_pair { |n,mach| each_mach.call(mach) } if each_mach
- machines = nil if machines.empty?
- machines
- end
-
- def get(rname=nil)
- Rudy::Machine.from_hash(@sdb.get(Rudy::DOMAIN, rname)) # Returns nil if empty
- end
-
-
- def running?
- !list.nil?
- # TODO: add logic that checks whether the instances are running.
- end
-
- end
-
-
- class Machines::Offline
- def list(more=[], less=[], &each_mach)
- m = Rudy::Machine.new
- m.dns_public = 'localhost'
- each_mach.call(m) if each_mach
- [m]
- end
- end
-
-end
View
69 old-metadata/metadata.rb
@@ -1,69 +0,0 @@
-
-module Rudy
- module Metadata
- include Rudy::Huxtable
-
- attr_reader :sdb_domain
-
- def initialize(*args)
- a, s, r = @@global.accesskey, @@global.secretkey, @@global.region
- @sdb = Rudy::AWS::SDB.new(a, s, r)
- @radd = Rudy::AWS::EC2::Addresses.new(a, s, r)
- @rinst = Rudy::AWS::EC2::Instances.new(a, s, r)
- @rgrp = Rudy::AWS::EC2::Groups.new(a, s, r)
- @rkey = Rudy::AWS::EC2::Keypairs.new(a, s, r)
- @sdb_domain = Rudy::DOMAIN
- init(*args)
- end
-
- def init(*args)
- end
-
- # 20090224-1813-36
- def format_timestamp(dat)
- mon, day, hour, min, sec = [dat.mon, dat.day, dat.hour, dat.min, dat.sec].collect { |v| v.to_s.rjust(2, "0") }
- [dat.year, mon, day, Rudy::DELIM, hour, min, Rudy::DELIM, sec].join
- end
-
- def destroy
- @sdb.destroy(@sdb_domain, name)
- true
- end
-
- private
-
- # Returns a generic zipped Array of metadata
- # (There is region, zone, environment, role, but no rtype)
- # NOTE: I've encoded some mega bullshit in here. more is actually an Array
- # of name values [n1, v1, n2, v2] (wtf!) which makes local unnecessary.
- def build_criteria(more=[], less=[], local={})
- # TODO: This build_criteria treats "more" differently than the
- # ObjectBase one. Sort it out! (This way is better)
- names = [:region, :zone, :environment, :role].compact
- names -= [*less].flatten.uniq.compact
- values = names.collect do |n|
- local[n.to_sym] || @@global.send(n.to_sym)
- end
- criteria = names.zip(values) + more # a weird array
- # If a position was specified we'll add that to the
- # select criteria. This is not added by default.
- unless (local[:position] || @@global.send(:position)).nil?
- criteria << [:position, (local[:position] || @@global.send(:position))]
- end
- ld criteria.inspect
- criteria
- end
-
- def to_query(more=[], less=[], local={})
- Rudy::AWS::SDB.generate_query build_criteria(more, less, local)
- end
-
- def to_select(more=[], less=[], local={})
- Rudy::AWS::SDB.generate_select ['*'], @sdb_domain, build_criteria(more, less, local)
- end
-
- end
-end
-
-Rudy::Utils.require_glob(RUDY_LIB, 'rudy', 'metadata', 'objectbase.rb')
-Rudy::Utils.require_glob(RUDY_LIB, 'rudy', 'metadata', '*.rb')
View
108 old-metadata/objectbase.rb
@@ -1,108 +0,0 @@
-
-
-module Rudy
- module Metadata
- module ObjectBase
- include Rudy::Huxtable
-
- attr_accessor :sdb_domain
-
- def initialize(*args)
- a, s, r = @@global.accesskey, @@global.secretkey, @@global.region
- @sdb = Rudy::AWS::SDB.new(a, s, r)
- @ec2inst = Rudy::AWS::EC2::Instances.new(a, s, r)
- @rvol = Rudy::AWS::EC2::Volumes.new(a, s, r)
- @radd = Rudy::AWS::EC2::Addresses.new(a, s, r)
- @rsnap = Rudy::AWS::EC2::Snapshots.new(a, s, r)
- @sdb_domain = Rudy::DOMAIN
- init(*args)
- end
-
- def init(*args); raise "Must override init"; end
-
- def valid?; raise "#{self.class} must override 'valid?'"; end
-
- def to_query(more=[], less=[])
- Rudy::AWS::SDB.generate_query build_criteria(more, less)
- end
-
- def to_select(more=[], less=[])
- s = Rudy::AWS::SDB.generate_select ['*'], @sdb_domain, build_criteria(more, less)
- s
- end
-
- def name(identifier, zon, env, rol, pos, *other)
- pos = pos.to_s.rjust 2, '0'
- [identifier, zon, env, rol, pos, *other].flatten.compact.join(Rudy::DELIM)
- end
-
- def save(replace=true)
- replace = true if replace.nil?
- @sdb ||= Rudy::AWS::SDB.new(@@global.accesskey, @@global.secretkey, @@global.region)
- @sdb.put(@sdb_domain, name, self.to_hash, replace) # Always returns nil
- true
- end
-
- def destroy
- @sdb ||= Rudy::AWS::SDB.new(@@global.accesskey, @@global.secretkey, @@global.region)
- @sdb.destroy(@sdb_domain, name)
- true
- end
-
- def refresh
- @sdb ||= Rudy::AWS::SDB.new(@@global.accesskey, @@global.secretkey, @@global.region)
- h = @sdb.get(@sdb_domain, name) || {}
- from_hash(h)
- end
-
- def ==(other)
- return false unless other.is_a?(self.class)
- self.name == other.name
- end
-
- # A generic default
- def to_s
- str = ""
- field_names.each do |key|
- str << sprintf(" %22s: %s#{$/}", key, self.send(key.to_sym))
- end
- str
- end
-
- def liner_note
- info = @awsid && !@awsid.empty? ? @awsid : "[no aws object]"
- "%s %s" % [self.name.bright, info]
- end
-
- def inspect
- lines = []
- lines << liner_note
- field_names.each do |key|
- next unless self.respond_to?(key)
- val = self.send(key)
- lines << sprintf(" %22s: %s", key, (val.is_a?(Array) ? val.join(', ') : val))
- end
- lines.join($/)
- end
-
- protected
-
- # Builds a zipped Array from a list of criteria.
- # The list of criteria is made up of metadata object attributes.
- # The list is constructed by taking the adding +more+, and
- # subtracting +less+ from <tt>:rtype, :region, :zone, :environment, :role, :position</tt>
- #
- # Returns [[:rtype, value], [:zone, value], ...]
- def build_criteria(more=[], less=[])
- criteria = [:rtype, :region, :zone, :environment, :role, :position, *more].compact
- criteria -= [*less].flatten.uniq.compact
- values = criteria.collect do |n|
- self.send(n.to_sym)
- end
- criteria.zip(values)
- end
-
- end
- end
-end
-
View
266 old-routines/routine-scraps.rb
@@ -1,266 +0,0 @@
-
-# * +machine_action+ a method on Rudy::Machines, one of: create, destroy, list
-# * +routine+ Override +routine+ with another routine (default: nil)
-# * +skip_check+ Don't check that the machine is up and SSH is available (default: false)
-# * +skip_header+ Don't print machine header (default: false)
-# * +routine_action+ is an optional block which will be executed for each
-# machine between the disk routine and after blocks. The block receives
-# two argument: an instance of Rudy::Machine and one of Rye::Box.
-def generic_machine_runner(machine_action, routine=nil, skip_check=false, skip_header=false, &routine_action)
- is_available= false
-
- if @@global.offline
- skip_check = true
- remote_user = Rudy.sysinfo.user
- else
- remote_user = 'root'
- end
-
- routine ||= @routine
- raise "No routine supplied" unless routine
- raise "No machine action supplied" unless machine_action
- unless Rudy::Machines.respond_to?(machine_action)
- raise "Unknown machine action #{machine_action}"
- end
-
- # Declare a couple vars so they're available outide the block
- before_dependencies = after_dependencies = nil
- enjoy_every_sandwich {
- # This gets and removes the dependencies from the routines hash.
- if Rudy::Routines::Handlers::Depends.has_depends?(:before, routine)
- before_dependencies = Rudy::Routines::Handlers::Depends.get(:before, routine)
- end
-
- # We grab the after ones now too, so we don't fool the ScriptHelper
- # later on in this routine (after keyword is used for scripts too)
- if Rudy::Routines::Handlers::Depends.has_depends?(:after, routine)
- after_dependencies = Rudy::Routines::Handlers::Depends.get(:after, routine)
- end
-
- # This calls generic_machine_runner for every dependent before routine.
- execute_dependency(before_dependencies, skip_check, skip_header)
- }
-
-
- lbox = Rye::Box.new('localhost', :info => (@@global.verbose > 3), :debug => false)
- sconf = fetch_script_config
-
- enjoy_every_sandwich {
- if Rudy::Routines::Handlers::Script.before_local?(routine) # before_local
- # Runs "before_local" scripts of routines config.
- task_separator("LOCAL SHELL")
- lbox.cd Dir.pwd # Run local command block from current working directory
- Rudy::Routines::Handlers::Script.before_local(routine, sconf, lbox, @option, @argv)
- end
- }
-
- enjoy_every_sandwich {
- if Rudy::Routines::Handlers::Script.script_local?(routine) # script_local
- # Runs "script_local" scripts of routines config.
- # NOTE: This is synonymous with before_local
- task_separator("LOCAL SHELL")
- lbox.cd Dir.pwd # Run local command block from current working directory
- Rudy::Routines::Handlers::Script.script_local(routine, sconf, lbox, @option, @argv)
- end
- }
-
- # Execute the action (create, list, destroy, restart)
- machines = enjoy_every_sandwich([]) { Rudy::Machines.send(machine_action) } || []
-
- machines.each do |machine|
- puts machine_separator(machine.name, machine.awsid) unless skip_header
-
- unless skip_check
- msg = preliminary_separator("Checking if instance is running...")
- Rudy::Utils.waiter(3, 120, STDOUT, msg, 0) {
- inst = machine.get_instance
- inst && inst.running?
- }
- end
-
- # Add instance info to machine and save it. This is really important
- # for the initial startup so the metadata is updated right away. But
- # it's also important to call here because if a routine was executed
- # and an unexpected exception occurs before this update is executed
- # the machine metadata won't contain the DNS information. Calling it
- # here ensure that the metadata is always up-to-date.
- machine.update
-
- # This is a short-circuit for Windows instances. We don't support
- # disks for windows yet and there's no SSH so routines are out of
- # the picture too. Here we simply run the per machine block which
- # is crucial for shutdown and possibly others as well.
- if (machine.os || '').to_s == 'win32'
- enjoy_every_sandwich {
- # Startup, shutdown, release, deploy, etc...
- routine_action.call(machine, nil) if routine_action
- }
-
- next # The short circuit
- end
-
- if !skip_check && has_remote_task?(routine)
- enjoy_every_sandwich {
- msg = preliminary_separator("Waiting for SSH daemon...")
- ret = Rudy::Utils.waiter(2, 30, STDOUT, msg, 0) {
- Rudy::Utils.service_available?(machine.dns_public, 22)
- }
- is_available = ret
- }
- end
-
- if is_available
- # TODO: trap rbox errors. We could get an authentication error.
- opts = { :keys => root_keypairpath, :user => remote_user,
- :info => @@global.verbose > 3, :debug => false }
- begin
- rbox = Rye::Box.new(machine.dns_public, opts)
- Rudy::Utils.waiter(2, 10, STDOUT, nil, 0) { rbox.connect }
- rescue Rye::NoHost => ex
- STDERR.puts "No host: #{ex.message}"
- exit 65
- end
-
- unless skip_check
- # Set the hostname if specified in the machines config.
- # :rudy -> change to Rudy's machine name
- # :default -> leave the hostname as it is
- # Anything else other than nil -> change to that value
- # NOTE: This will set hostname every time a routine is
- # run so we may want to make this an explicit action.
- enjoy_every_sandwich {
- hn = current_machine_hostname || :rudy
- if hn != :default
- hn = machine.name if hn == :rudy
- print preliminary_separator("Setting hostname to #{hn}... ")
- rbox.hostname(hn)
- puts "done"
- end
- }
- end
-
-
- ## NOTE: This prevents shutdown from doing its thing and prob
- ## isn't necessary.
- ##unless has_remote_task?(routine)
- ## puts "[no remote tasks]"
- ## next
- ##end
-
- enjoy_every_sandwich {
- if Rudy::Routines::Handlers::User.adduser?(routine) # adduser
- task_separator("ADD USER")
- Rudy::Routines::Handlers::User.adduser(routine, machine, rbox)
- end
- }
-
- enjoy_every_sandwich {
- if Rudy::Routines::Handlers::User.authorize?(routine) # authorize
- task_separator("AUTHORIZE USER")
- Rudy::Routines::Handlers::User.authorize(routine, machine, rbox)
- end
- }
-
- enjoy_every_sandwich {
- if Rudy::Routines::Handlers::Script.before?(routine) # before
- task_separator("REMOTE SHELL")
- Rudy::Routines::Handlers::Script.before(routine, sconf, machine, rbox, @option, @argv)
- end
- }
-
- enjoy_every_sandwich {
- if Rudy::Routines::Handlers::Disk.disks?(routine) # disk
- task_separator("DISKS")
- ##if rbox.ostype == "sunos"
- ## puts "Sorry, Solaris disks are not supported yet!"
- ##else
- Rudy::Routines::Handlers::Disk.execute(routine, machine, rbox)
- ##end
- end
- }
-
- end
-
- enjoy_every_sandwich {
- # Startup, shutdown, release, deploy, etc...
- routine_action.call(machine, rbox) if routine_action
- }
-
-
- if is_available
- # The "after" blocks are synonymous with "script" blocks.
- # For some routines, like startup, it makes sense to an
- # "after" block b/c "script" is ambiguous. In generic
- # routines, there is no concept of before or after. The
- # definition is the entire routine so we use "script".
- # NOTE: If both after and script are supplied they will
- # both be executed.
- enjoy_every_sandwich {
- if Rudy::Routines::Handlers::Script.script?(routine) # script
- task_separator("REMOTE SHELL")
- # Runs "after" scripts of routines config
- Rudy::Routines::Handlers::Script.script(routine, sconf, machine, rbox, @option, @argv)
- end
- }
-
- enjoy_every_sandwich {
- if Rudy::Routines::Handlers::Script.after?(routine) # after
- task_separator("REMOTE SHELL")
- # Runs "after" scripts of routines config
- Rudy::Routines::Handlers::Script.after(routine, sconf, machine, rbox, @option, @argv)
- end
- }
-
- rbox.disconnect
- end
- end
-
- enjoy_every_sandwich {
- if Rudy::Routines::Handlers::Script.after_local?(routine) # after_local
- task_separator("LOCAL SHELL")
- lbox.cd Dir.pwd # Run local command block from current working directory
- # Runs "after_local" scripts of routines config
- Rudy::Routines::Handlers::Script.after_local(routine, sconf, lbox, @option, @argv)
- end
- }
-
- # This calls generic_machine_runner for every dependent after routine
- enjoy_every_sandwich {
- execute_dependency(after_dependencies, skip_check, skip_header)
- }
-
- machines
-end
-
-
-
-
-# Does the given +routine+ define any remote tasks?
-def has_remote_task?(routine)
- any = [Rudy::Routines::Handlers::Disk.disks?(routine),
- Rudy::Routines::Handlers::Script.before?(routine),
- Rudy::Routines::Handlers::Script.after?(routine),
- Rudy::Routines::Handlers::Script.script?(routine),
- Rudy::Routines::Handlers::User.authorize?(routine),
- Rudy::Routines::Handlers::User.adduser?(routine),
- !@after_dependencies.nil?,
- !@before_dependencies.nil?]
- # Throw away all false answers (and nil answers)
- any = any.compact.select { |success| success }
- !any.empty? # Returns true if any element contains true
-end
-
-def task_separator(title)
- dashes = 59 - title.size
- dashes = 0 if dashes < 1
- puts ("%s--- %s %s" % [$/, title, '-'*dashes]) if @@global.verbose > 2
-end
-
-
-def routine_separator(name)
- # Not used (for now)
- name = name.to_s
- dashes = 59 - name.size #
- dashes = 0 if dashes < 1
- #puts '%-40s' % [name.bright]
-end
Please sign in to comment.
Something went wrong with that request. Please try again.