Class: Phase::CLI::Keys::AuthorizedKeys

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/phase/cli/keys.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bastions) ⇒ AuthorizedKeys

Returns a new instance of AuthorizedKeys.



113
114
115
116
117
# File 'lib/phase/cli/keys.rb', line 113

def initialize(bastions)
  @bastions = bastions
  @hash = Hash.new
  fetch_keys
end

Instance Attribute Details

#bastionsObject (readonly)

Returns the value of attribute bastions.



111
112
113
# File 'lib/phase/cli/keys.rb', line 111

def bastions
  @bastions
end

Instance Method Details

#add(key) ⇒ Object



136
137
138
# File 'lib/phase/cli/keys.rb', line 136

def add(key)
  self[key.email] = key
end

#backup!Object



155
156
157
# File 'lib/phase/cli/keys.rb', line 155

def backup!
  File.open( File.expand_path("~/.phase-keys"), "w" ) { |file| file << raw_keys }
end

#fetch_keysObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/phase/cli/keys.rb', line 119

def fetch_keys
  lines = raw_keys.lines.map(&:chomp).reject(&:empty?)
  idx = 0
  while idx < lines.count
    attrs_line = lines[idx]

    unless attrs_line.match(/\A### phase-key/)
      idx += 1
      next
    end

    key_line = lines[idx + 1]
    add( Keys::Key.parse(attrs_line, key_line) )
    idx += 2
  end
end

#persist!Object



150
151
152
153
# File 'lib/phase/cli/keys.rb', line 150

def persist!
  log "writing new keys..."
  bastions.exec("echo '#{to_s}' > ~/.ssh/authorized_keys")
end

#remove(key) ⇒ Object



140
141
142
# File 'lib/phase/cli/keys.rb', line 140

def remove(key)
  delete(key.email)
end

#to_sObject



144
145
146
147
148
# File 'lib/phase/cli/keys.rb', line 144

def to_s
  reduce("") do |out, (_, key)|
    out << key.to_s
  end
end