Class: ChefMetalFog::Providers::DigitalOcean

Inherits:
FogDriver
  • Object
show all
Defined in:
lib/chef_metal_fog/providers/digitalocean.rb

Constant Summary

Constants inherited from FogDriver

FogDriver::DEFAULT_OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FogDriver

__new__, #allocate_machine, #allocate_machines, canonicalize_url, #compute, #compute_options, #connect_to_machine, from_provider, from_url, #image_for, inherited, #initialize, new, #provider, provider_class_for, #ready_machine, register_provider_class, #stop_machine, #transport_for

Constructor Details

This class inherits a constructor from ChefMetalFog::FogDriver

Class Method Details

.compute_options_for(provider, id, config) ⇒ Object



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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/chef_metal_fog/providers/digitalocean.rb', line 65

def self.compute_options_for(provider, id, config)
  new_compute_options = {}
  new_compute_options[:provider] = provider
  new_config = { :driver_options => { :compute_options => new_compute_options }}
  new_defaults = {
    :driver_options => { :compute_options => {} },
    :machine_options => { :bootstrap_options => {} }
  }
  result = Cheffish::MergedConfig.new(new_config, config, new_defaults)

  new_compute_options[:digitalocean_client_id] = id if (id && id != '')

  # This uses ~/.tugboat, generated by "tugboat authorize" - see https://github.com/pearkes/tugboat
  tugboat_file = File.expand_path('~/.tugboat')
  if File.exist?(tugboat_file)
    tugboat_data = YAML.load(IO.read(tugboat_file))
    new_compute_options.merge!(
      :digitalocean_client_id => tugboat_data['authentication']['client_key'],
      :digitalocean_api_key => tugboat_data['authentication']['api_key']
    )
    new_defaults[:machine_options].merge!(
      #:ssh_username => tugboat_data['ssh']['ssh_user'],
      :ssh_options => {
        :port => tugboat_data['ssh']['ssh_port'],
        # TODO we ignore ssh_key_path in favor of ssh_key / key_name stuff
        #:key_data => [ IO.read(tugboat_data['ssh']['ssh_key_path']) ] # TODO use paths, not data?
      }
    )

    # TODO verify that the key_name exists and matches the ssh key path

    new_defaults[:machine_options][:bootstrap_options].merge!(
      :region_id => tugboat_data['defaults']['region'].to_i,
      :image_id => tugboat_data['defaults']['image'].to_i,
      :size_id => tugboat_data['defaults']['region'].to_i,
      :private_networking => tugboat_data['defaults']['private_networking'] == 'true',
      :backups_enabled => tugboat_data['defaults']['backups_enabled'] == 'true',
    )
    if tugboat_data['ssh']['ssh_key_path']
      new_defaults[:machine_options][:bootstrap_options][:key_path] = tugboat_data['ssh']['ssh_key_path']
    end
    ssh_key = tugboat_data['defaults']['ssh_key']
    if ssh_key && ssh_key.size > 0
      new_defaults[:machine_options][:bootstrap_options][:key_name] = ssh_key
    end
  end
  id = result[:driver_options][:compute_options][:digitalocean_client_id]

  [result, id]
end

Instance Method Details

#bootstrap_options_for(action_handler, machine_spec, machine_options) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/chef_metal_fog/providers/digitalocean.rb', line 11

def bootstrap_options_for(action_handler, machine_spec, machine_options)
  bootstrap_options = symbolize_keys(machine_options[:bootstrap_options] || {})
  if bootstrap_options[:key_path]
    bootstrap_options[:key_name] ||= File.basename(bootstrap_options[:key_path])
    # Verify that the provided key name and path are in line (or create the key pair if not!)
    driver = self
    ChefMetal.inline_resource(action_handler) do
      fog_key_pair bootstrap_options[:key_name] do
        private_key_path bootstrap_options[:key_path]
        driver driver
      end
    end
  else
    bootstrap_options[:key_name] = overwrite_default_key_willy_nilly(action_handler)
  end

  bootstrap_options[:tags]  = default_tags(machine_spec, bootstrap_options[:tags] || {})

  if !bootstrap_options[:image_id]
    bootstrap_options[:image_name] ||= 'CentOS 6.4 x32'
    bootstrap_options[:image_id] = compute.images.select { |image| image.name == bootstrap_options[:image_name] }.first.id
  end
  if !bootstrap_options[:flavor_id]
    bootstrap_options[:flavor_name] ||= '512MB'
    bootstrap_options[:flavor_id] = compute.flavors.select { |flavor| flavor.name == bootstrap_options[:flavor_name] }.first.id
  end
  if !bootstrap_options[:region_id]
    bootstrap_options[:region_name] ||= 'San Francisco 1'
    bootstrap_options[:region_id] = compute.regions.select { |region| region.name == bootstrap_options[:region_name] }.first.id
  end
  found_key = compute.ssh_keys.select { |k| k.name == bootstrap_options[:key_name] }.first
  if !found_key
    raise "Could not find key named '#{bootstrap_options[:key_name]}' on #{driver_url}"
  end
  bootstrap_options[:ssh_key_ids] ||= [ found_key.id ]

  # You don't get to specify name yourself
  bootstrap_options[:name] = machine_spec.name

  bootstrap_options
end

#creatorObject



7
8
9
# File 'lib/chef_metal_fog/providers/digitalocean.rb', line 7

def creator
  ''
end

#destroy_machine(action_handler, machine_spec, machine_options) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/chef_metal_fog/providers/digitalocean.rb', line 53

def destroy_machine(action_handler, machine_spec, machine_options)
  server = server_for(machine_spec)
  if server && server.state != 'archive'
    action_handler.perform_action "destroy machine #{machine_spec.name} (#{machine_spec.location['server_id']} at #{driver_url})" do
      server.destroy
    end
  end
  machine_spec.location = nil
  strategy = convergence_strategy_for(machine_spec, machine_options)
  strategy.cleanup_convergence(action_handler, machine_spec)
end