Module: Chef::Knife::VcCommon
- Included in:
- VcCatalogItemShow, VcCatalogShow, VcConfigure, VcLogin, VcNetworkShow, VcOrgList, VcOrgShow, VcOvfUpload, VcVappBootstrap, VcVappClone, VcVappCreate, VcVappDelete, VcVappNetworkExternal, VcVappNetworkInternal, VcVappReboot, VcVappReset, VcVappShow, VcVappSnapshot, VcVappStart, VcVappStop, VcVappSuspend, VcVdcShow, VcVmBootstrap, VcVmConfigGuest, VcVmConfigNetwork, VcVmNetwork, VcVmReboot, VcVmReset, VcVmSetDisks, VcVmSetInfo, VcVmShow, VcVmStart, VcVmStop, VcVmSuspend
- Defined in:
- lib/chef/knife/common/vc_common.rb
Class Method Summary collapse
Instance Method Summary collapse
- #connection ⇒ Object
- #deprecation_msg(value) ⇒ Object
-
#generate_key(dir = "#{File.join(Dir.home, '.chef')}", output = "vc_key.pem") ⇒ Object
Generate a new key pair and store it on knife.rb.
-
#get_password(keyfile) ⇒ Object
Retrieve a stored password.
- #locate_config_value(key) ⇒ Object
-
#locate_org_option ⇒ Object
Locate the correct organization option.
- #notice_msg(value) ⇒ Object
- #out_msg(label, value) ⇒ Object
- #pretty_symbol(key) ⇒ Object
- #sort_by_key(collection) ⇒ Object
-
#store_config(key, value) ⇒ Object
Update knife.rb with an entry knife = VALUE.
-
#store_password(keyfile) ⇒ Object
Store a password in knife.rb.
- #wait_task(connection, task_id) ⇒ Object
Class Method Details
.included(includer) ⇒ Object
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 76 77 78 |
# File 'lib/chef/knife/common/vc_common.rb', line 29 def self.included(includer) includer.class_eval do deps do require 'vcloud-rest/connection' require 'chef/api_client' end option :vcloud_url, :short => "-H URL", :long => "--url URL", :description => "The vCloud endpoint URL", :proc => Proc.new { |url| Chef::Config[:knife][:vcloud_url] = url } option :vcloud_user_login, :short => "-U USER", :long => "--user-login USER", :description => "Your vCloud User", :proc => Proc.new { |key| Chef::Config[:knife][:vcloud_user_login] = key } option :vcloud_password_login, :short => "-P SECRET", :long => "--password-login SECRET", :description => "Your vCloud secret key", :proc => Proc.new { |key| Chef::Config[:knife][:vcloud_password_login] = key } option :vcloud_org_login, :long => "--org-login ORGANIZATION", :description => "Your vCloud Organization", :proc => Proc.new { |key| Chef::Config[:knife][:vcloud_org_login] = key } option :vcloud_api_version, :short => "-A API_VERSION", :long => "--api-version API_VERSION", :description => "vCloud API version (1.5 and 5.1 supported)", :proc => Proc.new { |key| Chef::Config[:knife][:vcloud_api_version] = key } option :vcloud_system_admin, :long => "--[no-]system-admin", :description => "Set to true if user is a vCloud System Administrator", :proc => Proc.new { |key| Chef::Config[:knife][:vcloud_system_admin] = key }, :boolean => true, :default => false option :vcloud_org, :long => "--org ORG_NAME", :description => "Organization to use (only for System Administrators)", :proc => Proc.new { |key| Chef::Config[:knife][:vcloud_org] = key } end end |
Instance Method Details
#connection ⇒ Object
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 109 110 |
# File 'lib/chef/knife/common/vc_common.rb', line 80 def connection unless @connection pemfile = locate_config_value(:vcloud_pem) if locate_config_value(:vcloud_password_login) ui.info("#{ui.color('DEPRECATION WARNING:', :bold)} knife[:vcloud_password_login] is deprecated" \ " and will be removed in the next version. You should remove it and run 'knife vc configure'.") passwd = locate_config_value(:vcloud_password_login) else unless pemfile raise ConfigurationError, "PEM file not configured. Please run 'knife vc configure'" end unless locate_config_value(:vcloud_password) raise ConfigurationError, "Password not configured. Please run 'knife vc configure'" end passwd = get_password(pemfile) end @connection = VCloudClient::Connection.new( locate_config_value(:vcloud_url), locate_config_value(:vcloud_user_login), passwd, locate_config_value(:vcloud_org_login), locate_config_value(:vcloud_api_version) ) end @connection end |
#deprecation_msg(value) ⇒ Object
144 145 146 147 148 149 |
# File 'lib/chef/knife/common/vc_common.rb', line 144 def deprecation_msg(value) if value && !value.empty? ui.info("#{ui.color('DEPRECATION WARNING:', :bold)} This method is deprecated" \ " and will be removed in the next version. You should use #{value}.") end end |
#generate_key(dir = "#{File.join(Dir.home, '.chef')}", output = "vc_key.pem") ⇒ Object
Generate a new key pair and store it on knife.rb
180 181 182 183 184 185 186 187 188 189 |
# File 'lib/chef/knife/common/vc_common.rb', line 180 def generate_key(dir="#{File.join(Dir.home, '.chef')}", output="vc_key.pem") key = OpenSSL::PKey::RSA.new 2048 pemfile = File.join(dir, output) File.open("#{pemfile}", 'w') do |io| io.write key.to_pem end FileUtils.chmod 0600, pemfile store_config(:vcloud_pem, pemfile) end |
#get_password(keyfile) ⇒ Object
Retrieve a stored password
199 200 201 202 203 |
# File 'lib/chef/knife/common/vc_common.rb', line 199 def get_password(keyfile) priv_key = OpenSSL::PKey::RSA.new(File.read(keyfile)) result = priv_key.private_decrypt(Base64.decode64(locate_config_value(:vcloud_password))) result end |
#locate_config_value(key) ⇒ Object
151 152 153 154 |
# File 'lib/chef/knife/common/vc_common.rb', line 151 def locate_config_value(key) key = key.to_sym Chef::Config[:knife][key] || config[key] end |
#locate_org_option ⇒ Object
Locate the correct organization option
System Administrators can browse several organizations and thus –org can be used to specify different organizations
Only –org-login is valid for other users
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/chef/knife/common/vc_common.rb', line 118 def locate_org_option org = locate_config_value(:vcloud_org_login) if locate_config_value(:vcloud_system_admin) return locate_config_value(:vcloud_org) || org end if locate_config_value(:vcloud_org) ui.warn("--org option is available only for vCloud System Administrators. " \ "Using --org-login ('#{org}').") end return org end |
#notice_msg(value) ⇒ Object
138 139 140 141 142 |
# File 'lib/chef/knife/common/vc_common.rb', line 138 def notice_msg(value) if value && !value.empty? ui.info("#{ui.color('Note:', :bold)} #{value}") end end |
#out_msg(label, value) ⇒ Object
132 133 134 135 136 |
# File 'lib/chef/knife/common/vc_common.rb', line 132 def out_msg(label, value) if value && !value.empty? ui.msg("#{ui.color(label, :cyan)}: #{value}") end end |
#pretty_symbol(key) ⇒ Object
171 172 173 |
# File 'lib/chef/knife/common/vc_common.rb', line 171 def pretty_symbol(key) key.to_s.gsub('_', ' ').capitalize end |
#sort_by_key(collection) ⇒ Object
175 176 177 |
# File 'lib/chef/knife/common/vc_common.rb', line 175 def sort_by_key(collection) collection.sort_by {|k, v| k } end |
#store_config(key, value) ⇒ Object
Update knife.rb with an entry knife = VALUE
It checks whether a given configuration already exists and, if so, updates it
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/chef/knife/common/vc_common.rb', line 208 def store_config(key, value) configfile = File.join(Dir.home, '.chef', 'knife.rb') old_config = File.open(configfile, 'r').readlines full_key = "knife[:#{key}]" if Chef::Config[:knife][key] # Replace existing key File.open("#{configfile}.tmp", 'w') do |new_config| old_config.each do |line| if line =~ Regexp.new("^#{Regexp.escape(full_key)}") line = "#{full_key} = '#{value}'" end new_config.puts line end end FileUtils.mv("#{configfile}.tmp", configfile) else # Create a new one File.open(configfile, 'a') do |new_config| new_config.puts "#{full_key} = '#{value}'" end end # Reload Chef configuration self.configure_chef end |
#store_password(keyfile) ⇒ Object
Store a password in knife.rb
192 193 194 195 196 |
# File 'lib/chef/knife/common/vc_common.rb', line 192 def store_password(keyfile) pub_key = OpenSSL::PKey::RSA.new(File.read(keyfile)).public_key result = Base64.encode64(pub_key.public_encrypt(ui.ask("Enter your password: ") { |q| q.echo = false })) store_config(:vcloud_password, result.gsub("\n", '')) end |
#wait_task(connection, task_id) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/chef/knife/common/vc_common.rb', line 156 def wait_task(connection, task_id) result = connection.wait_task_completion task_id elapsed = humanize_elapsed_time(result[:start_time], result[:end_time]) out_msg("Summary", "Status: #{ui.color(result[:status], :cyan)} - time elapsed: #{elapsed}") if result[:errormsg] ui.warn(ui.color("ATTENTION: #{result[:errormsg]}", :red)) end result[:errormsg].nil? end |