Class: Cyclid::API::Plugins::Digitalocean

Inherits:
Builder
  • Object
show all
Defined in:
lib/cyclid/plugins/builder/digitalocean.rb

Overview

Digitalocean builder. Uses the Digitalocean API to obtain a build host instance.

Instance Method Summary collapse

Constructor Details

#initializeDigitalocean

Returns a new instance of Digitalocean.



34
35
36
37
# File 'lib/cyclid/plugins/builder/digitalocean.rb', line 34

def initialize
  @config = load_digitalocean_config(Cyclid.config.plugins)
  @client = DropletKit::Client.new(access_token: @config[:access_token])
end

Instance Method Details

#get(args = {}) ⇒ Object

Create & return a build host



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cyclid/plugins/builder/digitalocean.rb', line 40

def get(args = {})
  args.symbolize_keys!

  Cyclid.logger.debug "digitalocean: args=#{args}"

  # If there is one, split the 'os' into a 'distro' and 'release'
  if args.key? :os
    match = args[:os].match(/\A(\w*)_(.*)\Z/)
    distro = match[1] if match
    release = match[2] if match
  else
    # No OS was specified; use the default
    # XXX Defaults should be configurable
    distro = 'ubuntu'
    release = 'trusty'
  end

  # Convert Debian & Ubuntu release codenames
  release_version = if distro == 'ubuntu' or distro == 'debian'
                      codename_to_version(release)
                    else
                      release
                    end

  begin
    # Find, or create, the SSH key Cyclid can use
    build_key = build_ssh_key

    # Create the Droplet
    droplet = DropletKit::Droplet.new(name: create_name,
                                      region: @config[:region],
                                      image: "#{distro}-#{release_version}-x64",
                                      size: @config[:size],
                                      ssh_keys: [build_key.id])
    created = @client.droplets.create droplet

    # Wait for the droplet to become 'active'
    created = wait_for_droplet(created)

    # Create a buildhost from the active Droplet details
    buildhost = DigitaloceanHost.new(name: created.name,
                                     host: created.networks.v4.first.ip_address,
                                     id: created.id,
                                     username: 'root',
                                     workspace: '/root',
                                     key: @config[:ssh_private_key],
                                     distro: distro,
                                     release: release)
  rescue StandardError => ex
    Cyclid.logger.error "couldn't get a build host from Digitalocean: #{ex}"
    raise "digitalocean failed: #{ex}"
  end

  Cyclid.logger.debug "digitalocean buildhost=#{buildhost.inspect}"
  return buildhost
end

#release(_transport, buildhost) ⇒ Object

Destroy the build host



98
99
100
101
102
# File 'lib/cyclid/plugins/builder/digitalocean.rb', line 98

def release(_transport, buildhost)
  @client.droplets.delete(id: buildhost[:id])
rescue StandardError => ex
  Cyclid.logger.error "Digitalcoean destroy timed out: #{ex}"
end