Skip to content

Instantly share code, notes, and snippets.

@jugatsu
Last active September 1, 2017 18:09
Show Gist options
  • Save jugatsu/e10de648bdf6a08fe33f6c1d53141569 to your computer and use it in GitHub Desktop.
Save jugatsu/e10de648bdf6a08fe33f6c1d53141569 to your computer and use it in GitHub Desktop.
ak@ak-pc:~$ tree code/chef/cookbooks/gf_chef_client/
code/chef/cookbooks/gf_chef_client/
├── attributes
│   └── default.rb
├── Berksfile
├── Berksfile.lock
├── chefignore
├── metadata.rb
├── recipes
│   ├── config.rb
│   ├── default.rb
│   └── windows.rb
└── templates
    └── client.rb.erb

3 directories, 9 files

ak@ak-pc:~$ cat code/chef/cookbooks/gf_chef_client/recipes/default.rb
#
# Cookbook:: gf_chef_client
# Recipe:: default
#
# Copyright:: 2017, The Authors, All Rights Reserved.

if platform_family?('windows')
  include_recipe 'gf_chef_client::config'
  include_recipe 'gf_chef_client::windows'
  include_recipe 'chef-client::delete_validation' unless ENV['TEST_KITCHEN']
else
  Chef::Log.warn('`gf_chef_client` cookbook only supports Windows platform.')
end

ak@ak-pc:~$ cat code/chef/cookbooks/gf_chef_client/recipes/config.rb
#
# Cookbook:: gf_chef_client
# Recipe:: config
#
# Copyright:: 2017, The Authors, All Rights Reserved.

%w(run_path cache_path backup_path log_dir conf_dir).each do |dir|
  directory node['chef_client'][dir] do
    recursive true
  end
end

template "#{node['chef_client']['conf_dir']}\\client.rb" do
  source 'client.rb.erb'
  variables(
    chef_config: node['chef_client']['config']
  )
  action :create
end

ak@ak-pc:~$ cat code/chef/cookbooks/gf_chef_client/recipes/windows.rb
#
# Cookbook:: gf_chef_client
# Recipe:: windows
#
# Copyright:: 2017, The Authors, All Rights Reserved.

include_recipe 'chef-client::task'

# TODO: remove logging to file
client_cmd = "C:/opscode/chef/bin/chef-client"
client_cmd << " -L #{File.join(node['chef_client']['log_dir'], 'client-on-start.log')}"
client_cmd << " -c #{File.join(node['chef_client']['conf_dir'], 'client.rb')}"
# Add custom options
client_cmd << " #{node['chef_client']['daemon_options'].join(' ')}" if node['chef_client']['daemon_options'].any?

windows_task 'chef-client-on-start' do
  user node['chef_client']['task']['user']
  command "cmd /c \"#{client_cmd}\""
  run_level :highest
  frequency :onstart
end

file 'Ensure reboot stamp is created' do
  path 'C:\\.reboot.stamp'
  action :create
  notifies :run, 'execute[Ensure attribute is set to hidden]', :immediate
end

execute 'Ensure attribute is set to hidden' do
  command 'attrib +H C:\\.reboot.stamp'
  notifies :reboot_now, 'reboot[Ensure node is rebooted after chef-client cookbook run]', :immediate
  action :nothing
end

reboot 'Ensure node is rebooted after chef-client cookbook run' do
  action :nothing
end

ak@ak-pc:~$ cat code/chef/cookbooks/gf_chef_client/attributes/default.rb
case node['platform_version'].split('.')[0..1].join('.')
when '6.1'
  default['chef_client']['task']['user']                = 'система'
  default['chef_client']['task']['frequency']           = 'daily'
  default['chef_client']['task']['frequency_modifier']  = 1
  default['chef_client']['task']['start_time']          = '11:00'
  default['chef_client']['conf_dir']                    = 'D:\\chef'
  default['chef_client']['daemon_options']              = ["-k #{node['chef_client']['conf_dir']}\\client.pem"]
else
  default['chef_client']['conf_dir']                    = 'C:\\chef'
  default['chef_client']['task']['user']                = 'СИСТЕМА'
end

default['chef_client']['task']['password']              = 'N/A'
default['chef_client']['run_path']                      = "#{node['chef_client']['conf_dir']}\\run"
default['chef_client']['cache_path']                    = "#{node['chef_client']['conf_dir']}\\cache"
default['chef_client']['backup_path']                   = "#{node['chef_client']['conf_dir']}\\backup"
default['chef_client']['log_dir']                       = "#{node['chef_client']['conf_dir']}\\log"
default['chef_client']['config']                        = {
  'chef_server_url' => Chef::Config[:chef_server_url],
  'validation_client_name' => Chef::Config[:validation_client_name],
  'node_name' => Chef::Config[:node_name] == node['fqdn'] ? false : Chef::Config[:node_name],
  'verify_api_cert' => true,
  'file_cache_path' => "#{node['chef_client']['conf_dir']}\\cache",
  'file_backup_path' => "#{node['chef_client']['conf_dir']}\\backup",
  'log_location' => ':win_evt',
}

ak@ak-pc:~$ cat code/chef/cookbooks/gf_chef_client/templates/client.rb.erb
<% @chef_config.keys.sort.each do |option| -%>
  <% next if %w{ node_name exception_handlers report_handlers start_handlers http_proxy https_proxy no_proxy }.include?(option) -%>
  <% case option -%>
  <% when 'log_level', 'ssl_verify_mode', 'audit_mode' -%>
<%= option %> <%= @chef_config[option].gsub(/^:/, '').to_sym.inspect %>
  <% when 'log_location' -%>
  <%   if @chef_config[option].include? ":win_evt" -%>
<%= option %> <%= @chef_config[option].gsub(/^:/, '').to_sym.inspect %>
  <%   elsif @chef_config[option].instance_of? String -%>
<%= option %> <%= @chef_config[option] == 'STDOUT' ? 'STDOUT' : @chef_config[option].inspect %>
  <%   else -%>
<%= option %> <%= @chef_config[option] -%>
  <%   end -%>
  <% else -%>
<%= option %> <%= @chef_config[option].inspect %>
  <% end -%>
<% end -%>
<% if @chef_config['node_name'] -%>
node_name <%= @chef_config['node_name'].inspect %>
<% else -%>
# Using default node name (fqdn)
<% end -%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment