Class: Kitchen::Driver::Digitalocean

Inherits:
SSHBase
  • Object
show all
Defined in:
lib/kitchen/driver/digitalocean.rb

Overview

Digital Ocean driver for Kitchen.

Author:

Constant Summary collapse

PLATFORM_SLUG_MAP =
{
  "centos-7" => "centos-7-x64",
  "centos-8" => "centos-8-x64",
  "debian-9" => "debian-9-x64",
  "debian-10" => "debian-10-x64",
  "debian-11" => "debian-11-x64",
  "fedora-32" => "fedora-32-x64",
  "fedora-33" => "fedora-33-x64",
  "freebsd-11" => "freebsd-11-x64-zfs",
  "freebsd-12" => "freebsd-12-x64-zfs",
  "ubuntu-16" => "ubuntu-16-04-x64",
  "ubuntu-18" => "ubuntu-18-04-x64",
  "ubuntu-20" => "ubuntu-20-04-x64",
}.freeze

Instance Method Summary collapse

Instance Method Details

#create(state) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/kitchen/driver/digitalocean.rb', line 77

def create(state)
  server = create_server

  state[:server_id] = server.id

  info("DigitalOcean instance <#{state[:server_id]}> created.")

  loop do
    sleep 8
    droplet = client.droplets.find(id: state[:server_id])

    break if droplet && droplet.networks[:v4] &&
      droplet.networks[:v4].any? { |n| n[:type] == "public" }
  end
  droplet ||= client.droplets.find(id: state[:server_id])

  state[:hostname] = droplet.networks[:v4]
    .find { |n| n[:type] == "public" }["ip_address"]

  if config[:firewalls]
    debug("trying to add the firewall by id")
    fw_ids = if config[:firewalls].is_a?(String)
               config[:firewalls].split(/\s+|,\s+|,+/)
             elsif config[:firewalls].is_a?(Array)
               config[:firewalls]
             else
               warn("firewalls attribute is not string/array, ignoring")
               []
             end
    debug("firewall : #{YAML.dump(fw_ids.inspect)}")
    fw_ids.each do |fw_id|
      firewall = client.firewalls.find(id: fw_id)
      if firewall
        client.firewalls
          .add_droplets([droplet.id], id: firewall.id)
        debug("firewall added: #{firewall.id}")
      else
        warn("firewalls id: '#{fw_id}' was not found in api, ignoring")
      end
    end
  end

  wait_for_sshd(state[:hostname]); print "(ssh ready)\n"
  debug("digitalocean:create #{state[:hostname]}")
end

#default_imageObject

This method attempts to fetch the platform from a list of well-known platform => slug mappings, and falls back to using just the platform as provided if it can’t find a mapping.



151
152
153
154
# File 'lib/kitchen/driver/digitalocean.rb', line 151

def default_image
  PLATFORM_SLUG_MAP.fetch(instance.platform.name,
                          instance.platform.name)
end

#default_nameObject

Generate what should be a unique server name up to 63 total chars Base name: 15 Username: 15 Hostname: 23 Random string: 7 Separators: 3

Total: 63



164
165
166
167
168
169
170
171
# File 'lib/kitchen/driver/digitalocean.rb', line 164

def default_name
  [
    instance.name.gsub(/\W/, "")[0..14],
    (Etc.getlogin || "nologin").gsub(/\W/, "")[0..14],
    Socket.gethostname.gsub(/\W/, "")[0..22],
    Array.new(7) { rand(36).to_s(36) }.join,
  ].join("-").tr("_", "-")
end

#destroy(state) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/kitchen/driver/digitalocean.rb', line 123

def destroy(state)
  return if state[:server_id].nil?

  # A new droplet cannot be destroyed before it is active
  # Retry destroying the droplet as long as its status is "new"
  loop do
    droplet = client.droplets.find(id: state[:server_id])

    break unless droplet

    if droplet.status != "new"
      client.droplets.delete(id: state[:server_id])
      break
    end

    info("Waiting on DigitalOcean instance <#{state[:server_id]}>
         to be active to destroy it, retrying in 8 seconds")
    sleep 8
  end

  info("DigitalOcean instance <#{state[:server_id]}> destroyed.")
  state.delete(:server_id)
  state.delete(:hostname)
end