diff --git a/lib/vmpooler/aws_setup.rb b/lib/vmpooler/aws_setup.rb index 9e0d9c5..9b537f9 100644 --- a/lib/vmpooler/aws_setup.rb +++ b/lib/vmpooler/aws_setup.rb @@ -10,13 +10,18 @@ module Vmpooler ROOT_KEYS_SCRIPT = ENV['ROOT_KEYS_SCRIPT'] ROOT_KEYS_SYNC_CMD = "curl -k -o - -L #{ROOT_KEYS_SCRIPT} | %s" - def self.setup_node_by_ssh(host, platform) + def initialize(logger, new_vmname) + @logger = logger @key_file = ENV['AWS_KEY_FILE_LOCATION'] + @vm_name = new_vmname + end + + def setup_node_by_ssh(host, platform) conn = check_ssh_accepting_connections(host, platform) if conn - puts "#{host} connected" + @logger.log('s', "[>] [#{platform}] '#{@vm_name}' net:ssh connected") configure_host(host, platform, conn) - puts "#{host} configured" + @logger.log('s', "[>] [#{platform}] '#{@vm_name}' configured") end end @@ -34,7 +39,7 @@ module Vmpooler # # For an Ubuntu AMI, the user name is ubuntu. - def self.get_user(platform) + def get_user(platform) if platform =~ /centos/ 'centos' elsif platform =~ /ubuntu/ @@ -46,22 +51,22 @@ module Vmpooler end end - def self.check_ssh_accepting_connections(host, platform) + 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 - puts "#{host} Requested instances do not have sshd ready yet, try again for 300s (#{retries}): #{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 - puts "#{host} Jruby error, try again for 300s (#{retries}): #{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 - puts "#{host} Other error, cancelling aws_setup: #{e}" + @logger.log('s', "[>] [#{platform}] '#{@vm_name}' net:ssh other error, skipping aws_setup: #{e}") puts e.backtrace return nil end @@ -69,7 +74,7 @@ module Vmpooler # Configure the aws host by enabling root and setting the hostname # @param host [String] the internal dns name of the instance - def self.configure_host(host, platform, ssh) + 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}") @@ -81,7 +86,7 @@ module Vmpooler sync_root_keys(host, platform) end - def self.restart_sshd(host, platform, ssh) + 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 @@ -100,7 +105,7 @@ module Vmpooler ssh.loop end - def self.sync_root_keys(host, _platform) + def sync_root_keys(host, _platform) return if ROOT_KEYS_SCRIPT.nil? user = 'root' @@ -113,7 +118,7 @@ module Vmpooler # issue when using net ssh 6.1.0 with jruby # https://github.com/jruby/jruby-openssl/issues/105 # this will turn off some algos that match /^ecd(sa|h)-sha2/ - def self.netssh_jruby_workaround + 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 diff --git a/lib/vmpooler/providers/ec2.rb b/lib/vmpooler/providers/ec2.rb index 4a7c26c..b7b0721 100644 --- a/lib/vmpooler/providers/ec2.rb +++ b/lib/vmpooler/providers/ec2.rb @@ -31,6 +31,7 @@ module Vmpooler # The default connection pool timeout should be quite large - 60 seconds connpool_timeout = provider_config['connection_pool_timeout'].nil? ? 60 : provider_config['connection_pool_timeout'].to_i logger.log('d', "[#{name}] ConnPool - Creating a connection pool of size #{connpool_size} with timeout #{connpool_timeout}") + @logger = logger @connection_pool = Vmpooler::PoolManager::GenericConnectionPool.new( metrics: metrics, connpool_type: 'provider_connection_pool', @@ -223,10 +224,15 @@ module Vmpooler key: 'portfolio', value: 'ds-ci' } - ] } ] + if global_config[:config] && global_config[:config]['site_name'] + tag.first[:tags] << { + key: 'Name', + value: global_config[:config]['site_name'] + } + end config = { min_count: 1, max_count: 1, @@ -250,13 +256,14 @@ module Vmpooler created_instance = get_vm(pool_name, new_vmname) # extra setup steps - provision_node_aws(created_instance['private_dns_name'], pool_name) if to_provision(pool_name) == 'true' || to_provision(pool_name) == true + provision_node_aws(created_instance['private_dns_name'], pool_name, new_vmname) if to_provision(pool_name) == 'true' || to_provision(pool_name) == true created_instance end - def provision_node_aws(vm, pool_name) - AwsSetup.setup_node_by_ssh(vm, pool_name) + def provision_node_aws(vm, pool_name, new_vmname) + aws_setup = AwsSetup.new(@logger, new_vmname) + aws_setup.setup_node_by_ssh(vm, pool_name) end def get_block_device_mappings(image_id, volume_size) @@ -511,7 +518,7 @@ module Vmpooler def debug_logger(message, send_to_upstream: false) # the default logger is simple and does not enforce debug levels (the first argument) puts message if ENV['DEBUG_FLAG'] - logger.log('[g]', message) if send_to_upstream + @logger.log('[g]', message) if send_to_upstream end end end