Skip to content

Commit 75e949b

Browse files
committed
Adding rackspace/fog support
1 parent 56d0496 commit 75e949b

File tree

5 files changed

+287
-1
lines changed

5 files changed

+287
-1
lines changed

‎chef/lib/chef/config.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,5 +197,7 @@ def self.manage_secret_key
197197
cache_type"BasicFile"
198198
cache_options({:path=>"/var/chef/cache/checksums",:skip_expires=>true})
199199

200+
# Arbitrary knife configuration data
201+
knifeHash.new
200202
end
201203
end
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#
2+
# Author:: Adam Jacob (<[email protected]>)
3+
# Copyright:: Copyright (c) 2009 Opscode, Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require'chef/knife'
20+
require'json'
21+
22+
classChef
23+
classKnife
24+
classRackspaceServerCreate < Knife
25+
26+
banner"Sub-Command: rackspace server create [RUN LIST...] (options)"
27+
28+
option:flavor,
29+
:short=>"-f FLAVOR",
30+
:long=>"--flavor FLAVOR",
31+
:description=>"The flavor of server",
32+
:proc=>Proc.new{ |f| f.to_i},
33+
:default=>1
34+
35+
option:image,
36+
:short=>"-i IMAGE",
37+
:long=>"--image IMAGE",
38+
:description=>"The image of the server",
39+
:proc=>Proc.new{ |i| i.to_i},
40+
:default=>14362
41+
42+
option:server_name,
43+
:short=>"-N NAME",
44+
:long=>"--server-name NAME",
45+
:description=>"The server name",
46+
:default=>"wtf"
47+
48+
option:api_key,
49+
:short=>"-K KEY",
50+
:long=>"--rackspace-api-key KEY",
51+
:description=>"Your rackspace API key",
52+
:proc=>Proc.new{ |key| Chef::Config[:knife][:rackspace_api_key]=key}
53+
54+
option:api_username,
55+
:short=>"-A USERNAME",
56+
:long=>"--rackspace-api-username USERNAME",
57+
:description=>"Your rackspace API username",
58+
:proc=>Proc.new{ |username| Chef::Config[:knife][:rackspace_api_username]=username}
59+
60+
defh
61+
@highline ||= HighLine.new
62+
end
63+
64+
defrun
65+
require'fog'
66+
require'highline'
67+
require'net/ssh/multi'
68+
require'readline'
69+
70+
connection=Fog::Rackspace::Servers.new(
71+
:rackspace_api_key=>Chef::Config[:knife][:rackspace_api_key],
72+
:rackspace_username=>Chef::Config[:knife][:rackspace_api_username]
73+
)
74+
75+
server=connection.servers.new
76+
77+
server.flavor_id=config[:flavor]
78+
server.image_id=config[:image]
79+
server.name=config[:server_name]
80+
server.personality=[
81+
{
82+
'path'=>'/etc/install-chef',
83+
'contents'=><<-EOH
84+
#!/bin/bash
85+
# Customized rc.local for chef installation
86+
87+
if [ ! -f /usr/bin/chef-client ]; then
88+
apt-get update
89+
apt-get install -y ruby ruby1.8-dev build-essential wget libruby-extras libruby1.8-extras
90+
cd /tmp
91+
wget http://rubyforge.org/frs/download.php/69365/rubygems-1.3.6.tgz
92+
tar xvf rubygems-1.3.6.tgz
93+
cd rubygems-1.3.6
94+
ruby setup.rb
95+
cp /usr/bin/gem1.8 /usr/bin/gem
96+
gem install chef ohai --no-rdoc --no-ri --verbose
97+
fi
98+
99+
exit 0
100+
EOH
101+
},
102+
{
103+
'path'=>"/etc/chef/validation.pem",
104+
'contents'=>IO.read(Chef::Config[:validation_key])
105+
},
106+
{
107+
'path'=>"/etc/chef/client.rb",
108+
'contents'=><<-EOH
109+
log_level :info
110+
log_location STDOUT
111+
chef_server_url "#{Chef::Config[:chef_server_url]}"
112+
validation_client_name "#{Chef::Config[:validation_client_name]}"
113+
EOH
114+
},
115+
{
116+
'path'=>"/etc/chef/first-boot.json",
117+
'contents'=>{"run_list"=>@name_args}.to_json
118+
},
119+
]
120+
121+
server.save
122+
123+
$stdout.sync=true
124+
125+
puts"#{h.color("Name",:cyan)}: #{server.name}"
126+
puts"#{h.color("Flavor",:cyan)}: #{server.flavor_id}"
127+
puts"#{h.color("Image",:cyan)}: #{server.image_id}"
128+
puts"#{h.color("Public Address",:cyan)}: #{server.addresses["public"]}"
129+
puts"#{h.color("Private Address",:cyan)}: #{server.addresses["private"]}"
130+
puts"#{h.color("Password",:cyan)}: #{server.password}"
131+
132+
print"\n#{h.color("Requesting server",:magenta)}"
133+
saved_password=server.password
134+
135+
# wait for it to be ready to do stuff
136+
server.wait_for{print".";ready?}
137+
138+
puts"\nServer ready, waiting 15 seconds to bootstrap."
139+
sleep15
140+
141+
puts"\nBootstrapping #{h.color(server.name,:bold)}..."
142+
143+
ssh=Chef::Knife::Ssh.new
144+
ssh.name_args=[server.addresses["public"][0],"/bin/bash /etc/install-chef && /usr/bin/chef-client -j /etc/chef/first-boot.json"]
145+
ssh.config[:ssh_user]="root"
146+
ssh.config[:manual]=true
147+
ssh.config[:password]=saved_password
148+
ssh.password=saved_password
149+
ssh.run
150+
151+
end
152+
end
153+
end
154+
end
155+
156+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#
2+
# Author:: Adam Jacob (<[email protected]>)
3+
# Copyright:: Copyright (c) 2009 Opscode, Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require'chef/knife'
20+
require'json'
21+
22+
classChef
23+
classKnife
24+
classRackspaceServerDelete < Knife
25+
26+
banner"Sub-Command: rackspace server delete SERVER (options)"
27+
28+
defh
29+
@highline ||= HighLine.new
30+
end
31+
32+
defrun
33+
require'fog'
34+
require'highline'
35+
require'net/ssh/multi'
36+
require'readline'
37+
38+
connection=Fog::Rackspace::Servers.new(
39+
:rackspace_api_key=>Chef::Config[:knife][:rackspace_api_key],
40+
:rackspace_username=>Chef::Config[:knife][:rackspace_api_username]
41+
)
42+
43+
server=connection.servers.get(@name_args[0])
44+
45+
confirm("Do you really want to delete server ID #{server.id} named #{server.name}")
46+
47+
server.destroy
48+
49+
Chef::Log.warn("Deleted server #{server.id} named #{server.name}")
50+
end
51+
end
52+
end
53+
end
54+
55+
56+
57+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#
2+
# Author:: Adam Jacob (<[email protected]>)
3+
# Copyright:: Copyright (c) 2009 Opscode, Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require'chef/knife'
20+
require'json'
21+
22+
classChef
23+
classKnife
24+
classRackspaceServerList < Knife
25+
26+
banner"Sub-Command: rackspace server list (options)"
27+
28+
defh
29+
@highline ||= HighLine.new
30+
end
31+
32+
defrun
33+
require'fog'
34+
require'highline'
35+
require'net/ssh/multi'
36+
require'readline'
37+
38+
connection=Fog::Rackspace::Servers.new(
39+
:rackspace_api_key=>Chef::Config[:knife][:rackspace_api_key],
40+
:rackspace_username=>Chef::Config[:knife][:rackspace_api_username]
41+
)
42+
43+
server_list=[h.color('ID',:bold),h.color('Name',:bold),h.color('Public IP',:bold),h.color('Private IP',:bold),h.color('Flavor ID',:bold)]
44+
connection.servers.all.eachdo |server|
45+
server_list << server.id.to_s
46+
server_list << server.name
47+
server_list << server.addresses["public"][0]
48+
server_list << server.addresses["private"][0]
49+
server_list << server.flavor_id.to_s
50+
end
51+
putsh.list(server_list,:columns_across,5)
52+
53+
end
54+
end
55+
end
56+
end
57+
58+
59+

‎chef/lib/chef/knife/ssh.rb‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class Chef
2323
classKnife
2424
classSsh < Knife
2525

26+
attr_writer:password
27+
2628
banner"Sub-Command: ssh QUERY COMMAND (options)"
2729

2830
option:concurrency,
@@ -49,6 +51,11 @@ class Ssh < Knife
4951
:long=>"--ssh-user USERNAME",
5052
:description=>"The ssh username"
5153

54+
option:ssh_password,
55+
:short=>"-P PASSWORD",
56+
:long=>"--ssh-password PASSWORD",
57+
:description=>"The ssh password"
58+
5259
defsession
5360
@session ||= Net::SSH::Multi.start(:concurrent_connections=>config[:concurrency])
5461
end
@@ -75,7 +82,12 @@ def configure_session
7582
defsession_from_list(list)
7683
list.eachdo |item|
7784
Chef::Log.debug("Adding #{item}")
78-
session.useconfig[:ssh_user] ? "#{config[:ssh_user]}@#{item}" : item
85+
86+
ifconfig[:password]
87+
session.useconfig[:ssh_user] ? "#{config[:ssh_user]}@#{item}" : item,:password=>config[:password]
88+
else
89+
session.useconfig[:ssh_user] ? "#{config[:ssh_user]}@#{item}" : item
90+
end
7991
@longest=item.lengthifitem.length > @longest
8092
end
8193
session

0 commit comments

Comments
(0)