Class: PoolParty::Key
- Includes:
- SearchablePaths
- Defined in:
- lib/poolparty/poolparty/key.rb
Instance Attribute Summary collapse
-
#filepath ⇒ Object
Returns the value of attribute filepath.
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, then the key doesn’t exist.
-
#filename ⇒ Object
Just the filename of the keypair.
-
#full_filepath ⇒ Object
(also: #to_s)
Returns the full_filepath of the key.
-
#initialize(fpath = nil) ⇒ Key
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.
- #public_key=(str) ⇒ Object
-
#to_hash ⇒ Object
Turn the keypair into the a useful json string.
Methods included from SearchablePaths
Constructor Details
#initialize(fpath = nil) ⇒ Key
Create a new key that defaults to id_rsa as the name.
12 13 14 |
# File 'lib/poolparty/poolparty/key.rb', line 12 def initialize(fpath=nil) @filepath = (fpath.nil? || fpath.empty?) ? "id_rsa" : fpath end |
Instance Attribute Details
#filepath ⇒ Object
Returns the value of attribute filepath.
9 10 11 |
# File 'lib/poolparty/poolparty/key.rb', line 9 def filepath @filepath end |
Instance Method Details
#basename ⇒ Object
Basename of the keypair
49 50 51 |
# File 'lib/poolparty/poolparty/key.rb', line 49 def basename @basename ||= ::File.basename(full_filepath, ::File.extname(full_filepath)) rescue filepath end |
#content ⇒ Object
Read the content of the key
22 23 24 |
# File 'lib/poolparty/poolparty/key.rb', line 22 def content @content ||= exists? ? open(full_filepath).read : nil end |
#each {|full_filepath| ... } ⇒ Object
Support to add the enumerable each to keys
59 60 61 |
# File 'lib/poolparty/poolparty/key.rb', line 59 def each yield full_filepath end |
#exists? ⇒ Boolean
If the full_filepath is nil, then the key doesn’t exist
17 18 19 |
# File 'lib/poolparty/poolparty/key.rb', line 17 def exists? full_filepath != nil end |
#filename ⇒ Object
Just the filename of the keypair
54 55 56 |
# File 'lib/poolparty/poolparty/key.rb', line 54 def filename @filename ||= ::File.basename(full_filepath) rescue filepath end |
#full_filepath ⇒ Object 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
28 29 30 |
# File 'lib/poolparty/poolparty/key.rb', line 28 def full_filepath @full_filepath ||= ::File.file?(::File.(filepath)) ? ::File.(filepath) : search_in_known_locations(filepath) end |
#public_key ⇒ Object
TODO: gracefully handle the case when a passpharase is needed Generate a public key from the private key
35 36 37 38 39 40 41 42 |
# File 'lib/poolparty/poolparty/key.rb', line 35 def public_key if !@public_key_string || @public_key_string.empty? @public_key_string = `ssh-keygen -y -f #{full_filepath}` raise 'Unable to generate public_key_string' if @public_key_string.empty? else @public_key_string end end |
#public_key=(str) ⇒ Object
44 45 46 |
# File 'lib/poolparty/poolparty/key.rb', line 44 def public_key=(str) @public_key_string = str end |
#to_hash ⇒ Object
Turn the keypair into the a useful json string
64 65 66 |
# File 'lib/poolparty/poolparty/key.rb', line 64 def to_hash "{\"basename\":\"#{basename}\",\"full_filepath\": \"/etc/poolparty/#{filename}\"}" end |