Class: Zool::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/zool/server.rb

Defined Under Namespace

Classes: ConnectionVerificationExecption

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, options = {}) ⇒ Server

Returns a new instance of Server.



9
10
11
12
13
14
15
16
# File 'lib/zool/server.rb', line 9

def initialize(hostname, options = {})
  @options = {
    :user => 'root', 
    :password => ''
  }.update(options)
  @hostname = hostname
  @keyfile_location = default_keyfile_location
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



6
7
8
# File 'lib/zool/server.rb', line 6

def hostname
  @hostname
end

#keyfile_locationObject

Returns the value of attribute keyfile_location.



7
8
9
# File 'lib/zool/server.rb', line 7

def keyfile_location
  @keyfile_location
end

Instance Method Details

#create_backupObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/zool/server.rb', line 39

def create_backup
  begin
    backup = load_remote_file
    backup_filename = "#{@keyfile_location}_#{Time.now.to_i}"
    Net::SCP.upload!(@hostname, @options[:user], StringIO.new(backup), backup_filename, :ssh => {:password => @options[:password]})
    backup_filename
  rescue Net::SCP::Error => e
    logger.fatal "Error during backup of authorized keys file: #{e.message}"
    raise
  end
end

#dump_keyfilesObject



34
35
36
37
# File 'lib/zool/server.rb', line 34

def dump_keyfiles
 key_writer = KeyfileWriter.new
 key_writer.write_keys keys
end

#fetch_keysObject



18
19
20
21
# File 'lib/zool/server.rb', line 18

def fetch_keys
  @keys = nil
  @raw_authorized_keys = load_remote_file
end

#keysObject



23
24
25
26
27
28
# File 'lib/zool/server.rb', line 23

def keys
 @keys ||= begin
   @raw_authorized_keys ||= fetch_keys
   @raw_authorized_keys.split("\n").map {|key| key.strip}.uniq.reject {|key| key == ""}
 end
end

#keys=(new_keys) ⇒ Object



30
31
32
# File 'lib/zool/server.rb', line 30

def keys=(new_keys)
 @keys = new_keys
end

#to_sObject



81
82
83
# File 'lib/zool/server.rb', line 81

def to_s
 "<Zool::Server #{hostname}>"
end

#upload_keysObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/zool/server.rb', line 51

def upload_keys
  remote_backup_file = create_backup
  begin
    backup_channel = Net::SSH.start(@hostname, @options[:user], :password => @options[:password])
    main_channel   = Net::SSH.start(@hostname, @options[:user], :password => @options[:password])
    main_channel.scp.upload!(StringIO.new(keys.join("\n")), @keyfile_location)
    main_channel.close
    begin
      logger.info "Trying to connect to #{@hostname} to see if I still have access"
      Net::SSH.start(@hostname, @options[:user], :password => '')
      logger.info "Backup channel connection succeeded. Assuming everything went fine!"
    rescue Net::SSH::AuthenticationFailed => e
      if !@rolled_back
        logger.warn "!!!!!! Could not login to server after upload operation! Rolling back !!!!!!"
        backup_channel.exec "mv #{remote_backup_file} #{@keyfile_location}"
        backup_channel.loop
        @rolled_back = true
        retry
      else
        logger.fatal "Tried to role back... didnt work... giving up... sorry :("
        raise e
      end
    end
  ensure
    main_channel.close unless main_channel.closed?
    backup_channel.close unless backup_channel.closed?
  end
  raise ConnectionVerificationExecption.new("Error after uploading the keyfile to #{@hostname}") if @rolled_back
end

#userObject



85
86
87
# File 'lib/zool/server.rb', line 85

def user
  @options[:user]
end