Skip to content

Instantly share code, notes, and snippets.

@justmiles
Last active November 9, 2017 19:20
Show Gist options
  • Save justmiles/bb7f2cdb235eedc5d5a6aefad82d4316 to your computer and use it in GitHub Desktop.
Save justmiles/bb7f2cdb235eedc5d5a6aefad82d4316 to your computer and use it in GitHub Desktop.
userdata chef bootstrap
#!/bin/bash -xev
# Install Dependencies
yum install -y unzip
# Install jq
[ -e /usr/bin/jq ] || $(curl -L https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64 -o /usr/bin/jq \
&& chmod +x /usr/bin/jq)
# Set environment Variables
export INSTANCE_ID=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document/ | jq -r '.instanceId')
export NODE_NAME=AUTOSCALE-$INSTANCE_ID
function install_aws {
curl -s -O https://s3.amazonaws.com/aws-cli/awscli-bundle.zip \
&& unzip awscli-bundle.zip \
&& ./awscli-bundle/install -i /usr/local/aws -b /usr/bin/aws \
&& rm -rf ./awscli-bundle awscli-bundle.zip
}
[ -e /usr/local/aws ] || install_aws
# Do some chef pre-work
/bin/mkdir -p /etc/chef /var/lib/chef /var/log/chef
# Install chef
[ -e /bin/chef-client ] || curl -L https://omnitruck.chef.io/install.sh | bash -s -- -v ${chef_version}
# Get validator key
[ -e /etc/chef/"${validation_client_name}".pem ] || aws s3 cp s3://"${s3_repo}"/"${validation_client_name}".pem /etc/chef/"${validation_client_name}".pem
# Create first-boot.json
[ -e /etc/chef/first-boot.json ] || cat > "/etc/chef/first-boot.json" << EOF
{
"tags" : ["$INSTANCE_ID","${node_name_prefix}"],
"run_list" : ${run_list}
}
EOF
# Create client.rb
[ -e /etc/chef/client.rb ] || cat > "/etc/chef/client.rb" << EOF
log_location STDOUT
chef_server_url "${server_url}"
validation_client_name "${validation_client_name}"
validation_key "/etc/chef/"${validation_client_name}".pem"
node_name "$NODE_NAME"
EOF
chef-client -j /etc/chef/first-boot.json -E ${env}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment