diff options
author | Ian Meyer <ianmmeyer@gmail.com> | 2010-06-23 18:23:46 +0000 |
---|---|---|
committer | Daniel DeLeo <dan@opscode.com> | 2010-06-25 11:59:12 -0700 |
commit | d9fbf9292d331dd956b9a7077f89e0774dcb4984 (patch) | |
tree | 3d1288e845180044dfba9c8e11b0c6d241969034 | |
parent | 79a77b76c2461e82dcdbb551048e158ba00d4746 (diff) | |
download | chef-d9fbf9292d331dd956b9a7077f89e0774dcb4984.tar.gz |
CHEF-1366 Add slicehost support to knife
-rw-r--r-- | chef/lib/chef/knife/slicehost_images_list.rb | 53 | ||||
-rw-r--r-- | chef/lib/chef/knife/slicehost_server_create.rb | 103 | ||||
-rw-r--r-- | chef/lib/chef/knife/slicehost_server_delete.rb | 61 | ||||
-rw-r--r-- | chef/lib/chef/knife/slicehost_server_list.rb | 64 |
4 files changed, 281 insertions, 0 deletions
diff --git a/chef/lib/chef/knife/slicehost_images_list.rb b/chef/lib/chef/knife/slicehost_images_list.rb new file mode 100644 index 0000000000..a4a9717fd4 --- /dev/null +++ b/chef/lib/chef/knife/slicehost_images_list.rb @@ -0,0 +1,53 @@ +# +# Author:: Ian Meyer (<ianmmeyer@gmail.com>) +# Copyright:: Copyright (c) 2010 Ian Meyer +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/knife' +require 'json' + +class Chef + class Knife + class SlicehostImagesList < Knife + + banner "Sub-Command: slicehost images list" + + def highline + @highline ||= HighLine.new + end + + def run + require 'fog' + require 'highline' + + slicehost = Fog::Slicehost.new( + :slicehost_password => Chef::Config[:knife][:slicehost_password] + ) + + images = slicehost.images.inject({}) { |h,i| h[i.id] = i.name; h } + + image_list = [ highline.color('ID', :bold), highline.color('Name', :bold) ] + + slicehost.images.each do |server| + image_list << server.id.to_s + image_list << server.name + end + puts highline.list(image_list, :columns_across, 2) + + end + end + end +end diff --git a/chef/lib/chef/knife/slicehost_server_create.rb b/chef/lib/chef/knife/slicehost_server_create.rb new file mode 100644 index 0000000000..b6827c6d84 --- /dev/null +++ b/chef/lib/chef/knife/slicehost_server_create.rb @@ -0,0 +1,103 @@ +# +# Author:: Ian Meyer (<ianmmeyer@gmail.com>) +# Copyright:: Copyright (c) 2009 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/knife' +require 'json' + +class Chef + class Knife + class SlicehostServerCreate < Knife + + banner "Sub-Command: slicehost server create [RUN LIST...] (options)" + + option :flavor, + :short => "-f FLAVOR", + :long => "--flavor FLAVOR", + :description => "The flavor of server", + :proc => Proc.new { |f| f.to_i }, + :default => 1 + + option :image, + :short => "-i IMAGE", + :long => "--image IMAGE", + :description => "The image of the server", + :proc => Proc.new { |i| i.to_i }, + :default => 49 + + option :server_name, + :short => "-N NAME", + :long => "--server-name NAME", + :description => "The server name", + :default => "wtf" + + option :slicehost_password, + :short => "-K KEY", + :long => "--slicehost-password password", + :description => "Your slicehost API password", + :proc => Proc.new { |password| Chef::Config[:knife][:slicehost_password] = password } + + + def h + @highline ||= HighLine.new + end + + def run + require 'fog' + require 'highline' + require 'net/ssh/multi' + require 'readline' + + slicehost = Fog::Slicehost.new( + :slicehost_password => Chef::Config[:knife][:slicehost_password] + ) + + flavors = slicehost.flavors.inject({}) { |h,f| h[f.id] = f.name; h } + images = slicehost.images.inject({}) { |h,i| h[i.id] = i.name; h } + + response = slicehost.create_slice(config[:flavor], + config[:image], + config[:server_name]) + $stdout.sync = true + + puts "#{h.color("Name", :cyan)}: #{response.body['name']}" + puts "#{h.color("Flavor", :cyan)}: #{flavors[response.body['flavor-id']]}" + puts "#{h.color("Image", :cyan)}: #{images[response.body['image-id']]}" + puts "#{h.color("Public Address", :cyan)}: #{response.body['addresses'][1]}" + puts "#{h.color("Private Address", :cyan)}: #{response.body['addresses'][0]}" + puts "#{h.color("Password", :cyan)}: #{response.body['root-password']}" + + print "\n#{h.color("Requesting status of #{response.body['name']}", :magenta)}" + saved_password = response.body['root-password'] + + # wait for it to be ready to do stuff + loop do + sleep 15 + host = slicehost.get_slice(response.body['id']) + if host.body['status'] == 'active' + break + end + end + + puts "\nServer ready!!" + + end + end + end +end + + diff --git a/chef/lib/chef/knife/slicehost_server_delete.rb b/chef/lib/chef/knife/slicehost_server_delete.rb new file mode 100644 index 0000000000..0577dc8456 --- /dev/null +++ b/chef/lib/chef/knife/slicehost_server_delete.rb @@ -0,0 +1,61 @@ +# +# Author:: Ian Meyer (<ianmmeyer@gmail.com>) +# Copyright:: Copyright (c) 2009 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/knife' +require 'json' + +class Chef + class Knife + class SlicehostServerDelete < Knife + + banner "Sub-Command: slicehost server delete SLICENAME" + + def run + require 'fog' + require 'readline' + + slicehost = Fog::Slicehost.new( + :slicehost_password => Chef::Config[:knife][:slicehost_password] + ) + + # Build hash of slice => id + servers = slicehost.servers.inject({}) { |h,f| h[f.name] = f.id; h } + + unless servers.has_key?(@name_args[0]) + Chef::Log.warn("I can't find a slice named #{@name_args[0]}") + return false + end + + confirm("Do you really want to delete server ID #{servers[@name_args[0]]} named #{@name_args[0]}") + + begin + response = slicehost.delete_slice(servers[@name_args[0]]) + + if response.headers['status'] == "200 OK" + Chef::Log.warn("Deleted server #{servers[@name_args[0]]} named #{@name_args[0]}") + end + rescue Excon::Errors::UnprocessableEntity + Chef::Log.warn("There was a problem deleting #{@name_args[0]}, check your slice manager") + end + end + end + end +end + + + diff --git a/chef/lib/chef/knife/slicehost_server_list.rb b/chef/lib/chef/knife/slicehost_server_list.rb new file mode 100644 index 0000000000..e2c870177c --- /dev/null +++ b/chef/lib/chef/knife/slicehost_server_list.rb @@ -0,0 +1,64 @@ +# +# Author:: Ian Meyer (<ianmmeyer@gmail.com>) +# Copyright:: Copyright (c) 2009 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/knife' +require 'json' + +class Chef + class Knife + class SlicehostServerList < Knife + + banner "Sub-Command: slicehost server list (options)" + + def h + @highline ||= HighLine.new + end + + def run + require 'fog' + require 'highline' + require 'net/ssh/multi' + require 'readline' + + slicehost = Fog::Slicehost.new( + :slicehost_password => Chef::Config[:knife][:slicehost_password] + ) + + # Make hash of flavor id => name and image id => name + flavors = slicehost.flavors.inject({}) { |h,f| h[f.id] = f.name; h } + images = slicehost.images.inject({}) { |h,i| h[i.id] = i.name; h } + + server_list = [ h.color('ID', :bold), h.color('Name', :bold), h.color('Public IP', :bold), h.color('Private IP', :bold), h.color('Image', :bold), h.color('Flavor', :bold) ] + + slicehost.servers.each do |server| + server_list << server.id.to_s + server_list << server.name + server_list << server.addresses[1] + server_list << server.addresses[0] + server_list << images[server.image_id].to_s + server_list << flavors[server.flavor_id].to_s + end + puts h.list(server_list, :columns_across, 6) + + end + end + end +end + + + |