我做了一个用于自动设置Ansible Semaphore的脚本
首先
可能会有很少的人认为这个东西有用,但既然我已经做了,所以决定公开。
如果这可以作为一个Bash脚本的参考,那就太好了。
运用方式
如果在命令行中按照以下方式执行,将会安装Anaconda Python到用户的主目录,并且快速完成Conda环境的设置以及ansible和mariadb等配置。
$ bash Ansible-Semaphore-Setup.sh \
--fullname="Goichi Iisaka" \
--email="iisaka51@gmail.com" \
--password="hogehoge" --dbpass="hogehoge"
执行后,请不要忘记编辑$HOME/.bash_profile。
编辑示例:
export PATH=$HOME/conda/bin:$PATH:$HOME/.local/bin:$HOME/bin
下面是脚本。
#!/bin/bash
_SUMMARY_="Auto Setup for Ansible Semaphore"
_AUTHOR_="Goichi Iisaka <iisaka51@gmail.com>"
_VERSION_="0.2"
MINICONDA=MicroConda3-18.01-Linux-x86_64.sh
DEBUG=${DEBUG:-"0"}
PROG=$( basename $0 )
MYHOST=$( hostname -s )
FULLNAME=""
EMAIL=""
USERNAME=""
PASSWORD=""
DBPASS=""
VERSION() {
cat <<_EOF_ 1>&2
${PROG} - ${_SUMMARY_)}
Version: ${_VERSION_}
Copyright 2018, ${_AUTHOR_}
_EOF_
}
USAGE() {
cat <<_EOF_ 1>&2
Usage: ${PROG} [options]
OPTIONS:
--prefix - Install Prefix (default: ${PREFIX})
--envname - Conda EnvName for Ansible (default: ansible)
--port - Port number for semaphore (default: 3000)
--fullname - Your FullName
--email - Your Email
--username - Username for semaphore
--password - Password for semaphore
--dbname - Database name for semaphore (default: semaphore)
--dbuser - Database name for semaphore (default: semaphore)
--dbpass - Database password for semaphore
--dbport - Port number for database (default: 3306)
--help - This messages.
--version - Print version and author
_EOF_
}
LONG_OPTIONS=" -l help,version,debug:,fullname:,email:,username:,password:,prefix:,envname:,dbname:,dbpass:,dbport:,port:"
OPTIONS=$( /usr/bin/getopt -o D: ${LONG_OPTIONS} -- "$@" )
eval set -- "${OPTIONS}"
while true
do
case "$1" in
--prefix) PREFIX="$2" ; shift 2 ;;
--envname) ENVNAME="$2" ; shift 2 ;;
--fullname) FULLNAME="$2" ; shift 2 ;;
--email) EMAIL="$2" ; shift 2 ;;
--username) USERNAME="$2" ; shift 2 ;;
--password) PASSWORD="$2" ; shift 2 ;;
--port) PORT="$2" ; shift 2 ;;
--dbname) DBNAME="$2" ; shift 2 ;;
--dbpass) DBPASS="$2" ; shift 2 ;;
--dbport) DBPORT="$2" ; shift 2 ;;
--) shift ; break ;;
*) USAGE ; exit ;;
esac
done
PREFIX=${PREFIX:-"${HOME}/conda"}
ENVNAME=${ENVNAME:-"ansible"}
PORT=${PORT:-"3000"}
DBPORT=${DBPORT:-"3306"}
DBNAME=${DBNAME:-"semaphore"}
DBUSER=${DBNAME:-"semaphore"}
[ x"${FULLNAME}" = "x" ] && read -p "Your Fullname: " FULLNAME
[ x"${EMAIL}" = "x" ] && read -p "Your Email: " EMAIL
[ x"${USERNAME}" = "x" ] && USERNAME=$( echo ${EMAIL} | cut -d@ -f1 )
[ x"${PASSWORD}" = "x" ] && \
while true
do
read -es -p "Enter passowrd: " PASS1 ; echo
read -es -p "Verify passowrd: " PASS2 ; echo
if [ "${PASS1}" != "${PASS2}" ] ; then
echo "Passowrd do not match."
else
PASSWORD=${PASS1}
break
fi
done
[ x${DBPASS} = "x" ] && \
while true
do
read -es -p "Enter DB passowrd: " PASS1 ; echo
read -es -p "Verify DB passowrd: " PASS2 ; echo
if [ "${PASS1}" != "${PASS2}" ] ; then
echo "Passowrd do not match."
else
DBPASS=${PASS1}
break
fi
done
if [ ! -f ${MINICONDA} ] ; then
wget -O ${MINICONDA} http://repobank.openmyroad.com/conda/${MINICONDA}
chmod 700 ${MINICONDA}
bash ${MINICONDA} -b -p ${PREFIX}
export PATH=${PREFIX}/bin:${PATH}
fi
[ -f ${HOME}/.condarc ] || \
cat <<EOF > ${HOME}/.condarc
custom_multichannels:
openmyroad: [
'http://repobank.openmyroad.com/conda/pkgs/openmyroad/'
]
channels:
- defaults
- openmyroad
channels_alias: http://repobank.openmyroad.com/conda/pkgs
show_channel_urls: true
EOF
if [ ! -d ${PREFIX}/envs/${ENVNAME} ] ; then
conda create -y -n ${ENVNAME}
fi
source activate ${ENVNAME}
conda install -y ansible ansible-semaphore ansible-runner \
ansible-role ansible-vault ansible-review ansible-generator \
supervisor3 mariadb jq node.js bcrypt
ENV_PREFIX=$( conda info --json | jq -r .default_prefix )
install -m0700 -d ${HOME}/playbooks
install -m0700 -d ${ENV_PREFIX}/{logs,tmp}
install -m0700 -d ${ENV_PREFIX}/etc/supervisor.d
[ -f ${ENV_PREFIX}/etc/supervisord.conf ] || \
cat <<EOF > ${ENV_PREFIX}/etc/supervisord.conf
[unix_http_server]
file=${ENV_PREFIX}/tmp/supervisor.sock ; the path to the socket file
[supervisord]
logfile=${ENV_PREFIX}/tmp/supervisord.log ; main log file
logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10 ; # of main logfile backups; 0 means none, default 10
loglevel=info ; log level; default info; others: debug,warn,trace
pidfile=${ENV_PREFIX}/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false ; start in foreground if true; default false
minfds=1024 ; min. avail startup file descriptors; default 1024
minprocs=200 ; min. avail process descriptors;default 200
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix://${ENV_PREFIX}/tmp/supervisor.sock ; use a unix:// URL for a unix socket
[include]
files = supervisor.d/*.ini
EOF
[ -f ${ENV_PREFIX}/mariadb/data/mysql ] || \
${ENV_PREFIX}/mariadb/scripts/mysql_install_db \
--user=${USER} \
--basedir=${ENV_PREFIX}/mariadb \
--datadir=${ENV_PREFIX}/mariadb/data
[ -f ${ENV_PREFIX}/mariadb/errmsg.sys ] || \
cp ${ENV_PREFIX}/mariadb/share/english/errmsg.sys ${ENV_PREFIX}/mariadb
touch ${ENV_PREFIX}/mariadb/data/mysql.sock
sed -e "/]/ a port=${DBPORT}" \
-i ${ENV_PREFIX}/mariadb/etc/my.cnf
[ -f ${ENV_PREFIX}/etc/supervisor.d/mariadb.ini ] || \
cat <<EOF > ${ENV_PREFIX}/etc/supervisor.d/mariadb.ini
[program:mariadb]
command=${ENV_PREFIX}/mariadb/bin/mysqld
--user=${USER}
user=${USER}
autostart=false
autorestart=false
stdout_logfile=${ENV_PREFIX}/logs/maridb.log
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=4
stdout_capture_maxbytes=1MB
redirect_stderr=true
EOF
[ -f ${ENV_PREFIX}/etc/supervisor.d/semaphore.ini ] || \
cat <<EOF > ${ENV_PREFIX}/etc/supervisor.d/semaphore.ini
[program:semaphore]
command=${ENV_PREFIX}/bin/semaphore
--config=${ENV_PREFIX}/etc/semaphore.conf
user=${USER}
autostart=false
autorestart=false
stdout_logfile=${ENV_PREFIX}/logs/semaphore.log
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=4
stdout_capture_maxbytes=1MB
redirect_stderr=true
EOF
[ -f ${ENV_PREFIX}/etc/semaphore.conf ] || \
cat <<EOF > ${ENV_PREFIX}/etc/semaphore.conf
{
"mysql": {
"host": "127.0.0.1:${DBPORT}",
"user": "${DBNAME}",
"pass": "${DBPASS}",
"name": "${DBUSER}"
},
"port": "${PORT}",
"tmp_path": "${HOME}/playbooks",
"cookie_hash": "sJilmUw/WqxUCspXevZAswyJUzBhwIa3TafVizE7fPk=",
"cookie_encryption": "7zz90QwjBKB8/zygGC4R7hoAVXW8EQ4v3nsuA72WsAI=",
"email_sender": "",
"email_host": "",
"email_port": "",
"web_host": "",
"ldap_binddn": "",
"ldap_bindpassword": "",
"ldap_server": "",
"ldap_searchdn": "",
"ldap_searchfilter": "",
"ldap_mappings": {
"dn": "",
"mail": "",
"uid": "",
"cn": ""
},
"telegram_chat": "",
"telegram_token": "",
"concurrency_mode": "",
"max_parallel_tasks": 0,
"email_alert": false,
"telegram_alert": false,
"ldap_enable": false,
"ldap_needtls": false
}
EOF
supervisord
supervisorctl start mariadb
mysql -u root -e "CREATE DATABASE ${DBNAME};"
mysql -u root -e "CREATE USER '${DBUSER}'@'localhost' IDENTIFIED BY '${DBPASS}';"
mysql -u root -e "GRANT ALL PRIVILEGES ON ${DBNAME}.* TO '${DBUSER}'@'localhost' WITH GRANT OPTION;"
PASSWORDHASH=$( python -c "import bcrypt; P=bcrypt.hashpw('${PASSWORD}'.encode(),bcrypt.gensalt()); print(P.decode('utf-8'))" )
semaphore -config ${ENV_PREFIX}/etc/semaphore.conf -migrate
mysql -u ${DBUSER} -p${DBPASS} -e "INSERT INTO user set name='${FULLNAME}',username='${USERNAME}',email='${EMAIL}',password='${PASSWORDHASH}',admin=1,created=UTC_TIMESTAMP()" ${DBNAME}
supervisorctl start semaphore