Skip to content

Instantly share code, notes, and snippets.

@bfrancom
Last active January 3, 2024 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bfrancom/a95442c4206dd02121e3433c3ee9c361 to your computer and use it in GitHub Desktop.
Save bfrancom/a95442c4206dd02121e3433c3ee9c361 to your computer and use it in GitHub Desktop.
cloud-init example
#cloud-config
#
## Install additional packages on first boot
##
## Default: none
##
## if packages are specified, this apt_update will be set to true
##
## packages may be supplied as a single package name or as a list
## with the format [<package>, <version>] wherein the specifc
## package version will be installed.
#packages:
# - pwgen
# - pastebinit
# - [libpython2.7, 2.7.3-0ubuntu3.1]
packages:
- java-1.8.0-openjdk
- python2
- unzip
- wget
ssh_authorized_keys:
- ssh-rsa AAAABBB123jibberish/ bfrancom@C0MPU73R.local
- ssh-rsa - ssh-rsa CCC456yackityschmackity/ yourmom@S3RV3R.local
# install custom scripts
write_files:
-
content: |
[Unit]
Description=myapp App
After=network.target
[Service]
Type=simple
User=ec2-user
WorkingDirectory=/home/ec2-user
ExecStart=/bin/bash -c "/usr/bin/java $JAVA_FLAGS -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode -XX:MaxPermSize=2048M -XX:PermSize=2048M -Xmx2048M -jar latest.war cron --spring.config.location=settings.properties --spring.profiles.active=cron,uat,deploy,-local"
Environment="LANE=cron"
Restart=always
RestartSec=10s
[Install]
WantedBy=multi-user.target
owner: "root:root"
path: /usr/lib/systemd/system/myapp-app.service
permissions: "644"
-
content: |
#!/bin/bash
if [ ! -f /home/ec2-user/settings.properties ]; then
echo "FATAL: Could not find settings.properties!"
exit 1;
fi
. /home/ec2-user/.bash_profile
# Any errors beyond this point and we fail...
set -e
# Launch it!
echo -n 'Launching...'
sudo systemctl restart myapp-app
echo 'done.'
echo -n 'Waiting for app...'
until curl -sf localhost:8080; do
echo -n '.'
sleep 10
done
echo 'done.'
echo "Launched $FILE" >> /var/log/myapp/install.log
path: /run/bounce.sh
permissions: "700"
-
content: |
#!/bin/bash
if [ -z $1 ]; then
echo "FATAL: No build specified. Usage: $0 <build>"
echo "Example: 0.1.0-123-dev.war"
exit 1
fi
if [ ! -f settings.properties ]; then
echo "FATAL: Could not find settings.properties!"
exit 1;
fi
FILE=$1
. /home/ec2-user/.bash_profile
if [ -z "$LANE" ] ; then
echo "WARNING: Unknown lane."
LANE="unknown"
fi
# Any errors beyond this point and we fail...
set -e
# Download the latest from S3
aws s3 cp "s3://myapp-builds/$FILE" ./
# Leave a record of the latest in our log file
echo "$FILE" >> /etc/myapp/versions
# Rename our file to latest
mv "$FILE" latest.war
./bounce.sh
path: /run/deploy.sh
permissions: "700"
-
content: |
#!/usr/bin/env bash
lane=$1
if [[ -z "$lane" ]]
then
echo "Enter deploy lane:"
read lane
fi
lanes file push "${lane}" bounce.sh /home/ec2-user/
path: /run/installBounce.sh
permissions: "700"
-
content: |
#!/usr/bin/env bash
lane=$1
if [[ -z "$lane" ]]
then
echo "Enter deploy lane:"
read lane
fi
lanes file push "${lane}" deploy.sh /home/ec2-user/
path: /run/installDeploy.sh
permissions: "700"
# configure scalyr agent
-
content: |
// Configuration for the Scalyr Agent. For help:
//
// https://www.scalyr.com/help/scalyr-agent-2
{
// Enter a "Write Logs" api key for your account. These are available at https://www.scalyr.com/keys
api_key: "",
// Fields describing this server. These fields are attached to each log message, and
// can be used to filter data from a particular server or group of servers.
server_attributes: {
env: "myapp",
lane: "uat",
// Fill in this field if you'd like to override the server's hostname.
//serverHost: "uat-app",
// You can add whatever additional fields you'd like.
// tier: "uat"
}
// Log files to upload to Scalyr. You can use '*' wildcards here.
logs: [
// { path: "/var/log/httpd/access.log", attributes: {parser: "accessLog"} }
{path: "/var/log/myapp/myapp.log"},
{path: "/var/log/secure"}
],
monitors: [
]
}
path: /run/agent.json
permissions: "644"
-
content: |
/var/log/myapp/*.log {
rotate 30
daily
copytruncate
compress
missingok
notifempty
}
path: /etc/logrotate.conf
append: true
-
content: |
#!/bin/bash
set +e
rpm -ivh --nodeps https://scalyr-repo.s3.amazonaws.com/stable/yum/binaries/noarch/scalyr-agent-2-2.0.59-1.noarch.rpm
set -e
cp /run/agent.json /etc/scalyr-agent-2/agent.json
scalyr-agent-2 start
path: /etc/scalyr_install.sh
permissions: "744"
-
content: |
#!/bin/bash
path: /etc/environment
content: |
LANE=
append: true
runcmd:
#install aws cli
- curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
- unzip awscliv2.zip
- sudo ./aws/install
#copy custom scripts to correct location to run
- cp -a /run/*.sh /home/ec2-user/
- chown ec2-user:ec2-user /home/ec2-user/*
- chmod u+x /home/ec2-user/*.sh
- mkdir -p /var/log/myapp/
#upgrade OS
#- yum upgrade -y
#install scalyr
- ln -s /usr/bin/python2 /usr/bin/python
#- ln -s /usr/bin/python2 /usr/bin/python
#- rpm -ivh --nodeps https://scalyr-repo.s3.amazonaws.com/stable/yum/binaries/noarch/scalyr-agent-2-2.0.59-1.noarch.rpm
# - wget -q https://www.scalyr.com/scalyr-repo/stable/latest/scalyr-repo-bootstrap-1.2.2-1.noarch.rpm
# - yum remove scalyr-repo scalyr-repo-bootstrap -y
# - yum install --nogpgcheck scalyr-repo-bootstrap-1.2.2-1.noarch.rpm -y
# - yum install scalyr-repo -y
# - yum install scalyr-agent-2 -y --skip-broken
# - cp /run/agent.json /etc/scalyr-agent-2/agent.json
# - scalyr-agent-2 start
- systemctl daemon-reload
- [sh, /etc/scalyr_install.sh]
- reboot
output : { all : '| tee -a /var/log/cloud-init-output.log' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment