Class: Rudy::Machine

Inherits:
Storable
  • Object
show all
Includes:
Rudy::MetaData::ObjectBase
Defined in:
lib/rudy/machines.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rudy::MetaData::ObjectBase

#==, #initialize, #refresh, #save, #to_query, #to_select, #valid?

Methods included from Huxtable

change_environment, change_position, change_region, change_role, change_zone, #check_keys, #config_dirname, create_domain, #current_group_name, #current_machine_address, #current_machine_count, #current_machine_group, #current_machine_hostname, #current_machine_image, #current_machine_name, #current_machine_size, #current_user, #current_user_keypairpath, debug?, #debug?, domain, domain_exists?, #group_metadata, #has_keypair?, #has_keys?, #has_pem_keys?, #has_root_keypair?, keypair_path_to_name, #known_machine_group?, #root_keypairname, #root_keypairpath, #switch_user, update_config, update_global, update_logger, #user_keypairname, #user_keypairpath

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



24
25
26
# File 'lib/rudy/machines.rb', line 24

def instance
  @instance
end

Class Method Details

.generate_machine_dataObject



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rudy/machines.rb', line 151

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.to_s,
    :hosts => { # Add hosts to the /etc/hosts file 
      :dbmaster => "127.0.0.1",
    }
  } 
  data
end

.generate_name(zon, env, rol, pos) ⇒ Object



76
77
78
79
# File 'lib/rudy/machines.rb', line 76

def Machine.generate_name(zon, env, rol, pos)
  pos = pos.to_s.rjust 2, '0'
  ["m", zon, env, rol, pos].join(Rudy::DELIM)
end

Instance Method Details

#destroyObject



142
143
144
145
# File 'lib/rudy/machines.rb', line 142

def destroy
  @ec2inst.destroy(@awsid) if running?
  super
end

#dns_private?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/rudy/machines.rb', line 103

def dns_private?
  !@dns_private.nil? && !@dns_private.empty?
end

#dns_public?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/rudy/machines.rb', line 100

def dns_public?
  !@dns_public.nil? && !@dns_public.empty?
end

#find_next_positionObject



66
67
68
69
70
# File 'lib/rudy/machines.rb', line 66

def find_next_position
  list = @sdb.select(self.to_select(nil, [:position])) || []
  pos = list.size + 1
  pos.to_s.rjust(2, '0')
end

#get_instanceObject



81
82
83
# File 'lib/rudy/machines.rb', line 81

def get_instance
  @ec2inst.get(@awsid)
end

#initObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/rudy/machines.rb', line 26

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'
end

#inspectObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rudy/machines.rb', line 54

def inspect
  update #if !dns_public? && @awsid
  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

#liner_noteObject



37
38
39
40
41
# File 'lib/rudy/machines.rb', line 37

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

#nameObject



72
73
74
# File 'lib/rudy/machines.rb', line 72

def name
  Machine.generate_name(@zone, @environment, @role, @position)
end

#restartObject



147
148
149
# File 'lib/rudy/machines.rb', line 147

def restart
  @ec2inst.restart(@awsid) if running?
end

#running?Boolean

Returns:

  • (Boolean)


165
166
167
168
# File 'lib/rudy/machines.rb', line 165

def running?
  return false if @awsid.nil? || @awsid.empty?
  @ec2inst.running?(@awsid)
end

#start(opts = {}) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rudy/machines.rb', line 107

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)
  
  @ec2inst.create(opts) do |inst|
    @awsid = inst.awsid
    @created = @starts = Time.now
    @state = inst.state
    # Assign IP address only if we have one for that position
    if current_machine_address(@position)
      address = current_machine_address(@position)
      puts "Associating #{address} to #{inst.awsid}"
      begin
        @radd.associate(address, inst.awsid)
      rescue => ex 
        STDERR.puts "Error while associating address (#{ex.class.to_s})"
        Rudy::Utils.bug()
      end
    end
  end
  
  self.save
  
  self
end

#to_s(with_title = false) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/rudy/machines.rb', line 43

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

#updateObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rudy/machines.rb', line 85

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