Class: Keypair

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

Overview

ssh key used to login to remote instances\

Constant Summary collapse

SEARCH_SUFFIXES =

Amazon will not append suffix, but public key may have ‘.pem’ suffix

%w( .pem )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fpath, extra_paths = [], opts = {}) ⇒ Keypair

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



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

def initialize(fpath, extra_paths=[], opts={})
  @filepath = fpath
  @opts = opts
  @extra_paths = extra_paths.map {|a| File.expand_path(a) }
  @search_suffixes = SEARCH_SUFFIXES
end

Instance Attribute Details

#extra_pathsObject (readonly)

Returns the value of attribute extra_paths.



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

def extra_paths
  @extra_paths
end

#filepathObject

Returns the value of attribute filepath.



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

def filepath
  @filepath
end

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
end

#search_suffixesObject (readonly)

Returns the value of attribute search_suffixes.



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

def search_suffixes
  @search_suffixes
end

Instance Method Details

#basenameObject

Basename of the keypair



63
64
65
# File 'lib/keypair.rb', line 63

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

#contentObject

Read the content of the key



30
31
32
# File 'lib/keypair.rb', line 30

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

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

Support to add the enumerable each to keys

Yields:



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

def each
  yield full_filepath
end

#exists?Boolean

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

Returns:

  • (Boolean)


25
26
27
# File 'lib/keypair.rb', line 25

def exists?
  !! full_filepath
end

#filenameObject

Just the filename of the keypair



68
69
70
# File 'lib/keypair.rb', line 68

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

#full_filepathObject

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



36
37
38
39
40
# File 'lib/keypair.rb', line 36

def full_filepath
  @full_filepath ||= 
    find_file_in_path_with_suffix(filepath, extra_paths, 
                                  search_suffixes) || false
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.



49
50
51
52
53
54
55
56
# File 'lib/keypair.rb', line 49

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



58
59
60
# File 'lib/keypair.rb', line 58

def public_key=(str)
   @public_key_string = str
end

#to_sObject



42
43
44
# File 'lib/keypair.rb', line 42

def to_s
  basename
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)


80
81
82
# File 'lib/keypair.rb', line 80

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