Class: PoolParty::Key

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SearchablePaths

included

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

#filepathObject

Returns the value of attribute filepath.



9
10
11
# File 'lib/poolparty/poolparty/key.rb', line 9

def filepath
  @filepath
end

Instance Method Details

#basenameObject

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

#contentObject

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

Yields:



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

Returns:

  • (Boolean)


17
18
19
# File 'lib/poolparty/poolparty/key.rb', line 17

def exists?
  full_filepath != nil
end

#filenameObject

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



28
29
30
# File 'lib/poolparty/poolparty/key.rb', line 28

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

#public_keyObject

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_hashObject

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