From Sloppy Mosquito, 10 Years ago, written in Plain Text.
Embed
  1. #
  2. # Cookbook Name:: ntp
  3. # Recipe:: default
  4. # Author:: Joshua Timberman (<joshua@opscode.com>)
  5. # Author:: Tim Smith (<tsmith@limelight.com>)
  6. #
  7. # Copyright 2009-2013, Opscode, Inc
  8. #
  9. # Licensed under the Apache License, Version 2.0 (the "License");
  10. # you may not use this file except in compliance with the License.
  11. # You may obtain a copy of the License at
  12. #
  13. #     http://www.apache.org/licenses/LICENSE-2.0
  14. #
  15. # Unless required by applicable law or agreed to in writing, software
  16. # distributed under the License is distributed on an "AS IS" BASIS,
  17. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. # See the License for the specific language governing permissions and
  19. # limitations under the License.
  20.  
  21. ::Chef::Resource.send(:include, Opscode::Ntp::Helper)
  22.  
  23. if platform_family?('windows')
  24.   include_recipe 'ntp::windows_client'
  25. else
  26.  
  27.   node['ntp']['packages'].each do |ntppkg|
  28.     package ntppkg
  29.   end
  30.  
  31.   [node['ntp']['varlibdir'], node['ntp']['statsdir']].each do |ntpdir|
  32.     directory ntpdir do
  33.       owner node['ntp']['var_owner']
  34.       group node['ntp']['var_group']
  35.       mode  '0755'
  36.     end
  37.   end
  38.  
  39.   cookbook_file node['ntp']['leapfile'] do
  40.     owner node['ntp']['conf_owner']
  41.     group node['ntp']['conf_group']
  42.     mode  '0644'
  43.     source 'ntp.leapseconds'
  44.     notifies :restart, "service[#{node['ntp']['service']}]"
  45.   end
  46.  
  47.   include_recipe 'ntp::apparmor' if node['ntp']['apparmor_enabled']
  48. end
  49.  
  50. unless node['ntp']['servers'].size > 0
  51.   node.default['ntp']['servers'] = [
  52.     '0.pool.ntp.org',
  53.     '1.pool.ntp.org',
  54.     '2.pool.ntp.org',
  55.     '3.pool.ntp.org'
  56.   ]
  57.   Chef::Log.debug 'No NTP servers specified, using default ntp.org server pools'
  58. end
  59.  
  60. if node['ntp']['listen'].nil? && !node['ntp']['listen_network'].nil?
  61.   if node['ntp']['listen_network'] == 'primary'
  62.     node.set['ntp']['listen'] = node['ipaddress']
  63.   else
  64.     require 'ipaddr'
  65.     net = IPAddr.new(node['ntp']['listen_network'])
  66.  
  67.     node['network']['interfaces'].each do |iface, addrs|
  68.       addrs['addresses'].each do |ip, params|
  69.         addr = IPAddr.new(ip) if params['family'].eql?('inet') || params['family'].eql?('inet6')
  70.         node.set['ntp']['listen'] = addr if net.include?(addr)
  71.       end
  72.     end
  73.   end
  74. end
  75.  
  76. node.default['ntp']['tinker']['panic'] = 0 if node['virtualization'] &&
  77.                                               node['virtualization']['role'] == 'guest' &&
  78.                                               node['ntp']['disable_tinker_panic_on_virtualization_guest']
  79.  
  80. template node['ntp']['conffile'] do
  81.   source   'ntp.conf.erb'
  82.   owner    node['ntp']['conf_owner']
  83.   group    node['ntp']['conf_group']
  84.   mode     '0644'
  85.   notifies :restart, "service[#{node['ntp']['service']}]" unless node['ntp']['conf_restart_immediate']
  86.   notifies :restart, "service[#{node['ntp']['service']}]", :immediately if node['ntp']['conf_restart_immediate']
  87.   variables(
  88.     lazy { { :ntpd_supports_native_leapfiles => ntpd_supports_native_leapfiles } }
  89.   )
  90. end
  91.  
  92. if node['ntp']['sync_clock']
  93.   execute "Stop #{node['ntp']['service']} in preparation for ntpdate" do
  94.     command '/bin/true'
  95.     action :run
  96.     notifies :stop, "service[#{node['ntp']['service']}]", :immediately
  97.   end
  98.  
  99.   execute 'Force sync system clock with ntp server' do
  100.     command 'ntpd -q'
  101.     action :run
  102.     notifies :start, "service[#{node['ntp']['service']}]"
  103.   end
  104. end
  105.  
  106. execute 'Force sync hardware clock with system clock' do
  107.   command 'hwclock --systohc'
  108.   action :run
  109.   only_if { node['ntp']['sync_hw_clock'] && !platform_family?('windows') }
  110. end
  111.  
  112. service node['ntp']['service'] do
  113.   supports :status => true, :restart => true
  114.   action   [:enable, :start]
  115. end