Class: HetznerHost
- Inherits:
-
Object
- Object
- HetznerHost
- Includes:
- HetznerDocker
- Defined in:
- lib/hetzner-docker.rb,
lib/hetzner-docker/tools.rb,
lib/hetzner-docker/centos.rb,
lib/hetzner-docker/coreos.rb,
lib/hetzner-docker/ubuntu.rb,
lib/hetzner-docker/chefrun.rb,
lib/hetzner-docker/rescuemode.rb,
lib/hetzner-docker/chefbootstrap.rb
Constant Summary
Constants included from HetznerDocker
Instance Method Summary collapse
- #bootstrap_chef ⇒ Object
- #clear_known_key ⇒ Object
- #hostname ⇒ Object
-
#initialize(init) ⇒ HetznerHost
constructor
A new instance of HetznerHost.
- #install_centos ⇒ Object
- #install_coreos ⇒ Object
- #install_ubuntu ⇒ Object
- #put_key ⇒ Object
- #rescuemode ⇒ Object
- #run_chef ⇒ Object
- #test_ssh(port = 22) ⇒ Object
- #user ⇒ Object
- #wait_for_reboot ⇒ Object
Methods included from HetznerDocker
Constructor Details
#initialize(init) ⇒ HetznerHost
Returns a new instance of HetznerHost.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/hetzner-docker.rb', line 13 def initialize(init) init.each_pair do |key, val| instance_variable_set('@' + key.to_s, val) end unless defined?(@user) @user='root' end unless defined?(@hostname) and @hostname != nil @hostname="docker#{SecureRandom.hex(3)[email protected](".")[2]}" end unless defined?(@domain) and @domain != nil @domain="twiket.com" # this is our default, sorry end unless defined?(@url) and @url != nil @url="https://raw.githubusercontent.com/iMelnik/hetzner-docker/master/do-install-trusty.sh" end end |
Instance Method Details
#bootstrap_chef ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/hetzner-docker/chefbootstrap.rb', line 12 def bootstrap_chef kb = Chef::Knife::SoloPrepare.new kb.config[:ssh_user] = @user kb.name_args = [@ip] Chef::Config[:knife][:bootstrap_version] = @bootstrap_version kb.run end |
#clear_known_key ⇒ Object
17 18 19 |
# File 'lib/hetzner-docker/tools.rb', line 17 def clear_known_key system("ssh-keygen -q -R #{@ip} >/dev/null") end |
#hostname ⇒ Object
34 35 36 |
# File 'lib/hetzner-docker.rb', line 34 def hostname @hostname end |
#install_centos ⇒ Object
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 |
# File 'lib/hetzner-docker/centos.rb', line 4 def install_centos puts "Installing CentOS!" hetzner = Hetzner::API.new ENV['HETZNER_USER'], ENV['HETZNER_PASSWORD'] server = hetzner.server? @ip if server.parsed_response.has_key?("server") hostname = server.parsed_response["server"]["server_name"] puts "Resetting #{hostname} at #{@ip}" else raise server.parsed_response["error"]["message"] end hetzner.disable_rescue! @ip result = hetzner.boot_linux!(@ip,"CentOS 7.0 minimal", "64", "en" ) if result.parsed_response.has_key?("linux") @password = result.parsed_response["linux"]["password"] puts @password hetzner.reset! @ip, 'sw' wait_for_reboot put_key puts "Wait while CentOS is being installed" wait_for_reboot put_key puts "CentOS installed" else raise result.parsed_response["error"]["message"] end end |
#install_coreos ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/hetzner-docker/coreos.rb', line 6 def install_coreos puts "Goint to install coreos at #{@ip}" cloud_config=`cat cloud-config.yaml | sed s/EXTIP/#{@ip}/g | sed 's/HOSTNAME/#{hostname}/g'` Net::SSH.start(@ip, @user) do |ssh| #puts cloud_config ssh.exec "cat >/run/cloud-config.yaml <<EOF #cloud-config\n#{cloud_config} \nEOF" ssh.exec "wget --quiet https://raw.github.com/coreos/init/master/bin/coreos-install -O /root/coreos-install && /bin/bash /root/coreos-install -d /dev/sda -c /run/cloud-config.yaml && reboot" end clear_known_key wait_for_reboot puts "Fresh coreos installed at #{@ip}" end |
#install_ubuntu ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/hetzner-docker/ubuntu.rb', line 6 def install_ubuntu puts "Goint to install ubuntu at #{@ip}" Net::SSH.start(@ip, @user) do |ssh| ssh.scp.upload! "do-install-trusty.sh", "/root/do-install-trusty.sh" do |ch, name, sent, total| puts "#{name}: #{sent}/#{total}" end ssh.exec "/bin/bash /root/do-install-trusty.sh #{@hostname} #{@domain}" end clear_known_key wait_for_reboot put_key puts "Fresh ubuntu installed at #{@ip}" end |
#put_key ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/hetzner-docker/tools.rb', line 5 def put_key clear_known_key identity_file = File.(DEFAULT_IDENTITY_FILE) Net::SSH.start(@ip, @user, :password => @password) do |ssh| ssh.exec "test -d ~/.ssh || mkdir ~/.ssh" if @password ssh.scp.upload! identity_file, "/root/.ssh/authorized_keys" puts "ssh identity copied" end end end |
#rescuemode ⇒ Object
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 |
# File 'lib/hetzner-docker/rescuemode.rb', line 4 def rescuemode puts "to the rescue!" hetzner = Hetzner::API.new ENV['HETZNER_USER'], ENV['HETZNER_PASSWORD'] server = hetzner.server? @ip if server.parsed_response.has_key?("server") hostname = server.parsed_response["server"]["server_name"] puts "Resetting #{hostname} at #{@ip}" else raise server.parsed_response["error"]["message"] end hetzner.disable_rescue! @ip result = hetzner.enable_rescue! @ip if result.parsed_response.has_key?("rescue") @password = result.parsed_response["rescue"]["password"] puts @password hetzner.reset! @ip, 'hw' wait_for_reboot put_key else raise result.parsed_response["error"]["message"] end end |
#run_chef ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/hetzner-docker/chefrun.rb', line 18 def run_chef kb = Chef::Knife::SoloCook.new kb.config[:ssh_user] = @user kb.config[:override_runlist] = @cookbook kb.config[:librarian] = false kb.config[:host_key_verify] = false kb.name_args = [@ip] kb.run end |
#test_ssh(port = 22) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/hetzner-docker/tools.rb', line 28 def test_ssh(port = 22) socket = TCPSocket.new(@ip, port) IO.select([socket], nil, nil, 5) rescue SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH, IOError sleep 2 false rescue Errno::EPERM, Errno::ETIMEDOUT false ensure socket && socket.close end |
#user ⇒ Object
31 32 33 |
# File 'lib/hetzner-docker.rb', line 31 def user @user end |
#wait_for_reboot ⇒ Object
21 22 23 24 25 26 |
# File 'lib/hetzner-docker/tools.rb', line 21 def wait_for_reboot while test_ssh sleep 1 end puts "Waiting for #{@ip}..." until test_ssh end |