Class: Hetzner::Bootstrap::CoreOS::Target

Inherits:
Object
  • Object
show all
Defined in:
lib/hetzner/bootstrap/coreos/target.rb

Defined Under Namespace

Classes: CantActivateRescueSystemError, CantResetSystemError, InstallationError, NoCloudConfigProvidedError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Target

Returns a new instance of Target.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 24

def initialize(options = {})
  @rescue_os     = 'linux'
  @rescue_os_bit = '64'
  @retries       = 0
  @bootstrap_cmd = 'export TERM=xterm; /tmp/coreos-install -d /dev/sda -C stable -c /tmp/cloud-config.yaml'
  @login         = 'root'

  if cc = options.delete(:cloud_config)
    @cloud_config = CloudConfig.new cc
  else
    raise NoCloudConfigProvidedError.new 'No cloud config file provided.'
  end

  options.each_pair do |k,v|
    self.send("#{k}=", v)
  end
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



16
17
18
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 16

def actions
  @actions
end

#bootstrap_cmdObject

Returns the value of attribute bootstrap_cmd.



21
22
23
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 21

def bootstrap_cmd
  @bootstrap_cmd
end

#cloud_configObject

Returns the value of attribute cloud_config.



13
14
15
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 13

def cloud_config
  @cloud_config
end

#hostnameObject

Returns the value of attribute hostname.



17
18
19
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 17

def hostname
  @hostname
end

#ipObject

Returns the value of attribute ip.



10
11
12
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 10

def ip
  @ip
end

#loggerObject

Returns the value of attribute logger.



22
23
24
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 22

def logger
  @logger
end

#loginObject

Returns the value of attribute login.



11
12
13
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 11

def 
  @login
end

#passwordObject

Returns the value of attribute password.



12
13
14
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 12

def password
  @password
end

#post_install(options = {}) ⇒ Object

Returns the value of attribute post_install.



18
19
20
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 18

def post_install
  @post_install
end

#post_install_remote(options = {}) ⇒ Object

Returns the value of attribute post_install_remote.



19
20
21
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 19

def post_install_remote
  @post_install_remote
end

#public_keysObject

Returns the value of attribute public_keys.



20
21
22
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 20

def public_keys
  @public_keys
end

#rescue_osObject

Returns the value of attribute rescue_os.



14
15
16
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 14

def rescue_os
  @rescue_os
end

#rescue_os_bitObject

Returns the value of attribute rescue_os_bit.



15
16
17
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 15

def rescue_os_bit
  @rescue_os_bit
end

Instance Method Details

#default_log_formatterObject



232
233
234
235
236
237
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 232

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 42

def enable_rescue_mode(options = {})
  result = @api.enable_rescue! @ip, @rescue_os, @rescue_os_bit
  puts ">>> >>> >>> #{result.inspect}"

  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



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 119

def installimage(options = {})
  cloud_config = render_cloud_config

  remote do |ssh|
    ssh.exec! "echo \"#{cloud_config}\" > /tmp/cloud-config.yaml"
    ssh.exec! "wget https://raw.githubusercontent.com/coreos/init/master/bin/coreos-install -P /tmp"
    ssh.exec! "chmod a+x /tmp/coreos-install"
    logger.info "Remote executing: #{@bootstrap_cmd}"
    output = ssh.exec!(@bootstrap_cmd)
    logger.info output
  end
end

#local(&block) ⇒ Object



220
221
222
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 220

def local(&block)
  block.call
end

#port_open?(ip, port) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 80

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



132
133
134
135
136
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 132

def reboot(options = {})
  remote do |ssh|
    ssh.exec!("reboot")
  end
end

#remote(options = {}, &block) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 212

def remote(options = {}, &block)
  default = { :password => @password }
  default.merge! options
  Net::SSH.start(@ip, @login, default) do |ssh|
    block.call ssh
  end
end

#render_cloud_configObject



180
181
182
183
184
185
186
187
188
189
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 180

def render_cloud_config
  eruby = Erubis::Eruby.new @cloud_config.to_s

  params = {}
  params[:hostname] = @hostname
  params[:ip] = @ip
  params[:public_keys] = @public_keys

  return eruby.result(params)
end

#render_post_installObject



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 191

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

#reset(options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 64

def reset(options = {})
  result = @api.reset! @ip, :hw

  if result.success?
    reset_retries
  elsif @retries > 3
    logger.error "Resetting through web service 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_retriesObject



224
225
226
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 224

def reset_retries
  @retries = 0
end

#rolling_sleepObject



228
229
230
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 228

def rolling_sleep
  sleep @retries * @retries * 3 + 1 # => 1, 4, 13, 28, 49, 76, 109, 148, 193, 244, 301, 364 ... seconds
end

#update_local_known_hosts(options = {}) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 147

def update_local_known_hosts(options = {})
  remote do |ssh|
    puts ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
    `ssh-keygen -R #{@hostname}`
  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



203
204
205
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 203

def use_api(api_obj)
  @api = api_obj
end

#use_logger(logger_obj) ⇒ Object



207
208
209
210
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 207

def use_logger(logger_obj)
  @logger = logger_obj
  @logger.formatter = default_log_formatter
end

#verify_installation(options = {}) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 138

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 87

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/hetzner/bootstrap/coreos/target.rb', line 102

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