Skip to content

Instantly share code, notes, and snippets.

@likai24
Created June 23, 2016 04:38
Show Gist options
  • Save likai24/417e3ede55e136922997445f95295a92 to your computer and use it in GitHub Desktop.
Save likai24/417e3ede55e136922997445f95295a92 to your computer and use it in GitHub Desktop.
展示了接受参数的方法
#!/usr/bin/env bash
#
# @version : 0.0.1
#
#####################
# Global Configuration
set -o pipefail
set -o errexit
# Mark start
echo "Start App Init service "
# Include the password function
source script/common/logger.sh
source script/common/help.sh
usage=$(cat <<-EOF
-a | --ent-host-id Application Host id (assigned by salt minion)
-b | --run-ent-db Run Enterprise DB set up
-d | --dry-run Dryrun, without really execute anything on minions
-e | --run-ent Run Enterprise set up
-f | --run-auth-db Run Auth DB set up
-g | --run-sec-db Run Security DB set up
-h | --help To set up the application related
-r | --run-router Run Router set up
-w | --run-wp Run Wordpress set up
-D | --ent-db-host-id Enterprise DB Host id (assigned by salt minion)
-F | --auth-db-host-id Auth DB Host id (assigned by salt minion)
-G | --sec-db-host-id Security DB Host id (assigned by salt minion)
-R | --router-host-id Router Host id (assigned by salt minion)
-A | --wp-router-host WP Router Host id (assigned by salt minion)
-W | --wp-host-id Wordpress Host id (assigned by salt minion)
EOF
)
function cleanup_before_exit () {
echo "Job Done!"
}
trap cleanup_before_exit EXIT
# Exit if no value set
if [[ $# = 0 ]];
then
echo "No options provided, see below for help"
help "${usage}"
fi
OPTIONS=$(getopt -o :bc:defgrhwa:D:F:G:R:A:W: --long run-ent-db,config:,dry-run,run-ent,run-auth-db:,run-sec-db:,help,run-router,run-wp,ent-host-id:,db-host-id:,router-host-id:,wp-router-host:,wp-host-id: -n "$0" -- "$@")
eval set -- "$OPTIONS"
while :
do
case "$1" in
-a | --ent-host-id)
shift
if [[ ${1} = -* ]]; then
error "Application host id is required"
exit 1
fi
debug "Application host id is ${1}"
ent_host_id="${1}"
;;
-b | --run-ent-db)
debug 'Enterprise DB is enable'
run_ent_db=1 # Enable DB setup
;;
-c | --config)
shift
if [[ ${1} = -* ]]; then
error "Configuration file is required"
exit 1
fi
debug "Config file is set to ${1}"
config_file="${1}"
;;
-d | --dry-run)
debug 'Dryrun is enable'
dry_run=1 # Enable Dry run
;;
-e | --run-ent)
debug 'Enterprise web server is enable'
run_ent=1 # Enable enterprise application set up
;;
-f | --run-auth-db)
debug 'Auth DB is enable'
run_auth_db=1 # Enable DB setup
;;
-g | --run-sec-db)
debug 'is enable'
run_sec_db=1 # Enable DB setup
;;
-h | --help)
help "${usage}"
;;
-r | --run-router)
debug 'Router is enable'
run_router=1 # Enable router set up
;;
-w | --run-wp)
debug 'Wordpress is enable'
run_wp=1 # Enable wordpress set up
;;
-D | --ent-db-host-id)
shift
if [[ ${1} = -* ]]; then
error "DB host id is required"
exit 1
fi
debug "DB Host Id is ${1}"
ent_db_host_id=${1}
;;
-F | --auth-db-host-id)
shift
if [[ ${1} = -* ]]; then
error "Auth DB host id is required"
exit 1
fi
debug "Auth DB Host Id is ${1}"
auth_db_host_id=${1}
;;
-G | --sec-db-host-id)
shift
if [[ ${1} = -* ]]; then
error "Security DB host id is required"
exit 1
fi
debug "Security DB Host Id is ${1}"
sec_db_host_id=${1}
;;
-R | --router-host-id)
shift
if [[ ${1} = -* ]]; then
error "Router host id is required"
exit 1
fi
debug "Router Host Id is ${1}"
router_host_id=${1}
;;
-A | --wp-router-host)
shift
if [[ ${1} = -* ]]; then
error "WP Router host id is required"
exit 1
fi
debug "Router Host Id is ${1}"
wp_router_host_id=${1}
;;
-W | --wp-host-id)
shift
if [[ ${1} = -* ]]; then
error "Wordpress host id is required"
exit 1
fi
debug "WP Host Id is ${1}"
wp_host_id=${1}
;;
--) # End of all options
break
;;
-*)
error "Error: Unknown option: $1" >&2
help "${usage}"
;;
*) # No more options
break
;;
esac
shift
done
# Include all paramtesr
source ${config_file}
if [[ ! ${config_file} || ! ${ent_host_id} || ! ${router_host_id} || ! ${wp_router_host_id} || ! ${ent_db_host_id} || ! ${auth_db_host_id} || ! ${sec_db_host_id} || ! ${wp_host_id} ]];
then
error "config_file, ent_host_id, router_host_id,wp_router_host_id, ent_db_host_id, auth_db_host_id, sec_db_host_id, wp_host_id must be set, use -h for detail"
exit 1
fi
info "Everything looks good, we will go on setting up things"
if [[ ${dry_run} = 1 ]]; then
info "### RUN on DRY-RUN mode ###"
fi
# Setup Enterprise DB
pillar="{\"domain_name\":\"${org_domain}\",\"db_user\":\"${ent_db_user}\",\"db_pwd\":\"${ent_db_pwd}\",\"db_name\":\"${ent_db_name}\",\"db_host\":\"${ent_db_host}\",\"db_port\":\"${ent_db_port}\",\"db_authorized_host\":\"${ent_db_authorized_host}\",\"db_deploy_user\":\"${ent_db_deploy_user}\",\"db_deploy_pwd\":\"${ent_db_deploy_pwd}\",\"reg_db_host\":\"${reg_db_host}\",\"reg_db_pwd\":\"${reg_db_pwd}\",\"reg_db_user\":\"${reg_db_user}\",\"reg_db_port\":\"${reg_db_port}\",\"reg_db_name\":\"${reg_db_name}\",\"auth_db_host\":\"${auth_db_host}\",\"auth_db_pwd\":\"${auth_db_pwd}\",\"auth_db_user\":\"${auth_db_user}\",\"auth_db_port\":\"${auth_db_port}\",\"auth_db_name\":\"${auth_db_name}\"}"
if [[ ! -z ${run_ent_db+x} ]];
then
info "Run Enterprise DB Setup"
salt "${ent_db_host_id}" state.sls sls.db.setup_enterprise pillar="${pillar}" ${dry_run:+test=True}
fi
# Setup Auth DB
pillar="{\"domain_name\":\"${org_domain}\",\"db_user\":\"${auth_db_user}\",\"db_pwd\":\"${auth_db_pwd}\",\"db_name\":\"${auth_db_name}\",\"db_host\":\"${auth_db_host}\",\"db_port\":\"${auth_db_port}\",\"db_authorized_host\":\"${ent_db_authorized_host}\",\"db_deploy_user\":\"${auth_db_deploy_user}\",\"db_deploy_pwd\":\"${auth_db_deploy_pwd}\",\"reg_db_host\":\"${reg_db_host}\",\"reg_db_pwd\":\"${reg_db_pwd}\",\"reg_db_user\":\"${reg_db_user}\",\"reg_db_port\":\"${reg_db_port}\",\"reg_db_name\":\"${reg_db_name}\",\"auth_db_host\":\"${auth_db_host}\",\"auth_db_pwd\":\"${auth_db_pwd}\",\"auth_db_user\":\"${auth_db_user}\",\"auth_db_port\":\"${auth_db_port}\",\"auth_db_name\":\"${auth_db_name}\"}"
if [[ ! -z ${run_auth_db+x} ]];
then
info "Run Auth DB Setup"
salt "${auth_db_host_id}" state.sls sls.db.setup_auth pillar="${pillar}" ${dry_run:+test=True}
fi
# Setup Security DB
pillar="{\"domain_name\":\"${org_domain}\",\"db_user\":\"${sec_db_user}\",\"db_pwd\":\"${sec_db_pwd}\",\"db_name\":\"${sec_db_name}\",\"db_host\":\"${sec_db_host}\",\"db_port\":\"${sec_db_port}\",\"db_authorized_host\":\"${ent_db_authorized_host}\",\"db_deploy_user\":\"${sec_db_deploy_user}\",\"db_deploy_pwd\":\"${sec_db_deploy_pwd}\",\"reg_db_host\":\"${reg_db_host}\",\"reg_db_pwd\":\"${reg_db_pwd}\",\"reg_db_user\":\"${reg_db_user}\",\"reg_db_port\":\"${reg_db_port}\",\"reg_db_name\":\"${reg_db_name}\",\"auth_db_host\":\"${auth_db_host}\",\"auth_db_pwd\":\"${auth_db_pwd}\",\"auth_db_user\":\"${auth_db_user}\",\"auth_db_port\":\"${auth_db_port}\",\"auth_db_name\":\"${auth_db_name}\"}"
if [[ ! -z ${run_sec_db+x} ]];
then
info "Run Security DB Setup"
salt "${sec_db_host_id}" state.sls sls.db.setup_security pillar="${pillar}" ${dry_run:+test=True}
fi
# Setup both box and application server
pillar="{\"deployment_target_folder\":\"${deployment_target_folder}\",\"admin_app_name\":\"${admin_app_name}\",\"domain_name\":\"${admin_domain}\",\"port\":\"${admin_app_port}\", \"phpfpm_port\":\"${admin_app_fpmport}\",\"db_host\":\"${ent_db_host}\",\"db_port\":\"${ent_db_port}\",\"db_name\":\"${ent_db_name}\",\"db_user\":\"${ent_db_user}\",\"db_pwd\":\"${ent_db_pwd}\",\"mailer_transport\":\"${mailer_transport}\",\"mailer_host\":\"${mailer_host}\",\"mailer_user\":\"${mailer_user}\",\"mailer_pwd\":\"${mailer_pwd}\",\"locale\":\"${locale}\",\"secret\":\"${secret}\",\"admin_domain\":\"${admin_domain}\",\"memcache_host\":\"${memcache_host}\",\"enter_user_domain\": \"${enter_user_domain}\",\"asset_server_url\": \"${asset_server_url}\",\"asset_server_pubk\": \"${asset_server_pubk}\",\"asset_server_prik\": \"${asset_server_prik}\",\"att_domain\": \"${att_domain}\",\"portal_domain\": \"${portal_domain}\",\"security_domain\":\"${security_domain}\",\"org_admin_domain\":\"${org_admin_domain}\",\"audit_domain\":\"${audit_domain}\",\"portal_mgm_domain_suffix\":\"${portal_mgm_domain_suffix}\",\"wp_domain_suffix\":\"${wp_domain_suffix}\",\"admin_git_home\":\"${admin_git_home}\"}"
if [[ ! -z ${run_ent+x} ]];
then
info "Run enterprise setup"
salt "${ent_host_id}" state.sls sls.enterprise.setup_app pillar="${pillar}" ${dry_run:+test=True}
fi
# Set Organization Application Router
if [[ ! -z ${run_router+x} ]];
then
info "Run Router Setup"
salt "${router_host_id}" state.sls sls.router.add_route_item pillar="{\"domain_name\":\"${admin_domain}\", \"url\":\"${admin_app_host_url}\",\"port\":\"${admin_app_port}\"}" ${dry_run:+test=True}
# Set Organization WP Router
salt "${wp_router_host_id}" state.sls sls.router.add_route_item pillar="{\"domain_name\":\"${wp_domain}\", \"url\":\"${wp_app_host_url}\",\"port\":\"${wp_app_port}\"}" ${dry_run:+test=True}
fi
if [[ ! -z ${run_wp+x} ]];
then
info "Run wordpress Setup"
# Setup wordpress
salt "${wp_host_id}" state.sls sls.wordpress.setup_wp pillar="{\"org_domain\":\"${org_domain}\", \"org_id\":\"${org_id}\",\"wp_app_title\":\"${wp_app_title}\",\"wp_domain\":\"${wp_domain}\",\"root_domain\":\"${root_domain}\",\"admin_domain\":\"${admin_domain}\",\"wp_emrm\":\"${wp_emrm}\",\"wp_admin_user_mail\":\"${wp_admin_user_mail}\",\"wp_folder\":\"${wp_folder}\",\"wp_theme_name\":\"${wp_theme_name}\",\"wp_admin_pwd\":\"${wp_admin_pwd}\",\"wp_db_user\":\"${wp_db_user}\",\"wp_db_pwd\":\"${wp_db_pwd}\",\"wp_db_host\":\"${wp_db_host}\",\"wp_dba_user\":\"${wp_dba_user}\",\"wp_dba_pwd\":\"${wp_dba_pwd}\", \"wp_installation_folder\":\"${deployment_target_folder}\",\"wp_git_home\":\"${wp_git_home}\"}" ${dry_run:+test=True}
# Insert WP info into OA_AUDIT
salt "${audit_db_host_id}" state.sls sls.wordpress.setup_wp_routerwpuser pillar="{\"audit_db_host\":\"${audit_db_host}\",\"audit_db_deploy_user\":\"${audit_db_deploy_user}\",\"audit_db_deploy_pwd\":\"${audit_db_deploy_pwd}\",\"audit_db_name\":\"${audit_db_name}\",\"org_id\":\"${org_id}\",\"wp_admin_user\":\"${wp_admin_user}\",\"wp_admin_pwd\":\"${wp_admin_pwd}\"}" ${dry_run:+test=True}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment