Skip to content

Commit 70d7aa2

Browse files
authored
Merge pull request #367 from Temikus/bootstrap_autodelete
Bootstrap autodelete
2 parents 1185ec1 + 3dea8e2 commit 70d7aa2

File tree

4 files changed

+81
-24
lines changed

4 files changed

+81
-24
lines changed

CHANGELOG.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1
1010

1111
- \#338 `Fog::Google::SQL` resources are now created and destroyed synchronously by default.
1212
You can override it in a standard manner by passing a parameter to async method, e.g.:
13-
`Fog::Google::SQL::Instance.create(true)`
13+
`Fog::Google::SQL::Instance.create(true)` [temikus]
14+
- \#367 `Fog::Compute::Google::Server.bootstrap` changes [temikus]
15+
- Now creates instances with disks that automatically delete on instance shutdown.
16+
- Now creates instances with a public IP address by default.
1417

1518
#### Added
1619

17-
- \#361 `Fog::Compute::Google::Server` now recognises `network_ip` attribute to specify internal IP [mattimatti]
20+
- \#361 `Fog::Compute::Google::Server` now recognises `network_ip` attribute to specify internal IP. [mattimatti]
1821

1922
#### Fixed
2023

21-
- \#338 Fixed SQL Users model workflow
24+
- \#338 Fixed SQL Users model workflow [temikus]
2225
- \#359 Fix whitespace escaping in XML Storage methods [temikus]
2326
- \#366 Fixing `Server` model to properly accept `:private_key_path` and `:public_key_path` attributes again. [temikus]
27+
- \#367 `Fog::Compute::Google::Server.bootstrap` parameters are now properly merged with default ones. [tesmikus]
2428

2529
### Development changes
2630

examples/bootstrap.rb

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
# All examples presume that you have a ~/.fog credentials file set up.
22
# More info on it can be found here: http://fog.io/about/getting_started.html
3+
# Code can be ran by simply invoking `ruby bootstrap.rb`
4+
# Note: this example will require 'net-ssh' gem to be installed
35

46
require "bundler"
57
Bundler.require(:default, :development)
6-
# Uncomment this if you want to make real requests to GCE (you _will_ be billed!)
7-
# WebMock.disable!
88

9-
def test
10-
connection = Fog::Compute.new(:provider => "Google")
9+
p "Connecting to google..."
10+
p "======================="
11+
connection = Fog::Compute.new(:provider => "Google")
1112

12-
server = connection.servers.bootstrap
13+
p "Bootstrapping a server..."
14+
p "========================="
15+
server = connection.servers.bootstrap
1316

14-
raise "Could not bootstrap sshable server." unless server.ssh("whoami")
15-
raise "Could not delete server." unless server.destroy
16-
end
17+
p "Waiting for server to be sshable..."
18+
p "==================================="
19+
server.wait_for { sshable? }
1720

18-
test
21+
p "Trying to send an SSH command..."
22+
p "================================"
23+
raise "Could not bootstrap sshable server." unless server.ssh("whoami")
24+
25+
p "Deleting a server..."
26+
p "===================="
27+
raise "Could not delete server." unless server.destroy

lib/fog/compute/google/models/servers.rb

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,29 @@ def bootstrap(public_key_path: nil, **opts)
6363
disks = [disk]
6464
end
6565

66-
data = opts.merge(
67-
:name => name,
68-
:zone => zone_name,
69-
:disks => disks,
70-
:public_key_path => get_public_key(public_key_path),
71-
:username => ENV["USER"]
72-
)
66+
# TODO: Remove the network init when #360 is fixed
67+
network = { :network => "global/networks/default",
68+
:access_configs => [{ :name => "External NAT",
69+
:type => "ONE_TO_ONE_NAT" }] }
70+
71+
# Merge the options with the defaults, overwriting defaults
72+
# if an option is provided
73+
data = { :name => name,
74+
:zone => zone_name,
75+
:disks => disks,
76+
:network_interfaces => [network],
77+
:public_key => get_public_key(public_key_path),
78+
:username => ENV["USER"] }.merge(opts)
79+
7380
data[:machine_type] = "n1-standard-1" unless data[:machine_type]
7481

7582
server = new(data)
76-
server.save(:username => user, :public_key => public_key)
77-
# TODO: sshable? was removed, needs to be fixed for tests
78-
# server.wait_for { sshable? }
83+
server.save
84+
server.wait_for { ready? }
85+
86+
# Set the disk to be autodeleted
87+
server.set_disk_auto_delete(true)
88+
7989
server
8090
end
8191

@@ -99,7 +109,7 @@ def get_public_key(public_key_path)
99109
end
100110

101111
if public_key_path.nil? || public_key_path.empty?
102-
raise ArgumentError("cannot bootstrap instance without public key file")
112+
raise Fog::Errors::Error.new("Cannot bootstrap instance without a public key")
103113
end
104114

105115
File.read(File.expand_path(public_key_path))

test/integration/compute/test_servers.rb

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,40 @@ def test_set_metadata
1515
server.wait_for { ready? }
1616
server.set_metadata({ "foo" => "bar", "baz" => "foo" }, false)
1717
assert_equal [{ :key => "foo", :value => "bar" },
18-
{ :key=>"baz", :value=>"foo" }], server.metadata[:items]
18+
{ :key => "baz", :value => "foo" }], server.metadata[:items]
19+
end
20+
21+
def test_bootstrap
22+
key = "ssh-rsa IAMNOTAREALSSHKEYAMA== user@host.subdomain.example.com"
23+
user = "username"
24+
25+
File.stub :read, key do
26+
# Name is set this way so it will be cleaned up by CollectionFactory
27+
# Public_key_path is set to avoid stubbing out File.exist?
28+
server = @subject.bootstrap(:name => "#{CollectionFactory::PREFIX}-#{Time.now.to_i}",
29+
:username => user,
30+
:public_key_path => "foo")
31+
boot_disk = server.disks.detect { |disk| disk[:boot] }
32+
33+
assert_equal("RUNNING", server.status, "Bootstrapped server should be running")
34+
assert_equal(key, server.public_key, "Bootstrapped server should have a public key set")
35+
assert_equal(user, server.username, "Bootstrapped server should have user set to #{user}")
36+
assert(boot_disk[:auto_delete], "Bootstrapped server should have disk set to autodelete")
37+
38+
network_adapter = server.network_interfaces.detect { |x| x.has_key?(:access_configs) }
39+
40+
refute_nil(network_adapter[:access_configs].detect { |x| x[:nat_ip] },
41+
"Bootstrapped server should have an external ip by default")
42+
end
43+
end
44+
45+
def test_bootstrap_fail
46+
# Pretend the ssh key does not exist
47+
File.stub :exist?, nil do
48+
assert_raises(Fog::Errors::Error) {
49+
@subject.bootstrap(:name => "#{CollectionFactory::PREFIX}-#{Time.now.to_i}",
50+
:public_key_path => nil)
51+
}
52+
end
1953
end
2054
end

0 commit comments

Comments
 (0)