Class: Hetzner::Bootstrap::Target
- Inherits:
-
Object
- Object
- Hetzner::Bootstrap::Target
show all
- Defined in:
- lib/hetzner/bootstrap/target.rb
Defined Under Namespace
Classes: CantActivateRescueSystemError, CantResetSystemError, InstallationError, NoTemplateProvidedError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(options = {}) ⇒ Target
Returns a new instance of Target.
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/hetzner/bootstrap/target.rb', line 22
def initialize(options = {})
@rescue_os = 'linux'
@rescue_os_bit = '64'
@retries = 0
@bootstrap_cmd = 'export TERM=xterm; /root/.oldroot/nfs/install/installimage -a -c /tmp/template'
@login = 'root'
if tmpl = options.delete(:template)
@template = Template.new tmpl
else
raise NoTemplateProvidedError.new 'No imageinstall template provided.'
end
options.each_pair do |k,v|
self.send("#{k}=", v)
end
end
|
Instance Attribute Details
#actions ⇒ Object
Returns the value of attribute actions.
15
16
17
|
# File 'lib/hetzner/bootstrap/target.rb', line 15
def actions
@actions
end
|
#bootstrap_cmd ⇒ Object
Returns the value of attribute bootstrap_cmd.
19
20
21
|
# File 'lib/hetzner/bootstrap/target.rb', line 19
def bootstrap_cmd
@bootstrap_cmd
end
|
#hostname ⇒ Object
Returns the value of attribute hostname.
16
17
18
|
# File 'lib/hetzner/bootstrap/target.rb', line 16
def hostname
@hostname
end
|
#ip ⇒ Object
Returns the value of attribute ip.
9
10
11
|
# File 'lib/hetzner/bootstrap/target.rb', line 9
def ip
@ip
end
|
#logger ⇒ Object
Returns the value of attribute logger.
20
21
22
|
# File 'lib/hetzner/bootstrap/target.rb', line 20
def logger
@logger
end
|
#login ⇒ Object
Returns the value of attribute login.
10
11
12
|
# File 'lib/hetzner/bootstrap/target.rb', line 10
def login
@login
end
|
#password ⇒ Object
Returns the value of attribute password.
11
12
13
|
# File 'lib/hetzner/bootstrap/target.rb', line 11
def password
@password
end
|
#post_install(options = {}) ⇒ Object
Returns the value of attribute post_install.
17
18
19
|
# File 'lib/hetzner/bootstrap/target.rb', line 17
def post_install
@post_install
end
|
#public_keys ⇒ Object
Returns the value of attribute public_keys.
18
19
20
|
# File 'lib/hetzner/bootstrap/target.rb', line 18
def public_keys
@public_keys
end
|
#rescue_os ⇒ Object
Returns the value of attribute rescue_os.
13
14
15
|
# File 'lib/hetzner/bootstrap/target.rb', line 13
def rescue_os
@rescue_os
end
|
#rescue_os_bit ⇒ Object
Returns the value of attribute rescue_os_bit.
14
15
16
|
# File 'lib/hetzner/bootstrap/target.rb', line 14
def rescue_os_bit
@rescue_os_bit
end
|
#template ⇒ Object
Returns the value of attribute template.
12
13
14
|
# File 'lib/hetzner/bootstrap/target.rb', line 12
def template
@template
end
|
Instance Method Details
#copy_ssh_keys(options = {}) ⇒ Object
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/hetzner/bootstrap/target.rb', line 142
def copy_ssh_keys(options = {})
if @public_keys
remote do |ssh|
ssh.exec!("mkdir /root/.ssh")
Array(@public_keys).each do |key|
pub = File.read(File.expand_path(key))
ssh.exec!("echo \"#{pub}\" >> /root/.ssh/authorized_keys")
end
end
end
end
|
230
231
232
233
234
235
|
# File 'lib/hetzner/bootstrap/target.rb', line 230
def default_log_formatter
proc do |severity, datetime, progname, msg|
caller[4]=~/`(.*?)'/
"[#{datetime.strftime "%H:%M:%S"}][#{sprintf "%-15s", ip}][#{$1}] #{msg}\n"
end
end
|
#enable_rescue_mode(options = {}) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/hetzner/bootstrap/target.rb', line 40
def enable_rescue_mode(options = {})
result = @api.enable_rescue! @ip, @rescue_os, @rescue_os_bit
if result.success? && result['rescue']
@password = result['rescue']['password']
reset_retries
logger.info "IP: #{ip} => password: #{@password}"
elsif @retries > 3
logger.error "rescue system could not be activated"
raise CantActivateRescueSystemError, result
else
@retries += 1
logger.warn "problem while trying to activate rescue system (retries: #{@retries})"
@api.disable_rescue! @ip
rolling_sleep
enable_rescue_mode options
end
end
|
#installimage(options = {}) ⇒ Object
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/hetzner/bootstrap/target.rb', line 116
def installimage(options = {})
template = render_template
remote do |ssh|
ssh.exec! "echo \"#{template}\" > /tmp/template"
logger.info "remote executing: #{@bootstrap_cmd}"
output = ssh.exec!(@bootstrap_cmd)
logger.info output
end
end
|
#local(&block) ⇒ Object
218
219
220
|
# File 'lib/hetzner/bootstrap/target.rb', line 218
def local(&block)
block.call
end
|
#port_open?(ip, port) ⇒ Boolean
77
78
79
80
81
82
|
# File 'lib/hetzner/bootstrap/target.rb', line 77
def port_open? ip, port
ssh_port_probe = TCPSocket.new ip, port
IO.select([ssh_port_probe], nil, nil, 2)
ssh_port_probe.close
true
end
|
#reboot(options = {}) ⇒ Object
127
128
129
130
131
|
# File 'lib/hetzner/bootstrap/target.rb', line 127
def reboot(options = {})
remote do |ssh|
ssh.exec!("reboot")
end
end
|
#remote(options = {}, &block) ⇒ Object
208
209
210
211
212
213
214
215
216
|
# File 'lib/hetzner/bootstrap/target.rb', line 208
def remote(options = {}, &block)
default = { :paranoid => false, :password => @password }
default.merge! options
Net::SSH.start(@ip, @login, default) do |ssh|
block.call ssh
end
end
|
#render_post_install ⇒ Object
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/hetzner/bootstrap/target.rb', line 187
def render_post_install
eruby = Erubis::Eruby.new @post_install.to_s
params = {}
params[:hostname] = @hostname
params[:ip] = @ip
params[:login] = @login
params[:password] = @password
return eruby.result(params)
end
|
#render_template ⇒ Object
177
178
179
180
181
182
183
184
185
|
# File 'lib/hetzner/bootstrap/target.rb', line 177
def render_template
eruby = Erubis::Eruby.new @template.to_s
params = {}
params[:hostname] = @hostname
params[:ip] = @ip
return eruby.result(params)
end
|
#reset(options = {}) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/hetzner/bootstrap/target.rb', line 61
def reset(options = {})
result = @api.reset! @ip, :hw
if result.success?
reset_retries
elsif @retries > 3
logger.error "resetting through webservice failed."
raise CantResetSystemError, result
else
@retries += 1
logger.warn "problem while trying to reset/reboot system (retries: #{@retries})"
rolling_sleep
reset options
end
end
|
#reset_retries ⇒ Object
222
223
224
|
# File 'lib/hetzner/bootstrap/target.rb', line 222
def reset_retries
@retries = 0
end
|
#rolling_sleep ⇒ Object
226
227
228
|
# File 'lib/hetzner/bootstrap/target.rb', line 226
def rolling_sleep
sleep @retries * @retries * 3 + 1 end
|
#update_local_known_hosts(options = {}) ⇒ Object
154
155
156
157
158
159
160
161
|
# File 'lib/hetzner/bootstrap/target.rb', line 154
def update_local_known_hosts(options = {})
remote(:paranoid => true) do |ssh|
end
rescue Net::SSH::HostKeyMismatch => e
e.remember_host!
logger.info "remote host key added to local ~/.ssh/known_hosts file."
end
|
#use_api(api_obj) ⇒ Object
199
200
201
|
# File 'lib/hetzner/bootstrap/target.rb', line 199
def use_api(api_obj)
@api = api_obj
end
|
#use_logger(logger_obj) ⇒ Object
203
204
205
206
|
# File 'lib/hetzner/bootstrap/target.rb', line 203
def use_logger(logger_obj)
@logger = logger_obj
@logger.formatter = default_log_formatter
end
|
#verify_installation(options = {}) ⇒ Object
133
134
135
136
137
138
139
140
|
# File 'lib/hetzner/bootstrap/target.rb', line 133
def verify_installation(options = {})
remote do |ssh|
working_hostname = ssh.exec!("cat /etc/hostname")
unless @hostname == working_hostname.chomp
raise InstallationError, "hostnames do not match: assumed #{@hostname} but received #{working_hostname}"
end
end
end
|
#wait_for_ssh_down(options = {}) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/hetzner/bootstrap/target.rb', line 84
def wait_for_ssh_down(options = {})
loop do
sleep 2
Timeout::timeout(4) do
if port_open? @ip, 22
logger.debug "SSH UP"
else
raise Errno::ECONNREFUSED
end
end
end
rescue Timeout::Error, Errno::ECONNREFUSED
logger.debug "SSH DOWN"
end
|
#wait_for_ssh_up(options = {}) ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/hetzner/bootstrap/target.rb', line 99
def wait_for_ssh_up(options = {})
loop do
Timeout::timeout(4) do
if port_open? @ip, 22
logger.debug "SSH UP"
return true
else
raise Errno::ECONNREFUSED
end
end
end
rescue Errno::ECONNREFUSED, Timeout::Error
logger.debug "SSH DOWN"
sleep 2
retry
end
|