Module: Chef::Knife::RackspaceBase

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 :(



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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/chef/knife/rackspace_base.rb', line 29

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_version,
      :long => '--rackspace-version VERSION',
      :description => 'Rackspace Cloud Servers API version',
      :default => "v2",
      :proc => Proc.new { |version| Chef::Config[:knife][:rackspace_version] = version }

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

    option :rackspace_region,
      :long => "--rackspace-region REGION",
      :description => "Your rackspace region",
      :proc => Proc.new { |region| Chef::Config[:knife][:rackspace_region] = region }

    option :file,
      :long => '--file DESTINATION-PATH=SOURCE-PATH',
      :description => 'File to inject on node',
      :proc => Proc.new {|arg|
        Chef::Config[:knife][:file] ||= []
        Chef::Config[:knife][:file] << arg
      }
  end
end

Instance Method Details

#auth_endpointObject



140
141
142
143
144
# File 'lib/chef/knife/rackspace_base.rb', line 140

def auth_endpoint
  url = locate_config_value(:rackspace_auth_url)
  return url if url
  (locate_config_value(:rackspace_region) == 'lon') ? ::Fog::Rackspace::UK_AUTH_ENDPOINT : ::Fog::Rackspace::US_AUTH_ENDPOINT
end

#connectionObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/chef/knife/rackspace_base.rb', line 77

def connection
  Chef::Log.debug("version #{Chef::Config[:knife][:rackspace_version]} (config)")
  Chef::Log.debug("version #{config[:rackspace_version]} (cli)")
  Chef::Log.debug("rackspace_api_key #{Chef::Config[:knife][:rackspace_api_key]}")
  Chef::Log.debug("rackspace_username #{Chef::Config[:knife][:rackspace_username]}")
  Chef::Log.debug("rackspace_api_username #{Chef::Config[:knife][:rackspace_api_username]}")
  Chef::Log.debug("rackspace_auth_url #{Chef::Config[:knife][:rackspace_auth_url]}")
  Chef::Log.debug("rackspace_auth_url #{config[:rackspace_api_auth_url]}")
  Chef::Log.debug("rackspace_auth_url #{auth_endpoint} (using)")
  Chef::Log.debug("rackspace_region #{Chef::Config[:knife][:rackspace_region]}")
  Chef::Log.debug("rackspace_region #{config[:rackspace_region]}")

  if version_one?
    Chef::Log.debug("rackspace v1")
    region_warning_for_v1
    @connection ||= begin
      connection = Fog::Compute.new(connection_params({
        :version => 'v1'
        }))
    end
  else
    Chef::Log.debug("rackspace v2")
    @connection ||= begin
      connection = Fog::Compute.new(connection_params({
        :version => 'v2'
      }))
    end
  end
end

#connection_params(options = {}) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/chef/knife/rackspace_base.rb', line 113

def connection_params(options={})
  unless locate_config_value(:rackspace_region)
    ui.error "Please specify region via the command line using the --rackspace-region switch or add a knife[:rackspace_region] = REGION to your knife file."
    exit 1
  end

  hash = options.merge({
    :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 => auth_endpoint,
    :rackspace_region => locate_config_value(:rackspace_region)
  })

  hash[:connection_options] ||= {}
  Chef::Log.debug("https_proxy #{ Chef::Config[:https_proxy] || "<not specified>"} (config)")
  Chef::Log.debug("http_proxy #{ Chef::Config[:http_proxy] || "<not specified>"} (config)")
  if Chef::Config.has_key?(:https_proxy) || Chef::Config.has_key?(:http_proxy)
    hash[:connection_options] = {:proxy => Chef::Config[:https_proxy] || Chef::Config[:http_proxy] }
  end
  Chef::Log.debug("using proxy #{hash[:connection_options][:proxy] || "<none>"} (config)")
  Chef::Log.debug("ssl_verify_peer #{Chef::Config[:knife].has_key?(:ssl_verify_peer) ? Chef::Config[:knife][:ssl_verify_peer] : "<not specified>"} (config)")
  hash[:connection_options][:ssl_verify_peer] = Chef::Config[:knife][:ssl_verify_peer] if Chef::Config[:knife].has_key?(:ssl_verify_peer)

  hash
end

#ip_address(server, network = 'public') ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/chef/knife/rackspace_base.rb', line 157

def ip_address(server, network='public')
  if version_one?
    case network
    when 'public'; v1_public_ip(server)
    when 'private'; v1_private_ip(server)
    else raise NotImplementedError
    end
  else
    if network == 'public' && v2_access_ip(server) != ""
      v2_access_ip(server)
    else
      v2_ip_address(server, network)
    end
  end
end

#locate_config_value(key) ⇒ Object



146
147
148
149
# File 'lib/chef/knife/rackspace_base.rb', line 146

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

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



151
152
153
154
155
# File 'lib/chef/knife/rackspace_base.rb', line 151

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

#public_dns_name(server) ⇒ Object



173
174
175
176
177
178
179
180
181
# File 'lib/chef/knife/rackspace_base.rb', line 173

def public_dns_name(server)
  if public_ip_address = ip_address(server, 'public')
    @public_dns_name ||= begin
      Resolv.getname(public_ip_address)
    rescue
      "#{public_ip_address}.xip.io"
    end
  end
end

#region_warning_for_v1Object



107
108
109
110
111
# File 'lib/chef/knife/rackspace_base.rb', line 107

def region_warning_for_v1
  if Chef::Config[:knife][:rackspace_region] || config[:rackspace_region]
    Chef::Log.warn("Ignoring the rackspace_region parameter as it is only supported for Next Gen Cloud Servers (v2)")
  end
end