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/rackspace"
      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



154
155
156
157
158
159
# File 'lib/chef/knife/rackspace_base.rb', line 154

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
106
107
108
# 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]} (config)")
  Chef::Log.debug("rackspace_api_key #{config[:rackspace_api_key]} (cli)")
  Chef::Log.debug("rackspace_username #{Chef::Config[:knife][:rackspace_username]} (config)")
  Chef::Log.debug("rackspace_username #{config[:rackspace_username]} (cli)")
  Chef::Log.debug("rackspace_api_username #{Chef::Config[:knife][:rackspace_api_username]} (config)")
  Chef::Log.debug("rackspace_api_username #{config[:rackspace_api_username]} (cli)")
  Chef::Log.debug("rackspace_auth_url #{Chef::Config[:knife][:rackspace_auth_url]} (config)")
  Chef::Log.debug("rackspace_auth_url #{config[:rackspace_api_auth_url]} (cli)")
  Chef::Log.debug("rackspace_auth_url #{auth_endpoint} (using)")
  Chef::Log.debug("rackspace_region #{Chef::Config[:knife][:rackspace_region]} (config)")
  Chef::Log.debug("rackspace_region #{config[:rackspace_region]} (cli)")

  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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/chef/knife/rackspace_base.rb', line 116

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

  username = locate_config_value(:rackspace_username) || locate_config_value(:rackspace_api_username)
  unless username
    ui.error "Please specify an api username via the command line using the --rackspace-username switch or add a knife[:rackspace_username] = USERNAME to your knife file."
    exit 1
  end

  unless locate_config_value(:rackspace_api_key)
    ui.error "Please specify an api key via the command line using the --rackspace-api-key switch or add a knife[:rackspace_api_key] = USERNAME to your knife file."
    exit 1
  end

  hash = options.merge({
    provider: "Rackspace",
    rackspace_api_key: locate_config_value(:rackspace_api_key),
    rackspace_username: 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.key?(:https_proxy) || Chef::Config.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].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].key?(:ssl_verify_peer)

  hash
end

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



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/chef/knife/rackspace_base.rb', line 172

def ip_address(server, network = "public")
  if version_one?
    case network
    when "public" then v1_public_ip(server)
    when "private" then 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



161
162
163
164
# File 'lib/chef/knife/rackspace_base.rb', line 161

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

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



166
167
168
169
170
# File 'lib/chef/knife/rackspace_base.rb', line 166

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



188
189
190
191
192
193
194
195
196
# File 'lib/chef/knife/rackspace_base.rb', line 188

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



110
111
112
113
114
# File 'lib/chef/knife/rackspace_base.rb', line 110

def region_warning_for_v1
  if locate_config_value(:rackspace_region)
    Chef::Log.warn("Ignoring the rackspace_region parameter as it is only supported for Next Gen Cloud Servers (v2)")
  end
end