Last active
May 16, 2017 07:17
-
-
Save xuqingfeng/b559874bc85a4605327aa3d659f82c37 to your computer and use it in GitHub Desktop.
This file contains 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
#!/bin/sh | |
############################################################ | |
# # | |
# This is a bootstrap script for setting up wcl monitoring # | |
# Author: wcl # | |
# Website: https://wiredcraft.com/ # | |
# # | |
############################################################ | |
set -e | |
echo "[I] Updating" | |
yum update -y | |
echo "[I] Installing epel repo..." | |
yum install -y epel-release | |
echo "[I] Installing git..." | |
yum install -y git | |
echo "[I] Installing pip..." | |
yum install -y python2-pip | |
echo "[I] Installing Ansible..." | |
pip install ansible | |
tmp_folder=$(mktemp -d) | |
cd $tmp_folder | |
cat > requirements.yml <<-EOF | |
--- | |
- src: https://github.com/wiredcraft-ops/role-wcl-common.git | |
name: wcl-common | |
version: master | |
- src: https://github.com/wiredcraft-ops/role-wcl-docker.git | |
name: wcl-docker | |
version: master | |
EOF | |
ansible-galaxy install -r requirements.yml -p roles | |
cat > bootstrap.yml <<-EOF | |
--- | |
- hosts: all | |
roles: | |
- role: wcl-common | |
common_users: | |
- name: wcladmin | |
sudo: yes | |
ssh: yes | |
authorized: | |
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC45xILcAxud6vtjSwxnoH3pKDAwoGeoPWy2t8ZXtpi1yMxe9I6kuFlVi2QRxz88GNlFckaQPLNMe05g7l3+KFZT67TvwuTyHH2NnlEBlRngsc62wtFVXNPxFVC9aDfmPZuF0RsOGxhxZvyiaJzW0jXF4O8P6OkSF1weB8mf/cmZZCG8pZR4ITCLyPZFG1juv9ZdGYwrg/YrpKlqrMvBR6V7vJuK2wQrfLm6nAxgwwt+Ohr6KqICKLzpAM6og8rUskZxLnTv96NoxnX7BpZLddHQ/jBaHCL+pwAk2J18UIVBVzoCs6OV61Ga9i3XL2VL3sw2X2Y0MPQgktYgH3pHo6B vincent@wired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8JbWxznzQ3BAsrg5HWWQUO8PzGPqxdGLVQRxIWWz/+MjXZ3nesmsjFO7V/9zQ== | |
hostname: monitoring | |
- role: wcl-docker | |
docker_users: | |
- wcladmin | |
EOF | |
cat > inventory <<-EOF | |
localhost ansible_connection=local | |
EOF | |
ansible-playbook -i inventory bootstrap.yml | |
######################### | |
# Get released playbook # | |
######################### | |
echo "[I] Installing jq..." | |
yum install -y jq | |
# remove old | |
CURRENT_TIME=$(date "+%s") | |
BACKUP_FOLDER=/opt/wcl/backup/${CURRENT_TIME} | |
mkdir -p ${BACKUP_FOLDER} | |
mv /opt/wcl/monitoring ${BACKUP_FOLDER} | |
mkdir -p /opt/wcl/monitoring | |
chmod 755 /opt/wcl | |
cd /opt/wcl/monitoring | |
# https://gist.github.com/maxim/6e15aa45ba010ab030c4 | |
TOKEN=$1 | |
VERSION=$2 # tag name or the word "latest" | |
FILE=$3 # the name of your release asset file, e.g. build.tar.gz | |
# TODO: check from different aspects | |
if [ -z "$1" ]; then | |
echo "[E] Invalid TOKEN" | |
exit 1 | |
fi | |
if [ -z "$2" ]; then | |
echo "[E] Invalid VERSION" | |
exit 1 | |
fi | |
if [ -z "$3" ]; then | |
echo "[E] Invalid FILE" | |
exit 1 | |
fi | |
REPO="Wiredcraft/products" | |
GITHUB="https://api.github.com" | |
function gh_curl() { | |
curl -H "Authorization: token $TOKEN" \ | |
-H "Accept: application/vnd.github.v3.raw" \ | |
$@ | |
} | |
if [ "$VERSION" = "latest" ]; then | |
# Github should return the latest release first. | |
parser=".[0].assets | map(select(.name == \"$FILE\"))[0].id" | |
else | |
parser=". | map(select(.tag_name == \"$VERSION\"))[0].assets | map(select(.name == \"$FILE\"))[0].id" | |
fi | |
CONTENT=`gh_curl -s $GITHUB/repos/$REPO/releases` | |
TYPE=`echo "$CONTENT" | jq 'type'` | |
if [ "$TYPE" = "object" ]; then | |
MESSAGE=`echo "$CONTENT" | jq '.message'` | |
echo "[E] curl github fail: $MESSAGE" | |
exit 1 | |
else | |
# TYPE = "array" | |
ASSET_ID=`echo "$CONTENT" | jq "$parser"` | |
if [ "$ASSET_ID" = "null" ]; then | |
echo "[E] version not found: $VERSION" | |
exit 1 | |
fi | |
fi | |
wget -q --auth-no-challenge --header='Accept:application/octet-stream' \ | |
https://$TOKEN:@api.github.com/repos/$REPO/releases/assets/$ASSET_ID \ | |
-O $3 | |
tar xzvf $3 | |
chown -R wcladmin:wcladmin /opt/wcl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment