Module: ForemanOpennebula::KeyPairComputeResource

Extended by:
ActiveSupport::Concern
Included in:
Opennebula
Defined in:
app/models/concerns/foreman_opennebula/key_pair_compute_resource.rb

Instance Method Summary collapse

Instance Method Details

#delete_key_from_resource(remote_key_pair = key_pair.name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/concerns/foreman_opennebula/key_pair_compute_resource.rb', line 36

def delete_key_from_resource(remote_key_pair = key_pair.name)
  logger.info "removing key from compute resource #{name} "\
              "(#{provider_friendly_name}): #{remote_key_pair}"
  opennebula_user = available_users.detect { |u| u.name == user }
  template_hash = opennebula_user.to_hash['USER']['TEMPLATE']
  template_hash.delete('SSH_PUBLIC_KEY')
  template_str = template_hash.map { |k, v| "#{k}=\"#{v}\"" }.join("\n")
  opennebula_user.update(template_str)
  KeyPair.destroy_by :compute_resource_id => id
rescue => e
  Foreman::Logging.exception(
    "Failed to delete key pair from #{provider_friendly_name}: #{name}, you "\
    "might need to cleanup manually: #{e}",
    e,
    :level => :warn
  )
end

#get_compute_key_pairsObject



19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/concerns/foreman_opennebula/key_pair_compute_resource.rb', line 19

def get_compute_key_pairs
  return [] unless capabilities.include?(:key_pair)
  active_key = key_pair
  return [] if key_pairs.nil? || active_key.nil?
  akey = SSHKey.new(active_key.secret)
  key_pairs.map do |key|
    key_fingerprint = SSHKey.fingerprint(key)
    key_name = key_fingerprint == akey.md5_fingerprint ? active_key.name : 'unknown'
    ComputeResourceKeyPair.new(key_name, key_fingerprint, active_key.name, active_key.id)
  end
end

#key_pairsObject



12
13
14
15
16
17
# File 'app/models/concerns/foreman_opennebula/key_pair_compute_resource.rb', line 12

def key_pairs
  opennebula_user = available_users.detect { |u| u.name == user }
  public_key = opennebula_user['TEMPLATE/SSH_PUBLIC_KEY']
  return unless public_key.present? && SSHKey.valid_ssh_public_key?(public_key)
  [public_key]
end

#recreateObject



31
32
33
34
# File 'app/models/concerns/foreman_opennebula/key_pair_compute_resource.rb', line 31

def recreate
  destroy_key_pair
  setup_key_pair
end