Last active
March 31, 2017 02:56
-
-
Save JC1738/9378979 to your computer and use it in GitHub Desktop.
Use Node Search and Powershell to invoke a chef run by role
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#powershell | |
Set-Location c:\chef | |
#debugging, set to false, so don't execute run | |
$invokeRun = $true | |
#update these with teamcity parameters | |
$userName = "XXXXX" | |
$password = "XXXXX" | |
$searchRole = "gc-web" | |
$deployRole = "gc-deploy" | |
$webRole = "gc-web" | |
$buildToDeploy = "Trunk_UmamiFrontEnd_6082.prod.com.zip" | |
$deployRoleFile = "gc-deploy.json" | |
$deployTemplateRoleFile = "gc-deploy.template.json" | |
$environment = "env_pp_guestcenter" | |
#$deploy = "web" | |
$deploy = "deploysite" | |
$knifeCmd = @' | |
knife role show gc-deploy -F json | |
'@ | |
#$knifeCmd | |
$results = Invoke-Expression -Command:$knifeCmd | |
$newDeploy = $results -replace ("Trunk_UmamiFrontEnd_[0-9]+.prod.com.zip", $buildToDeploy) | |
$newDeploy | |
$filePath = "./{0}" -F $deployRoleFile | |
Set-content -path $filePath -value $newDeploy | |
$knifeCmd = @' | |
knife role from file {0} | |
'@ | |
$knifeCmd = $knifeCmd -f $deployRoleFile | |
$knifeCmd | |
Invoke-Expression -Command:$knifeCmd | |
$knifeCmd = @' | |
knife exec -E "nodes.find('chef_environment:envReplace AND role:roleReplace') { |node| puts node.name}" | |
'@ | |
$knifeCmd = $knifeCmd -replace 'roleReplace', $searchRole | |
$knifeCmd = $knifeCmd -replace 'envReplace', $environment | |
$knifeCmd | |
$machines = Invoke-Expression -Command:$knifeCmd | |
$machines = $machines.split("`n") | |
$machines.Count | |
foreach ($nodeName in $machines) { | |
$nodeName | |
#needs to be left justified because of here string | |
$command = @' | |
knife exec -E "nodes.find(:name => 'machine') { |node| puts node['ipaddress'] }" | |
'@ | |
$knifeCmd = $command -replace 'machine', $nodeName | |
$knifeCmd | |
$ipAddress = Invoke-Expression -Command:$knifeCmd | |
$ipAddress | |
if ($deploy -eq "deploysite") | |
{ | |
$knifeCmd = @' | |
knife exec -E "nodes.find(:name => 'machine') { |n| n.run_list.remove('role[roleReplace]');n.save }" | |
'@ | |
$knifeCmd = $knifeCmd -replace 'roleReplace', $searchRole | |
$knifeCmd = $knifeCmd -replace 'machine', $nodeName | |
$knifeCmd | |
Invoke-Expression -Command:$knifeCmd | |
$knifeCmd = @' | |
knife exec -E "nodes.find(:name => 'machine') { |n| puts n.run_list('role[roleReplace]');n.save}" | |
'@ | |
$knifeCmd = $knifeCmd -replace 'roleReplace', $deployRole | |
$knifeCmd = $knifeCmd -replace 'machine', $nodeName | |
$knifeCmd | |
Invoke-Expression -Command:$knifeCmd | |
} | |
#needs to be left justified because of here string | |
$command = @' | |
knife winrm {0} 'chef-client -c c:/chef/client.rb' -m -x {1} -P {2} | |
'@ | |
$knifeCmd = $command -f $ipAddress, $userName, $password | |
$knifeCmd | |
#actual do run based of variable, makes easier debugging | |
if ($invokeRun -eq $true) | |
{ | |
Invoke-Expression -Command:$knifeCmd | |
} | |
if ($deploy -eq "deploysite") | |
{ | |
$knifeCmd = @' | |
knife exec -E "nodes.find(:name => 'machine') { |n| n.run_list.remove('role[roleReplace]');n.save }" | |
'@ | |
$knifeCmd = $knifeCmd -replace 'roleReplace', $deployRole | |
$knifeCmd = $knifeCmd -replace 'machine', $nodeName | |
$knifeCmd | |
Invoke-Expression -Command:$knifeCmd | |
$knifeCmd = @' | |
knife exec -E "nodes.find(:name => 'machine') { |n| puts n.run_list('role[roleReplace]');n.save}" | |
'@ | |
$knifeCmd = $knifeCmd -replace 'roleReplace', $searchRole | |
$knifeCmd = $knifeCmd -replace 'machine', $nodeName | |
$knifeCmd | |
Invoke-Expression -Command:$knifeCmd | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment