summaryrefslogtreecommitdiff
path: root/habitat/plan.ps1
blob: dfb0c9cdcc23cc006f91420a38c5ffa09e01c221 (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
$ErrorActionPreference = 'Stop'
$pkg_name="chef-infra-client"
$pkg_origin="chef"
$pkg_version=(Get-Content $PLAN_CONTEXT/../VERSION)
$pkg_description="Chef Infra Client is an agent that runs locally on every node that is under management by Chef Infra. This package is binary-only to provide Chef Infra Client executables. It does not define a service to run."
$pkg_maintainer="The Chef Maintainers <maintainers@chef.io>"
$pkg_upstream_url="https://github.com/chef/chef"
$pkg_license=@("Apache-2.0")
$pkg_filename="${pkg_name}-${pkg_version}.zip"
$pkg_bin_dirs=@(
    "bin"
    "vendor/bin"
)
$pkg_deps=@(
  "core/cacerts"
  "chef/ruby31-plus-devkit"
  "chef/chef-powershell-shim"
)

function Invoke-Begin {
    Write-BuildLine " ** Invoke-Begin Top"
    [Version]$hab_version = (hab --version).split(" ")[1].split("/")[0]
    if ($hab_version -lt [Version]"0.85.0" ) {
        Write-Warning "(╯°□°)╯︵ ┻━┻ I CAN'T WORK UNDER THESE CONDITIONS!"
        Write-Warning ":habicat: I'm being built with $hab_version. I need at least Hab 0.85.0, because I use the -IsPath option for setting/pushing paths in SetupEnvironment."
        throw "unable to build: required minimum version of Habitat not installed"
    } else {
        Write-BuildLine ":habicat: I think I have the version I need to build."
    }
}

function Invoke-SetupEnvironment {
    Write-BuildLine " ** Invoke-SetupEnvironment Top"
    Push-RuntimeEnv -IsPath GEM_PATH "$pkg_prefix/vendor"

    Set-RuntimeEnv APPBUNDLER_ALLOW_RVM "true" # prevent appbundler from clearing out the carefully constructed runtime GEM_PATH
    Set-RuntimeEnv FORCE_FFI_YAJL "ext" # Always use the C-extensions because we use MRI on all the things and C is fast.
    Set-RuntimeEnv -IsPath SSL_CERT_FILE "$(Get-HabPackagePath cacerts)/ssl/cert.pem"
    Set-RuntimeEnv LANG "en_US.UTF-8"
    Set-RuntimeEnv LC_CTYPE "en_US.UTF-8"
}

# function Invoke-Download() {
#     Write-BuildLine " ** Locally creating archive of latest repository commit at ${HAB_CACHE_SRC_PATH}/${pkg_filename}"
#     # source is in this repo, so we're going to create an archive from the
#     # appropriate path within the repo and place the generated tarball in the
#     # location expected by do_unpack
#     # $env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
#     # $git_path = "c:\\Program Files\\Git\\cmd"
#     # $env:Path = $git_path + ";" + [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
#     try {
#         # Push-Location (Resolve-Path "$PLAN_CONTEXT/../").Path
#         # # Write-Output "`n *** Installing Choco *** `n"
#         Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
#         choco install git -y
#         $env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
#         # $env:Path = $git_path + ";" + [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
#         $full_git_path = $("C:\\Program Files\\Git\\cmd\\git.exe")
#         Write-Output "Is Git REALLY Installed? "
#         Test-Path -Path $full_git_path
#         # # Get-Command "Git"
#         Write-Output "Hab source path is : ${HAB_CACHE_SRC_PATH}`n"
#         Write-Output "Package Filename is : ${pkg_filename}"
#         Write-Output "getting Variables now`n"
#         $path = Get-Variable -Name HAB_CACHE_SRC_PATH -ValueOnly
#         $file = Get-Variable -Name pkg_filename -ValueOnly
#         Write-Output "Here's the path : $path"
#         Write-Output "Here's the file : $file"
#         $command = "c:\\Program` Files\\Git\\cmd\\git.exe archive --format=zip --output=$($path + "\" + $file) HEAD"
#         Write-Output "Here's the whole command : $command"
#         # c:\\Program` Files\\Git\\cmd\\git.exe archive --format=zip --output=$($path + "\" + $file) HEAD
#         # Invoke-Expression "& $command" -Verbose -ErrorAction Stop
#         # [System.Diagnostics.Process]::Start("c:\\Program` Files\\Git\\cmd\\git.exe archive --format=zip --output=$(${HAB_CACHE_SRC_PATH} + "\\" + ${pkg_filename}) HEAD --verbose")
#         # Write-Output "Now archiving the repo"
#         # [System.Diagnostics.Process]::Start("$full_git_path archive --format=zip --output=${HAB_CACHE_SRC_PATH}\\${pkg_filename} HEAD --verbose")
#         # Invoke-Expression -Command "$($full_git_path) archive --format=zip --output=${HAB_CACHE_SRC_PATH}\\${pkg_filename} HEAD --verbose"
#         # Write-Output "Zipping the Repo is finished"
#         # Start-Sleep -Seconds 30
#         # Write-Output " *** Finished Creating the Archive *** `n"
#         # # getting an error about the archive being in use, adding the sleep to let other handles on the file finish.
#         if (-not $?) { throw "unable to create archive of source" }
#         Write-Output "Made it to the bottom of the Try statement"
#     catch{
#         Write-BuildLine "Plan.ps1 threw an error in Invoke-Download - An error occurred:"
#         Write-BuildLine $_
#     }
#     } finally {
#         Write-Output " *** Executing Pop-Location *** "
#         Pop-Location
#         Write-Output " *** Finished Pop-Location *** "
#     }
# }
function Invoke-Download {
    Write-BuildLine " ** Invoke-Download Top"
    Write-BuildLine " ** Locally creating archive of latest repository commit at ${HAB_CACHE_SRC_PATH}/${pkg_filename}"
    $env:HAB_CACHE_SRC_PATH = ${HAB_CACHE_SRC_PATH}
    $env:pkg_filename = ${pkg_filename}
    try {
        Push-Location (Resolve-Path "$PLAN_CONTEXT/../").Path
        if(-not(Test-Path -Path "C:\\ProgramData\\chocolatey\\bin\\choco.exe" -ErrorAction Ignore)){
            Write-Host "`n *** Installing Choco *** `n"
            Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
        }
        if(-not(Test-Path -Path "C:\\Program Files\\Git\\cmd\\git.exe" -ErrorAction Ignore )){
            Write-Host "Installing Git"
            choco install git -y
            $env:Path += "C:\Program` Files\Git\cmd;"
        }
        $env:Path = 'C:\Program Files\Git\cmd;' + [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
        Invoke-Expression -Command "git archive --format=zip --output=$HAB_CACHE_SRC_PATH\\$pkg_filename HEAD" -ErrorAction Stop -Verbose

        if(Test-Path -Path "$HAB_CACHE_SRC_PATH\\$pkg_filename") {
          Write-Host (Get-ChildItem -Path "$HAB_CACHE_SRC_PATH\\$pkg_filename")
        }
        Write-Host "Searching for Ruby"
        $paths = Get-ChildItem -Path c:\ -File "ruby.exe" -Recurse -ErrorAction SilentlyContinue
        foreach($path in $paths){
            if ($path -match "hab") {
                $hab_path = Split-Path -Path $path -Parent
                $env:Path += $hab_path + ";" + $nev:path
            }
        }
    }
    catch {
        Write-BuildLine "Plan.ps1 threw an error in Invoke-Download - An error occurred:"
        Write-BuildLine $_
    }
    finally {
        Pop-Location
    }
}

function Invoke-Verify {
    Write-BuildLine " ** Invoke Verify Top"
    Write-BuildLine " ** Skipping checksum verification on the archive we just created."
    Write-BuildLine " HAB_CACHE_SRC_PATH = $HAB_CACHE_SRC_PATH "
    Write-BuildLine " pkg_filename = $pkg_filename "
    return 0
}

function Invoke-Prepare {
    Write-BuildLine " ** Invoke Prepare Top"
    $env:GEM_HOME = "$pkg_prefix/vendor"

    try {
        Push-Location "${HAB_CACHE_SRC_PATH}/${pkg_dirname}"
        gem install bundler
        $output = Get-ChildItem -Path C:\ -File "bundle.*" -Recurse -ErrorAction SilentlyContinue
        Write-Output "Here's the output : $output"
        Write-BuildLine " ** Configuring bundler for this build environment"
        bundle config --local without server docgen maintenance pry travis integration ci chefstyle
        if (-not $?) { throw "unable to configure bundler to restrict gems to be installed" }
        bundle config --local retry 5
        bundle config --local silence_root_warning 1
    } finally {
        Pop-Location
    }
}

function Invoke-Build {
    Write-BuildLine " ** Invoke-Build Top"
    try {
        Push-Location "${HAB_CACHE_SRC_PATH}/${pkg_dirname}"

        $env:_BUNDLER_WINDOWS_DLLS_COPIED = "1"

        Write-BuildLine " ** Using bundler to retrieve the Ruby dependencies"
        bundle install --jobs=3 --retry=3
        if (-not $?) { throw "unable to install gem dependencies" }
        Write-BuildLine " ** 'rake install' any gem sourced as a git reference so they'll look like regular gems."
        foreach($git_gem in (Get-ChildItem "$env:GEM_HOME/bundler/gems")) {
            try {
                Push-Location $git_gem
                Write-BuildLine " -- installing $git_gem"
                # The rest client doesn't have an 'Install' task so it bombs out when we call Rake Install for it
                # Happily, its Rakefile ultimately calls 'gem build' to build itself with. We're doing that here.
                if ($git_gem -match "rest-client"){
                    $gemspec_path = $git_gem.ToString() + "\rest-client.windows.gemspec"
                    gem build $gemspec_path
                    $gem_path = $git_gem.ToString() + "\rest-client*.gem"
                    gem install $gem_path
                }
                else {
                    rake install $git_gem --trace=stdout # this needs to NOT be 'bundle exec'd else bundler complains about dev deps not being installed
                }
                if (-not $?) { throw "unable to install $($git_gem) as a plain old gem" }
            } finally {
                Pop-Location
            }
        }
        Write-BuildLine " ** Running the chef project's 'rake install' to install the path-based gems so they look like any other installed gem."
        $install_attempt = 0
        do {
            Start-Sleep -Seconds 5
            $install_attempt++
            Write-BuildLine "Install attempt $install_attempt"
            bundle exec rake install:local --trace=stdout
        } while ((-not $?) -and ($install_attempt -lt 5))

    } finally {
        Pop-Location
    }
}

function Invoke-Install {
    Write-BuildLine " ** Invoke-Install Top"
    try {
        Push-Location $pkg_prefix
        $env:BUNDLE_GEMFILE="${HAB_CACHE_SRC_PATH}/${pkg_dirname}/Gemfile"
        $app_bundler = @"
#! ruby
#
# This file was generated by RubyGems.
#
# The application 'appbundler' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

Gem.use_gemdeps

version = ">= 0.a"

str = ARGV.first
if str
    str = str.b[/\A_(.*)_\z/, 1]
    if str and Gem::Version.correct?(str)
    version = str
    ARGV.shift
    end
end

if Gem.respond_to?(:activate_bin_path)
load Gem.activate_bin_path('appbundler', 'appbundler', version)
else
gem "appbundler", version
load Gem.bin_path("appbundler", "appbundler", version)
end
"@

        $app_bundler_bat = @"
@ECHO OFF
"ruby.exe" "%~dpn0" %*
"@
        Write-Output "What is the hab_path at this point? : $hab_path"
        Set-Content -Path "$hab_path\appbundler" -Value $app_bundler
        Set-Content -Path "$hab_path\appbundler.bat" -Value $app_bundler_bat
        foreach($gem in ("chef-bin", "chef", "inspec-core-bin", "ohai")) {
            Write-BuildLine "** generating binstubs for $gem with precise version pins"
            Start-Process -FilePath "$hab_path\appbundler.bat" -ArgumentList "${HAB_CACHE_SRC_PATH}/${pkg_dirname} $pkg_prefix/bin $gem" -Wait -NoNewWindow
            # appbundler.bat "${HAB_CACHE_SRC_PATH}/${pkg_dirname}" $pkg_prefix/bin $gem
            if (-not $?) { throw "Failed to create appbundled binstubs for $gem"}
        }
        Remove-StudioPathFrom -File $pkg_prefix/vendor/gems/chef-$pkg_version*/Gemfile
    } finally {
        Pop-Location
    }
}

function Invoke-After {
    Write-BuildLine " ** Invoke-After Top"
    # Trim the fat before packaging

    # We don't need the cache of downloaded .gem files ...
    Remove-Item $pkg_prefix/vendor/cache -Recurse -Force
    # ... or bundler's cache of git-ref'd gems
    Remove-Item $pkg_prefix/vendor/bundler -Recurse -Force

    # We don't need the gem docs.
    Remove-Item $pkg_prefix/vendor/doc -Recurse -Force
    # We don't need to ship the test suites for every gem dependency,
    # only Chef's for package verification.
    Get-ChildItem $pkg_prefix/vendor/gems -Filter "spec" -Directory -Recurse -Depth 1 `
        | Where-Object -FilterScript { $_.FullName -notlike "*chef-$pkg_version*" }   `
        | Remove-Item -Recurse -Force
    # Remove the byproducts of compiling gems with extensions
    Get-ChildItem $pkg_prefix/vendor/gems -Include @("gem_make.out", "mkmf.log", "Makefile") -File -Recurse `
        | Remove-Item -Force
}

function Remove-StudioPathFrom {
    Param(
        [Parameter(Mandatory=$true)]
        [String]
        $File
    )
    Write-BuildLine " ** Remove-StudioPathFrom Top"
    (Get-Content $File) -replace ($env:FS_ROOT -replace "\\","/"),"" | Set-Content $File
}