Module: Keyble
Instance Method Summary collapse
- #cache ⇒ Object
- #cache_file_path ⇒ Object
- #cache_file_path=(value) ⇒ Object
- #cache_merge!(data) ⇒ Object
- #cache_save! ⇒ Object
- #keys_add(keys, servers) ⇒ Object
- #keys_display(keys) ⇒ Object
- #keys_get(servers) ⇒ Object
- #keys_import(file) ⇒ Object
- #keys_reassign(servers) ⇒ Object
- #keys_remove(keys, servers) ⇒ Object
- #parse(data) ⇒ Object
- #read(path) ⇒ Object
- #ssh_authorized_keys_path ⇒ Object
- #write(path, data) ⇒ Object
Instance Method Details
#cache ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/keyble.rb', line 46 def cache @cache ||= if (File.exist?(cache_file_path)) read(cache_file_path) else { } end end |
#cache_file_path ⇒ Object
42 43 44 |
# File 'lib/keyble.rb', line 42 def cache_file_path @cache_file_path or File.(".keyble", ENV['HOME']) end |
#cache_file_path=(value) ⇒ Object
38 39 40 |
# File 'lib/keyble.rb', line 38 def cache_file_path=(value) @cache_file_path = value end |
#cache_merge!(data) ⇒ Object
55 56 57 58 59 |
# File 'lib/keyble.rb', line 55 def cache_merge!(data) cache.merge!(data) cache end |
#cache_save! ⇒ Object
61 62 63 64 65 |
# File 'lib/keyble.rb', line 61 def cache_save! return unless (@cache) write(cache_file_path, @cache) end |
#keys_add(keys, servers) ⇒ Object
95 96 97 98 99 |
# File 'lib/keyble.rb', line 95 def keys_add(keys, servers) keys_reassign(servers) do |server, existing_keys| existing_keys.merge(keys) end end |
#keys_display(keys) ⇒ Object
124 125 126 127 128 129 130 |
# File 'lib/keyble.rb', line 124 def keys_display(keys) longest_key = keys.keys.collect { |k| k.to_s.length }.sort[-1] keys.sort.each do |key, line| puts "%-#{longest_key}s %s" % [ key, line.split(/\s+/)[1][-20, 20] ] end end |
#keys_get(servers) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/keyble.rb', line 67 def keys_get(servers) result = { } servers.each do |server| Net::SCP.start(server, nil) do |scp| result[server] = parse(scp.download!()) end end result end |
#keys_import(file) ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'lib/keyble.rb', line 115 def keys_import(file) imported = read(file) cache_merge!(imported) cache_save! imported end |
#keys_reassign(servers) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/keyble.rb', line 79 def keys_reassign(servers) servers.each do |server| Net::SCP.start(server, nil) do |scp| existing = parse(scp.download!()) merged = yield(server, existing.dup) if (merged != existing) = StringIO.new(merged.values.join("\n")) scp.upload!(, ) end end end end |
#keys_remove(keys, servers) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/keyble.rb', line 101 def keys_remove(keys, servers) if (keys.respond_to?(:keys)) keys = keys.keys end keys_reassign(servers) do |server, existing_keys| keys.each do |key| existing_keys.delete(key) end existing_keys end end |
#parse(data) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/keyble.rb', line 2 def parse(data) result = { } data.split(/\r?\n/).collect do |line| type, key, comment = line.split(/\s+/) if (comment and !comment.empty? and comment.match(/\S+\@\S+/)) result[comment] = line end end result end |
#read(path) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/keyble.rb', line 16 def read(path) result = { } File.open(path) do |f| result = parse(f.read) end result end |
#ssh_authorized_keys_path ⇒ Object
34 35 36 |
# File 'lib/keyble.rb', line 34 def ".ssh/authorized_keys" end |
#write(path, data) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/keyble.rb', line 26 def write(path, data) File.open(path, 'w') do |f| data.each do |key, line| f.puts(line) end end end |