-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
131 lines (110 loc) · 3.1 KB
/
server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Handles cloud.dk server automation API
# https://docs.onapp.com/display/33API/OnApp+3.3+API+Guide
#
# ssh keys must be setup beforehand so password is not needed
require 'rubygems'
require 'net/http'
require 'net/https'
require 'json'
require File.expand_path( File.join( File.dirname(__FILE__), 'configuration' ) )
# Manages a single virtual server
class Server
attr_reader :config, :key, :settings, :id, :hostname, :ssh_user
def initialize config, key
@config = config
raise "Config missing!" unless @config
@key = key
@settings = config['servers'][key.to_s]
raise "Settings for server #{key} missing!" unless @settings
@id = @settings['id']
raise "Server id for server #{key} missing!" unless @id
@hostname = @settings['hostname']
raise "Hostname id for server #{key} missing!" unless @hostname
@ssh_user = @settings['ssh_user']
raise "SSH user for server #{key} missing!" unless @ssh_user
end
def status
response = http_get 'status'
status = JSON.parse(response.body)['virtual_machine']
if status['locked'] == false
if status['booted'] == true
:up
else
:down
end
else
:pending
end
end
def up?
status == :up
end
def check_response response
raise "API error code #{response.code}" unless [200,201].include? response.code.to_i
end
def startup
puts "Starting update server at #{Time.now}"
response = http_post 'startup'
end
def shutdown
puts "Shutting down update server at #{Time.now}"
response = http_post 'shutdown'
end
def up
unless up?
startup
else
puts "Update server already up"
end
wait_for_ssh
end
def down
if up?
shutdown
else
puts "Update server already down"
end
end
def wait_for_ssh
puts 'Waiting for SSH...'
600.times do
if system %{ssh -o PasswordAuthentication=no -q #{@ssh_user}@#{@hostname} "whoami" 2>&1 > /dev/null}
puts 'SSH Ready'
return
end
sleep 1
end
raise "Timeout while waiting for SSH"
end
def initiate cmd
# use ssh to run command on remote server
# run script in background using '&'
# use nohup, so it's not terminated when we log out of ssh
# to avoid ssh hanging, make sure to redirect all three stream: stdout, stderr, stdin
t = %{ssh #{@ssh_user}@#{@hostname} "nohup #{cmd} 2>&1 < /dev/null &"}
puts "--> #{t}"
system t
end
private
def http_get call
http_call call, :get
end
def http_post call
http_call call, :post
end
def http_call call, method=:get
uri = URI("#{@config['api_hostname']}/virtual_machines/#{@id}/#{call}.json")
http = Net::HTTP.new(uri.host, uri.port)
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.use_ssl = true
if method == :post
request = Net::HTTP::Post.new(uri.path)
else
request = Net::HTTP::Get.new(uri.path)
end
request.basic_auth @config['api_username'], @config['api_password']
response = http.start {|http| http.request(request) }
check_response response
response
end
end