Method: Beaker::HostPrebuiltSteps#timesync

Defined in:
lib/beaker/host_prebuilt_steps.rb

#timesync(host, opts) ⇒ Object

Run timesync on the provided hosts

Parameters:

  • host (Host, Array<Host>)

    One or more hosts to act upon

  • opts (Hash{Symbol=>String})

    Options to alter execution.

Options Hash (opts):



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/beaker/host_prebuilt_steps.rb', line 40

def timesync host, opts
  logger = opts[:logger]
  ntp_server = opts[:ntp_server] ? opts[:ntp_server] : NTPSERVER
  block_on host do |host|
    logger.notify "Update system time sync for '#{host.name}'"
    if host['platform'].include? 'windows'
      # The exit code of 5 is for Windows 2008 systems where the w32tm /register command
      # is not actually necessary.
      host.exec(Command.new("w32tm /register"), :acceptable_exit_codes => [0,5])
      host.exec(Command.new("net start w32time"), :acceptable_exit_codes => [0,2])
      host.exec(Command.new("w32tm /config /manualpeerlist:#{ntp_server} /syncfromflags:manual /update"))
      host.exec(Command.new("w32tm /resync"))
      logger.notify "NTP date succeeded on #{host}"
    else
      case
      when host['platform'] =~ /el-8/
        ntp_command = "chronyc add server #{ntp_server} prefer trust;chronyc makestep;chronyc burst 1/2"
      when host['platform'] =~ /sles-/
        ntp_command = "sntp #{ntp_server}"
      when host['platform'] =~ /cisco_nexus/
        ntp_server = host.exec(Command.new("getent hosts #{NTPSERVER} | head -n1 |cut -d \" \" -f1"), :acceptable_exit_codes => [0]).stdout
        ntp_command = "sudo -E sh -c 'export DCOS_CONTEXT=2;/isan/bin/ntpdate -u -t 20 #{ntp_server}'"
      else
        ntp_command = "ntpdate -u -t 20 #{ntp_server}"
      end
      success=false
      try = 0
      until try >= TRIES do
        try += 1
        if host.exec(Command.new(ntp_command), :accept_all_exit_codes => true).exit_code == 0
          success=true
          break
        end
        sleep SLEEPWAIT
      end
      if success
        logger.notify "NTP date succeeded on #{host} after #{try} tries"
      else
        raise "NTP date was not successful after #{try} tries"
      end
    end
  end
  nil
rescue => e
  report_and_raise(logger, e, "timesync (--ntp)")
end