Last active
January 17, 2019 17:50
-
-
Save mikew/ff0c0422df3ecdeb6e034067e64b5c92 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
main() { | |
local service_name="${1:-systemd-cloudwatch}" | |
local group_name="${2:-vineview}" | |
local filter_unit="${3}" | |
[ -z "${service_name}" ] && usage | |
local final_dir="$HOME/${service_name}" | |
if [ -d "${final_dir}" ]; then | |
echo "${final_dir} already exists, exiting." | |
exit 1 | |
fi | |
local service_file="sst.${service_name}.service" | |
mkdir -p \ | |
"${final_dir}" \ | |
"${final_dir}/data" | |
docker-compose-config > "${final_dir}/docker-compose.yml" | |
env-file > "${final_dir}/env" | |
systemd-cloudwatch-config "${group_name}" "${filter_unit}" > "${final_dir}/data/systemd-cloud-watch.conf" | |
systemd-service "${final_dir}" > "${final_dir}/${service_file}" | |
if [ ! -f "/etc/systemd/system/${service_file}" ]; then | |
sudo cp "${final_dir}/${service_file}" /etc/systemd/system/ | |
sudo systemctl daemon-reload | |
sudo systemctl enable "${service_file}" | |
sudo systemctl start "${service_file}" | |
fi | |
} | |
usage() { | |
echo "$0 service_name [group_name] [filter_unit]" | |
exit 1 | |
} | |
docker-compose-config() { | |
echo " | |
main: | |
image: marcogroppo/systemd-cloud-watch | |
command: /conf/systemd-cloud-watch.conf | |
env_file: env | |
volumes: | |
- ./data/systemd-cloud-watch.conf:/conf/systemd-cloud-watch.conf:ro | |
- /var/log/journal:/var/log/journal:ro | |
- /run/log/journal:/run/log/journal:ro | |
" | |
} | |
env-file() { | |
echo " | |
AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID} | |
AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY} | |
" | |
} | |
systemd-cloudwatch-config() { | |
echo " | |
log_group = \"$1\" | |
aws_region = \"us-east-1\" | |
tail = true | |
" | |
if [ -n "$2" ]; then | |
echo "filters = [\"_SYSTEMD_UNIT=$2\"]" | |
fi | |
} | |
systemd-service() { | |
echo " | |
[Unit] | |
Description=Systemd CloudWatch | |
After=docker.service | |
Requires=docker.service | |
[Service] | |
User=ubuntu | |
WorkingDirectory=$1 | |
ExecStartPre=/usr/local/bin/docker-compose-pre-start | |
ExecStart=/usr/local/bin/docker-compose-start | |
TimeoutStartSec=0 | |
Restart=always | |
[Install] | |
WantedBy=multi-user.target | |
" | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment