Sha256: 9da783026b395d5c9cbd1ca3caa710c026f90c4e888d768e88b24e133c4f2a71

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

require 'net/ssh'

module AmiSpec
  class WaitForRC
    def self.wait(ip_address, user, key, port=22)
      Net::SSH.start(ip_address, user, keys: [key], paranoid: false, port: port) do |ssh|
        distrib_stdout = ""
        # Determine the OS family
        ssh.exec!("source /etc/*release && echo -n $DISTRIB_ID && echo -n $ID") do |channel, stream, data|
          distrib_stdout << data if stream == :stdout
        end
        if distrib_stdout == "Ubuntu"
          codename_stdout = ""
          ssh.exec!("source /etc/*release && echo -n $DISTRIB_CODENAME") do |channel, stream, data|
            codename_stdout << data if stream == :stdout
          end
          if codename_stdout == "trusty"
            ssh.exec 'while /usr/sbin/service rc status | grep -q "^rc start/running, process"; do sleep 1; done'
          elsif codename_stdout == "xenial"
            ssh.exec 'while /usr/sbin/service rc status >/dev/null; do sleep 1; done'
          else
            puts "WARNING: Only Ubuntu trusty and xenial supported and we detected '#{codename_stdout}'. --wait-for-rc has no effect."
          end
        elsif distrib_stdout == "amzn"
          version_stdout = ""
          ssh.exec!("source /etc/*release && echo -n $VERSION_ID") do |channel, stream, data|
            version_stdout << data if stream == :stdout
          end
          if version_stdout =~ %r{201[0-9]{1}\.[0-9]+}
            ssh.exec 'while initctl status rc |grep -q "^rc start/running"; do sleep 1; done'
          else
            puts "WARNING: Only Amazon Linux 1 is supported and we detected '#{version_stdout}'. --wait-for-rc has no effect."
          end
        else
          puts "WARNING: Only Ubuntu and Amazon linux are supported and we detected '#{distrib_stdout}'. --wait-for-rc has no effect."
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ami_spec-1.2.0 lib/ami_spec/wait_for_rc.rb
ami_spec-1.1.0 lib/ami_spec/wait_for_rc.rb