Class: Cloudspeq::Providers::DigitalOcean

Inherits:
Base
  • Object
show all
Defined in:
lib/cloudspeq/providers/digital_ocean.rb

Defined Under Namespace

Classes: Machine

Instance Method Summary collapse

Methods inherited from Base

#destroy, #exec, #exec!, find, #refresh, #root_exec, #sync

Constructor Details

#initialize(settings) ⇒ DigitalOcean

Returns a new instance of DigitalOcean.



5
6
7
8
9
10
11
# File 'lib/cloudspeq/providers/digital_ocean.rb', line 5

def initialize(settings)
  ::Digitalocean.client_id  = settings.digital_ocean['client_id']
  ::Digitalocean.api_key    = settings.digital_ocean['api_key']
  @settings =               settings
  @digital  =               settings.digital_ocean
  @machines =               machines
end

Instance Method Details

#create(count = @digital['machine_count']) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cloudspeq/providers/digital_ocean.rb', line 17

def create(count=@digital['machine_count'])
  created = []
  count.to_i.times do 
    response = Digitalocean::Droplet.create({name: generate_machine_name, 
                                             size_id: size.id, 
                                             image_id: image.id, 
                                             region_id: region.id, 
                                             ssh_key_ids: ssh_key.id})
    if response.status == "OK"
      created << response.droplet
    else
      puts response
    end
  end
  created.each do |machine|
    @machines << Machine.new(Digitalocean::Droplet.find(machine.id).droplet.to_h)
  end

end

#imageObject



64
65
66
# File 'lib/cloudspeq/providers/digital_ocean.rb', line 64

def image
  ::Digitalocean::Image.all.images.select{|i| i.name == @digital['image_name']}.first
end

#machines(file = 'cloudspeq_machines.yml') ⇒ Object



37
38
39
40
41
42
# File 'lib/cloudspeq/providers/digital_ocean.rb', line 37

def machines(file='cloudspeq_machines.yml')
  File.write(file, '') unless File.exist?(file)
  yaml = YAML.load_file(file)
  yaml =  [] if yaml == false
  yaml.class == Array ? yaml : [yaml]
end

#provider_settingsObject



13
14
15
# File 'lib/cloudspeq/providers/digital_ocean.rb', line 13

def provider_settings
  @digital
end

#regionObject



76
77
78
# File 'lib/cloudspeq/providers/digital_ocean.rb', line 76

def region
  ::Digitalocean::Region.all.regions.select{|r| r.slug == @digital['region_slug']}.first
end

#remote_machines(all = false) ⇒ Object

by default, only grab machines that look like they’re related to testing be careful if you set this to true and have other machines on your account



46
47
48
49
50
51
52
53
54
# File 'lib/cloudspeq/providers/digital_ocean.rb', line 46

def remote_machines(all=false)
  if all
   machines = ::Digitalocean::Droplet.all
  else
    string = @settings.machine_prefix ? "#{@settings.machine_prefix}-" : "test-"
    machines = ::Digitalocean::Droplet.all.droplets.select{|d| d.name.start_with?(string)}
  end
  machines.collect{|m| Machine.new(m)}
end

#sizeObject



72
73
74
# File 'lib/cloudspeq/providers/digital_ocean.rb', line 72

def size
  ::Digitalocean::Size.all.sizes.select{|s| s.slug == @digital['size_slug']}.first
end

#spool_down_allObject



88
89
90
# File 'lib/cloudspeq/providers/digital_ocean.rb', line 88

def spool_down_all
  destroy remote_machines
end

#ssh_keyObject



68
69
70
# File 'lib/cloudspeq/providers/digital_ocean.rb', line 68

def ssh_key
  ::Digitalocean::SshKey.all.ssh_keys.select{|i| i.name == @digital['ssh_key_name']}.first
end

#statusObject



80
81
82
83
84
85
86
# File 'lib/cloudspeq/providers/digital_ocean.rb', line 80

def status
  status_hashes = machines.collect {|m| {"#{m.attributes.name}" => m.attributes.status} }
  statuses = status_hashes.collect{|m| m.values}.flatten
  new_machines = statuses.select{|m| m == 'new'}
  active_machines = statuses.select{|m| m == 'active'}
  {'total' => statuses.count, 'new' => new_machines.count, 'active' => active_machines.count, 'machines' => status_hashes}
end

#write_machines(machs, file = ) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/cloudspeq/providers/digital_ocean.rb', line 56

def write_machines(machs,file=@digital['machine_file'])
  @machines= machs
  File.open(file,"w") do |f|
    f.write(machs.to_yaml)
  end
  
end