forked from Nordstrom/chefdk_bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.ps1
163 lines (131 loc) · 5.12 KB
/
bootstrap.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Copyright 2015 Nordstrom, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Warning "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!"
Break
}
$targetChefDk = '0.10.0'
$bootstrapCookbook = 'chefdk_bootstrap'
function promptContinue {
param ($msg="Chefdk_bootstrap encountered an error")
$yn = Read-Host "$Msg. Continue? [y|N]"
if ( $yn -NotLike 'y*' ) {
Break
}
}
function die {
param ($msg="Chefdk_bootstrap encountered an error. Exiting")
Write-host "$msg."
Break
}
if ($args[0]) {
$bootstrapCookbook = $args[0]
}
if ($args[1]) {
$privateSource = "source '$args[1]'"
}
$userChefDir = Join-Path -path $env:USERPROFILE -childPath 'chef'
$dotChefDKDir = Join-Path -path $env:LOCALAPPDATA -childPath 'chefdk'
$tempInstallDir = Join-Path -path $env:TEMP -childpath 'chefdk_bootstrap'
$berksfilePath = Join-Path -path $tempInstallDir -childPath 'Berksfile'
$chefConfigPath = Join-Path -path $tempInstallDir -childPath 'client.rb'
$omniUrl = "https://omnitruck.chef.io/install.ps1"
# Set HOME to be c:\users\<username> so cookbook gem installs are on the c:\
# drive
$env:HOME = $env:USERPROFILE
$berksfile = @"
source 'https://supermarket.chef.io'
$privateSource
cookbook '$bootstrapCookbook'
"@
$chefConfig = @"
cookbook_path File.join(Dir.pwd, 'berks-cookbooks')
"@
$introduction = @"
### This bootstrap script will:
1. Install the ChefDK version $targetChefDk.
2. Download the chefdk_bootstrap cookbook via Berkshelf
3. Run chef-client to install the rest of the tools you'll need.
"@
Clear-Host
Write-Host $introduction
# create the temporary installation directory
if (!(Test-Path $tempInstallDir -pathType container)) {
New-Item -ItemType 'directory' -path $tempInstallDir
}
# Write out a local Berksfile for Berkshelf to use
$berksfile | Out-File -FilePath $berksfilePath -Encoding ASCII
# Write out minimal chef-client config file
$chefConfig | Out-File -FilePath $chefConfigPath -Encoding ASCII
# Install ChefDK from chef omnitruck, unless installed already
Write-host "Checking for installed ChefDK version"
$app = Get-CimInstance -classname win32_product -filter "Name like 'chef development kit%'"
$version = $app.Version
if ( $version -like "$targetChefDk*" ) {
Write-Host "The ChefDK version $version is already installed."
} else {
if ( $version -eq $null ) {
Write-Host "No ChefDK found. Installing the ChefDK version $targetChefDk"
} else {
Write-Host "Upgrading the ChefDK from $version to $targetChefDk"
Write-Host "Uninstalling ChefDK version $version. This might take a while..."
Invoke-CimMethod -InputObject $app -MethodName Uninstall
if ( -not $? ) { promptContinue "Error uninstalling ChefDK version $version" }
if (Test-Path $dotChefDKDir) {
Remove-Item -Recurse $dotChefDKDir
}
}
if ( $env:http_proxy ) {
$installScript = Invoke-WebRequest -UseBasicParsing $omniUrl -Proxy $env:http_proxy -ProxyUseDefaultCredentials
if ( -not $? ) { die "Error downloading $omniUrl using proxy $env:http_proxy." }
} else {
$installScript = Invoke-WebRequest -UseBasicParsing $omniUrl
if ( -not $? ) { die "Error downloading $omniUrl. Do you need to set `$env:http_proxy ?" }
}
$installScript | Invoke-Expression
if ( -not $? ) { die "Error running installation script" }
Write-Host "Installing ChefDK version $targetChefDk. This might take a while..."
install -channel stable -project chefdk -version $targetChefDk
if ( -not $? ) { die "Error installing the ChefDK version $targetChefDk" }
}
# Add ChefDK to the path
$env:Path += ";C:\opscode\chefdk\bin"
Push-Location $tempInstallDir
# Install the bootstrap cookbooks using Berkshelf
$env:BERKSHELF_CHEF_CONFIG = $chefConfigPath
berks vendor
if ( -not $? ) { Pop-Location; die "Error running berks to download cookbooks." }
# run chef-client (installed by ChefDK) to bootstrap this machine
chef-client -A -z -l error -c $chefConfigPath -o $bootstrapCookbook
if ( -not $? ) { Pop-Location; die "Error running chef-client." }
# Cleanup
if (Test-Path $berksfilePath) {
Remove-Item $berksfilePath
}
if (Test-Path "$berksfilePath.lock") {
Remove-Item "$berksfilePath.lock"
}
if (Test-Path $chefConfigPath) {
Remove-Item $chefConfigPath
}
if (Test-Path nodes) {
Remove-Item -Recurse nodes
}
if (Test-Path berks-cookbooks) {
Remove-Item -Recurse berks-cookbooks
}
Pop-Location
# End message to indicate completion of setup
Write-Host "`n`nCongrats!!! Your workstation is now set up for Chef Development!"