Class: Vmpooler::PoolManager::AwsSetup

Inherits:
Object
  • Object
show all
Defined in:
lib/vmpooler/aws_setup.rb

Overview

This class connects to existing running VMs via NET:SSH it uses a local key to do so and then setup SSHD on the hosts to enable dev and CI users to connect.

Constant Summary collapse

ROOT_KEYS_SCRIPT =
ENV['ROOT_KEYS_SCRIPT']
ROOT_KEYS_SYNC_CMD =
"curl -k -o - -L #{ROOT_KEYS_SCRIPT} | %s"

Instance Method Summary collapse

Constructor Details

#initialize(logger, new_vmname) ⇒ AwsSetup

Returns a new instance of AwsSetup.



13
14
15
16
17
# File 'lib/vmpooler/aws_setup.rb', line 13

def initialize(logger, new_vmname)
  @logger = logger
  @key_file = ENV['AWS_KEY_FILE_LOCATION']
  @vm_name = new_vmname
end

Instance Method Details

#check_ssh_accepting_connections(host, platform) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/vmpooler/aws_setup.rb', line 54

def check_ssh_accepting_connections(host, platform)
  retries = 0
  begin
    user = get_user(platform)
    netssh_jruby_workaround
    Net::SSH.start(host, user, keys: @key_file, timeout: 10)
  rescue Net::SSH::ConnectionTimeout, Errno::ECONNREFUSED => e
    @logger.log('s', "[>] [#{platform}] '#{@vm_name}' net:ssh requested instances do not have sshd ready yet, try again for 300s (#{retries}/300): #{e}")
    sleep 1
    retry if (retries += 1) < 300
  rescue Errno::EBADF => e
    @logger.log('s', "[>] [#{platform}] '#{@vm_name}' net:ssh jruby error, try again for 300s (#{retries}/30): #{e}")
    sleep 10
    retry if (retries += 1) < 30
  rescue StandardError => e
    @logger.log('s', "[>] [#{platform}] '#{@vm_name}' net:ssh other error, skipping aws_setup: #{e}")
    puts e.backtrace
  end
end

#configure_host(host, platform, ssh) ⇒ Object

Configure the aws host by enabling root and setting the hostname

Parameters:

  • host (String)

    the internal dns name of the instance



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vmpooler/aws_setup.rb', line 76

def configure_host(host, platform, ssh)
  ssh.exec!('sudo cp -r .ssh /root/.')
  ssh.exec!("sudo sed -ri 's/^#?PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config")
  ssh.exec!("sudo hostname #{host}")
  if platform =~ /amazon/
    # Amazon Linux requires this to preserve host name changes across reboots.
    ssh.exec!("sudo sed -ie '/^HOSTNAME/ s/=.*/=#{host}/' /etc/sysconfig/network")
  end
  restart_sshd(host, platform, ssh)
  sync_root_keys(host, platform)
end

#get_user(platform) ⇒ Object

For an Amazon Linux AMI, the user name is ec2-user.

For a Centos AMI, the user name is centos.

For a Debian AMI, the user name is admin or root.

For a Fedora AMI, the user name is ec2-user or fedora.

For a RHEL AMI, the user name is ec2-user or root.

For a SUSE AMI, the user name is ec2-user or root.

For an Ubuntu AMI, the user name is ubuntu.


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vmpooler/aws_setup.rb', line 42

def get_user(platform)
  if platform =~ /centos/
    'centos'
  elsif platform =~ /ubuntu/
    'ubuntu'
  elsif platform =~ /debian/
    'root'
  else
    'ec2-user'
  end
end

#netssh_jruby_workaroundObject

issue when using net ssh 6.1.0 with jruby github.com/jruby/jruby-openssl/issues/105 this will turn off some algos that match /^ecd(sa|h)-sha2/



120
121
122
123
# File 'lib/vmpooler/aws_setup.rb', line 120

def netssh_jruby_workaround
  Net::SSH::Transport::Algorithms::ALGORITHMS.each_value { |algs| algs.reject! { |a| a =~ /^ecd(sa|h)-sha2/ } }
  Net::SSH::KnownHosts::SUPPORTED_TYPE.reject! { |t| t =~ /^ecd(sa|h)-sha2/ }
end

#restart_sshd(host, platform, ssh) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/vmpooler/aws_setup.rb', line 88

def restart_sshd(host, platform, ssh)
  ssh.open_channel do |channel|
    channel.request_pty do |ch, success|
      raise "can't get pty request" unless success

      if platform =~ /centos|el-|redhat|fedora|eos|amazon/
        ch.exec('sudo -E /sbin/service sshd reload')
      elsif platform =~ /debian|ubuntu|cumulus/
        ch.exec('sudo su -c \"service sshd restart\"')
      elsif platform =~ /arch|centos-7|el-7|redhat-7|fedora-(1[4-9]|2[0-9])/
        ch.exec('sudo -E systemctl restart sshd.service')
      else
        services.logger.error("Attempting to update ssh on non-supported platform: #{host}: #{platform}")
      end
    end
  end
  ssh.loop
end

#setup_node_by_ssh(host, platform) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/vmpooler/aws_setup.rb', line 19

def setup_node_by_ssh(host, platform)
  conn = check_ssh_accepting_connections(host, platform)
  return unless conn

  @logger.log('s', "[>] [#{platform}] '#{@vm_name}' net:ssh connected")
  configure_host(host, platform, conn)
  @logger.log('s', "[>] [#{platform}] '#{@vm_name}' configured")
end

#sync_root_keys(host, _platform) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/vmpooler/aws_setup.rb', line 107

def sync_root_keys(host, _platform)
  return if ROOT_KEYS_SCRIPT.nil?

  user = 'root'
  netssh_jruby_workaround
  Net::SSH.start(host, user, keys: @key_file) do |ssh|
    ssh.exec!(ROOT_KEYS_SYNC_CMD % 'env PATH="/usr/gnu/bin:$PATH" bash')
  end
end