Skip to content

Instantly share code, notes, and snippets.

@gusztavvargadr
Last active April 5, 2024 10:37
Show Gist options
  • Save gusztavvargadr/1f0d7dddc7f48549368eaaedf19bfe55 to your computer and use it in GitHub Desktop.
Save gusztavvargadr/1f0d7dddc7f48549368eaaedf19bfe55 to your computer and use it in GitHub Desktop.
Chef scripts
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
$chefVersion = if ($env:CHEF_VERSION) { $env:CHEF_VERSION } else { "18.4.12" }
$chefPolicy = if ($env:CHEF_POLICY) { $env:CHEF_POLICY } else { "hello-world" }
$chefAttributes = if ($env:CHEF_ATTRIBUTES) { $env:CHEF_ATTRIBUTES } else { "default" }
$chefRunList = if ($env:CHEF_RUN_LIST) { $env:CHEF_RUN_LIST } else { "" }
$consulTemplateVersion = if ($env:CONSUL_TEMPLATE_VERSION) { $env:CONSUL_TEMPLATE_VERSION } else { "0.37.4" }
$chocolateyVersion = if ($env:CHOCOLATEY_VERSION) { $env:CHOCOLATEY_VERSION } else { "2.2.2" }
if ((Get-Command "C:/opscode/chef/bin/chef-client.bat" -ErrorAction Ignore).Count -eq 0) {
. { iwr -useb https://omnitruck.chef.io/install.ps1 } | iex; install -project chef -version "$($chefVersion)"
}
C:/opscode/chef/bin/chef-client.bat --version
$env:CHEF_LICENSE = "accept-silent"
if ((Get-Command "consul-template" -ErrorAction Ignore).Count -eq 0) {
@"
consul_template_version = '$($consulTemplateVersion)'
consul_template_architecture = 'amd64'
consul_template_archive_file = "consul-template_#{consul_template_version}_windows_#{consul_template_architecture}.zip"
consul_template_archive_remote_uri = "https://releases.hashicorp.com/consul-template/#{consul_template_version}/#{consul_template_archive_file}"
consul_template_archive_local_path = "C:/Windows/Temp/#{consul_template_archive_file}"
remote_file consul_template_archive_local_path do
source consul_template_archive_remote_uri
action :create
end
archive_file consul_template_archive_local_path do
destination 'C:/Windows/System32'
overwrite :auto
action :extract
end
"@ | C:/opscode/chef/bin/chef-apply.bat -s
}
consul-template --version
if ((Get-Command "choco" -ErrorAction Ignore).Count -eq 0) {
$env:chocolateyVersion = $chocolateyVersion
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 --version
$chefRunOptions = "-z"
$chefPolicyDir = "C:/Windows/Temp/chef-policy-$($chefPolicy)"
mkdir -Force $($chefPolicyDir)
pushd $($chefPolicyDir)
$chefPolicyUrl = "https://gist.github.com/gusztavvargadr/97d69ecebc4d9ac60bc5771ef483220e/raw/$($chefPolicy).tgz"
iwr -useb $($chefPolicyUrl) -o ./$($chefPolicy).tgz
tar -xzf ./$($chefPolicy).tgz
$chefAttributesFile="./attributes.${chefAttributes}.json"
$chefAttributesTemplateFile="./attributes.$($chefAttributes).json.template"
if (Test-Path $($chefAttributesTemplateFile)) {
consul-template -template="$($chefAttributesTemplateFile):$($chefAttributesFile)" -once -vault-renew-token=false -vault-retry-attempts=1
}
if (Test-Path $($chefAttributesFile)) {
$chefRunOptions = "$($chefRunOptions) -j $($chefAttributesFile)"
}
if ($($chefRunList)) {
$chefRunOptions = "$($chefRunOptions) -n $($chefRunList)"
}
"C:/opscode/chef/bin/chef-client.bat $($chefRunOptions)" | iex
popd
rm -Recurse -Force $($chefPolicyDir)
#!/usr/bin/env bash
set -euo pipefail
CHEF_VERSION="${CHEF_VERSION:-18.4.12}"
CHEF_POLICY="${CHEF_POLICY:-hello-world}"
CHEF_ATTRIBUTES="${CHEF_ATTRIBUTES:-default}"
CHEF_RUN_LIST="${CHEF_RUN_LIST:-}"
CONSUL_TEMPLATE_VERSION="${CONSUL_TEMPLATE_VERSION:-0.37.4}"
if ! [ -x "$(command -v chef-client)" ]; then
curl -Ls https://omnitruck.chef.io/install.sh | bash -s -- -P chef -v ${CHEF_VERSION}
apt-mark hold chef
fi
chef-client --version
export CHEF_LICENSE="accept-silent"
if ! [ -x "$(command -v consul-template)" ]; then
cat <<- EOF | chef-apply -s
consul_template_version = '${CONSUL_TEMPLATE_VERSION}'
consul_template_architecture = '$(dpkg --print-architecture)'.strip
consul_template_archive_file = "consul-template_#{consul_template_version}_linux_#{consul_template_architecture}.zip"
consul_template_archive_remote_uri = "https://releases.hashicorp.com/consul-template/#{consul_template_version}/#{consul_template_archive_file}"
consul_template_archive_local_path = "/tmp/#{consul_template_archive_file}"
remote_file consul_template_archive_local_path do
source consul_template_archive_remote_uri
action :create
end
archive_file consul_template_archive_local_path do
destination '/usr/bin'
mode '0755'
overwrite :auto
action :extract
end
EOF
fi
consul-template --version
CHEF_RUN_OPTIONS="-z"
CHEF_POLICY_DIR="/tmp/chef-policy-${CHEF_POLICY}"
mkdir -p ${CHEF_POLICY_DIR}
pushd ${CHEF_POLICY_DIR}
CHEF_POLICY_URL="https://gist.github.com/gusztavvargadr/97d69ecebc4d9ac60bc5771ef483220e/raw/${CHEF_POLICY}.tgz"
curl -Lso ./${CHEF_POLICY}.tgz ${CHEF_POLICY_URL}
tar -xzf ./${CHEF_POLICY}.tgz
CHEF_ATTRIBUTES_FILE="./attributes.${CHEF_ATTRIBUTES}.json"
CHEF_ATTRIBUTES_TEMPLATE_FILE="./attributes.${CHEF_ATTRIBUTES}.json.template"
if [ -f ${CHEF_ATTRIBUTES_TEMPLATE_FILE} ]; then
consul-template -template="${CHEF_ATTRIBUTES_TEMPLATE_FILE}:${CHEF_ATTRIBUTES_FILE}" -once -vault-renew-token=false -vault-retry-attempts=1 || true
fi
if [ -f ${CHEF_ATTRIBUTES_FILE} ]; then
CHEF_RUN_OPTIONS="${CHEF_RUN_OPTIONS} -j ${CHEF_ATTRIBUTES_FILE}"
fi
if ! [ -z ${CHEF_RUN_LIST} ]; then
CHEF_RUN_OPTIONS="${CHEF_RUN_OPTIONS} -n ${CHEF_RUN_LIST}"
fi
chef-client ${CHEF_RUN_OPTIONS}
popd
rm -Rf ${CHEF_POLICY_DIR}
provision_env = {
'CHEF_VERSION' => ENV['CHEF_VERSION'],
'CHEF_POLICY' => ENV['CHEF_POLICY'],
'CHEF_ATTRIBUTES' => ENV['CHEF_ATTRIBUTES'],
'CHEF_RUN_LIST' => ENV['CHEF_RUN_LIST'],
'CONSUL_TEMPLATE_VERSION' => ENV['CONSUL_TEMPLATE_VERSION'],
'VAULT_ADDR' => ENV['VAULT_ADDR'],
'VAULT_TOKEN' => ENV['VAULT_TOKEN'],
}
Vagrant.configure("2") do |config|
config.vm.define "windows", autostart: false do |config|
config.vm.box = "gusztavvargadr/windows-server-core"
config.vm.provision "shell",
path: "#{File.dirname(__FILE__)}/provision.ps1",
env: provision_env
end
config.vm.define "ubuntu", autostart: false do |config|
config.vm.box = "gusztavvargadr/ubuntu-server"
config.vm.provision "shell",
path: "#{File.dirname(__FILE__)}/provision.sh",
env: provision_env
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment