Class: Keypair

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

Overview

ssh key used to login to remote instances\

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SearchablePaths

included

Constructor Details

#initialize(fpath, extra_paths = []) ⇒ Keypair

Create a new key that defaults to id_rsa as the name.



13
14
15
16
17
# File 'lib/keypair.rb', line 13

def initialize(fpath, extra_paths=[])
  @filepath = fpath
  @extra_paths = extra_paths.map {|a| File.expand_path(a) }
  valid?
end

Instance Attribute Details

#extra_pathsObject (readonly)

Returns the value of attribute extra_paths.



10
11
12
# File 'lib/keypair.rb', line 10

def extra_paths
  @extra_paths
end

#filepathObject

Returns the value of attribute filepath.



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

def filepath
  @filepath
end

Instance Method Details

#basenameObject

Basename of the keypair



57
58
59
# File 'lib/keypair.rb', line 57

def basename
  @basename ||= ::File.basename(filepath, ::File.extname(filepath))
end

#contentObject

Read the content of the key



25
26
27
# File 'lib/keypair.rb', line 25

def content
  @content ||= exists? ? open(full_filepath).read : nil
end

#each {|full_filepath| ... } ⇒ Object

Support to add the enumerable each to keys

Yields:



67
68
69
# File 'lib/keypair.rb', line 67

def each
  yield full_filepath
end

#exists?Boolean

If the full_filepath is nil, then the key doesn’t exist

Returns:

  • (Boolean)


20
21
22
# File 'lib/keypair.rb', line 20

def exists?
  full_filepath != nil
end

#filenameObject

Just the filename of the keypair



62
63
64
# File 'lib/keypair.rb', line 62

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



31
32
33
34
35
36
37
# File 'lib/keypair.rb', line 31

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

#public_keyObject

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.



43
44
45
46
47
48
49
50
# File 'lib/keypair.rb', line 43

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



52
53
54
# File 'lib/keypair.rb', line 52

def public_key=(str)
   @public_key_string = str
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)

Returns:

  • (Boolean)


74
75
76
# File 'lib/keypair.rb', line 74

def valid?
  validations.each {|validation| self.send(validation.to_sym) }
end