Class: PoolParty::Key

Inherits:
Object show all
Defined in:
lib/poolparty/poolparty/key.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#filepathObject

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_pathsObject

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

#basenameObject

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

#contentObject

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

Yields:



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

Returns:

  • (Boolean)


15
16
17
# File 'lib/poolparty/poolparty/key.rb', line 15

def exists?
  full_filepath != nil
end

#filenameObject

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_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



26
27
28
# File 'lib/poolparty/poolparty/key.rb', line 26

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

#search_in_known_locationsObject

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.expand_path(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_jsonObject

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