Class: Keypair
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
-
#extra_paths ⇒ Object
readonly
Returns the value of attribute extra_paths.
-
#filepath ⇒ Object
Returns the value of attribute filepath.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#search_suffixes ⇒ Object
readonly
Returns the value of attribute search_suffixes.
Instance Method Summary collapse
-
#basename ⇒ Object
Basename of the keypair.
-
#content ⇒ Object
Read the content of the key.
-
#each {|full_filepath| ... } ⇒ Object
Support to add the enumerable each to keys.
-
#exists? ⇒ Boolean
If the full_filepath is nil or false, then the key doesn’t exist.
-
#filename ⇒ Object
Just the filename of the keypair.
-
#full_filepath ⇒ Object
Returns the full_filepath of the key.
-
#initialize(fpath, extra_paths = [], opts = {}) ⇒ Keypair
constructor
Create a new key that defaults to id_rsa as the name.
-
#public_key ⇒ Object
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.
- #public_key=(str) ⇒ Object
- #to_s ⇒ Object
-
#valid? ⇒ Boolean
Validation checks if all of the validations pass, the object is considered valid the validations are responsible for raising a PoolPartyError (StandardError).
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.(a) } @search_suffixes = SEARCH_SUFFIXES end |
Instance Attribute Details
#extra_paths ⇒ Object (readonly)
Returns the value of attribute extra_paths.
13 14 15 |
# File 'lib/keypair.rb', line 13 def extra_paths @extra_paths end |
#filepath ⇒ Object
Returns the value of attribute filepath.
12 13 14 |
# File 'lib/keypair.rb', line 12 def filepath @filepath end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
13 14 15 |
# File 'lib/keypair.rb', line 13 def opts @opts end |
#search_suffixes ⇒ Object (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
#basename ⇒ Object
Basename of the keypair
63 64 65 |
# File 'lib/keypair.rb', line 63 def basename @basename ||= ::File.basename(filepath, ::File.extname(filepath)) end |
#content ⇒ Object
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
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
25 26 27 |
# File 'lib/keypair.rb', line 25 def exists? !! full_filepath end |
#filename ⇒ Object
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_filepath ⇒ Object
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_key ⇒ Object
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_s ⇒ Object
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)
80 81 82 |
# File 'lib/keypair.rb', line 80 def valid? validations.each {|validation| self.send(validation.to_sym) } end |