Skip to content

Commit

Permalink
created base module for plugin to DRY up code
Browse files Browse the repository at this point in the history
  • Loading branch information
schisamo committed Jul 23, 2011
1 parent ffff153 commit 0e2bd54
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 190 deletions.
85 changes: 85 additions & 0 deletions lib/chef/knife/rackspace_base.rb
@@ -0,0 +1,85 @@
#
# Author:: Seth Chisamore (<schisamo@opscode.com>)
# Copyright:: Copyright (c) 2011 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'

class Chef
class Knife
module RackspaceBase

# :nodoc:
# Would prefer to do this in a rational way, but can't be done b/c of
# Mixlib::CLI's design :(
def self.included(includer)
includer.class_eval do

deps do
require 'fog'
require 'net/ssh/multi'
require 'readline'
require 'chef/json_compat'
end

option :rackspace_api_key,
:short => "-K KEY",
:long => "--rackspace-api-key KEY",
:description => "Your rackspace API key",
:proc => Proc.new { |key| Chef::Config[:knife][:rackspace_api_key] = key }

option :rackspace_username,
:short => "-A USERNAME",
:long => "--rackspace-username USERNAME",
:description => "Your rackspace API username",
:proc => Proc.new { |username| Chef::Config[:knife][:rackspace_username] = username }

option :rackspace_api_auth_url,
:long => "--rackspace-api-auth-url URL",
:description => "Your rackspace API auth url",
:default => "auth.api.rackspacecloud.com",
:proc => Proc.new { |url| Chef::Config[:knife][:rackspace_api_auth_url] = url }
end
end

def connection
@connection ||= begin
connection = Fog::Compute.new(
:provider => 'Rackspace',
:rackspace_api_key => Chef::Config[:knife][:rackspace_api_key],
:rackspace_username => (Chef::Config[:knife][:rackspace_username] || Chef::Config[:knife][:rackspace_api_username]),
:rackspace_auth_url => Chef::Config[:knife][:rackspace_api_auth_url] || config[:rackspace_api_auth_url]
)
end
end

def locate_config_value(key)
key = key.to_sym
Chef::Config[:knife][key] || config[key]
end

def public_dns_name(server)
@public_dns_name ||= begin
Resolv.getname(server.addresses["public"][0])
rescue
"#{server.addresses["public"][0].gsub('.','-')}.static.cloud-ips.com"
end
end
end
end
end


42 changes: 10 additions & 32 deletions lib/chef/knife/rackspace_flavor_list.rb
Expand Up @@ -16,47 +16,25 @@
# limitations under the License.
#

require 'chef/knife'
require 'chef/knife/rackspace_base'

class Chef
class Knife
class RackspaceFlavorList < Knife

deps do
require 'fog'
require 'chef/json_compat'
end
include Knife::RackspaceBase

banner "knife rackspace flavor list (options)"

option :rackspace_api_key,
:short => "-K KEY",
:long => "--rackspace-api-key KEY",
:description => "Your rackspace API key",
:proc => Proc.new { |key| Chef::Config[:knife][:rackspace_api_key] = key }

option :rackspace_username,
:short => "-A USERNAME",
:long => "--rackspace-username USERNAME",
:description => "Your rackspace API username",
:proc => Proc.new { |username| Chef::Config[:knife][:rackspace_username] = username }

option :rackspace_api_auth_url,
:long => "--rackspace-api-auth-url URL",
:description => "Your rackspace API auth url",
:default => "auth.api.rackspacecloud.com",
:proc => Proc.new { |url| Chef::Config[:knife][:rackspace_api_auth_url] = url }

def run

connection = Fog::Compute.new(
:provider => 'Rackspace',
:rackspace_api_key => Chef::Config[:knife][:rackspace_api_key],
:rackspace_username => (Chef::Config[:knife][:rackspace_username] || Chef::Config[:knife][:rackspace_api_username]),
:rackspace_auth_url => Chef::Config[:knife][:rackspace_api_auth_url] || config[:rackspace_api_auth_url]
)

flavor_list = [ ui.color('ID', :bold), ui.color('Name', :bold), ui.color('Architecture', :bold), ui.color('RAM', :bold), ui.color('Disk', :bold) , ui.color('Cores', :bold) ]
flavor_list = [
ui.color('ID', :bold),
ui.color('Name', :bold),
ui.color('Architecture', :bold),
ui.color('RAM', :bold),
ui.color('Disk', :bold),
ui.color('Cores', :bold)
]
connection.flavors.sort_by(&:id).each do |flavor|
flavor_list << flavor.id.to_s
flavor_list << flavor.name
Expand Down
37 changes: 7 additions & 30 deletions lib/chef/knife/rackspace_image_list.rb
Expand Up @@ -16,50 +16,27 @@
# limitations under the License.
#

require 'chef/knife'
require 'chef/knife/rackspace_base'

class Chef
class Knife
class RackspaceImageList < Knife

deps do
require 'fog'
require 'chef/json_compat'
end
include Knife::RackspaceBase

banner "knife rackspace image list (options)"

option :rackspace_api_key,
:short => "-K KEY",
:long => "--rackspace-api-key KEY",
:description => "Your rackspace API key",
:proc => Proc.new { |key| Chef::Config[:knife][:rackspace_api_key] = key }

option :rackspace_username,
:short => "-A USERNAME",
:long => "--rackspace-username USERNAME",
:description => "Your rackspace API username",
:proc => Proc.new { |username| Chef::Config[:knife][:rackspace_username] = username }

option :rackspace_api_auth_url,
:long => "--rackspace-api-auth-url URL",
:description => "Your rackspace API auth url",
:default => "auth.api.rackspacecloud.com",
:proc => Proc.new { |url| Chef::Config[:knife][:rackspace_api_auth_url] = url }

def run
connection = Fog::Compute.new(
:provider => 'Rackspace',
:rackspace_api_key => Chef::Config[:knife][:rackspace_api_key],
:rackspace_username => (Chef::Config[:knife][:rackspace_username] || Chef::Config[:knife][:rackspace_api_username]),
:rackspace_auth_url => Chef::Config[:knife][:rackspace_api_auth_url] || config[:rackspace_api_auth_url]
)
image_list = [
ui.color('ID', :bold),
ui.color('Name', :bold)
]

image_list = [ ui.color('ID', :bold), ui.color('Name', :bold) ]
connection.images.sort_by(&:name).each do |image|
image_list << image.id.to_s
image_list << image.name
end

puts ui.list(image_list, :columns_across, 2)
end
end
Expand Down
46 changes: 4 additions & 42 deletions lib/chef/knife/rackspace_server_create.rb
Expand Up @@ -22,15 +22,15 @@ class Chef
class Knife
class RackspaceServerCreate < Knife

include Knife::RackspaceBase

deps do
require 'chef/knife/bootstrap'
Chef::Knife::Bootstrap.load_deps
require 'fog'
require 'highline'
require 'net/ssh/multi'
require 'readline'
require 'resolv'
require 'chef/json_compat'
require 'chef/knife/bootstrap'
Chef::Knife::Bootstrap.load_deps
end

banner "knife rackspace server create (options)"
Expand Down Expand Up @@ -73,24 +73,6 @@ class RackspaceServerCreate < Knife
:short => "-i IDENTITY_FILE",
:long => "--identity-file IDENTITY_FILE",
:description => "The SSH identity file used for authentication"

option :rackspace_api_key,
:short => "-K KEY",
:long => "--rackspace-api-key KEY",
:description => "Your rackspace API key",
:proc => Proc.new { |key| Chef::Config[:knife][:rackspace_api_key] = key }

option :rackspace_username,
:short => "-A USERNAME",
:long => "--rackspace-username USERNAME",
:description => "Your rackspace API username",
:proc => Proc.new { |username| Chef::Config[:knife][:rackspace_username] = username }

option :rackspace_api_auth_url,
:long => "--rackspace-api-auth-url URL",
:description => "Your rackspace API auth url; default is 'auth.api.rackspacecloud.com'",
:proc => Proc.new { |url| Chef::Config[:knife][:rackspace_api_auth_url] = url },
:default => "auth.api.rackspacecloud.com"

option :prerelease,
:long => "--prerelease",
Expand Down Expand Up @@ -126,7 +108,6 @@ class RackspaceServerCreate < Knife
:proc => lambda { |o| o.split(/[\s,]+/) },
:default => []


option :rackspace_metadata,
:short => "-M JSON",
:long => "--rackspace-metadata JSON",
Expand Down Expand Up @@ -161,13 +142,6 @@ def tcp_test_ssh(hostname)
def run
$stdout.sync = true

connection = Fog::Compute.new(
:provider => 'Rackspace',
:rackspace_api_key => Chef::Config[:knife][:rackspace_api_key],
:rackspace_username => (Chef::Config[:knife][:rackspace_username] || Chef::Config[:knife][:rackspace_api_username]),
:rackspace_auth_url => locate_config_value(:rackspace_api_auth_url)
)

unless Chef::Config[:knife][:image]
ui.error("You have not provided a valid image value. Please note the short option for this value recently changed from '-i' to '-I'.")
exit 1
Expand Down Expand Up @@ -237,18 +211,6 @@ def bootstrap_for_node(server)
bootstrap
end

def locate_config_value(key)
key = key.to_sym
Chef::Config[:knife][key] || config[key]
end

def public_dns_name(server)
@public_dns_name ||= begin
Resolv.getname(server.addresses["public"][0])
rescue
"#{server.addresses["public"][0].gsub('.','-')}.static.cloud-ips.com"
end
end
end
end
end
77 changes: 21 additions & 56 deletions lib/chef/knife/rackspace_server_delete.rb
Expand Up @@ -16,76 +16,41 @@
# limitations under the License.
#

require 'chef/knife'
require 'chef/knife/rackspace_base'

class Chef
class Knife
class RackspaceServerDelete < Knife

deps do
require 'fog'
require 'chef/knife'
require 'chef/json_compat'
require 'resolv'
end

banner "knife rackspace server delete SERVER_ID (options)"

option :rackspace_api_key,
:short => "-K KEY",
:long => "--rackspace-api-key KEY",
:description => "Your rackspace API key",
:proc => Proc.new { |key| Chef::Config[:knife][:rackspace_api_key] = key }

option :rackspace_username,
:short => "-A USERNAME",
:long => "--rackspace-username USERNAME",
:description => "Your rackspace API username",
:proc => Proc.new { |username| Chef::Config[:knife][:rackspace_username] = username }
include Knife::RackspaceBase

option :rackspace_api_auth_url,
:long => "--rackspace-api-auth-url URL",
:description => "Your rackspace API auth url",
:default => "auth.api.rackspacecloud.com",
:proc => Proc.new { |url| Chef::Config[:knife][:rackspace_api_auth_url] = url }
banner "knife rackspace server delete SERVER_ID [SERVER_ID] (options)"

def run
require 'fog'
require 'highline'
require 'net/ssh/multi'
require 'readline'

connection = Fog::Compute.new(
:provider => 'Rackspace',
:rackspace_api_key => Chef::Config[:knife][:rackspace_api_key],
:rackspace_username => (Chef::Config[:knife][:rackspace_username] || Chef::Config[:knife][:rackspace_api_username]),
:rackspace_auth_url => Chef::Config[:knife][:rackspace_api_auth_url] || config[:rackspace_api_auth_url]
)
@name_args.each do |instance_id|

server = connection.servers.get(instance_id)

server = connection.servers.get(@name_args[0])
msg("Instance ID", server.id)
msg("Host ID", server.host_id)
msg("Name", server.name)
msg("Flavor", server.flavor.name)
msg("Image", server.image.name)
msg("Public DNS Name", server.addresses["public"][0])
msg("Private IP Address", server.addresses["private"][0])

puts "#{ui.color("Instance ID", :cyan)}: #{server.id}"
puts "#{ui.color("Host ID", :cyan)}: #{server.host_id}"
puts "#{ui.color("Name", :cyan)}: #{server.name}"
puts "#{ui.color("Flavor", :cyan)}: #{server.flavor.name}"
puts "#{ui.color("Image", :cyan)}: #{server.image.name}"
puts "#{ui.color("Public DNS Name", :cyan)}: #{public_dns_name(server)}"
puts "#{ui.color("Public IP Address", :cyan)}: #{server.addresses["public"][0]}"
puts "#{ui.color("Private IP Address", :cyan)}: #{server.addresses["private"][0]}"
puts "\n"
confirm("Do you really want to delete this server")

puts "\n"
confirm("Do you really want to delete this server")
server.destroy

server.destroy

ui.warn("Deleted server #{server.id} named #{server.name}")
ui.warn("Deleted server #{server.id} named #{server.name}")
end
end

def public_dns_name(server)
@public_dns_name ||= begin
Resolv.getname(server.addresses["public"][0])
rescue
"#{server.addresses["public"][0].gsub('.','-')}.static.cloud-ips.com"
def msg(label, value)
if value && !value.empty?
puts "#{ui.color(label, :cyan)}: #{value}"
end
end
end
Expand Down

0 comments on commit 0e2bd54

Please sign in to comment.