Class: CloudRunner::DigitalOcean::Run

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_runner/digital_ocean/run.rb

Constant Summary collapse

DROPLET_DEFAULT_NAMES =
{
  :region => "New York 1",
  :image => "Ubuntu 12.04 x64 Server",
  :size => "512MB",
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(api) ⇒ Run

Returns a new instance of Run.



13
14
15
# File 'lib/cloud_runner/digital_ocean/run.rb', line 13

def initialize(api)
  @api = api
end

Instance Method Details

#create_droplet(opts = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cloud_runner/digital_ocean/run.rb', line 38

def create_droplet(opts={})
  raise "Ssh key must be created" unless @public_ssh_key

  attrs = extract_droplet_attrs(opts)

  @droplet ||= @api.droplets.create(
    :name => "#{name}-droplet",
    :region_id => attrs[:region].id,
    :image_id => attrs[:image].id,
    :size_id => attrs[:size].id,
    :ssh_key_ids => "#{@public_ssh_key.id}",
  ).droplet

  wait_for_droplet_to_be_alive
end

#create_ssh_key(ssh_key) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/cloud_runner/digital_ocean/run.rb', line 21

def create_ssh_key(ssh_key)
  use_ssh_key(ssh_key)

  @public_ssh_key ||= @api.ssh_keys.add(
    :name => "#{name}-ssh-key",
    :ssh_pub_key => ssh_key.public,
  ).ssh_key
end

#delete_dropletObject



62
63
64
# File 'lib/cloud_runner/digital_ocean/run.rb', line 62

def delete_droplet
  @api.droplets.delete(@droplet.id) if @droplet
end

#delete_ssh_keyObject



34
35
36
# File 'lib/cloud_runner/digital_ocean/run.rb', line 34

def delete_ssh_key
  @api.ssh_keys.delete(@public_ssh_key.id) if @public_ssh_key
end

#find_droplet_by_id(droplet_id) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/cloud_runner/digital_ocean/run.rb', line 54

def find_droplet_by_id(droplet_id)
  raise "Droplet id must be specified" unless droplet_id

  @droplet ||= @api.droplets.show(droplet_id).droplet

  wait_for_droplet_to_be_alive
end

#nameObject



17
18
19
# File 'lib/cloud_runner/digital_ocean/run.rb', line 17

def name
  @name ||= "run-#{SecureRandom.hex}"
end

#run_script(local_path, out, err, opts = {}) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/cloud_runner/digital_ocean/run.rb', line 66

def run_script(local_path, out, err, opts={})
  raise "Droplet must be created" unless @droplet
  raise "Local path must be specified" unless local_path

  ssh = CloudRunner::Ssh.new(@droplet.ip_address, "root", @ssh_key)
  ssh.run_script(local_path, out, err, opts)
end

#use_ssh_key(ssh_key) ⇒ Object



30
31
32
# File 'lib/cloud_runner/digital_ocean/run.rb', line 30

def use_ssh_key(ssh_key)
  raise "Ssh key must specified" unless @ssh_key = ssh_key
end