Class: Keypair

Inherits:
Object show all
Includes:
SearchablePaths
Defined in:
lib/keypair.rb

Overview

ssh key used to login to remote instances\

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SearchablePaths

included

Constructor Details

#initialize(fpath) ⇒ Keypair

Create a new key that defaults to id_rsa as the name.



12
13
14
15
# File 'lib/keypair.rb', line 12

def initialize(fpath)
  @filepath = fpath
  valid?
end

Instance Attribute Details

#filepathObject

Returns the value of attribute filepath.



9
10
11
# File 'lib/keypair.rb', line 9

def filepath
  @filepath
end

Instance Method Details

#basenameObject

Basename of the keypair



55
56
57
# File 'lib/keypair.rb', line 55

def basename
  @basename ||= ::File.basename(filepath, ::File.extname(filepath))
end

#contentObject

Read the content of the key



23
24
25
# File 'lib/keypair.rb', line 23

def content
  @content ||= exists? ? open(full_filepath).read : nil
end

#each {|full_filepath| ... } ⇒ Object

Support to add the enumerable each to keys

Yields:



65
66
67
# File 'lib/keypair.rb', line 65

def each
  yield full_filepath
end

#exists?Boolean

If the full_filepath is nil, then the key doesn’t exist

Returns:

  • (Boolean)


18
19
20
# File 'lib/keypair.rb', line 18

def exists?
  full_filepath != nil
end

#filenameObject

Just the filename of the keypair



60
61
62
# File 'lib/keypair.rb', line 60

def filename
  @filename ||= ::File.basename(full_filepath) rescue filepath
end

#full_filepathObject Also known as: to_s

Returns the full_filepath of the key. If a full filepath is passed, we just return the expanded filepath for the keypair, otherwise query where it is against known locations



29
30
31
32
33
34
35
# File 'lib/keypair.rb', line 29

def full_filepath
  @full_filepath ||= if File.file?(::File.expand_path(filepath))
    ::File.expand_path(filepath)
    else
      search_in_known_locations(filepath)
    end
end

#public_keyObject

TODO: gracefully handle the case when a passpharase is needed Generate a public key from the private key net/ssh already has this built-in from our extension.



41
42
43
44
45
46
47
48
# File 'lib/keypair.rb', line 41

def public_key
  if !@public_key_string || @public_key_string.empty?
    pkey = Net::SSH::KeyFactory.load_private_key(full_filepath)
    @public_key_string = pkey.public_key
  else
    @public_key_string
  end
end

#public_key=(str) ⇒ Object



50
51
52
# File 'lib/keypair.rb', line 50

def public_key=(str)
   @public_key_string = str
end

#valid?Boolean

Validation checks if all of the validations pass, the object is considered valid the validations are responsible for raising a PoolPartyError (StandardError)

Returns:

  • (Boolean)


72
73
74
# File 'lib/keypair.rb', line 72

def valid?
  validations.each {|validation| self.send(validation.to_sym) }
end