Class: Slushy::Instance

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, instance_id) ⇒ Instance

Returns a new instance of Instance.



12
13
14
15
# File 'lib/slushy/instance.rb', line 12

def initialize(connection, instance_id)
  @connection = connection
  @instance_id = instance_id
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



4
5
6
# File 'lib/slushy/instance.rb', line 4

def connection
  @connection
end

#instance_idObject (readonly)

Returns the value of attribute instance_id.



4
5
6
# File 'lib/slushy/instance.rb', line 4

def instance_id
  @instance_id
end

Class Method Details

.launch(connection, config) ⇒ Object



6
7
8
9
10
# File 'lib/slushy/instance.rb', line 6

def self.launch(connection, config)
  server = connection.servers.create(config)
  server.wait_for { ready? } or raise Slushy::TimeoutError.new("Timeout launching server #{server.id}")
  new(connection, server.id)
end

Instance Method Details

#apt_installsObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/slushy/instance.rb', line 69

def apt_installs
  retry_block(5, [Slushy::CommandFailedError], Slushy::CommandFailedError.new("Command 'apt-get' failed")) do
    puts "Updating apt cache..."
    run_command!('sudo apt-get update')
    puts "Installing ruby..."
    run_command!('sudo apt-get -y install ruby')
    puts "Installing rubygems..."
    run_command!('sudo apt-get -y install rubygems1.8')
  end
end

#bootstrapObject



80
81
82
83
84
85
# File 'lib/slushy/instance.rb', line 80

def bootstrap
  wait_for_connectivity
  apt_installs
  puts "Installing chef..."
  run_command!('sudo gem install chef --no-ri --no-rdoc --version 0.10.8')
end

#converge(cookbooks_path) ⇒ Object

TODO: find the standard Chef term for this



87
88
89
90
91
92
93
94
95
96
# File 'lib/slushy/instance.rb', line 87

def converge(cookbooks_path) # TODO: find the standard Chef term for this
  puts "Copying chef resources from provision directory..."
  cookbooks_path = "#{cookbooks_path}/" unless cookbooks_path.to_s.end_with?('/')
  run_command!('sudo rm -rf /tmp/chef-solo') # scp does not clobber existing files
  scp(cookbooks_path, '/tmp/chef-solo', :recursive => true)
  puts "Converging server, this may take a while (10-20 minutes)"
  path_part = 'PATH=/var/lib/gems/1.8/bin:/usr/local/bin:$PATH'
  cmd = %Q{cd /tmp/chef-solo && sudo sh -c "#{path_part} chef-solo -c solo.rb -j dna.json"}
  run_command!(cmd)
end

#dns_name(*args) ⇒ Object



29
30
31
# File 'lib/slushy/instance.rb', line 29

def dns_name(*args)
  server.dns_name(*args)
end

#run_command(command) ⇒ Object



60
61
62
63
# File 'lib/slushy/instance.rb', line 60

def run_command(command)
  jobs = ssh(command)
  jobs_succeeded?(jobs)
end

#run_command!(command) ⇒ Object



65
66
67
# File 'lib/slushy/instance.rb', line 65

def run_command!(command)
  raise Slushy::CommandFailedError.new("Failed running '#{command}'") unless run_command(command)
end

#scp(*args) ⇒ Object



25
26
27
# File 'lib/slushy/instance.rb', line 25

def scp(*args)
  server.scp(*args)
end

#serverObject



17
18
19
# File 'lib/slushy/instance.rb', line 17

def server
  @server ||= @connection.servers.get(instance_id)
end

#snapshot(name, description) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/slushy/instance.rb', line 33

def snapshot(name, description)
  response = connection.create_image(instance_id, name, description)
  image_id = response.body["imageId"]
  image = connection.images.get(image_id)
  image.wait_for(3600) { ready? } or raise Slushy::TimeoutError.new("Timeout creating snapshot #{image_id}")
  image_id
end

#ssh(*args) ⇒ Object



21
22
23
# File 'lib/slushy/instance.rb', line 21

def ssh(*args)
  server.ssh(*args)
end

#stopObject



46
47
48
49
# File 'lib/slushy/instance.rb', line 46

def stop
  server.stop
  server.wait_for { state == "stopped" } or raise Slushy::TimeoutError.new("Timeout stopping server #{server.id}")
end

#terminateObject



41
42
43
44
# File 'lib/slushy/instance.rb', line 41

def terminate
  server.destroy
  server.wait_for { state == "terminated" } or raise Slushy::TimeoutError.new("Timeout terminating server #{server.id}")
end

#wait_for_connectivityObject



51
52
53
54
55
56
57
58
# File 'lib/slushy/instance.rb', line 51

def wait_for_connectivity
  puts "Waiting for ssh connectivity..."
  retry_block(5, [Errno::ECONNREFUSED, Timeout::Error], Slushy::TimeoutError.new("Timeout connecting to server #{server.id}")) do
    sleep 10
    Timeout.timeout(60) { ssh('ls') }
  end
  puts "Server up and listening for SSH!"
end