Class: Rubber::Cloud::DigitalOcean

Inherits:
Fog
  • Object
show all
Defined in:
lib/rubber/cloud/digital_ocean.rb

Instance Attribute Summary

Attributes inherited from Base

#capistrano, #env

Instance Method Summary collapse

Methods inherited from Fog

#after_create_volume, #after_destroy_volume, #attach_static_ip, #before_create_volume, #before_destroy_volume, #compute_provider, #create_image, #create_static_ip, #create_volume, #describe_images, #describe_load_balancers, #describe_static_ips, #destroy_image, #destroy_spot_instance_request, #destroy_static_ip, #destroy_volume, #detach_static_ip, #reboot_instance, #should_destroy_volume_when_instance_destroyed?, #start_instance, #stop_instance, #storage, #storage_provider, #table_store

Methods inherited from Base

#after_create_instance, #after_refresh_instance, #after_start_instance, #after_stop_instance, #before_create_instance, #before_refresh_instance, #before_start_instance, #before_stop_instance, #describe_security_groups, #inject_auto_security_groups, #isolate_group_name, #isolate_groups, #isolate_prefix, #setup_security_groups, #setup_vpc, #should_disable_password_based_ssh_login?

Constructor Details

#initialize(env, capistrano) ⇒ DigitalOcean

Returns a new instance of DigitalOcean.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rubber/cloud/digital_ocean.rb', line 9

def initialize(env, capistrano)
  compute_credentials = {
    :provider => 'DigitalOcean',
    :version => 'v2',
    :digitalocean_token => env.digital_ocean_token,
  }

  if env.cloud_providers && env.cloud_providers.aws
    storage_credentials = {
      :provider => 'AWS',
      :aws_access_key_id => env.cloud_providers.aws.access_key,
      :aws_secret_access_key => env.cloud_providers.aws.secret_access_key,
      :path_style => true
    }

    storage_credentials[:region] = env.cloud_providers.aws.region

    env['storage_credentials'] = storage_credentials
  end

  env['compute_credentials'] = compute_credentials
  super(env, capistrano)
end

Instance Method Details

#active_stateObject



137
138
139
# File 'lib/rubber/cloud/digital_ocean.rb', line 137

def active_state
  'active'
end

#create_instance(instance_alias, image_name, image_type, security_groups, availability_zone, region, fog_options = {}) ⇒ Object



33
34
35
36
37
38
39
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
# File 'lib/rubber/cloud/digital_ocean.rb', line 33

def create_instance(instance_alias, image_name, image_type, security_groups, availability_zone, region, fog_options={})
  do_region = compute_provider.regions.find { |r| [r.name, r.slug].include?(region) }
  if do_region.nil?
    raise "Invalid region for DigitalOcean: #{region}"
  end

  if env.private_networking && ! do_region.features.include?("private_networking")
    raise "Private networking is enabled, but region #{region} does not support it"
  end

  image = compute_provider.images.find { |i| i.name == image_name }
  if image.nil?
    raise "Invalid image name for DigitalOcean: #{image_name}"
  end

  # Downcase image_type for backward compatability with v1
  flavor = compute_provider.flavors.find { |f| f.slug == image_type.downcase }

  if flavor.nil?
    raise "Invalid image type for DigitalOcean: #{image_type}"
  end

  if env.key_name.nil?
    raise 'missing key_name for DigitalOcean'
  end

  # Check if the SSH key has been added to DigitalOcean yet.
  # TODO (nirvdrum 03/23/13): DigitalOcean has an API for getting a single SSH key, but it hasn't been added to fog yet.  We should add it.
  ssh_key = compute_provider.list_ssh_keys.body['ssh_keys'].find { |key| key['name'] == env.key_name }
  if ssh_key.nil?
    if env.key_file
      compute_provider.create_ssh_key(env.key_name, File.read("#{env.key_file}.pub"))

      # Although not documented, DigitalOcean is eventually consistent.  Receiving a 200 response with the key
      # body does not mean the key has propagated through their systems yet.  Thus we need to query to see if
      # the key is yet available.  Otherwise our request will end up creating a droplet without an attached key.

      begin
        sleep(0.5)
        ssh_key = compute_provider.list_ssh_keys.body['ssh_keys'].find { |key| key['name'] == env.key_name }
      end while ssh_key.nil?

    else
      raise 'Missing key_file for DigitalOcean'
    end
  end

  response = compute_provider.servers.create({:name => "#{Rubber.env}-#{instance_alias}",
                                              :image => image.slug,
                                              :size => flavor.slug,
                                              :flavor => flavor.slug,
                                              :region => do_region.slug,
                                              :ssh_keys => [ssh_key['id']],
                                              :private_networking => (env.private_networking.to_s.downcase == 'true')
                                             }
                                              .merge(Rubber::Util.symbolize_keys(fog_options))
                                            )

  response.id
end

#describe_instances(instance_id = nil) ⇒ Object



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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rubber/cloud/digital_ocean.rb', line 94

def describe_instances(instance_id=nil)
  instances = []
  opts = {}

  if instance_id
    response = [compute_provider.servers.get(instance_id)]
  else
    response = compute_provider.servers.all(opts)
  end

  response.each do |item|
    instance = {}
    instance[:id] = item.id
    instance[:state] = item.status
    instance[:type] = item.size_slug

    public_networking_info = item.networks['v4'].find do |n|
      n['type'] == 'public'
    end

    if public_networking_info
      instance[:external_ip] = public_networking_info['ip_address']
    end

    private_networking_info = item.networks['v4'].find do |n|
      n['type'] == 'private'
    end

    if private_networking_info
      instance[:internal_ip] = private_networking_info['ip_address']
    elsif public_networking_info
      instance[:internal_ip] = public_networking_info['ip_address']
    end

    instance[:region_id] = item.region
    instance[:provider] = 'digital_ocean'
    instance[:platform] = Rubber::Platforms::LINUX
    instances << instance
  end

  return instances
end

#destroy_instance(instance_id) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rubber/cloud/digital_ocean.rb', line 141

def destroy_instance(instance_id)
  # The Digital Ocean API will return a 422 if we attempt to destroy an
  # instance that's in the middle of booting up, so wait until it's
  # in a non-"new" state
  print 'Waiting for non-new instance state'

  loop do
    instance = describe_instances(instance_id).first

    print '.'

    break unless instance[:state] == 'new'

    sleep 1
  end

  response = compute_provider.servers.get(instance_id).delete()
end