-#!/bin/bash -x
-RES=0
-package=$1
-ROOT_MYSQL_PASSWORD="heat"
-conf_file=/etc/heat/heat.conf
-
-# ini_has_option config-file section option
-function ini_has_option {
- local xtrace=$(set +o | grep xtrace)
- set +o xtrace
- local file=$1
- local section=$2
- local option=$3
- local line
-
- line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
- $xtrace
- [ -n "$line" ]
-}
-
-# Set an option in an INI file
-# iniset config-file section option value
-function iniset {
- local xtrace=$(set +o | grep xtrace)
- set +o xtrace
- local file=$1
- local section=$2
- local option=$3
- local value=$4
-
- [[ -z $section || -z $option ]] && return
-
- if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
- # Add section at the end
- echo -e "\n[$section]" >>"$file"
- fi
- if ! ini_has_option "$file" "$section" "$option"; then
- # Add it
- sed -i -e "/^\[$section\]/ a\\
-$option = $value
-" "$file"
- else
- local sep=$(echo -ne "\x01")
- # Replace it
- sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
- fi
- $xtrace
-}
-
-# Normalize config values to True or False
-# Accepts as False: 0 no No NO false False FALSE
-# Accepts as True: 1 yes Yes YES true True TRUE
-# VAR=$(trueorfalse default-value test-value)
-function trueorfalse {
- local xtrace=$(set +o | grep xtrace)
- set +o xtrace
- local default=$1
- local testval=$2
-
- [[ -z "$testval" ]] && { echo "$default"; return; }
- [[ "0 no No NO false False FALSE" =~ $testval ]] && { echo "False"; return; }
- [[ "1 yes Yes YES true True TRUE" =~ $testval ]] && { echo "True"; return; }
- echo "$default"
- $xtrace
-}
-
-OS_ID=""
-prepare_vm() {
- if [[ `cat /etc/*-release | head -n 1 | awk '{print $1}'` =~ Ubuntu ]]
- then
- OS_ID="Ubuntu"
- echo "mysql-server mysql-server/root_password select $ROOT_MYSQL_PASSWORD" | sudo debconf-set-selections
- echo "mysql-server mysql-server/root_password_again select $ROOT_MYSQL_PASSWORD" | sudo debconf-set-selections
- apt-get install --yes --force-yes mysql-server mysql-client python-mysqldb rabbitmq-server screen curl
- else
- OS_ID="Centos"
- setenforce 0 |:
- service firewalld stop
- yum install screen curl mysql-client mysql-server mysql-libs MySQL-python rabbitmq-server -y
- systemctl enable rabbitmq-server
- MYSQL_PASSWORD=$(cat /root/.mysql_secret | head -1 | awk -F': ' '{print $2}')
- fi
- mycnf=/etc/my.cnf
- cat $mycnf
- service mysql start
- sleep 10
- mysqladmin -u root -p$MYSQL_PASSWORD password $ROOT_MYSQL_PASSWORD
- service rabbitmq-server restart
-# MySQL DB
- _mysql_db_name="heat_db"
- _mysql_db_user="heatUser"
- _mysql_db_pass="${ROOT_MYSQL_PASSWORD}"
- _mysql_root_pass="${ROOT_MYSQL_PASSWORD}"
- _mysql_cmd="CREATE DATABASE IF NOT EXISTS $_mysql_db_name DEFAULT CHARACTER SET=utf8; \
- GRANT ALL PRIVILEGES ON $_mysql_db_name.* TO '$_mysql_db_user'@'localhost' IDENTIFIED BY '$_mysql_db_pass'; \
- GRANT ALL PRIVILEGES ON $_mysql_db_name.* TO '$_mysql_db_user'@'%' IDENTIFIED BY '$_mysql_db_pass';"
- mysql -uroot -p$_mysql_root_pass -e "$_mysql_cmd" |:
- DAEMON_DB_CONSTR="mysql://$_mysql_db_user:$_mysql_db_pass@127.0.0.1:3306/$_mysql_db_name"
- iniset $conf_file DEFAULT debug 'True'
- iniset $conf_file DEFAULT verbose 'True'
- iniset $conf_file DEFAULT use_syslog 'False'
- iniset $conf_file DEFAULT log_dir /var/log/heat
- iniset $conf_file database connection ${DAEMON_DB_CONSTR}
- sleep 1
- heat-manage -d --config-file $conf_file db_sync
- if [ "$?" != "0" ]
- then
- echo "Couldn't execute 'heat-manage db_sync'"
- RES=1
- fi
- sleep 1
-}
-
-case $package in
- openstack-heat-common)
- python -c 'import heat'
- if [ $? -ne '0' ]; then
- echo -e "Couldn't import module 'heat'"
- RES=1
- fi
- if [ -z "$(cut -d: -f1 /etc/passwd | grep 'heat')" ]; then
- echo -e "User 'heat' doesn't exist"
- RES=1
- fi
- config_files="policy.json heat.conf"
- for i in $config_files; do
- if [ ! -f "/etc/heat/$i" ]; then
- RES=1
- echo "File /etc/heat/$i doesn't exist"
- fi
- done
- ;;
- python-heat)
- python -c 'import heat'
- if [ $? -ne '0' ]; then
- echo -e "Couldn't import module 'heat'"
- RES=1
- fi
- ;;
- heat-common)
- if [ -z "$(cut -d: -f1 /etc/passwd | grep 'heat')" ]; then
- echo -e "User 'heat' doesn't exist"
- RES=1
- fi
- config_files="policy.json heat.conf"
- for i in $config_files; do
- if [ ! -f "/etc/heat/$i" ]; then
- RES=1
- echo "File /etc/heat/$i doesn't exist"
- fi
- done
- ;;
- heat-api|openstack-heat-api)
- prepare_vm
- log_file='/var/log/heat/heat-api.log'
- case ${OS_ID} in
- 'Ubuntu')
- screen -dmS heat-api
- sleep 3
- echo "Starting Heat API..."
- screen -S heat-api -p 0 -X stuff "heat-api --config-file $conf_file -d --log-file $log_file 2>>$log_file
- "
- ;;
- 'Centos')
- systemctl enable openstack-heat-api
- systemctl start openstack-heat-api
- sleep 5
- systemctl status openstack-heat-api
- ;;
- esac
- sleep 7
- curl_req=`curl http://localhost:8004/v1/tenant/stacks`
- cat $log_file
- if [ "$(netstat -nat | grep 8004 | wc -l)" != 0 ] && [[ $curl_req =~ Auth ]]
- then
- echo "Heat API successfully started"
- else
- echo "Heat API didn't start"
- RES=1
- fi
- systemctl stop openstack-heat-api
- ;;
- heat-engine|openstack-heat-engine)
- prepare_vm
- # Do not use default log file due to bug/1562753
- # Revert changes after clean DB synchronization.
- # log_file='/var/log/heat/heat-engine.log'
- log_file='/var/log/heat/heat-engine_bogus.log'
- case ${OS_ID} in
- 'Ubuntu')
- screen -dmS heat-engine
- sleep 3
- echo "Starting Heat Engine..."
- screen -S heat-engine -p 0 -X stuff "heat-engine --config-file $conf_file -d --log-file $log_file 2>>$log_file
- "
- ;;
- 'Centos')
- systemctl enable openstack-heat-engine
- systemctl start openstack-heat-engine
- sleep 5
- systemctl status openstack-heat-engine
- ;;
- esac
- sleep 4
- trace_count=`cat $log_file | grep -i Traceback | wc -l`
- if [ "$trace_count" -ne "0" ]
- then
- echo "Something went wrong! Log file have trace errors! Check logs"
- cat $log_file
- RES=1
- else
- amqp_errors=$(grep -P '\[Errno 111\] ECONNREFUSED' $log_file | wc -l)
- if [ $amqp_errors -gt 0 ]; then
- if [ $(grep -P '(\[Errno 111\] ECONNREFUSED|(Reconnected|Connected) to AMQP server)' $log_file \
- | tail -1 | grep -P '(Reconnected|Connected) to AMQP server' | wc -l) -eq 1 ]; then
- echo 'Heat-engine is connected to AMQP server'
- else
- cat $log_file
- echo 'Heat-engine is not connected to AMQP server'
- RES=1
- fi
- else
- echo 'Heat-engine is connected to AMQP server'
- fi
- fi
- systemctl stop openstack-heat-engine
- ;;
- heat-docker|openstack-heat-docker)
- prepare_vm
- # Do not use default log file due to bug/1562753
- # Revert changes after clean DB synchronization.
- # log_file='/var/log/heat/heat-engine.log'
- log_file='/var/log/heat/heat-engine_bogus.log'
- case ${OS_ID} in
- 'Ubuntu')
- screen -dmS heat-engine
- sleep 3
- echo "Starting Heat Engine..."
- screen -S heat-engine -p 0 -X stuff "heat-engine --config-file $conf_file -d --log-file $log_file 2>>$log_file
- "
- ;;
- 'Centos')
- systemctl enable openstack-heat-engine
- systemctl start openstack-heat-engine
- sleep 5
- systemctl status openstack-heat-engine
- ;;
- esac
- sleep 4
- trace_count=$(grep -ic Traceback $log_file)
- if [ "$trace_count" -ne "0" ]
- then
- echo "Something went wrong! Log file have trace errors! Check logs"
- cat $log_file
- RES=1
- else
- docker_regitered=$(grep -c 'DockerInc::Docker::Container' $log_file)
- if [ "$docker_regitered" -eq "0" ]
- then
- echo "Heat Docker: engine can't load docker resource"
- RES=1
- else
- echo "Heat Docker: engine loaded docker resource successfully"
- fi
- fi
- systemctl stop openstack-heat-engine
- ;;
- heat-api-cfn|openstack-heat-api-cfn)
- prepare_vm
- log_file='/var/log/heat/heat-api-cfn.log'
- case ${OS_ID} in
- 'Ubuntu')
- screen -dmS heat-api-cfn
- sleep 3
- echo "Starting Heat API CFN..."
- screen -S heat-api-cfn -p 0 -X stuff "heat-api-cfn --config-file $conf_file -d --log-file $log_file 2>>$log_file
- "
- ;;
- 'Centos')
- systemctl enable openstack-heat-api-cfn
- systemctl start openstack-heat-api-cfn
- sleep 5
- systemctl status openstack-heat-api-cfn
- ;;
- esac
- sleep 4
- check_start_cfn=`cat $log_file | grep 'Starting Heat API on' | wc -l`
- if [ "$check_start_cfn" -ne "0" ]
- then
- echo "Heat API CFN successfully started"
- else
- RES=1
- echo "Heat API CFN didn't start"
- fi
- systemctl stop openstack-heat-api-cfn
- ;;
- heat-api-cloudwatch|openstack-heat-api-cloudwatch)
- prepare_vm
- log_file='/var/log/heat/heat-api-cloudwatch.log'
- case ${OS_ID} in
- 'Ubuntu')
- screen -dmS heat-api-cloudwatch
- sleep 3
- echo "Starting Heat API Cloudwatch..."
- screen -S heat-api-cloudwatch -p 0 -X stuff "heat-api-cloudwatch --config-file $conf_file -d --log-file $log_file 2>>$log_file
- "
- ;;
- 'Centos')
- systemctl enable openstack-heat-api-cloudwatch
- systemctl start openstack-heat-api-cloudwatch
- sleep 5
- systemctl status openstack-heat-api-cloudwatch
- ;;
- esac
- sleep 4
- check_start_clw=`cat $log_file | grep 'Starting Heat CloudWatch API on' | wc -l`
- if [ "$check_start_clw" -ne "0" ]
- then
- echo "Heat API Cloudwatch successfully started"
- else
- RES=1
- echo "Heat API Cloudwatch didn't start"
- fi
- systemctl stop openstack-heat-api-cloudwatch
- ;;
- *)
- echo "test not defined, skipping..."
- ;;
-esac
-case ${OS_ID} in
- 'Ubuntu')
- killall -15 screen
- ;;
-esac
-exit $RES