Chef - How Reboot VM and continue performing actions

瘦欲@ 提交于 2019-12-18 06:56:39

问题


In a Chef recipe, I need to perform a reboot on a Node after doing some actions, and after the reboot is done, I need to continue doing another actions:

Recipe: -action 1 -action 2 -reboot -action3 -action4....

I have checked some existing cookbook in the community: reboot-handler, chef-reboot, chef-restart, chef-dominous, but I cannot make any of them work.

Is there some method in Chef to get what I need? Please provide detailed examples.

Thanks a lot for the help.


回答1:


How about using chef tags to implement a series of state transitions? This approach can be combined this with reboot-handler cookbook to manage the actual reboots.

Example

default.rb

#
# Action recipes
#
include_recipe "mycookbook::action1"
include_recipe "mycookbook::action2"

#
# State transitions
#
if tagged?("doAction1")

  untag("doAction1")
  tag("doAction2")

elsif tagged?("doAction2")

  untag("doAction2")

end    

action1.rb

if tagged?("doAction1")

  ..
  ..

end

action2.rb

include_recipe "reboot-handler"

if tagged?("doAction2")

  ..
  ..

  # Trigger reboot at end of chef run
  node.run_state['reboot'] = true
end



回答2:


This is a recipe of mine you can try.
# Cookbook Name:: reboot-test
# Recipe:: default

bash 'reboot' do
     cwd '/tmp'
      code <<-EOH
      touch reboot.lock
      chmod +x /etc/rc.d/rc.local
#     echo "/usr/local/bin/chef-client" >> /etc/rc.d/rc.local
      cp /etc/rc.d/rc.local /etc/rc.d/rc.local_bkp
      echo "/bin/chef-client" >> /etc/rc.d/rc.local
      reboot
      EOH
      ignore_failure true
      not_if do ::File.exists?('/tmp/reboot.lock') end
      not_if 'ls -lrth /tmp/reboot.lock'
     end

#This is where you need to code for the config automation like the execute #resource but before the bash resource "CHEF-CLIENT-#EXIT"

execute 'touch' do
     command '/bin/touch /tmp/suman.test'
     end

bash 'chef_client_exit' do
      code <<-EOH
      rm -rf /tmp/reboot.lock
#     sed -i '/\/usr\/local\/bin\/chef-client/d' /etc/rc.d/rc.local
#     sed -e '/^\/bin\/chef-client/d' -i /etc/rc.d/rc.local
#     sed -e '\|^/bin/chef-client|d' -i /etc/rc.d/rc.local
      cp /etc/rc.d/rc.local_bkp /etc/rc.d/rc.local
      rm -rf /etc/rc.d/rc.local_bkp
      EOH
      only_if 'grep -i chef-client /etc/rc.d/rc.local'
     end



回答3:


I have done it using FLAG file method, example given below. Example of domain join-

Blockquote

---------------------------Script Start------------------------

    powershell_script 'Domain_Join' do
    guard_interpreter :powershell_script
    code -domainJoin
    $currentTime = Get-Date
    $currentTimeString = $currentTime.ToString()

  $systemDomain = (Get-WmiObject Win32_ComputerSystem).Domain
  If (($systemDomain) -eq 'domainName')
  { 
    Try {
  write-host "$env:computerName is DOMAIN MEMBER"
  Remove-Item C:\\DomainJoinFlag.txt -ErrorAction Stop
  Unregister-ScheduledTask -TaskName "Chef client schedule_DJ" -Confirm:$false
  } # end of Try
          Catch [System.Management.Automation.ItemNotFoundException]
            { write-host "Server is already domain member, Or Exception raised due to either missing FLAG file or Server startup schedule task configuration." 
              eventcreate /t INFORMATION /ID 0909 /L APPLICATION /SO "ChefClient_$env:computerName" /D "Server is already domain member, Or Exception raised due to either missing FLAG file or Server startup schedule task configuration. Refer to the CHEF reciepie for DomainJoin or check Administrative credentials for creting schedule task"}
            }
    else { write-host "$env:computerName is NOT domain member, joining the server to the domain. Server will be rebooting in a while..."
            eventcreate /t INFORMATION /ID 0909 /L APPLICATION /SO "ChefClient_$env:computerName" /D "Joining the server : $env:ComputerName to the domain ININLAB.COM (Server Time): $currentTimeString"
            New-Item C:\\DomainJoinFlag.txt -type file -force
            write-host "$env:computerName DOMAIN JOIN INITIATED for the server"
            $cred = New-Object System.Management.Automation.PsCredential("domain\\domain_user", (ConvertTo-SecureString "Password" -AsPlainText -Force))
            Add-Computer -DomainName "domainName" -Credential $cred -OUPath "OU=HyperV,OU=Infrastructure,OU=Servers,DC=domain,DC=name"
            shutdown -r -t 120
            } #end_of_else
            domainJoin
  notifies :run, 'windows_task[Chef client schedule_DJ]', :immediately
end

windows_task 'Chef client schedule_DJ' do
  user 'SYSTEM'
  command 'chef-client -L C:\chef\chef-client_after_reboot_domainJoin.log'
  run_level :highest
  frequency :onstart
  frequency_modifier 30
  action :create
  only_if { ::File.exist?('C:\\DomainJoinFlag.txt') }
end

---------------------------Script Ends------------------------


来源:https://stackoverflow.com/questions/24497840/chef-how-reboot-vm-and-continue-performing-actions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!