Skip to content

Commit

Permalink
Fix support for knife-digtal_ocean >= 2.0.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnichol committed Dec 3, 2014
1 parent d9a85b7 commit fcdeaf3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 48 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ To spin up your Chef Server on [Digital Ocean][do_site]:
```bash
knife server bootstrap digitalocean \
--node-name chefapalooza.example.com \
--digital_ocean_client_id $DIGITAL_OCEAN_CLIENT_ID \
--digital_ocean_api_key $DIGITAL_OCEAN_API_KEY \
--location 3 \
--size 63 \
--image 5588928 \
--digital_ocean_access_token $DIGITAL_OCEAN_ACCESS_TOKEN \
--location sfo1 \
--size 1gb \
--image ubuntu-12-04-x64 \
--ssh-keys $DIGITAL_OCEAN_SSH_KEY_ID \
--identity-file ~/.ssh/id_rsa-do
```
Expand Down Expand Up @@ -216,8 +215,7 @@ knife[:flavor] = "t1.micro"
knife[:linode_api_key] = "MY_KEY"

# for digitalocean
knife[:digital_ocean_client_id] = "MY_CLIENT_ID"
knife[:digital_ocean_api_key] = "MY_KEY"
knife[:digital_ocean_acceess_token] = "MY_TOKEN"
```

Better yet, why not try a more generic [knife.rb][chef_bootstrap_knife_rb] file
Expand Down
2 changes: 1 addition & 1 deletion knife-server.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Gem::Specification.new do |gem|
gem.add_dependency "chef", ">= 0.10.10"

gem.add_development_dependency "rake"
gem.add_development_dependency "knife-digital_ocean"
gem.add_development_dependency "knife-digital_ocean", ">= 2.0.0"
gem.add_development_dependency "knife-ec2", ">= 0.5.12"
gem.add_development_dependency "knife-linode"
gem.add_development_dependency "knife-openstack", ">= 1.0.0"
Expand Down
14 changes: 6 additions & 8 deletions lib/chef/knife/server_bootstrap_digitalocean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ServerBootstrapDigitalocean < Knife

begin
require "chef/knife/digital_ocean_droplet_create"
require "fog"
require "droplet_kit"
Chef::Knife::DigitalOceanDropletCreate.load_deps

current_options = options
Expand Down Expand Up @@ -96,19 +96,17 @@ def digital_ocean_bootstrap
end

def digital_ocean_connection
@digital_ocean_connection ||= Fog::Compute.new(
:provider => "DigitalOcean",
:digitalocean_client_id => config_val(:digital_ocean_client_id),
:digitalocean_api_key => config_val(:digital_ocean_api_key)
@digital_ocean_connection ||= DropletKit::Client.new(
:access_token => config_val(:digital_ocean_access_token)
)
end

def server_ip_address
server = digital_ocean_connection.servers.find do |s|
s.state == "active" && s.name == config_val(:chef_node_name)
server = digital_ocean_connection.droplets.all.find do |s|
s.status == "active" && s.name == config_val(:chef_node_name)
end

server && server.public_ip_address
server && server.public_ip
end

private
Expand Down
50 changes: 18 additions & 32 deletions spec/chef/knife/server_bootstrap_digitalocean_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,15 @@
@knife.config[:ssh_user] = "root"
end

let(:connection) { double(Fog::Compute::AWS) }
let(:connection) { double(DropletKit::Client) }

describe "#digital_ocean_bootstrap" do

before do
@knife.config[:chef_node_name] = "shave.yak"
@knife.config[:ssh_user] = "jdoe"
@knife.config[:identity_file] = "~/.ssh/mykey_dsa"
@knife.config[:digital_ocean_client_id] = "clientxyz"
@knife.config[:digital_ocean_api_key] = "do123"
@knife.config[:digital_ocean_access_token] = "do123"
@knife.config[:distro] = "distro-praha"
@knife.config[:webui_password] = "daweb"
@knife.config[:amqp_password] = "queueitup"
Expand Down Expand Up @@ -79,12 +78,8 @@
expect(bootstrap.config[:identity_file]).to eq("~/.ssh/mykey_dsa")
end

it "configs the bootstrap's digital_ocean_client_id" do
expect(bootstrap.config[:digital_ocean_client_id]).to eq("clientxyz")
end

it "configs the bootstrap's digital_ocean_api_key" do
expect(bootstrap.config[:digital_ocean_api_key]).to eq("do123")
it "configs the bootstrap's digital_ocean_access_token" do
expect(bootstrap.config[:digital_ocean_access_token]).to eq("do123")
end

it "configs the bootstrap's distro" do
Expand Down Expand Up @@ -138,67 +133,58 @@
before do
@before_config = Hash.new
@before_config[:knife] = Hash.new
[:digital_ocean_client_id, :digital_ocean_api_key].each do |key|
[:digital_ocean_access_token].each do |key|
@before_config[:knife][key] = Chef::Config[:knife][key]
end

Chef::Config[:knife][:digital_ocean_client_id] = "client"
Chef::Config[:knife][:digital_ocean_api_key] = "api"
Chef::Config[:knife][:digital_ocean_access_token] = "api"
end

after do
[:digital_ocean_client_id, :digital_ocean_api_key].each do |key|
[:digital_ocean_access_token].each do |key|
Chef::Config[:knife][key] = @before_config[:knife][key]
end
end

it "constructs a connection" do
expect(Fog::Compute).to receive(:new).with(
:provider => "DigitalOcean",
:digitalocean_client_id => "client",
:digitalocean_api_key => "api"
)
expect(DropletKit::Client).to receive(:new).with(:access_token => "api")

@knife.digital_ocean_connection
end
end

describe "#server_ip_address" do

let(:droplets_resource) { double(:all => droplets) }

before do
@knife.config[:chef_node_name] = "yak"
allow(@knife).to receive(:digital_ocean_connection) { connection }
allow(connection).to receive(:droplets) { droplets_resource }
end

context "when server is found" do

before do
allow(connection).to receive(:servers) { [server] }
end
context "when single server is found" do

let(:server) do
double(
:name => "yak",
:state => "active",
:public_ip_address => "10.11.12.13"
)
double(:name => "yak", :status => "active", :public_ip => "10.11.12.13")
end

let(:droplets) { [server] }

it "returns the provisioned ip address" do
expect(@knife.server_ip_address).to eq("10.11.12.13")
end

it "ignores terminated instances" do
allow(server).to receive(:state) { "foobar" }
allow(server).to receive(:status) { "foobar" }

expect(@knife.server_ip_address).to be_nil
end
end

context "when server is not found" do
before do
allow(connection).to receive(:servers) { [] }
end

let(:droplets) { [] }

it "returns nil" do
expect(@knife.server_ip_address).to be_nil
Expand Down

0 comments on commit fcdeaf3

Please sign in to comment.