summaryrefslogtreecommitdiff
path: root/habitat-packages/scaffolding-chef/lib/scaffolding.ps1
blob: d4107a01aa75f6e9b3d523165eb944b58e3bceda (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
#
# A scaffolding for Chef Policyfile packages
#

if (!$scaffold_policy_name) {
    Write-Host "You must set `$scaffold_policy_name to a valid policy name. For example:`n `$scaffold_policy_name=base `n Will build a base.rb policyfile."
    exit 1
}

function Load-Scaffolding {
    $scaffold_chef_client = "stuartpreston/chef-client-detox"
    $scaffold_chef_dk = "core/chef-dk"

    $pkg_deps += @("$scaffold_chef_client", "core/cacerts")
    $pkg_build_deps += @("$scaffold_chef_dk", "core/git")
    $pkg_svc_run = "set_just_so_you_will_render"
}

function Invoke-DefaultBuildService {
    New-Item -ItemType directory -Path "$pkg_prefix/hooks"

    Add-Content -Path "$pkg_prefix/hooks/run" -Value @"
function Invoke-ChefClient {
  {{pkgPathFor "stuartpreston/chef-client-detox"}}/bin/chef-client.bat -z -l {{cfg.log_level}} -c $pkg_svc_config_path/client-config.rb -j $pkg_svc_config_path/attributes.json --once --no-fork --run-lock-timeout {{cfg.run_lock_timeout}}
}

`$splay_duration = Get-Random -InputObject (0..{{cfg.splay}}) -Count 1

`$splay_first_run_duration = Get-Random -InputObject (0..{{cfg.splay_first_run}}) -Count 1

`$env:SSL_CERT_FILE="{{pkgPathFor "core/cacerts"}}/ssl/cert.pem"

cd {{pkg.path}}

Start-Sleep -Seconds `$splay_first_run_duration
Invoke-ChefClient

while(`$true){
  Start-Sleep -Seconds `$splay_duration
  Start-Sleep -Seconds {{cfg.interval}}
  Invoke-ChefClient
}
"@
}


function Invoke-DefaultBuild {
    if (!(Test-Path -Path "$scaffold_policyfile_path")) {
        Write-BuildLine "Could not detect a policyfiles directory, this is required to proceed!"
        exit 1
    }

    Remove-Item "$scaffold_policyfile_path/*.lock.json" -Force
    $policyfile = "$scaffold_policyfile_path/$scaffold_policy_name.rb"

    Get-Content $policyfile | ? { $_.StartsWith("include_policy") } | % {
        $p = $_.Split()[1]
        $p = $p.Replace("`"", "").Replace(",", "")
        Write-BuildLine "Detected included policyfile, $p.rb, installing"
        chef install "$scaffold_policyfile_path/$p.rb"
    }
    Write-BuildLine "Installing $policyfile"
    chef install "$policyfile"
}

function Invoke-DefaultInstall {
    Write-BuildLine "Exporting Chef Infra Repository"
    chef export "$scaffold_policyfile_path/$scaffold_policy_name.lock.json" "$pkg_prefix"

    Write-BuildLine "Creating Chef Infra configuration"
    New-Item -ItemType directory -Path "$pkg_prefix/config"
    Add-Content -Path "$pkg_prefix/.chef/config.rb" -Value @"
cache_path "$($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("$pkg_svc_data_path/cache").Replace("\","/"))"
node_path "$($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("$pkg_svc_data_path/nodes").Replace("\","/"))"
role_path "$($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("$pkg_svc_data_path/roles").Replace("\","/"))"
chef_zero.enabled true
ENV['PSModulePath'] += "C:/Program\ Files/WindowsPowerShell/Modules"
"@

    Write-BuildLine "Creating initial bootstrap configuration"
    Copy-Item -Path "$pkg_prefix/.chef/config.rb" -Destination "$pkg_prefix/config/bootstrap-config.rb"
    Add-Content -Path "$pkg_prefix/config/bootstrap-config.rb" -Value @"
ENV['PATH'] += ";C:/WINDOWS;C:/WINDOWS/system32/;C:/WINDOWS/system32/WindowsPowerShell/v1.0;C:/ProgramData/chocolatey/bin"
"@

    Write-BuildLine "Creating Chef Infra client configuration"
    Copy-Item -Path "$pkg_prefix/.chef/config.rb" -Destination "$pkg_prefix/config/client-config.rb"
    Add-Content -Path "$pkg_prefix/config/client-config.rb" -Value @"
ssl_verify_mode {{cfg.ssl_verify_mode}}
ENV['PATH'] += "{{cfg.env_path_prefix}}"

{{#if cfg.data_collector.enable ~}}
chef_guid "{{sys.member_id}}"
data_collector.token "{{cfg.data_collector.token}}"
data_collector.server_url "{{cfg.data_collector.server_url}}"
{{/if ~}}
"@

    Write-BuildLine "Generating config/attributes.json"
    Add-Content -Path "$pkg_prefix/config/attributes.json" -Value @"
{{#if cfg.attributes}}
{{toJson cfg.attributes}}
{{else ~}}
{}
{{/if ~}}
"@

    Write-BuildLine "Generating Chef Habiat configuration, default.toml"
    Add-Content -Path "$pkg_prefix/default.toml" -Value @"
interval = 1800
splay = 1800
splay_first_run = 0
run_lock_timeout = 1800
log_level = "warn"
env_path_prefix = ";C:/WINDOWS;C:/WINDOWS/system32/;C:/WINDOWS/system32/WindowsPowerShell/v1.0;C:/ProgramData/chocolatey/bin"
ssl_verify_mode = ":verify_peer"

[chef_license]
acceptance = "undefined"

[data_collector]
enable = false
token = "set_to_your_token"
server_url = "set_to_your_url"
"@

    $scaffold_data_bags_path = "not_using_data_bags" # Set default to some string so Test-Path returns false instead of error. Thanks Powershell!
    if (Test-Path "$scaffold_data_bags_path") {
        Write-BuildLine "Detected a data bags directory, installing into package"
        Copy-Item "$scaffold_data_bags_path/*" -Destination "$pkg_prefix" -Recurse
    }
}