Chef handler to reboot windows machine


#1

Hi All,

I have a use case where I need to reboot windows machine after some package
installation.

would like to know the possible best practice to implement this use case;

a) Make a single recipe that will do the package installation and product
installation and then at last call chef handler to reboot the machine

a) make two different chef recipe, the first recipe will install packages
and then reboot the machine,
the second recipe will do the remaining stuff. I don’t know this
will work or not.
Is there a way, we can return the chef client to run the same
cookbook after reboot??

I also gone through some documentation but nothing is useful.

Any help would the useful .

Thanks & Regards,
Sachin Kumar


#2

Sachin,

make sure that the 'windows::reboot_handler 'is at the top of your run list
as well as putting ('depends ‘windows’) in your metadata.rb.

The install what you want and Chef will reboot and continue.

here is an example of installing RDSH for preparation for Citrix XenApp
install. It will starts the RDSH, reboot and then come back and do the
desktop experience, and so on… As long as you installed the chef service
during client install or created a scheduled task.

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

if you still have issues, create a library file called reboot_pending.rb in
you cookbook - I was given this by Alex Vinyar from Chef. This was added to
Chef Client back in version 11.12…But I still use it

reboot_pending.rb

class Chef
class Resource
# include Chef::Mixin::ShellOut

def reboot_pending?
  # Any files listed here means reboot needed
  (Registry.key_exists?('HKLM\SYSTEM\CurrentControlSet\Control\Session

Manager\PendingFileRenameOperations’) &&
Registry.get_value(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager’,‘PendingFileRenameOperations’).any?) ||
# 1 for any value means reboot pending
# “9306cdfc-c4a1-4a22-9996-848cb67eddc3”=1

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’) &&

Registry.get_values(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’).select{|v| v[2] == 1 }.any?) ||
# 1 or 2 for ‘Flags’ value means reboot pending

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’)
&&

[1,2].include?(Registry::get_value(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’,‘Flags’)))
||
# added by Alex

Registry.key_exists?(‘HKLM\Software\Microsoft\Windows\CurrentVersion\Component
Based Servicing\RebootPending’)
end

end
end

On Mon, Nov 17, 2014 at 8:30 AM, Sachin Gupta guptasachin1112mrt@gmail.com
wrote:

Hi All,

I have a use case where I need to reboot windows machine after some
package installation.

would like to know the possible best practice to implement this use case;

a) Make a single recipe that will do the package installation and product
installation and then at last call chef handler to reboot the machine

a) make two different chef recipe, the first recipe will install
packages and then reboot the machine,
the second recipe will do the remaining stuff. I don’t know this
will work or not.
Is there a way, we can return the chef client to run the same
cookbook after reboot??

I also gone through some documentation but nothing is useful.

Any help would the useful .

Thanks & Regards,
Sachin Kumar

Todd Pigram


www.linkedin.com/in/toddpigram/
@pigram86 on twitter

Mobile - 216-224-5769


#3

Also, Chef 12 adds a new resource that moves the cookbook-based reboot
resource into core Chef:

http://docs.getchef.com/resource_reboot.html

On Mon, Nov 17, 2014 at 7:20 AM, Todd Pigram todd@toddpigram.com wrote:

Sachin,

make sure that the 'windows::reboot_handler 'is at the top of your run
list as well as putting ('depends ‘windows’) in your metadata.rb.

The install what you want and Chef will reboot and continue.

here is an example of installing RDSH for preparation for Citrix XenApp
install. It will starts the RDSH, reboot and then come back and do the
desktop experience, and so on… As long as you installed the chef service
during client install or created a scheduled task.

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

if you still have issues, create a library file called reboot_pending.rb
in you cookbook - I was given this by Alex Vinyar from Chef. This was added
to Chef Client back in version 11.12…But I still use it

reboot_pending.rb

class Chef
class Resource
# include Chef::Mixin::ShellOut

def reboot_pending?
  # Any files listed here means reboot needed
  (Registry.key_exists?('HKLM\SYSTEM\CurrentControlSet\Control\Session

Manager\PendingFileRenameOperations’) &&
Registry.get_value(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager’,‘PendingFileRenameOperations’).any?) ||
# 1 for any value means reboot pending
# “9306cdfc-c4a1-4a22-9996-848cb67eddc3”=1

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’) &&

Registry.get_values(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’).select{|v| v[2] == 1 }.any?) ||
# 1 or 2 for ‘Flags’ value means reboot pending

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’)
&&

[1,2].include?(Registry::get_value(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’,‘Flags’)))
||
# added by Alex

Registry.key_exists?(‘HKLM\Software\Microsoft\Windows\CurrentVersion\Component
Based Servicing\RebootPending’)
end

end
end

On Mon, Nov 17, 2014 at 8:30 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Hi All,

I have a use case where I need to reboot windows machine after some
package installation.

would like to know the possible best practice to implement this use case;

a) Make a single recipe that will do the package installation and
product installation and then at last call chef handler to reboot the
machine

a) make two different chef recipe, the first recipe will install
packages and then reboot the machine,
the second recipe will do the remaining stuff. I don’t know this
will work or not.
Is there a way, we can return the chef client to run the same
cookbook after reboot??

I also gone through some documentation but nothing is useful.

Any help would the useful .

Thanks & Regards,
Sachin Kumar

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769


#4

James,

That is awesome!!!

On Mon, Nov 17, 2014 at 11:46 AM, James Scott jamescott@opscode.com wrote:

Also, Chef 12 adds a new resource that moves the cookbook-based reboot
resource into core Chef:

http://docs.getchef.com/resource_reboot.html

On Mon, Nov 17, 2014 at 7:20 AM, Todd Pigram todd@toddpigram.com wrote:

Sachin,

make sure that the 'windows::reboot_handler 'is at the top of your run
list as well as putting ('depends ‘windows’) in your metadata.rb.

The install what you want and Chef will reboot and continue.

here is an example of installing RDSH for preparation for Citrix XenApp
install. It will starts the RDSH, reboot and then come back and do the
desktop experience, and so on… As long as you installed the chef service
during client install or created a scheduled task.

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

if you still have issues, create a library file called reboot_pending.rb
in you cookbook - I was given this by Alex Vinyar from Chef. This was added
to Chef Client back in version 11.12…But I still use it

reboot_pending.rb

class Chef
class Resource
# include Chef::Mixin::ShellOut

def reboot_pending?
  # Any files listed here means reboot needed

(Registry.key_exists?(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager\PendingFileRenameOperations’) &&
Registry.get_value(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager’,‘PendingFileRenameOperations’).any?) ||
# 1 for any value means reboot pending
# “9306cdfc-c4a1-4a22-9996-848cb67eddc3”=1

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’) &&

Registry.get_values(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’).select{|v| v[2] == 1 }.any?) ||
# 1 or 2 for ‘Flags’ value means reboot pending

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’)
&&

[1,2].include?(Registry::get_value(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’,‘Flags’)))
||
# added by Alex

Registry.key_exists?(‘HKLM\Software\Microsoft\Windows\CurrentVersion\Component
Based Servicing\RebootPending’)
end

end
end

On Mon, Nov 17, 2014 at 8:30 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Hi All,

I have a use case where I need to reboot windows machine after some
package installation.

would like to know the possible best practice to implement this use case;

a) Make a single recipe that will do the package installation and
product installation and then at last call chef handler to reboot the
machine

a) make two different chef recipe, the first recipe will install
packages and then reboot the machine,
the second recipe will do the remaining stuff. I don’t know this
will work or not.
Is there a way, we can return the chef client to run the same
cookbook after reboot??

I also gone through some documentation but nothing is useful.

Any help would the useful .

Thanks & Regards,
Sachin Kumar

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram


www.linkedin.com/in/toddpigram/
@pigram86 on twitter

Mobile - 216-224-5769


#5

Thanks James/Todd for sharing this info.

Do you know when chef 12 will be released??

In the meantime, I also tried to add the following code for reboot at the
end of Apache installation.

windows_reboot 60 do
reason "Installed a new Apache package"
action:request
end

At the top of the Apache recipe, I had also added the line ( include_recipe
’windows::reboot_handler’ and 'include_recipe ‘chef_handler::default’) and
also added (depends “windows” and chef_handler)in apache metadata.rb.

I am able to get the reboot message and system gets rebooted after a period
of 60 sec. From this use case it is clear that reboot is done only at the
end of the chef client.

Now if I want to do a reboot at the middle of execution, then will this
approach work??

a) will break the recipe into two parts, first part will installed the
required package and second part will do the remaining stuff.

Now the biggest challenge is how to start the chef client again after
reboot.
I have to make a reboot.txt file before the reboot in first recipe and
after that system will be rebooted. during the startup script if I pass
the chef-solo command to run the chef client with the second recipe and in
that recipe it will check whether the reboot.txt file is present or not. if
reboot.txt is present then only second recipe will move forward otherwise
it will not do anything and exit.

James/Todd Is this making any sense and would like to know whether I am
moving on a correct path??

Thanks & Regards,
Sachin Kumar

On Mon, Nov 17, 2014 at 11:11 PM, Todd Pigram todd@toddpigram.com wrote:

James,

That is awesome!!!

On Mon, Nov 17, 2014 at 11:46 AM, James Scott jamescott@opscode.com
wrote:

Also, Chef 12 adds a new resource that moves the cookbook-based reboot
resource into core Chef:

http://docs.getchef.com/resource_reboot.html

On Mon, Nov 17, 2014 at 7:20 AM, Todd Pigram todd@toddpigram.com wrote:

Sachin,

make sure that the 'windows::reboot_handler 'is at the top of your run
list as well as putting ('depends ‘windows’) in your metadata.rb.

The install what you want and Chef will reboot and continue.

here is an example of installing RDSH for preparation for Citrix XenApp
install. It will starts the RDSH, reboot and then come back and do the
desktop experience, and so on… As long as you installed the chef service
during client install or created a scheduled task.

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

if you still have issues, create a library file called reboot_pending.rb
in you cookbook - I was given this by Alex Vinyar from Chef. This was added
to Chef Client back in version 11.12…But I still use it

reboot_pending.rb

class Chef
class Resource
# include Chef::Mixin::ShellOut

def reboot_pending?
  # Any files listed here means reboot needed

(Registry.key_exists?(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager\PendingFileRenameOperations’) &&

Registry.get_value(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager’,‘PendingFileRenameOperations’).any?) ||
# 1 for any value means reboot pending
# “9306cdfc-c4a1-4a22-9996-848cb67eddc3”=1

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’) &&

Registry.get_values(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’).select{|v| v[2] == 1 }.any?) ||
# 1 or 2 for ‘Flags’ value means reboot pending

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’)
&&

[1,2].include?(Registry::get_value(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’,‘Flags’)))
||
# added by Alex

Registry.key_exists?(‘HKLM\Software\Microsoft\Windows\CurrentVersion\Component
Based Servicing\RebootPending’)
end

end
end

On Mon, Nov 17, 2014 at 8:30 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Hi All,

I have a use case where I need to reboot windows machine after some
package installation.

would like to know the possible best practice to implement this use
case;

a) Make a single recipe that will do the package installation and
product installation and then at last call chef handler to reboot the
machine

a) make two different chef recipe, the first recipe will install
packages and then reboot the machine,
the second recipe will do the remaining stuff. I don’t know this
will work or not.
Is there a way, we can return the chef client to run the same
cookbook after reboot??

I also gone through some documentation but nothing is useful.

Any help would the useful .

Thanks & Regards,
Sachin Kumar

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769


#6

Hi Sachin,

I build my AMI’s using Chef and I have also split them into two
parts(before and after reboot). Before boot adds a windows task to run
Chef on the next boot:
https://github.com/opscode-cookbooks/windows#windows_task . It needs to
run as SYSTEM without a password to work without login(IIRC). After boot
recipe removes the task.

Cheers,
-Greg

On Tue, Nov 25, 2014 at 2:03 AM, Sachin Gupta guptasachin1112mrt@gmail.com
wrote:

Thanks James/Todd for sharing this info.

Do you know when chef 12 will be released??

In the meantime, I also tried to add the following code for reboot at the
end of Apache installation.

windows_reboot 60 do
reason "Installed a new Apache package"
action:request
end

At the top of the Apache recipe, I had also added the line (
include_recipe ‘windows::reboot_handler’ and ‘include_recipe
’chef_handler::default’) and also added (depends “windows” and
chef_handler)in apache metadata.rb.

I am able to get the reboot message and system gets rebooted after a
period of 60 sec. From this use case it is clear that reboot is done only
at the end of the chef client.

Now if I want to do a reboot at the middle of execution, then will this
approach work??

a) will break the recipe into two parts, first part will installed the
required package and second part will do the remaining stuff.

Now the biggest challenge is how to start the chef client again after
reboot.
I have to make a reboot.txt file before the reboot in first recipe and
after that system will be rebooted. during the startup script if I pass
the chef-solo command to run the chef client with the second recipe and in
that recipe it will check whether the reboot.txt file is present or not. if
reboot.txt is present then only second recipe will move forward otherwise
it will not do anything and exit.

James/Todd Is this making any sense and would like to know whether I am
moving on a correct path??

Thanks & Regards,
Sachin Kumar

On Mon, Nov 17, 2014 at 11:11 PM, Todd Pigram todd@toddpigram.com wrote:

James,

That is awesome!!!

On Mon, Nov 17, 2014 at 11:46 AM, James Scott jamescott@opscode.com
wrote:

Also, Chef 12 adds a new resource that moves the cookbook-based reboot
resource into core Chef:

http://docs.getchef.com/resource_reboot.html

On Mon, Nov 17, 2014 at 7:20 AM, Todd Pigram todd@toddpigram.com
wrote:

Sachin,

make sure that the 'windows::reboot_handler 'is at the top of your run
list as well as putting ('depends ‘windows’) in your metadata.rb.

The install what you want and Chef will reboot and continue.

here is an example of installing RDSH for preparation for Citrix XenApp
install. It will starts the RDSH, reboot and then come back and do the
desktop experience, and so on… As long as you installed the chef service
during client install or created a scheduled task.

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

if you still have issues, create a library file called
reboot_pending.rb in you cookbook - I was given this by Alex Vinyar from
Chef. This was added to Chef Client back in version 11.12…But I still use
it

reboot_pending.rb

class Chef
class Resource
# include Chef::Mixin::ShellOut

def reboot_pending?
  # Any files listed here means reboot needed

(Registry.key_exists?(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager\PendingFileRenameOperations’) &&

Registry.get_value(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager’,‘PendingFileRenameOperations’).any?) ||
# 1 for any value means reboot pending
# “9306cdfc-c4a1-4a22-9996-848cb67eddc3”=1

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’) &&

Registry.get_values(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’).select{|v| v[2] == 1 }.any?) ||
# 1 or 2 for ‘Flags’ value means reboot pending

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’)
&&

[1,2].include?(Registry::get_value(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’,‘Flags’)))
||
# added by Alex

Registry.key_exists?(‘HKLM\Software\Microsoft\Windows\CurrentVersion\Component
Based Servicing\RebootPending’)
end

end
end

On Mon, Nov 17, 2014 at 8:30 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Hi All,

I have a use case where I need to reboot windows machine after some
package installation.

would like to know the possible best practice to implement this use
case;

a) Make a single recipe that will do the package installation and
product installation and then at last call chef handler to reboot the
machine

a) make two different chef recipe, the first recipe will install
packages and then reboot the machine,
the second recipe will do the remaining stuff. I don’t know
this will work or not.
Is there a way, we can return the chef client to run the same
cookbook after reboot??

I also gone through some documentation but nothing is useful.

Any help would the useful .

Thanks & Regards,
Sachin Kumar

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769


#7

Sachin,

I guess it depends on how you are installing the chef client? If you have a
bare bone image with Chef Client installed, you can install the Chef
Service using the MSI. If you are using Knife, then you have a couple of
options:

  1. Using windows_task as Greg mentioned
  2. or create a batch recipe that runs the following to install the service
    and start it - chef-service-manager -a install & chef-service-manager -a
    start

I do both as starting with 11.16.2 + , after a RDSH install the service
doesn’t start even though I have it automatic(delayed) and recover -
restart service 3 times and set 0days 1 minute, it doesn’t start.

Once you have the this set.

In my previous response, you see that I have

not_if {reboot_pending?} this tells chef if there is a pending reboot,
don’t run until next time.

So in my example for a RDSH install:

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end – This reboots

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end — Restarts the run, then reboots

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end - restarts next run no reboot.

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

Here is an example of a complete run list that I use.


usually completes without intervention.

I use the bare bone image method as my Chef workstation is not on the same
network as the instances, so I spin the image, put in the client.rb, and
validation.pem, run chef-client locally once, then use the UI to assign the
runlist. I wish I could just use knife, but I can’t at the moment. Plus
this makes it easier for my teammates to use as they aren’t totally up on
Chef, but they understand Windows.

Hope this helps

Todd

On Mon, Nov 24, 2014 at 8:21 AM, Greg Zapp greg.zapp@gmail.com wrote:

Hi Sachin,

I build my AMI’s using Chef and I have also split them into two
parts(before and after reboot). Before boot adds a windows task to run
Chef on the next boot:
https://github.com/opscode-cookbooks/windows#windows_task . It needs to
run as SYSTEM without a password to work without login(IIRC). After boot
recipe removes the task.

Cheers,
-Greg

On Tue, Nov 25, 2014 at 2:03 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Thanks James/Todd for sharing this info.

Do you know when chef 12 will be released??

In the meantime, I also tried to add the following code for reboot at the
end of Apache installation.

windows_reboot 60 do
reason "Installed a new Apache package"
action:request
end

At the top of the Apache recipe, I had also added the line (
include_recipe ‘windows::reboot_handler’ and ‘include_recipe
’chef_handler::default’) and also added (depends “windows” and
chef_handler)in apache metadata.rb.

I am able to get the reboot message and system gets rebooted after a
period of 60 sec. From this use case it is clear that reboot is done only
at the end of the chef client.

Now if I want to do a reboot at the middle of execution, then will this
approach work??

a) will break the recipe into two parts, first part will installed the
required package and second part will do the remaining stuff.

Now the biggest challenge is how to start the chef client again after
reboot.
I have to make a reboot.txt file before the reboot in first recipe and
after that system will be rebooted. during the startup script if I pass
the chef-solo command to run the chef client with the second recipe and in
that recipe it will check whether the reboot.txt file is present or not. if
reboot.txt is present then only second recipe will move forward otherwise
it will not do anything and exit.

James/Todd Is this making any sense and would like to know whether I am
moving on a correct path??

Thanks & Regards,
Sachin Kumar

On Mon, Nov 17, 2014 at 11:11 PM, Todd Pigram todd@toddpigram.com
wrote:

James,

That is awesome!!!

On Mon, Nov 17, 2014 at 11:46 AM, James Scott jamescott@opscode.com
wrote:

Also, Chef 12 adds a new resource that moves the cookbook-based reboot
resource into core Chef:

http://docs.getchef.com/resource_reboot.html

On Mon, Nov 17, 2014 at 7:20 AM, Todd Pigram todd@toddpigram.com
wrote:

Sachin,

make sure that the 'windows::reboot_handler 'is at the top of your run
list as well as putting ('depends ‘windows’) in your metadata.rb.

The install what you want and Chef will reboot and continue.

here is an example of installing RDSH for preparation for Citrix
XenApp install. It will starts the RDSH, reboot and then come back and do
the desktop experience, and so on… As long as you installed the chef
service during client install or created a scheduled task.

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

if you still have issues, create a library file called
reboot_pending.rb in you cookbook - I was given this by Alex Vinyar from
Chef. This was added to Chef Client back in version 11.12…But I still use
it

reboot_pending.rb

class Chef
class Resource
# include Chef::Mixin::ShellOut

def reboot_pending?
  # Any files listed here means reboot needed

(Registry.key_exists?(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager\PendingFileRenameOperations’) &&

Registry.get_value(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager’,‘PendingFileRenameOperations’).any?) ||
# 1 for any value means reboot pending
# “9306cdfc-c4a1-4a22-9996-848cb67eddc3”=1

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’) &&

Registry.get_values(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’).select{|v| v[2] == 1 }.any?) ||
# 1 or 2 for ‘Flags’ value means reboot pending

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’)
&&

[1,2].include?(Registry::get_value(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’,‘Flags’)))
||
# added by Alex

Registry.key_exists?(‘HKLM\Software\Microsoft\Windows\CurrentVersion\Component
Based Servicing\RebootPending’)
end

end
end

On Mon, Nov 17, 2014 at 8:30 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Hi All,

I have a use case where I need to reboot windows machine after some
package installation.

would like to know the possible best practice to implement this use
case;

a) Make a single recipe that will do the package installation and
product installation and then at last call chef handler to reboot the
machine

a) make two different chef recipe, the first recipe will install
packages and then reboot the machine,
the second recipe will do the remaining stuff. I don’t know
this will work or not.
Is there a way, we can return the chef client to run the same
cookbook after reboot??

I also gone through some documentation but nothing is useful.

Any help would the useful .

Thanks & Regards,
Sachin Kumar

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram


www.linkedin.com/in/toddpigram/
@pigram86 on twitter

Mobile - 216-224-5769


#8

Oh, forgot, I create the images with Autounattend.xml files. These bare
bone images have both the Chef-Client and the ChefService installed. I keep
a folder that houses the client.rb and validation.pem as the servers could
potentially be in different domains, and that would mess with FQDN so we
basically name and add to domain first, then enroll into Chef…I am
hoping to have chef-provision fix a lot of that very soon…I just need
to get around to it.

On Tue, Nov 25, 2014 at 6:52 AM, Todd Pigram todd@toddpigram.com wrote:

Sachin,

I guess it depends on how you are installing the chef client? If you have
a bare bone image with Chef Client installed, you can install the Chef
Service using the MSI. If you are using Knife, then you have a couple of
options:

  1. Using windows_task as Greg mentioned
  2. or create a batch recipe that runs the following to install the service
    and start it - chef-service-manager -a install & chef-service-manager -a
    start

I do both as starting with 11.16.2 + , after a RDSH install the service
doesn’t start even though I have it automatic(delayed) and recover -
restart service 3 times and set 0days 1 minute, it doesn’t start.

Once you have the this set.

In my previous response, you see that I have

not_if {reboot_pending?} this tells chef if there is a pending reboot,
don’t run until next time.

So in my example for a RDSH install:

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end – This reboots

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end — Restarts the run, then reboots

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end - restarts next run no reboot.

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

Here is an example of a complete run list that I use.


usually completes without intervention.

I use the bare bone image method as my Chef workstation is not on the same
network as the instances, so I spin the image, put in the client.rb, and
validation.pem, run chef-client locally once, then use the UI to assign the
runlist. I wish I could just use knife, but I can’t at the moment. Plus
this makes it easier for my teammates to use as they aren’t totally up on
Chef, but they understand Windows.

Hope this helps

Todd

On Mon, Nov 24, 2014 at 8:21 AM, Greg Zapp greg.zapp@gmail.com wrote:

Hi Sachin,

I build my AMI’s using Chef and I have also split them into two
parts(before and after reboot). Before boot adds a windows task to run
Chef on the next boot:
https://github.com/opscode-cookbooks/windows#windows_task . It needs to
run as SYSTEM without a password to work without login(IIRC). After boot
recipe removes the task.

Cheers,
-Greg

On Tue, Nov 25, 2014 at 2:03 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Thanks James/Todd for sharing this info.

Do you know when chef 12 will be released??

In the meantime, I also tried to add the following code for reboot at
the end of Apache installation.

windows_reboot 60 do
reason "Installed a new Apache package"
action:request
end

At the top of the Apache recipe, I had also added the line (
include_recipe ‘windows::reboot_handler’ and ‘include_recipe
’chef_handler::default’) and also added (depends “windows” and
chef_handler)in apache metadata.rb.

I am able to get the reboot message and system gets rebooted after a
period of 60 sec. From this use case it is clear that reboot is done only
at the end of the chef client.

Now if I want to do a reboot at the middle of execution, then will this
approach work??

a) will break the recipe into two parts, first part will installed the
required package and second part will do the remaining stuff.

Now the biggest challenge is how to start the chef client again after
reboot.
I have to make a reboot.txt file before the reboot in first recipe and
after that system will be rebooted. during the startup script if I pass
the chef-solo command to run the chef client with the second recipe and in
that recipe it will check whether the reboot.txt file is present or not. if
reboot.txt is present then only second recipe will move forward otherwise
it will not do anything and exit.

James/Todd Is this making any sense and would like to know whether I
am moving on a correct path??

Thanks & Regards,
Sachin Kumar

On Mon, Nov 17, 2014 at 11:11 PM, Todd Pigram todd@toddpigram.com
wrote:

James,

That is awesome!!!

On Mon, Nov 17, 2014 at 11:46 AM, James Scott jamescott@opscode.com
wrote:

Also, Chef 12 adds a new resource that moves the cookbook-based reboot
resource into core Chef:

http://docs.getchef.com/resource_reboot.html

On Mon, Nov 17, 2014 at 7:20 AM, Todd Pigram todd@toddpigram.com
wrote:

Sachin,

make sure that the 'windows::reboot_handler 'is at the top of your
run list as well as putting ('depends ‘windows’) in your metadata.rb.

The install what you want and Chef will reboot and continue.

here is an example of installing RDSH for preparation for Citrix
XenApp install. It will starts the RDSH, reboot and then come back and do
the desktop experience, and so on… As long as you installed the chef
service during client install or created a scheduled task.

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

if you still have issues, create a library file called
reboot_pending.rb in you cookbook - I was given this by Alex Vinyar from
Chef. This was added to Chef Client back in version 11.12…But I still use
it

reboot_pending.rb

class Chef
class Resource
# include Chef::Mixin::ShellOut

def reboot_pending?
  # Any files listed here means reboot needed

(Registry.key_exists?(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager\PendingFileRenameOperations’) &&

Registry.get_value(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager’,‘PendingFileRenameOperations’).any?) ||
# 1 for any value means reboot pending
# “9306cdfc-c4a1-4a22-9996-848cb67eddc3”=1

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’) &&

Registry.get_values(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’).select{|v| v[2] == 1 }.any?) ||
# 1 or 2 for ‘Flags’ value means reboot pending

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’)
&&

[1,2].include?(Registry::get_value(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’,‘Flags’)))
||
# added by Alex

Registry.key_exists?(‘HKLM\Software\Microsoft\Windows\CurrentVersion\Component
Based Servicing\RebootPending’)
end

end
end

On Mon, Nov 17, 2014 at 8:30 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Hi All,

I have a use case where I need to reboot windows machine after some
package installation.

would like to know the possible best practice to implement this use
case;

a) Make a single recipe that will do the package installation and
product installation and then at last call chef handler to reboot the
machine

a) make two different chef recipe, the first recipe will install
packages and then reboot the machine,
the second recipe will do the remaining stuff. I don’t know
this will work or not.
Is there a way, we can return the chef client to run the same
cookbook after reboot??

I also gone through some documentation but nothing is useful.

Any help would the useful .

Thanks & Regards,
Sachin Kumar

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram


www.linkedin.com/in/toddpigram/
@pigram86 on twitter

Mobile - 216-224-5769


#9

Thanks Todd!!.

currently I am using windows_task to create a scheduled task that will call
the second recipe after reboot but facing with the issues.

windows_task ‘Chef client’ do
user "Administrator"
password ""
cwd 'C:\chef-repo’
command 'C:\opscode\chef\bin\chef-client -l info -c config.rb -j
apacheStart.json’
run_level :highest
frequency :once
end

Here I am calling apacheStart json file that will call apacheStart recipe.

[2014-11-25T18:09:39+05:30] DEBUG: Mixlib::ShellOut::ShellCommandFailed:
windows_task[Chef_client] (apache_win::stopApac
he line 40) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected
process to exit with [0], but received ‘1’
---- Begin output of schtasks /Query /FO LIST /V /TN “Chef_client” 2> NUL

STDOUT:
STDERR:
---- End output of schtasks /Query /FO LIST /V /TN “Chef_client” 2> NUL ----
Ran schtasks /Query /FO LIST /V /TN “Chef_client” 2> NUL returned 1

Any idea how to overcome this error??

Thanks & Regards,
Sachin Kumar

On Tue, Nov 25, 2014 at 5:28 PM, Todd Pigram todd@toddpigram.com wrote:

Oh, forgot, I create the images with Autounattend.xml files. These bare
bone images have both the Chef-Client and the ChefService installed. I keep
a folder that houses the client.rb and validation.pem as the servers could
potentially be in different domains, and that would mess with FQDN so we
basically name and add to domain first, then enroll into Chef…I am
hoping to have chef-provision fix a lot of that very soon…I just need
to get around to it.

On Tue, Nov 25, 2014 at 6:52 AM, Todd Pigram todd@toddpigram.com wrote:

Sachin,

I guess it depends on how you are installing the chef client? If you have
a bare bone image with Chef Client installed, you can install the Chef
Service using the MSI. If you are using Knife, then you have a couple of
options:

  1. Using windows_task as Greg mentioned
  2. or create a batch recipe that runs the following to install the
    service and start it - chef-service-manager -a install &
    chef-service-manager -a start

I do both as starting with 11.16.2 + , after a RDSH install the service
doesn’t start even though I have it automatic(delayed) and recover -
restart service 3 times and set 0days 1 minute, it doesn’t start.

Once you have the this set.

In my previous response, you see that I have

not_if {reboot_pending?} this tells chef if there is a pending reboot,
don’t run until next time.

So in my example for a RDSH install:

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end – This reboots

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end — Restarts the run, then reboots

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end - restarts next run no reboot.

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

Here is an example of a complete run list that I use.


usually completes without intervention.

I use the bare bone image method as my Chef workstation is not on the
same network as the instances, so I spin the image, put in the client.rb,
and validation.pem, run chef-client locally once, then use the UI to assign
the runlist. I wish I could just use knife, but I can’t at the moment. Plus
this makes it easier for my teammates to use as they aren’t totally up on
Chef, but they understand Windows.

Hope this helps

Todd

On Mon, Nov 24, 2014 at 8:21 AM, Greg Zapp greg.zapp@gmail.com wrote:

Hi Sachin,

I build my AMI’s using Chef and I have also split them into two
parts(before and after reboot). Before boot adds a windows task to run
Chef on the next boot:
https://github.com/opscode-cookbooks/windows#windows_task . It needs
to run as SYSTEM without a password to work without login(IIRC). After
boot recipe removes the task.

Cheers,
-Greg

On Tue, Nov 25, 2014 at 2:03 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Thanks James/Todd for sharing this info.

Do you know when chef 12 will be released??

In the meantime, I also tried to add the following code for reboot at
the end of Apache installation.

windows_reboot 60 do
reason "Installed a new Apache package"
action:request
end

At the top of the Apache recipe, I had also added the line (
include_recipe ‘windows::reboot_handler’ and ‘include_recipe
’chef_handler::default’) and also added (depends “windows” and
chef_handler)in apache metadata.rb.

I am able to get the reboot message and system gets rebooted after a
period of 60 sec. From this use case it is clear that reboot is done only
at the end of the chef client.

Now if I want to do a reboot at the middle of execution, then will this
approach work??

a) will break the recipe into two parts, first part will installed the
required package and second part will do the remaining stuff.

Now the biggest challenge is how to start the chef client again after
reboot.
I have to make a reboot.txt file before the reboot in first recipe and
after that system will be rebooted. during the startup script if I pass
the chef-solo command to run the chef client with the second recipe and in
that recipe it will check whether the reboot.txt file is present or not. if
reboot.txt is present then only second recipe will move forward otherwise
it will not do anything and exit.

James/Todd Is this making any sense and would like to know whether I
am moving on a correct path??

Thanks & Regards,
Sachin Kumar

On Mon, Nov 17, 2014 at 11:11 PM, Todd Pigram todd@toddpigram.com
wrote:

James,

That is awesome!!!

On Mon, Nov 17, 2014 at 11:46 AM, James Scott jamescott@opscode.com
wrote:

Also, Chef 12 adds a new resource that moves the cookbook-based
reboot resource into core Chef:

http://docs.getchef.com/resource_reboot.html

On Mon, Nov 17, 2014 at 7:20 AM, Todd Pigram todd@toddpigram.com
wrote:

Sachin,

make sure that the 'windows::reboot_handler 'is at the top of your
run list as well as putting ('depends ‘windows’) in your metadata.rb.

The install what you want and Chef will reboot and continue.

here is an example of installing RDSH for preparation for Citrix
XenApp install. It will starts the RDSH, reboot and then come back and do
the desktop experience, and so on… As long as you installed the chef
service during client install or created a scheduled task.

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

if you still have issues, create a library file called
reboot_pending.rb in you cookbook - I was given this by Alex Vinyar from
Chef. This was added to Chef Client back in version 11.12…But I still use
it

reboot_pending.rb

class Chef
class Resource
# include Chef::Mixin::ShellOut

def reboot_pending?
  # Any files listed here means reboot needed

(Registry.key_exists?(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager\PendingFileRenameOperations’) &&

Registry.get_value(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager’,‘PendingFileRenameOperations’).any?) ||
# 1 for any value means reboot pending
# “9306cdfc-c4a1-4a22-9996-848cb67eddc3”=1

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’) &&

Registry.get_values(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’).select{|v| v[2] == 1 }.any?) ||
# 1 or 2 for ‘Flags’ value means reboot pending

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’)
&&

[1,2].include?(Registry::get_value(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’,‘Flags’)))
||
# added by Alex

Registry.key_exists?(‘HKLM\Software\Microsoft\Windows\CurrentVersion\Component
Based Servicing\RebootPending’)
end

end
end

On Mon, Nov 17, 2014 at 8:30 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Hi All,

I have a use case where I need to reboot windows machine after some
package installation.

would like to know the possible best practice to implement this use
case;

a) Make a single recipe that will do the package installation and
product installation and then at last call chef handler to reboot the
machine

a) make two different chef recipe, the first recipe will install
packages and then reboot the machine,
the second recipe will do the remaining stuff. I don’t know
this will work or not.
Is there a way, we can return the chef client to run the
same cookbook after reboot??

I also gone through some documentation but nothing is useful.

Any help would the useful .

Thanks & Regards,
Sachin Kumar

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769


#10

Sachin,

you are getting an error in line 40 of your Apache_win::stopApache
recipe…Are you trying to stop a service that isn’t running?
could try something like this

service “PeerDistSvc” do
provider Chef::Provider::Service::Windows
action :disable

  • only_if {service.exists?} - *
    end

I use only_if and not_if to keep idempotence.

On Tue, Nov 25, 2014 at 7:54 AM, Sachin Gupta guptasachin1112mrt@gmail.com
wrote:

Thanks Todd!!.

currently I am using windows_task to create a scheduled task that will
call the second recipe after reboot but facing with the issues.

windows_task ‘Chef client’ do
user "Administrator"
password ""
cwd 'C:\chef-repo’
command 'C:\opscode\chef\bin\chef-client -l info -c config.rb -j
apacheStart.json’
run_level :highest
frequency :once
end

Here I am calling apacheStart json file that will call apacheStart recipe.

[2014-11-25T18:09:39+05:30] DEBUG: Mixlib::ShellOut::ShellCommandFailed:
windows_task[Chef_client] (apache_win::stopApac
he line 40) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected
process to exit with [0], but received ‘1’
---- Begin output of schtasks /Query /FO LIST /V /TN “Chef_client” 2> NUL

STDOUT:
STDERR:
---- End output of schtasks /Query /FO LIST /V /TN “Chef_client” 2> NUL

Ran schtasks /Query /FO LIST /V /TN “Chef_client” 2> NUL returned 1

Any idea how to overcome this error??

Thanks & Regards,
Sachin Kumar

On Tue, Nov 25, 2014 at 5:28 PM, Todd Pigram todd@toddpigram.com wrote:

Oh, forgot, I create the images with Autounattend.xml files. These bare
bone images have both the Chef-Client and the ChefService installed. I keep
a folder that houses the client.rb and validation.pem as the servers could
potentially be in different domains, and that would mess with FQDN so we
basically name and add to domain first, then enroll into Chef…I am
hoping to have chef-provision fix a lot of that very soon…I just need
to get around to it.

On Tue, Nov 25, 2014 at 6:52 AM, Todd Pigram todd@toddpigram.com wrote:

Sachin,

I guess it depends on how you are installing the chef client? If you
have a bare bone image with Chef Client installed, you can install the Chef
Service using the MSI. If you are using Knife, then you have a couple of
options:

  1. Using windows_task as Greg mentioned
  2. or create a batch recipe that runs the following to install the
    service and start it - chef-service-manager -a install &
    chef-service-manager -a start

I do both as starting with 11.16.2 + , after a RDSH install the service
doesn’t start even though I have it automatic(delayed) and recover -
restart service 3 times and set 0days 1 minute, it doesn’t start.

Once you have the this set.

In my previous response, you see that I have

not_if {reboot_pending?} this tells chef if there is a pending reboot,
don’t run until next time.

So in my example for a RDSH install:

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end – This reboots

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end — Restarts the run, then reboots

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end - restarts next run no reboot.

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

Here is an example of a complete run list that I use.


usually completes without intervention.

I use the bare bone image method as my Chef workstation is not on the
same network as the instances, so I spin the image, put in the client.rb,
and validation.pem, run chef-client locally once, then use the UI to assign
the runlist. I wish I could just use knife, but I can’t at the moment. Plus
this makes it easier for my teammates to use as they aren’t totally up on
Chef, but they understand Windows.

Hope this helps

Todd

On Mon, Nov 24, 2014 at 8:21 AM, Greg Zapp greg.zapp@gmail.com wrote:

Hi Sachin,

I build my AMI’s using Chef and I have also split them into two
parts(before and after reboot). Before boot adds a windows task to run
Chef on the next boot:
https://github.com/opscode-cookbooks/windows#windows_task . It needs
to run as SYSTEM without a password to work without login(IIRC). After
boot recipe removes the task.

Cheers,
-Greg

On Tue, Nov 25, 2014 at 2:03 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Thanks James/Todd for sharing this info.

Do you know when chef 12 will be released??

In the meantime, I also tried to add the following code for reboot at
the end of Apache installation.

windows_reboot 60 do
reason "Installed a new Apache package"
action:request
end

At the top of the Apache recipe, I had also added the line (
include_recipe ‘windows::reboot_handler’ and ‘include_recipe
’chef_handler::default’) and also added (depends “windows” and
chef_handler)in apache metadata.rb.

I am able to get the reboot message and system gets rebooted after a
period of 60 sec. From this use case it is clear that reboot is done only
at the end of the chef client.

Now if I want to do a reboot at the middle of execution, then will
this approach work??

a) will break the recipe into two parts, first part will installed the
required package and second part will do the remaining stuff.

Now the biggest challenge is how to start the chef client again after
reboot.
I have to make a reboot.txt file before the reboot in first recipe and
after that system will be rebooted. during the startup script if I pass
the chef-solo command to run the chef client with the second recipe and in
that recipe it will check whether the reboot.txt file is present or not. if
reboot.txt is present then only second recipe will move forward otherwise
it will not do anything and exit.

James/Todd Is this making any sense and would like to know whether I
am moving on a correct path??

Thanks & Regards,
Sachin Kumar

On Mon, Nov 17, 2014 at 11:11 PM, Todd Pigram todd@toddpigram.com
wrote:

James,

That is awesome!!!

On Mon, Nov 17, 2014 at 11:46 AM, James Scott jamescott@opscode.com
wrote:

Also, Chef 12 adds a new resource that moves the cookbook-based
reboot resource into core Chef:

http://docs.getchef.com/resource_reboot.html

On Mon, Nov 17, 2014 at 7:20 AM, Todd Pigram todd@toddpigram.com
wrote:

Sachin,

make sure that the 'windows::reboot_handler 'is at the top of your
run list as well as putting ('depends ‘windows’) in your metadata.rb.

The install what you want and Chef will reboot and continue.

here is an example of installing RDSH for preparation for Citrix
XenApp install. It will starts the RDSH, reboot and then come back and do
the desktop experience, and so on… As long as you installed the chef
service during client install or created a scheduled task.

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

if you still have issues, create a library file called
reboot_pending.rb in you cookbook - I was given this by Alex Vinyar from
Chef. This was added to Chef Client back in version 11.12…But I still use
it

reboot_pending.rb

class Chef
class Resource
# include Chef::Mixin::ShellOut

def reboot_pending?
  # Any files listed here means reboot needed

(Registry.key_exists?(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager\PendingFileRenameOperations’) &&

Registry.get_value(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager’,‘PendingFileRenameOperations’).any?) ||
# 1 for any value means reboot pending
# “9306cdfc-c4a1-4a22-9996-848cb67eddc3”=1

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’) &&

Registry.get_values(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’).select{|v| v[2] == 1 }.any?) ||
# 1 or 2 for ‘Flags’ value means reboot pending

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’)
&&

[1,2].include?(Registry::get_value(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’,‘Flags’)))
||
# added by Alex

Registry.key_exists?(‘HKLM\Software\Microsoft\Windows\CurrentVersion\Component
Based Servicing\RebootPending’)
end

end
end

On Mon, Nov 17, 2014 at 8:30 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Hi All,

I have a use case where I need to reboot windows machine after
some package installation.

would like to know the possible best practice to implement this
use case;

a) Make a single recipe that will do the package installation and
product installation and then at last call chef handler to reboot the
machine

a) make two different chef recipe, the first recipe will install
packages and then reboot the machine,
the second recipe will do the remaining stuff. I don’t know
this will work or not.
Is there a way, we can return the chef client to run the
same cookbook after reboot??

I also gone through some documentation but nothing is useful.

Any help would the useful .

Thanks & Regards,
Sachin Kumar

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram


www.linkedin.com/in/toddpigram/
@pigram86 on twitter

Mobile - 216-224-5769


#11

apache stop service is running fine .

But getting error while creating a windows task form

windows_task ‘Chef client’ do
user "Administrator"
password ""
cwd 'C:\chef-repo’
command 'C:\opscode\chef\bin\chef-client -l info -c config.rb -j
apacheStart.json’
run_level :highest
frequency :once
end

Error Message is

[2014-11-25T18:09:38+05:30] DEBUG: looking for existing tasks

================================================================================
Error executing action create on resource ‘windows_task[Chef_client]’

================================================================================

Mixlib::ShellOut::ShellCommandFailed
------------------------------------
Expected process to exit with [0], but received '1'
---- Begin output of schtasks /Query /FO LIST /V /TN "Chef_client" 2>

NUL ----
STDOUT:
STDERR:
---- End output of schtasks /Query /FO LIST /V /TN “Chef_client” 2> NUL

Ran schtasks /Query /FO LIST /V /TN "Chef_client" 2> NUL returned 1

Resource Declaration:
---------------------

On Tue, Nov 25, 2014 at 6:40 PM, Todd Pigram todd@toddpigram.com wrote:

Sachin,

you are getting an error in line 40 of your Apache_win::stopApache
recipe…Are you trying to stop a service that isn’t running?
could try something like this

service “PeerDistSvc” do
provider Chef::Provider::Service::Windows
action :disable

  • only_if {service.exists?} - *
    end

I use only_if and not_if to keep idempotence.

On Tue, Nov 25, 2014 at 7:54 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Thanks Todd!!.

currently I am using windows_task to create a scheduled task that will
call the second recipe after reboot but facing with the issues.

windows_task ‘Chef client’ do
user "Administrator"
password ""
cwd 'C:\chef-repo’
command 'C:\opscode\chef\bin\chef-client -l info -c config.rb -j
apacheStart.json’
run_level :highest
frequency :once
end

Here I am calling apacheStart json file that will call apacheStart recipe.

[2014-11-25T18:09:39+05:30] DEBUG: Mixlib::ShellOut::ShellCommandFailed:
windows_task[Chef_client] (apache_win::stopApac
he line 40) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected
process to exit with [0], but received ‘1’
---- Begin output of schtasks /Query /FO LIST /V /TN “Chef_client” 2> NUL

STDOUT:
STDERR:
---- End output of schtasks /Query /FO LIST /V /TN “Chef_client” 2> NUL

Ran schtasks /Query /FO LIST /V /TN “Chef_client” 2> NUL returned 1

Any idea how to overcome this error??

Thanks & Regards,
Sachin Kumar

On Tue, Nov 25, 2014 at 5:28 PM, Todd Pigram todd@toddpigram.com wrote:

Oh, forgot, I create the images with Autounattend.xml files. These bare
bone images have both the Chef-Client and the ChefService installed. I keep
a folder that houses the client.rb and validation.pem as the servers could
potentially be in different domains, and that would mess with FQDN so we
basically name and add to domain first, then enroll into Chef…I am
hoping to have chef-provision fix a lot of that very soon…I just need
to get around to it.

On Tue, Nov 25, 2014 at 6:52 AM, Todd Pigram todd@toddpigram.com
wrote:

Sachin,

I guess it depends on how you are installing the chef client? If you
have a bare bone image with Chef Client installed, you can install the Chef
Service using the MSI. If you are using Knife, then you have a couple of
options:

  1. Using windows_task as Greg mentioned
  2. or create a batch recipe that runs the following to install the
    service and start it - chef-service-manager -a install &
    chef-service-manager -a start

I do both as starting with 11.16.2 + , after a RDSH install the service
doesn’t start even though I have it automatic(delayed) and recover -
restart service 3 times and set 0days 1 minute, it doesn’t start.

Once you have the this set.

In my previous response, you see that I have

not_if {reboot_pending?} this tells chef if there is a pending reboot,
don’t run until next time.

So in my example for a RDSH install:

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end – This reboots

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end — Restarts the run, then reboots

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end - restarts next run no reboot.

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

Here is an example of a complete run list that I use.


usually completes without intervention.

I use the bare bone image method as my Chef workstation is not on the
same network as the instances, so I spin the image, put in the client.rb,
and validation.pem, run chef-client locally once, then use the UI to assign
the runlist. I wish I could just use knife, but I can’t at the moment. Plus
this makes it easier for my teammates to use as they aren’t totally up on
Chef, but they understand Windows.

Hope this helps

Todd

On Mon, Nov 24, 2014 at 8:21 AM, Greg Zapp greg.zapp@gmail.com wrote:

Hi Sachin,

I build my AMI’s using Chef and I have also split them into two
parts(before and after reboot). Before boot adds a windows task to run
Chef on the next boot:
https://github.com/opscode-cookbooks/windows#windows_task . It needs
to run as SYSTEM without a password to work without login(IIRC). After
boot recipe removes the task.

Cheers,
-Greg

On Tue, Nov 25, 2014 at 2:03 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Thanks James/Todd for sharing this info.

Do you know when chef 12 will be released??

In the meantime, I also tried to add the following code for reboot at
the end of Apache installation.

windows_reboot 60 do
reason "Installed a new Apache package"
action:request
end

At the top of the Apache recipe, I had also added the line (
include_recipe ‘windows::reboot_handler’ and ‘include_recipe
’chef_handler::default’) and also added (depends “windows” and
chef_handler)in apache metadata.rb.

I am able to get the reboot message and system gets rebooted after a
period of 60 sec. From this use case it is clear that reboot is done only
at the end of the chef client.

Now if I want to do a reboot at the middle of execution, then will
this approach work??

a) will break the recipe into two parts, first part will installed
the required package and second part will do the remaining stuff.

Now the biggest challenge is how to start the chef client again after
reboot.
I have to make a reboot.txt file before the reboot in first recipe
and after that system will be rebooted. during the startup script if I
pass the chef-solo command to run the chef client with the second recipe
and in that recipe it will check whether the reboot.txt file is present or
not. if reboot.txt is present then only second recipe will move forward
otherwise it will not do anything and exit.

James/Todd Is this making any sense and would like to know whether
I am moving on a correct path??

Thanks & Regards,
Sachin Kumar

On Mon, Nov 17, 2014 at 11:11 PM, Todd Pigram todd@toddpigram.com
wrote:

James,

That is awesome!!!

On Mon, Nov 17, 2014 at 11:46 AM, James Scott <jamescott@opscode.com

wrote:

Also, Chef 12 adds a new resource that moves the cookbook-based
reboot resource into core Chef:

http://docs.getchef.com/resource_reboot.html

On Mon, Nov 17, 2014 at 7:20 AM, Todd Pigram todd@toddpigram.com
wrote:

Sachin,

make sure that the 'windows::reboot_handler 'is at the top of your
run list as well as putting ('depends ‘windows’) in your metadata.rb.

The install what you want and Chef will reboot and continue.

here is an example of installing RDSH for preparation for Citrix
XenApp install. It will starts the RDSH, reboot and then come back and do
the desktop experience, and so on… As long as you installed the chef
service during client install or created a scheduled task.

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

if you still have issues, create a library file called
reboot_pending.rb in you cookbook - I was given this by Alex Vinyar from
Chef. This was added to Chef Client back in version 11.12…But I still use
it

reboot_pending.rb

class Chef
class Resource
# include Chef::Mixin::ShellOut

def reboot_pending?
  # Any files listed here means reboot needed

(Registry.key_exists?(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager\PendingFileRenameOperations’) &&

Registry.get_value(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager’,‘PendingFileRenameOperations’).any?) ||
# 1 for any value means reboot pending
# “9306cdfc-c4a1-4a22-9996-848cb67eddc3”=1

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’) &&

Registry.get_values(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’).select{|v| v[2] == 1 }.any?) ||
# 1 or 2 for ‘Flags’ value means reboot pending

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’)
&&

[1,2].include?(Registry::get_value(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’,‘Flags’)))
||
# added by Alex

Registry.key_exists?(‘HKLM\Software\Microsoft\Windows\CurrentVersion\Component
Based Servicing\RebootPending’)
end

end
end

On Mon, Nov 17, 2014 at 8:30 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Hi All,

I have a use case where I need to reboot windows machine after
some package installation.

would like to know the possible best practice to implement this
use case;

a) Make a single recipe that will do the package installation
and product installation and then at last call chef handler to reboot
the machine

a) make two different chef recipe, the first recipe will
install packages and then reboot the machine,
the second recipe will do the remaining stuff. I don’t
know this will work or not.
Is there a way, we can return the chef client to run the
same cookbook after reboot??

I also gone through some documentation but nothing is useful.

Any help would the useful .

Thanks & Regards,
Sachin Kumar

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769


#12

Unfortunately we aren’t logging the command to debug so it’s kinda hard to
tell:
https://github.com/opscode-cookbooks/windows/blob/master/providers/task.rb#L40
. I recall there being issues setting the user to Administrator and
leaving the password blank though. I would recommend using ‘system’ as the
user, :onstart as the frequency, leave out the password, and don’t escape
the backslashes in a single quoted string(I authored the commit that made
it possible for no-logon onstart’s to be added):

windows_task ‘Chef client’ do
user 'system’
command 'C:\opscode\chef\bin\chef-client -l info -c config.rb -j
apacheStart.json’
frequency :onstart
end

Hopefully this will work for you.

-Greg

On Wed, Nov 26, 2014 at 3:12 AM, Sachin Gupta guptasachin1112mrt@gmail.com
wrote:

apache stop service is running fine .

But getting error while creating a windows task form

windows_task ‘Chef client’ do
user "Administrator"
password ""
cwd 'C:\chef-repo’
command 'C:\opscode\chef\bin\chef-client -l info -c config.rb -j
apacheStart.json’
run_level :highest
frequency :once
end

Error Message is

[2014-11-25T18:09:38+05:30] DEBUG: looking for existing tasks

================================================================================
Error executing action create on resource ‘windows_task[Chef_client]’

================================================================================

Mixlib::ShellOut::ShellCommandFailed
------------------------------------
Expected process to exit with [0], but received '1'
---- Begin output of schtasks /Query /FO LIST /V /TN "Chef_client" 2>

NUL ----
STDOUT:
STDERR:
---- End output of schtasks /Query /FO LIST /V /TN “Chef_client” 2>
NUL ----
Ran schtasks /Query /FO LIST /V /TN “Chef_client” 2> NUL returned 1

Resource Declaration:
---------------------

On Tue, Nov 25, 2014 at 6:40 PM, Todd Pigram todd@toddpigram.com wrote:

Sachin,

you are getting an error in line 40 of your Apache_win::stopApache
recipe…Are you trying to stop a service that isn’t running?
could try something like this

service “PeerDistSvc” do
provider Chef::Provider::Service::Windows
action :disable

  • only_if {service.exists?} - *
    end

I use only_if and not_if to keep idempotence.

On Tue, Nov 25, 2014 at 7:54 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Thanks Todd!!.

currently I am using windows_task to create a scheduled task that will
call the second recipe after reboot but facing with the issues.

windows_task ‘Chef client’ do
user "Administrator"
password ""
cwd 'C:\chef-repo’
command 'C:\opscode\chef\bin\chef-client -l info -c config.rb -j
apacheStart.json’
run_level :highest
frequency :once
end

Here I am calling apacheStart json file that will call apacheStart
recipe.

[2014-11-25T18:09:39+05:30] DEBUG: Mixlib::ShellOut::ShellCommandFailed:
windows_task[Chef_client] (apache_win::stopApac
he line 40) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected
process to exit with [0], but received ‘1’
---- Begin output of schtasks /Query /FO LIST /V /TN “Chef_client” 2>
NUL ----
STDOUT:
STDERR:
---- End output of schtasks /Query /FO LIST /V /TN “Chef_client” 2> NUL

Ran schtasks /Query /FO LIST /V /TN “Chef_client” 2> NUL returned 1

Any idea how to overcome this error??

Thanks & Regards,
Sachin Kumar

On Tue, Nov 25, 2014 at 5:28 PM, Todd Pigram todd@toddpigram.com
wrote:

Oh, forgot, I create the images with Autounattend.xml files. These bare
bone images have both the Chef-Client and the ChefService installed. I keep
a folder that houses the client.rb and validation.pem as the servers could
potentially be in different domains, and that would mess with FQDN so we
basically name and add to domain first, then enroll into Chef…I am
hoping to have chef-provision fix a lot of that very soon…I just need
to get around to it.

On Tue, Nov 25, 2014 at 6:52 AM, Todd Pigram todd@toddpigram.com
wrote:

Sachin,

I guess it depends on how you are installing the chef client? If you
have a bare bone image with Chef Client installed, you can install the Chef
Service using the MSI. If you are using Knife, then you have a couple of
options:

  1. Using windows_task as Greg mentioned
  2. or create a batch recipe that runs the following to install the
    service and start it - chef-service-manager -a install &
    chef-service-manager -a start

I do both as starting with 11.16.2 + , after a RDSH install the
service doesn’t start even though I have it automatic(delayed) and recover

  • restart service 3 times and set 0days 1 minute, it doesn’t start.

Once you have the this set.

In my previous response, you see that I have

not_if {reboot_pending?} this tells chef if there is a pending reboot,
don’t run until next time.

So in my example for a RDSH install:

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end – This reboots

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end — Restarts the run, then reboots

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end - restarts next run no reboot.

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

Here is an example of a complete run list that I use.


usually completes without intervention.

I use the bare bone image method as my Chef workstation is not on the
same network as the instances, so I spin the image, put in the client.rb,
and validation.pem, run chef-client locally once, then use the UI to assign
the runlist. I wish I could just use knife, but I can’t at the moment. Plus
this makes it easier for my teammates to use as they aren’t totally up on
Chef, but they understand Windows.

Hope this helps

Todd

On Mon, Nov 24, 2014 at 8:21 AM, Greg Zapp greg.zapp@gmail.com
wrote:

Hi Sachin,

I build my AMI’s using Chef and I have also split them into two
parts(before and after reboot). Before boot adds a windows task to run
Chef on the next boot:
https://github.com/opscode-cookbooks/windows#windows_task . It
needs to run as SYSTEM without a password to work without login(IIRC).
After boot recipe removes the task.

Cheers,
-Greg

On Tue, Nov 25, 2014 at 2:03 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Thanks James/Todd for sharing this info.

Do you know when chef 12 will be released??

In the meantime, I also tried to add the following code for reboot
at the end of Apache installation.

windows_reboot 60 do
reason "Installed a new Apache package"
action:request
end

At the top of the Apache recipe, I had also added the line (
include_recipe ‘windows::reboot_handler’ and ‘include_recipe
’chef_handler::default’) and also added (depends “windows” and
chef_handler)in apache metadata.rb.

I am able to get the reboot message and system gets rebooted after a
period of 60 sec. From this use case it is clear that reboot is done only
at the end of the chef client.

Now if I want to do a reboot at the middle of execution, then will
this approach work??

a) will break the recipe into two parts, first part will installed
the required package and second part will do the remaining stuff.

Now the biggest challenge is how to start the chef client again
after reboot.
I have to make a reboot.txt file before the reboot in first recipe
and after that system will be rebooted. during the startup script if I
pass the chef-solo command to run the chef client with the second recipe
and in that recipe it will check whether the reboot.txt file is present or
not. if reboot.txt is present then only second recipe will move forward
otherwise it will not do anything and exit.

James/Todd Is this making any sense and would like to know whether
I am moving on a correct path??

Thanks & Regards,
Sachin Kumar

On Mon, Nov 17, 2014 at 11:11 PM, Todd Pigram todd@toddpigram.com
wrote:

James,

That is awesome!!!

On Mon, Nov 17, 2014 at 11:46 AM, James Scott <
jamescott@opscode.com> wrote:

Also, Chef 12 adds a new resource that moves the cookbook-based
reboot resource into core Chef:

http://docs.getchef.com/resource_reboot.html

On Mon, Nov 17, 2014 at 7:20 AM, Todd Pigram todd@toddpigram.com
wrote:

Sachin,

make sure that the 'windows::reboot_handler 'is at the top of
your run list as well as putting ('depends ‘windows’) in your
metadata.rb.

The install what you want and Chef will reboot and continue.

here is an example of installing RDSH for preparation for Citrix
XenApp install. It will starts the RDSH, reboot and then come back and do
the desktop experience, and so on… As long as you installed the chef
service during client install or created a scheduled task.

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

if you still have issues, create a library file called
reboot_pending.rb in you cookbook - I was given this by Alex Vinyar from
Chef. This was added to Chef Client back in version 11.12…But I still use
it

reboot_pending.rb

class Chef
class Resource
# include Chef::Mixin::ShellOut

def reboot_pending?
  # Any files listed here means reboot needed

(Registry.key_exists?(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager\PendingFileRenameOperations’) &&

Registry.get_value(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager’,‘PendingFileRenameOperations’).any?) ||
# 1 for any value means reboot pending
# “9306cdfc-c4a1-4a22-9996-848cb67eddc3”=1

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’) &&

Registry.get_values(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’).select{|v| v[2] == 1 }.any?) ||
# 1 or 2 for ‘Flags’ value means reboot pending

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’)
&&

[1,2].include?(Registry::get_value(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’,‘Flags’)))
||
# added by Alex

Registry.key_exists?(‘HKLM\Software\Microsoft\Windows\CurrentVersion\Component
Based Servicing\RebootPending’)
end

end
end

On Mon, Nov 17, 2014 at 8:30 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Hi All,

I have a use case where I need to reboot windows machine after
some package installation.

would like to know the possible best practice to implement this
use case;

a) Make a single recipe that will do the package installation
and product installation and then at last call chef handler to reboot
the machine

a) make two different chef recipe, the first recipe will
install packages and then reboot the machine,
the second recipe will do the remaining stuff. I don’t
know this will work or not.
Is there a way, we can return the chef client to run the
same cookbook after reboot??

I also gone through some documentation but nothing is useful.

Any help would the useful .

Thanks & Regards,
Sachin Kumar

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769


#13

Thanks Greg,

currently I am not using community based windows cookbook nor chef handler
to implement this.

I followed a simple step and some windows basic commands.

the steps are:

a) break the json file in two parts with recipes one before the reboot and
another after the reboot.
b) In recipe before the reboot, I have added simple execute statement that
will create a scheduled task to run a batch file. In the batch file calling
chef solo command that will execute the after reboot recipes.
c) added the code in after reboot recipe that will delete previously
created scheduled task

code before reboot recipe
execute “Create startChefClient” do
command “SchTasks /Create /SC ONSTART /TN “startChefClient” /TR
"E:\test.bat” /ru ******** /rp *******"
action :run
end

execute “reboot windows machine” do
command 'shutdown /r’
action :run
end

code in test.bat
@echo off
start cmd.exe /k “chef-solo -l debug -L E:\Scripts\log.txt -c C:\config.rb
-j E:\test_1.json”

sample code in after reboot recipe
execute “Delete startChefClient” do
command "schtasks /delete /TN “startChefClient” /f "
action :run
end

by doing this, I am able to reboot the windows machine and also able to run
the after reboot recipes.

Now while running test.bat, if chef -solo fails, I want to send the email
notifications,

my question is shall I use blat utility to send the mail or is there any
inbuilt email handler in chef??

Thanks & Regards,
Sachin Gupta

On Tue, Nov 25, 2014 at 8:07 PM, Greg Zapp greg.zapp@gmail.com wrote:

Unfortunately we aren’t logging the command to debug so it’s kinda hard to
tell:
https://github.com/opscode-cookbooks/windows/blob/master/providers/task.rb#L40
. I recall there being issues setting the user to Administrator and
leaving the password blank though. I would recommend using ‘system’ as the
user, :onstart as the frequency, leave out the password, and don’t escape
the backslashes in a single quoted string(I authored the commit that made
it possible for no-logon onstart’s to be added):

windows_task ‘Chef client’ do
user 'system’
command 'C:\opscode\chef\bin\chef-client -l info -c config.rb -j
apacheStart.json’
frequency :onstart
end

Hopefully this will work for you.

-Greg

On Wed, Nov 26, 2014 at 3:12 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

apache stop service is running fine .

But getting error while creating a windows task form

windows_task ‘Chef client’ do
user "Administrator"
password ""
cwd 'C:\chef-repo’
command 'C:\opscode\chef\bin\chef-client -l info -c config.rb -j
apacheStart.json’
run_level :highest
frequency :once
end

Error Message is

[2014-11-25T18:09:38+05:30] DEBUG: looking for existing tasks

================================================================================
Error executing action create on resource
’windows_task[Chef_client]’

================================================================================

Mixlib::ShellOut::ShellCommandFailed
------------------------------------
Expected process to exit with [0], but received '1'
---- Begin output of schtasks /Query /FO LIST /V /TN "Chef_client" 2>

NUL ----
STDOUT:
STDERR:
---- End output of schtasks /Query /FO LIST /V /TN “Chef_client” 2>
NUL ----
Ran schtasks /Query /FO LIST /V /TN “Chef_client” 2> NUL returned 1

Resource Declaration:
---------------------

On Tue, Nov 25, 2014 at 6:40 PM, Todd Pigram todd@toddpigram.com wrote:

Sachin,

you are getting an error in line 40 of your Apache_win::stopApache
recipe…Are you trying to stop a service that isn’t running?
could try something like this

service “PeerDistSvc” do
provider Chef::Provider::Service::Windows
action :disable

  • only_if {service.exists?} - *
    end

I use only_if and not_if to keep idempotence.

On Tue, Nov 25, 2014 at 7:54 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Thanks Todd!!.

currently I am using windows_task to create a scheduled task that will
call the second recipe after reboot but facing with the issues.

windows_task ‘Chef client’ do
user "Administrator"
password ""
cwd 'C:\chef-repo’
command 'C:\opscode\chef\bin\chef-client -l info -c config.rb -j
apacheStart.json’
run_level :highest
frequency :once
end

Here I am calling apacheStart json file that will call apacheStart
recipe.

[2014-11-25T18:09:39+05:30] DEBUG:
Mixlib::ShellOut::ShellCommandFailed: windows_task[Chef_client]
(apache_win::stopApac
he line 40) had an error: Mixlib::ShellOut::ShellCommandFailed:
Expected process to exit with [0], but received ‘1’
---- Begin output of schtasks /Query /FO LIST /V /TN “Chef_client” 2>
NUL ----
STDOUT:
STDERR:
---- End output of schtasks /Query /FO LIST /V /TN “Chef_client” 2> NUL

Ran schtasks /Query /FO LIST /V /TN “Chef_client” 2> NUL returned 1

Any idea how to overcome this error??

Thanks & Regards,
Sachin Kumar

On Tue, Nov 25, 2014 at 5:28 PM, Todd Pigram todd@toddpigram.com
wrote:

Oh, forgot, I create the images with Autounattend.xml files. These
bare bone images have both the Chef-Client and the ChefService installed. I
keep a folder that houses the client.rb and validation.pem as the servers
could potentially be in different domains, and that would mess with FQDN so
we basically name and add to domain first, then enroll into Chef…I am
hoping to have chef-provision fix a lot of that very soon…I just need
to get around to it.

On Tue, Nov 25, 2014 at 6:52 AM, Todd Pigram todd@toddpigram.com
wrote:

Sachin,

I guess it depends on how you are installing the chef client? If you
have a bare bone image with Chef Client installed, you can install the Chef
Service using the MSI. If you are using Knife, then you have a couple of
options:

  1. Using windows_task as Greg mentioned
  2. or create a batch recipe that runs the following to install the
    service and start it - chef-service-manager -a install &
    chef-service-manager -a start

I do both as starting with 11.16.2 + , after a RDSH install the
service doesn’t start even though I have it automatic(delayed) and recover

  • restart service 3 times and set 0days 1 minute, it doesn’t start.

Once you have the this set.

In my previous response, you see that I have

not_if {reboot_pending?} this tells chef if there is a pending
reboot, don’t run until next time.

So in my example for a RDSH install:

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end – This reboots

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end — Restarts the run, then reboots

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end - restarts next run no reboot.

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

Here is an example of a complete run list that I use.


usually completes without intervention.

I use the bare bone image method as my Chef workstation is not on the
same network as the instances, so I spin the image, put in the client.rb,
and validation.pem, run chef-client locally once, then use the UI to assign
the runlist. I wish I could just use knife, but I can’t at the moment. Plus
this makes it easier for my teammates to use as they aren’t totally up on
Chef, but they understand Windows.

Hope this helps

Todd

On Mon, Nov 24, 2014 at 8:21 AM, Greg Zapp greg.zapp@gmail.com
wrote:

Hi Sachin,

I build my AMI’s using Chef and I have also split them into two
parts(before and after reboot). Before boot adds a windows task to run
Chef on the next boot:
https://github.com/opscode-cookbooks/windows#windows_task . It
needs to run as SYSTEM without a password to work without login(IIRC).
After boot recipe removes the task.

Cheers,
-Greg

On Tue, Nov 25, 2014 at 2:03 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Thanks James/Todd for sharing this info.

Do you know when chef 12 will be released??

In the meantime, I also tried to add the following code for reboot
at the end of Apache installation.

windows_reboot 60 do
reason "Installed a new Apache package"
action:request
end

At the top of the Apache recipe, I had also added the line (
include_recipe ‘windows::reboot_handler’ and ‘include_recipe
’chef_handler::default’) and also added (depends “windows” and
chef_handler)in apache metadata.rb.

I am able to get the reboot message and system gets rebooted after
a period of 60 sec. From this use case it is clear that reboot is done only
at the end of the chef client.

Now if I want to do a reboot at the middle of execution, then will
this approach work??

a) will break the recipe into two parts, first part will installed
the required package and second part will do the remaining stuff.

Now the biggest challenge is how to start the chef client again
after reboot.
I have to make a reboot.txt file before the reboot in first recipe
and after that system will be rebooted. during the startup script if I
pass the chef-solo command to run the chef client with the second recipe
and in that recipe it will check whether the reboot.txt file is present or
not. if reboot.txt is present then only second recipe will move forward
otherwise it will not do anything and exit.

James/Todd Is this making any sense and would like to know
whether I am moving on a correct path??

Thanks & Regards,
Sachin Kumar

On Mon, Nov 17, 2014 at 11:11 PM, Todd Pigram todd@toddpigram.com
wrote:

James,

That is awesome!!!

On Mon, Nov 17, 2014 at 11:46 AM, James Scott <
jamescott@opscode.com> wrote:

Also, Chef 12 adds a new resource that moves the cookbook-based
reboot resource into core Chef:

http://docs.getchef.com/resource_reboot.html

On Mon, Nov 17, 2014 at 7:20 AM, Todd Pigram <todd@toddpigram.com

wrote:

Sachin,

make sure that the 'windows::reboot_handler 'is at the top of
your run list as well as putting ('depends ‘windows’) in your
metadata.rb.

The install what you want and Chef will reboot and continue.

here is an example of installing RDSH for preparation for Citrix
XenApp install. It will starts the RDSH, reboot and then come back and do
the desktop experience, and so on… As long as you installed the chef
service during client install or created a scheduled task.

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

if you still have issues, create a library file called
reboot_pending.rb in you cookbook - I was given this by Alex Vinyar from
Chef. This was added to Chef Client back in version 11.12…But I still use
it

reboot_pending.rb

class Chef
class Resource
# include Chef::Mixin::ShellOut

def reboot_pending?
  # Any files listed here means reboot needed

(Registry.key_exists?(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager\PendingFileRenameOperations’) &&

Registry.get_value(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager’,‘PendingFileRenameOperations’).any?) ||
# 1 for any value means reboot pending
# “9306cdfc-c4a1-4a22-9996-848cb67eddc3”=1

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’) &&

Registry.get_values(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’).select{|v| v[2] == 1 }.any?) ||
# 1 or 2 for ‘Flags’ value means reboot pending

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’)
&&

[1,2].include?(Registry::get_value(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’,‘Flags’)))
||
# added by Alex

Registry.key_exists?(‘HKLM\Software\Microsoft\Windows\CurrentVersion\Component
Based Servicing\RebootPending’)
end

end
end

On Mon, Nov 17, 2014 at 8:30 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Hi All,

I have a use case where I need to reboot windows machine after
some package installation.

would like to know the possible best practice to implement this
use case;

a) Make a single recipe that will do the package installation
and product installation and then at last call chef handler to reboot
the machine

a) make two different chef recipe, the first recipe will
install packages and then reboot the machine,
the second recipe will do the remaining stuff. I don’t
know this will work or not.
Is there a way, we can return the chef client to run the
same cookbook after reboot??

I also gone through some documentation but nothing is useful.

Any help would the useful .

Thanks & Regards,
Sachin Kumar

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769


#14

You can use powershell to send the email.

powershell_script “Send Confirmation Email” do
code <<-EOB
$name_fqdn = env:computername+"."+((gwmi Win32_ComputerSystem).domain)
Send-MailMessage -From "Chef <CHEF@DOMAIN.com>"
-To “NAME NAME@DOMAIN.com-Body "$name_fqdn has been Chef'd for the first time."
-Subject “$name_fqdn is Chef’d” `
-SmtpServer mailhost.DOMAIN.com
New-Item -Path C:\chef\firstchef -type file -force
EOB
not_if { File.exists?(“c:\chef\firstchef”) }
end

Something like this should work.

On Tue, Dec 2, 2014 at 1:23 AM, Sachin Gupta guptasachin1112mrt@gmail.com
wrote:

Thanks Greg,

currently I am not using community based windows cookbook nor chef handler
to implement this.

I followed a simple step and some windows basic commands.

the steps are:

a) break the json file in two parts with recipes one before the reboot
and another after the reboot.
b) In recipe before the reboot, I have added simple execute statement
that will create a scheduled task to run a batch file. In the batch file
calling chef solo command that will execute the after reboot recipes.
c) added the code in after reboot recipe that will delete previously
created scheduled task

code before reboot recipe
execute “Create startChefClient” do
command “SchTasks /Create /SC ONSTART /TN “startChefClient” /TR
"E:\test.bat” /ru ******** /rp *******"
action :run
end

execute “reboot windows machine” do
command 'shutdown /r’
action :run
end


code in test.bat
@echo off
start cmd.exe /k “chef-solo -l debug -L E:\Scripts\log.txt -c C:\config.rb
-j E:\test_1.json”


sample code in after reboot recipe
execute “Delete startChefClient” do
command "schtasks /delete /TN “startChefClient” /f "
action :run
end

by doing this, I am able to reboot the windows machine and also able to
run the after reboot recipes.

Now while running test.bat, if chef -solo fails, I want to send the email
notifications,

my question is shall I use blat utility to send the mail or is there any
inbuilt email handler in chef??

Thanks & Regards,
Sachin Gupta

On Tue, Nov 25, 2014 at 8:07 PM, Greg Zapp greg.zapp@gmail.com wrote:

Unfortunately we aren’t logging the command to debug so it’s kinda hard
to tell:
https://github.com/opscode-cookbooks/windows/blob/master/providers/task.rb#L40
. I recall there being issues setting the user to Administrator and
leaving the password blank though. I would recommend using ‘system’ as the
user, :onstart as the frequency, leave out the password, and don’t escape
the backslashes in a single quoted string(I authored the commit that made
it possible for no-logon onstart’s to be added):

windows_task ‘Chef client’ do
user 'system’
command 'C:\opscode\chef\bin\chef-client -l info -c config.rb -j
apacheStart.json’
frequency :onstart
end

Hopefully this will work for you.

-Greg

On Wed, Nov 26, 2014 at 3:12 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

apache stop service is running fine .

But getting error while creating a windows task form

windows_task ‘Chef client’ do
user "Administrator"
password ""
cwd 'C:\chef-repo’
command 'C:\opscode\chef\bin\chef-client -l info -c config.rb -j
apacheStart.json’
run_level :highest
frequency :once
end

Error Message is

[2014-11-25T18:09:38+05:30] DEBUG: looking for existing tasks

================================================================================
Error executing action create on resource
’windows_task[Chef_client]’

================================================================================

Mixlib::ShellOut::ShellCommandFailed
------------------------------------
Expected process to exit with [0], but received '1'
---- Begin output of schtasks /Query /FO LIST /V /TN "Chef_client"

2> NUL ----
STDOUT:
STDERR:
---- End output of schtasks /Query /FO LIST /V /TN “Chef_client” 2>
NUL ----
Ran schtasks /Query /FO LIST /V /TN “Chef_client” 2> NUL returned 1

Resource Declaration:
---------------------

On Tue, Nov 25, 2014 at 6:40 PM, Todd Pigram todd@toddpigram.com
wrote:

Sachin,

you are getting an error in line 40 of your Apache_win::stopApache
recipe…Are you trying to stop a service that isn’t running?
could try something like this

service “PeerDistSvc” do
provider Chef::Provider::Service::Windows
action :disable

  • only_if {service.exists?} - *
    end

I use only_if and not_if to keep idempotence.

On Tue, Nov 25, 2014 at 7:54 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Thanks Todd!!.

currently I am using windows_task to create a scheduled task that will
call the second recipe after reboot but facing with the issues.

windows_task ‘Chef client’ do
user "Administrator"
password ""
cwd 'C:\chef-repo’
command 'C:\opscode\chef\bin\chef-client -l info -c config.rb -j
apacheStart.json’
run_level :highest
frequency :once
end

Here I am calling apacheStart json file that will call apacheStart
recipe.

[2014-11-25T18:09:39+05:30] DEBUG:
Mixlib::ShellOut::ShellCommandFailed: windows_task[Chef_client]
(apache_win::stopApac
he line 40) had an error: Mixlib::ShellOut::ShellCommandFailed:
Expected process to exit with [0], but received ‘1’
---- Begin output of schtasks /Query /FO LIST /V /TN “Chef_client” 2>
NUL ----
STDOUT:
STDERR:
---- End output of schtasks /Query /FO LIST /V /TN “Chef_client” 2>
NUL ----
Ran schtasks /Query /FO LIST /V /TN “Chef_client” 2> NUL returned 1

Any idea how to overcome this error??

Thanks & Regards,
Sachin Kumar

On Tue, Nov 25, 2014 at 5:28 PM, Todd Pigram todd@toddpigram.com
wrote:

Oh, forgot, I create the images with Autounattend.xml files. These
bare bone images have both the Chef-Client and the ChefService installed. I
keep a folder that houses the client.rb and validation.pem as the servers
could potentially be in different domains, and that would mess with FQDN so
we basically name and add to domain first, then enroll into Chef…I am
hoping to have chef-provision fix a lot of that very soon…I just need
to get around to it.

On Tue, Nov 25, 2014 at 6:52 AM, Todd Pigram todd@toddpigram.com
wrote:

Sachin,

I guess it depends on how you are installing the chef client? If you
have a bare bone image with Chef Client installed, you can install the Chef
Service using the MSI. If you are using Knife, then you have a couple of
options:

  1. Using windows_task as Greg mentioned
  2. or create a batch recipe that runs the following to install the
    service and start it - chef-service-manager -a install &
    chef-service-manager -a start

I do both as starting with 11.16.2 + , after a RDSH install the
service doesn’t start even though I have it automatic(delayed) and recover

  • restart service 3 times and set 0days 1 minute, it doesn’t start.

Once you have the this set.

In my previous response, you see that I have

not_if {reboot_pending?} this tells chef if there is a pending
reboot, don’t run until next time.

So in my example for a RDSH install:

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end – This reboots

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end — Restarts the run, then reboots

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end - restarts next run no reboot.

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

Here is an example of a complete run list that I use.


usually completes without intervention.

I use the bare bone image method as my Chef workstation is not on
the same network as the instances, so I spin the image, put in the
client.rb, and validation.pem, run chef-client locally once, then use the
UI to assign the runlist. I wish I could just use knife, but I can’t at the
moment. Plus this makes it easier for my teammates to use as they aren’t
totally up on Chef, but they understand Windows.

Hope this helps

Todd

On Mon, Nov 24, 2014 at 8:21 AM, Greg Zapp greg.zapp@gmail.com
wrote:

Hi Sachin,

I build my AMI’s using Chef and I have also split them into two
parts(before and after reboot). Before boot adds a windows task to run
Chef on the next boot:
https://github.com/opscode-cookbooks/windows#windows_task . It
needs to run as SYSTEM without a password to work without login(IIRC).
After boot recipe removes the task.

Cheers,
-Greg

On Tue, Nov 25, 2014 at 2:03 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Thanks James/Todd for sharing this info.

Do you know when chef 12 will be released??

In the meantime, I also tried to add the following code for reboot
at the end of Apache installation.

windows_reboot 60 do
reason "Installed a new Apache package"
action:request
end

At the top of the Apache recipe, I had also added the line (
include_recipe ‘windows::reboot_handler’ and ‘include_recipe
’chef_handler::default’) and also added (depends “windows” and
chef_handler)in apache metadata.rb.

I am able to get the reboot message and system gets rebooted after
a period of 60 sec. From this use case it is clear that reboot is done only
at the end of the chef client.

Now if I want to do a reboot at the middle of execution, then will
this approach work??

a) will break the recipe into two parts, first part will installed
the required package and second part will do the remaining stuff.

Now the biggest challenge is how to start the chef client again
after reboot.
I have to make a reboot.txt file before the reboot in first recipe
and after that system will be rebooted. during the startup script if I
pass the chef-solo command to run the chef client with the second recipe
and in that recipe it will check whether the reboot.txt file is present or
not. if reboot.txt is present then only second recipe will move forward
otherwise it will not do anything and exit.

James/Todd Is this making any sense and would like to know
whether I am moving on a correct path??

Thanks & Regards,
Sachin Kumar

On Mon, Nov 17, 2014 at 11:11 PM, Todd Pigram <todd@toddpigram.com

wrote:

James,

That is awesome!!!

On Mon, Nov 17, 2014 at 11:46 AM, James Scott <
jamescott@opscode.com> wrote:

Also, Chef 12 adds a new resource that moves the cookbook-based
reboot resource into core Chef:

http://docs.getchef.com/resource_reboot.html

On Mon, Nov 17, 2014 at 7:20 AM, Todd Pigram <
todd@toddpigram.com> wrote:

Sachin,

make sure that the 'windows::reboot_handler 'is at the top of
your run list as well as putting ('depends ‘windows’) in your
metadata.rb.

The install what you want and Chef will reboot and continue.

here is an example of installing RDSH for preparation for
Citrix XenApp install. It will starts the RDSH, reboot and then come back
and do the desktop experience, and so on… As long as you installed the
chef service during client install or created a scheduled task.

install RDS

powershell node[‘w2k8’][‘rds’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature RDS-RD-Server
EOH
not_if {reboot_pending?}
end

Install desktop experience

powershell node[‘w2k8’][‘deskexp’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Desktop-Experience
EOH
not_if {reboot_pending?}
end

powershell node[‘w2k8’][‘xps’] do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature XPS-Viewer
EOH
not_if {reboot_pending?}
end

windows_reboot 30 do
reason 'Chef said to’
only_if {reboot_pending?}
end

if you still have issues, create a library file called
reboot_pending.rb in you cookbook - I was given this by Alex Vinyar from
Chef. This was added to Chef Client back in version 11.12…But I still use
it

reboot_pending.rb

class Chef
class Resource
# include Chef::Mixin::ShellOut

def reboot_pending?
  # Any files listed here means reboot needed

(Registry.key_exists?(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager\PendingFileRenameOperations’) &&

Registry.get_value(‘HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager’,‘PendingFileRenameOperations’).any?) ||
# 1 for any value means reboot pending
# “9306cdfc-c4a1-4a22-9996-848cb67eddc3”=1

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’) &&

Registry.get_values(‘HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired’).select{|v| v[2] == 1 }.any?) ||
# 1 or 2 for ‘Flags’ value means reboot pending

(Registry.key_exists?(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’)
&&

[1,2].include?(Registry::get_value(‘HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile’,‘Flags’)))
||
# added by Alex

Registry.key_exists?(‘HKLM\Software\Microsoft\Windows\CurrentVersion\Component
Based Servicing\RebootPending’)
end

end
end

On Mon, Nov 17, 2014 at 8:30 AM, Sachin Gupta <
guptasachin1112mrt@gmail.com> wrote:

Hi All,

I have a use case where I need to reboot windows machine after
some package installation.

would like to know the possible best practice to implement
this use case;

a) Make a single recipe that will do the package installation
and product installation and then at last call chef handler to reboot
the machine

a) make two different chef recipe, the first recipe will
install packages and then reboot the machine,
the second recipe will do the remaining stuff. I don’t
know this will work or not.
Is there a way, we can return the chef client to run
the same cookbook after reboot??

I also gone through some documentation but nothing is useful.

Any help would the useful .

Thanks & Regards,
Sachin Kumar

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Todd Pigram
http://about.me/ToddPigram
www.linkedin.com/in/toddpigram/
@pigram86 on twitter
https://plus.google.com/+ToddPigram86
Mobile - 216-224-5769

Kenneth Barry
TuneIn | Build and Release Engineer
M: 409-673-0544
www.tunein.com