Class: Sumo

Inherits:
Object
  • Object
show all
Defined in:
lib/sumo.rb

Instance Method Summary collapse

Instance Method Details

#attach(volume, instance, device) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/sumo.rb', line 57

def attach(volume, instance, device)
  result = ec2.attach_volume(
    :volume_id => volume,
    :instance_id => instance,
    :device => device
  )
  "done"
end

#attached_volumes ⇒ Object



49
50
51
# File 'lib/sumo.rb', line 49

def attached_volumes
  volumes.select { |vol| vol[:status] == 'in-use' }
end

#available_volumes ⇒ Object



45
46
47
# File 'lib/sumo.rb', line 45

def available_volumes
  volumes.select { |vol| vol[:status] == 'available' }
end

#bootstrap_chef(hostname) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/sumo.rb', line 190

def bootstrap_chef(hostname)
  commands = [
    'sudo apt-get update',
    'sudo apt-get autoremove -y',
    'if [ ! -f /usr/lib/ruby/1.8/net/https.rb ]; then sudo apt-get install -y xfsprogs xfsdump xfslibs-dev ruby ruby-dev rubygems libopenssl-ruby1.8 git-core; fi',
    'if [ ! -d /etc/chef ]; then sudo mkdir /etc/chef; fi',
    'if [ ! -d /var/lib/gems/1.8/gems/chef-* ]; then sudo gem install chef ohai --no-rdoc --no-ri; fi',
    config['cookbooks_url'] ? "if [ -d chef-cookbooks ]; then cd chef-cookbooks; git pull; else git clone #{config['cookbooks_url']} chef-cookbooks; fi" : "echo done"
  ]
  ssh(hostname, commands)
  if !config['cookbooks_url'] && config['cookbooks_dir']
    scp(hostname, config['cookbooks_dir'], "chef-cookbooks")
  end
  if config['chef-validation']
    scp(hostname, config['chef-validation'], "validation.pem")
    ssh(hostname, ["sudo mv validation.pem /etc/chef/"])
   end
end

#config ⇒ Object



307
308
309
# File 'lib/sumo.rb', line 307

def config
  @config ||= default_config.merge read_config
end

#console_output(instance_id) ⇒ Object



303
304
305
# File 'lib/sumo.rb', line 303

def console_output(instance_id)
  ec2.get_console_output(:instance_id => instance_id)["output"]
end

#create_keypair ⇒ Object



338
339
340
341
342
# File 'lib/sumo.rb', line 338

def create_keypair
  keypair = ec2.create_keypair(:key_name => "sumo").keyMaterial
  File.open(keypair_file, 'w') { |f| f.write keypair }
  File.chmod 0600, keypair_file
end

#create_security_group ⇒ Object



344
345
346
347
# File 'lib/sumo.rb', line 344

def create_security_group
  ec2.create_security_group(:group_name => 'sumo', :group_description => 'Sumo')
rescue AWS::InvalidGroupDuplicate
end

#create_volume(size) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/sumo.rb', line 71

def create_volume(size)
  result = ec2.create_volume(
    :availability_zone => config['availability_zone'],
    :size => size.to_s
  )
  result["volumeId"]
end

#default_config ⇒ Object



315
316
317
318
319
320
321
322
# File 'lib/sumo.rb', line 315

def default_config
  {
    'user' => 'ubuntu',
    'ami' => 'ami-1234de7b', # Ubuntu 10.04 LTS (Lucid Lynx)
    'availability_zone' => 'us-east-1d',
    'instance_size' => 't1.micro'
  }
end

#destroy_volume(volume) ⇒ Object



87
88
89
90
# File 'lib/sumo.rb', line 87

def destroy_volume(volume)
  ec2.delete_volume(:volume_id => volume)
  "done"
end

#detach(volume) ⇒ Object



66
67
68
69
# File 'lib/sumo.rb', line 66

def detach(volume)
  result = ec2.detach_volume(:volume_id => volume, :force => "true")
  "done"
end

#ec2 ⇒ Object



360
361
362
363
364
365
366
# File 'lib/sumo.rb', line 360

def ec2
   @ec2 ||= AWS::EC2::Base.new(
     :access_key_id => config['access_id'], 
     :secret_access_key => config['access_secret'], 
     :server => server
   )
end

#fetch_list ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sumo.rb', line 92

def fetch_list
  result = ec2.describe_instances
  return [] unless result.reservationSet

  instances = []
  result.reservationSet.item.each do |r|
    r.instancesSet.item.each do |item|
      instances << {
        :instance_id => item.instanceId,
        :status => item.instanceState.name,
        :hostname => item.dnsName,
        :local_dns => item.privateDnsName,
        :private_ip => item.privateIpAddress
      }
    end
  end
  instances
end

#fetch_resources(hostname) ⇒ Object



286
287
288
289
290
291
# File 'lib/sumo.rb', line 286

def fetch_resources(hostname)
  cmd = "ssh -i #{keypair_file} #{config['user']}@#{hostname} 'sudo cat /root/resources' 2>&1"
  out = IO.popen(cmd, 'r') { |pipe| pipe.read }
  abort "failed to read resources, output:\n#{out}" unless $?.success?
  parse_resources(out, hostname)
end

#find(id_or_hostname) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/sumo.rb', line 111

def find(id_or_hostname)
  return unless id_or_hostname
  id_or_hostname = id_or_hostname.strip.downcase
  list.detect do |inst|
    inst[:hostname] == id_or_hostname or
    inst[:instance_id] == id_or_hostname or
    inst[:instance_id].gsub(/^i-/, '') == id_or_hostname
  end
end

#find_volume(volume_id) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/sumo.rb', line 121

def find_volume(volume_id)
  return unless volume_id
  volume_id = volume_id.strip.downcase
  volumes.detect do |volume|
    volume[:volume_id] == volume_id or
    volume[:volume_id].gsub(/^vol-/, '') == volume_id
  end
end

#format_volume(volume, instance, device, mountpoint) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/sumo.rb', line 79

def format_volume(volume, instance, device, mountpoint)
commands = [
  "if [ ! -d #{mountpoint} ]; then sudo mkdir #{mountpoint}; fi",
  "if [ -b /dev/#{device}1 ]; then sudo mount /dev/#{device}1 #{mountpoint}; else echo ',,L' | sudo sfdisk /dev/#{device} && sudo mkfs.xfs /dev/#{device}1 && sudo mount /dev/#{device}1 #{mountpoint}; fi"
]
ssh(instance, commands)
end

#instance_info(instance_id) ⇒ Object



142
143
144
145
146
# File 'lib/sumo.rb', line 142

def instance_info(instance_id)
  fetch_list.detect do |inst|
    inst[:instance_id] == instance_id
  end
end

#keypair_file ⇒ Object



334
335
336
# File 'lib/sumo.rb', line 334

def keypair_file
  "#{sumo_dir}/keypair.pem"
end

#launch ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sumo.rb', line 7

def launch
  ami = config['ami']
  raise "No AMI selected" unless ami

  create_keypair unless File.exists? keypair_file

  create_security_group
  open_firewall(22)

  result = ec2.run_instances(
    :image_id => ami,
    :instance_type => config['instance_size'],
    :key_name => 'sumo',
    :security_group => [ 'sumo' ],
    :availability_zone => config['availability_zone']
  )
  result.instancesSet.item[0].instanceId
end

#list ⇒ Object



26
27
28
# File 'lib/sumo.rb', line 26

def list
  @list ||= fetch_list
end

#list_by_status(status) ⇒ Object



138
139
140
# File 'lib/sumo.rb', line 138

def list_by_status(status)
  list.select { |i| i[:status] == status }
end

#new_ssh(hostname, cmds) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/sumo.rb', line 247

def new_ssh(hostname, cmds)
   Net::SSH.start(hostname, config['user'], :keys => [keypair_file], :compression => "none") do |ssh|
     # capture all stderr and stdout output from a remote process
           
     File.open(File.expand_path("~/.sumo/ssh.log"), "w") do |log|
       ssh.open_channel do |channel|          
         cmds.each do |cmd|
           channel.exec cmd do |ch, success|
            abort "failed on #{cmd}\nCheck ~/.sumo/ssh.log for the output" unless success

             channel.on_data do |ch, data|
               puts "Got data #{data.inspect}"
               log << data
               log.flush
             end

             channel.on_extended_data do |ch, type, data|
               puts "Got data #{data.inspect}"
               log << data
               log.flush
             end
             
             channel.on_close do |ch|
               puts "channel is closing!"
             end
           end
         end
       end

       ssh.loop
     end
   end
end

#nondestroyed_volumes ⇒ Object



53
54
55
# File 'lib/sumo.rb', line 53

def nondestroyed_volumes
  volumes.select { |vol| vol[:status] != 'deleting' }
end

#open_firewall(port) ⇒ Object



349
350
351
352
353
354
355
356
357
358
# File 'lib/sumo.rb', line 349

def open_firewall(port)
  ec2.authorize_security_group_ingress(
    :group_name => 'sumo',
    :ip_protocol => 'tcp',
    :from_port => port,
    :to_port => port,
    :cidr_ip => '0.0.0.0/0'
  )
rescue AWS::InvalidPermissionDuplicate
end

#parse_resources(raw, hostname) ⇒ Object



293
294
295
296
297
# File 'lib/sumo.rb', line 293

def parse_resources(raw, hostname)
  raw.split("\n").map do |line|
    line.gsub(/localhost/, hostname)
  end
end

#pending ⇒ Object



134
135
136
# File 'lib/sumo.rb', line 134

def pending
  list_by_status('pending')
end

#read_config ⇒ Object



328
329
330
331
332
# File 'lib/sumo.rb', line 328

def read_config
  YAML.load File.read("#{sumo_dir}/config.yml")
rescue Errno::ENOENT
  raise "Sumo is not configured, please fill in ~/.sumo/config.yml"
end

#resources(hostname) ⇒ Object



281
282
283
284
# File 'lib/sumo.rb', line 281

def resources(hostname)
  @resources ||= {}
  @resources[hostname] ||= fetch_resources(hostname)
end

#running ⇒ Object



130
131
132
# File 'lib/sumo.rb', line 130

def running
  list_by_status('running')
end

#scp(hostname, directory, endpoint = ".") ⇒ Object



240
241
242
243
244
245
# File 'lib/sumo.rb', line 240

def scp(hostname, directory, endpoint=".")
  `scp -i #{keypair_file} -r #{directory} #{config['user']}@#{hostname}:#{endpoint}`
  unless $?.success?
    abort "failed to transfer #{directory}"
  end
end

#server ⇒ Object



368
369
370
371
372
# File 'lib/sumo.rb', line 368

def server
 zone = config['availability_zone']
 host = zone.slice(0, zone.length - 1)
 "#{host}.ec2.amazonaws.com"
end

#set(key, value) ⇒ Object



311
312
313
# File 'lib/sumo.rb', line 311

def set(key, value)
  config[key] = value
end

#setup_role(hostname, instance_id, role) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/sumo.rb', line 209

def setup_role(hostname, instance_id, role)
  commands = [
    "if [ ! -f /etc/chef/client.pem ]; then cd chef-cookbooks",
    "sudo /var/lib/gems/1.8/bin/chef-solo -c config/solo.rb -j roles/bootstrap.json -r http://s3.amazonaws.com/chef-solo/bootstrap-latest.tar.gz",
    "if [ -f config/client.rb ]; then sudo cp config/client.rb /etc/chef/client.rb; fi",
    "sudo /var/lib/gems/1.8/bin/chef-client",
    "sudo rm /etc/chef/validation.pem; fi"
  ]
  ssh(hostname, commands)
  `knife node run_list add #{instance_id} "role[#{role}]"`
  ssh(hostname, ["sudo /var/lib/gems/1.8/bin/chef-client"])
end

#ssh(hostname, cmds) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/sumo.rb', line 222

def ssh(hostname, cmds)
  unless IO.read(File.expand_path("~/.ssh/known_hosts")).include?(hostname)
    `ssh-keyscan -t rsa #{hostname} >> $HOME/.ssh/known_hosts`
    if config['deploy_key']
      scp(hostname, config['deploy_key'], ".ssh/id_rsa")
     end
     if config['known_hosts']
       scp(hostname, config['known_hosts'], ".ssh/known_hosts")
     end
   end
  IO.popen("ssh -i #{keypair_file} #{config['user']}@#{hostname} > #{config['logfile'] || "~/.sumo/ssh.log"} 2>&1", "w") do |pipe|
    pipe.puts cmds.join(' && ')
  end
  unless $?.success?
    abort "failed\nCheck #{config['logfile'] || "~/.sumo/ssh.log"} for the output"
  end
end

#sumo_dir ⇒ Object



324
325
326
# File 'lib/sumo.rb', line 324

def sumo_dir
  "#{ENV['HOME']}/.sumo"
end

#terminate(instance_id) ⇒ Object



299
300
301
# File 'lib/sumo.rb', line 299

def terminate(instance_id)
  ec2.terminate_instances(:instance_id => [ instance_id ])
end

#volumes ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sumo.rb', line 30

def volumes
  result = ec2.describe_volumes
  return [] unless result.volumeSet

  result.volumeSet.item.map do |row|
    {
      :volume_id => row["volumeId"],
      :size => row["size"],
      :status => row["status"],
      :device => (row["attachmentSet"]["item"].first["device"] rescue ""),
      :instance_id => (row["attachmentSet"]["item"].first["instanceId"] rescue ""),
    }
  end
end

#wait_for_hostname(instance_id) ⇒ Object

Raises:

  • (ArgumentError)


148
149
150
151
152
153
154
155
156
157
158
# File 'lib/sumo.rb', line 148

def wait_for_hostname(instance_id)
  raise ArgumentError unless instance_id and instance_id.match(/^i-/)
  loop do
    if inst = instance_info(instance_id)
      if hostname = inst[:hostname]
        return hostname
      end
    end
    sleep 1
  end
end

#wait_for_private_ip(instance_id) ⇒ Object

Raises:

  • (ArgumentError)


160
161
162
163
164
165
166
167
168
169
170
# File 'lib/sumo.rb', line 160

def wait_for_private_ip(instance_id)
  raise ArgumentError unless instance_id and instance_id.match(/^i-/)
  loop do
    if inst = instance_info(instance_id)
      if private_ip = inst[:private_ip]
        return private_ip
      end
    end
    sleep 1
  end
end

#wait_for_ssh(hostname) ⇒ Object

Raises:

  • (ArgumentError)


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/sumo.rb', line 172

def wait_for_ssh(hostname)
  raise ArgumentError unless hostname
  loop do
    begin
      Timeout::timeout(4) do
        TCPSocket.new(hostname, 22)
        IO.popen("ssh -i #{keypair_file} #{config['user']}@#{hostname} > #{config['logfile'] || "~/.sumo/ssh.log"} 2>&1", "w") do |pipe|
          pipe.puts "ls"
        end
        if $?.success?
          return
        end
      end
    rescue SocketError, Timeout::Error, Errno::ECONNREFUSED, Errno::EHOSTUNREACH
    end
  end
end