Module: Chef::Knife::CsBase

Included in:
CsServerList
Defined in:
lib/chef/knife/cs_base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object

:nodoc: Would prefer to do this in a rational way, but can’t be done b/c of Mixlib::CLI’s design :(



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/chef/knife/cs_base.rb', line 28

def self.included(includer)
  includer.class_eval do

    deps do
      require 'fog'
      require 'readline'
      require 'chef/json_compat'
    end

    option :cloudstack_url,
           :short => "-U URL",
           :long => "--cloudstack-url URL",
           :description => "The CloudStack endpoint URL",
           :proc => Proc.new { |url| Chef::Config[:knife][:cloudstack_url] = url }

    option :cloudstack_api_key,
           :short => "-A KEY",
           :long => "--cloudstack-api-key KEY",
           :description => "Your CloudStack API key",
           :proc => Proc.new { |key| Chef::Config[:knife][:cloudstack_api_key] = key }

    option :cloudstack_secret_key,
           :short => "-K SECRET",
           :long => "--cloudstack-secret-key SECRET",
           :description => "Your CloudStack secret key",
           :proc => Proc.new { |key| Chef::Config[:knife][:cloudstack_secret_key] = key }
  end
end

Instance Method Details

#connectionObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/chef/knife/cs_base.rb', line 58

def connection
  @connection ||= begin
    cloudstack_uri =  URI.parse(Chef::Config[:knife][:cloudstack_url])
    connection = Fog::Compute.new(
        :provider              => :cloudstack,
        :cloudstack_api_key    => Chef::Config[:knife][:cloudstack_api_key],
        :cloudstack_secret_access_key => Chef::Config[:knife][:cloudstack_secret_key],
        :cloudstack_host       => cloudstack_uri.host,
        :cloudstack_port       => cloudstack_uri.port,
        :cloudstack_path       => cloudstack_uri.path,
        :cloudstack_scheme     => cloudstack_uri.scheme
    )
  end
end

#is_image_windows?Boolean

Returns:

  • (Boolean)


84
85
86
87
# File 'lib/chef/knife/cs_base.rb', line 84

def is_image_windows?
  image_info = connection.images.get(@server.image_id)
  return image_info.platform == 'windows'
end

#locate_config_value(key) ⇒ Object



73
74
75
76
# File 'lib/chef/knife/cs_base.rb', line 73

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

#msg_pair(label, value, color = :cyan) ⇒ Object



78
79
80
81
82
# File 'lib/chef/knife/cs_base.rb', line 78

def msg_pair(label, value, color=:cyan)
  if value && !value.to_s.empty?
    puts "#{ui.color(label, color)}: #{value}"
  end
end

#validate!Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/chef/knife/cs_base.rb', line 89

def validate!
  errors = []
  # simple validation for the moment, we need to impove this later.

  if locate_config_value(:cloudstack_url).nil?
    errors << "Please provide the API url within the configuration or with the option -U"
  end

  if locate_config_value(:cloudstack_api_key).nil?
    errors << "Please provide the API key within the configuration or with the option -A"
  end

  if locate_config_value(:cloudstack_secret_key).nil?
    errors << "Please provide the secret key within the configuration or with the option -K"
  end

  if errors.each{|e| ui.error(e)}.any?
    exit 1
  end
end