Class: PoolParty::Key
Instance Attribute Summary collapse
-
#filepath ⇒ Object
Returns the value of attribute filepath.
Class Method Summary collapse
-
.keypair_paths ⇒ Object
Default locations to search for the key.
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.
-
#search_in_known_locations ⇒ Object
Search for the key in default locations with the entire filepath if the file exists.
-
#to_json ⇒ Object
Turn the keypair into the a useful json string.
Constructor Details
#initialize(fpath = nil) ⇒ Key
Create a new key that defaults to id_rsa as the name.
10 11 12 |
# File 'lib/poolparty/poolparty/key.rb', line 10 def initialize(fpath=nil) @filepath = (fpath.nil? || fpath.empty?) ? "id_rsa" : fpath end |
Instance Attribute Details
#filepath ⇒ Object
Returns the value of attribute filepath.
7 8 9 |
# File 'lib/poolparty/poolparty/key.rb', line 7 def filepath @filepath end |
Class Method Details
.keypair_paths ⇒ Object
Default locations to search for the key
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/poolparty/poolparty/key.rb', line 55 def self.keypair_paths [ "#{ENV["HOME"]}/.ssh", "#{Default.poolparty_home_path}/keys", PoolParty::Default.base_keypair_path, PoolParty::Default.base_config_directory, PoolParty::Default.base_ssh_path, PoolParty::Default.remote_storage_path, Dir.pwd ] end |
Instance Method Details
#basename ⇒ Object
Basename of the keypair
32 33 34 |
# File 'lib/poolparty/poolparty/key.rb', line 32 def basename @basename ||= ::File.basename(full_filepath, ::File.extname(full_filepath)) rescue filepath end |
#content ⇒ Object
Read the content of the key
20 21 22 |
# File 'lib/poolparty/poolparty/key.rb', line 20 def content @content ||= exists? ? open(full_filepath).read : nil end |
#each {|full_filepath| ... } ⇒ Object
Support to add the enumerable each to keys
68 69 70 |
# File 'lib/poolparty/poolparty/key.rb', line 68 def each yield full_filepath end |
#exists? ⇒ Boolean
If the full_filepath is nil, then the key doesn’t exist
15 16 17 |
# File 'lib/poolparty/poolparty/key.rb', line 15 def exists? full_filepath != nil end |
#filename ⇒ Object
Just the filename of the keypair
37 38 39 |
# File 'lib/poolparty/poolparty/key.rb', line 37 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
26 27 28 |
# File 'lib/poolparty/poolparty/key.rb', line 26 def full_filepath @full_filepath ||= ::File.file?(::File.(filepath)) ? ::File.(filepath) : search_in_known_locations end |
#search_in_known_locations ⇒ Object
Search for the key in default locations with the entire filepath if the file exists. If it doesn’t exist in the default locations, then it returns nil and assumes we it doesn’t exist
44 45 46 47 48 49 50 51 52 |
# File 'lib/poolparty/poolparty/key.rb', line 44 def search_in_known_locations self.class.keypair_paths.each do |path| full_path = ::File.join( ::File.(path), ::File.basename(filepath)) return full_path if ::File.exists?(full_path) end # raise Exception.new("We cannot continue without a keypair. Please define a keypair in your clouds.rb") # TODO: Add raise for keypair nil end |
#to_json ⇒ Object
Turn the keypair into the a useful json string
73 74 75 |
# File 'lib/poolparty/poolparty/key.rb', line 73 def to_json "{\"basename\":\"#{basename}\",\"full_filepath\": \"/etc/poolparty/#{filename}\"}" end |