deploy 6.08 KB
Newer Older
Sandeep Kadam's avatar
Sandeep Kadam committed
1 2
#!/bin/bash

Sandeep Kadam's avatar
Sandeep Kadam committed
3
set -x;
Sandeep Kadam's avatar
Sandeep Kadam committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
set -e;
set -u;

# determine root of repo
ROOT=$(cd $(dirname ${0})/.. 2>/dev/null && pwd -P);
cd ${ROOT};

function usage {
  echo "Deploy site to virtual machines"
  echo "";
  echo "Usage: $0 <dev|stage|prod>";
  echo "";
  exit 1;
}

# check which environment to deploy to
case "${1:-invalid}" in
  dev) ENVIRONMENT=dev ;;
  stage) ENVIRONMENT=stage ;;
  prod) ENVIRONMENT=prod ;;
  *) usage ;;
esac;

# set environment variables
set -a; source ${ROOT}/.env; set +a;

# check which environment to deploy to
case "${ENVIRONMENT:-invalid}" in
32 33 34
  dev) SERVER="${DEV_SERVER}"; WP_SITE_URL="${DEV_SITE_URL}"; MASTER_SERVER="${DEV_SERVER%% *}"; ;;
  stage) SERVER="${STAGE_SERVER}"; WP_SITE_URL="${STAGE_SITE_URL}"; MASTER_SERVER="${STAGE_SERVER%% *}"; ;;
  prod) SERVER="${PROD_SERVER}"; WP_SITE_URL="${PROD_SITE_URL}"; MASTER_SERVER="${PROD_SERVER%% *}"; ;;
Sandeep Kadam's avatar
Sandeep Kadam committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  *) SERVER="" && usage ;;
esac;

# check we have a list of servers to deploy to
[ -z "${SERVER}" ] && echo "No server specified, cannot continue." && exit 1;

# check site has been built
[ ! -d "${LOCAL_CORE_PATH}" ] && echo "Site must be built first, cannot continue" && exit 1;

# run deployment
for _SERVER in ${SERVER};
do
  # copy code to VM
  rsync -acv --delete-after --exclude-from=.gitignore --exclude=.git ${LOCAL_CORE_PATH}/. ${SSH_OWNER}@${_SERVER}:${VM_CORE_PATH}/;

50 51 52 53 54 55
  # create owner folders
  ssh ${SSH_OWNER}@${_SERVER} "/nfs/public/rw/webdevel/projects/drupal/bin/create_dir_owner $ENVIRONMENT $DEPLOY_INSTANCE ${PROJECT_NAME}";

  # create user folders
  ssh ${SSH_APACHE}@${_SERVER} "/nfs/public/rw/webdevel/projects/drupal/bin/create_dir_user $ENVIRONMENT $DEPLOY_INSTANCE ${PROJECT_NAME}";

Sandeep Kadam's avatar
Sandeep Kadam committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69
  # ensure no write permissions for group or other
  ssh ${SSH_OWNER}@${_SERVER} "chmod -R go-w ${VM_CORE_PATH}";

  # ensure directories exist
  ssh ${SSH_OWNER}@${_SERVER} "mkdir -pv ${VM_CORE_PATH}/${RELATIVE_SETTINGS_SRC}";
  ssh ${SSH_APACHE}@${_SERVER} "mkdir -pv ${NFS_FILES_SRC}";
  ssh ${SSH_OWNER}@${_SERVER} "mkdir -pv ${NFS_SQLDUMP_SRC}";
  ssh ${SSH_APACHE}@${_SERVER} "mkdir -pv ${NFS_TEMP_SRC}";

  # create symlinks
  ssh ${SSH_OWNER}@${_SERVER} "rm -rf ${VM_CORE_PATH}/${RELATIVE_FILES_SRC} || true; ln -svf ${NFS_FILES_SRC} ${VM_CORE_PATH}/${RELATIVE_FILES_SRC}";
  ssh ${SSH_OWNER}@${_SERVER} "rm -rf ${VM_CORE_PATH}/${RELATIVE_SQLDUMP_SRC} || true; ln -svf ${NFS_SQLDUMP_SRC} ${VM_CORE_PATH}/${RELATIVE_SQLDUMP_SRC}";
  ssh ${SSH_OWNER}@${_SERVER} "rm -rf ${VM_CORE_PATH}/${RELATIVE_TEMP_SRC} || true; ln -svf ${NFS_TEMP_SRC} ${VM_CORE_PATH}/${RELATIVE_TEMP_SRC}";

70 71 72 73
   # Step - Create DB
   ssh ${SSH_OWNER}@${_SERVER} "/nfs/public/rw/webdevel/projects/drupal/bin/mysql_create_db_script '${ENVIRONMENT}' '${DEPLOY_INSTANCE}' '${PROJECT_NAME}'" || true;


Sandeep Kadam's avatar
Sandeep Kadam committed
74 75 76 77 78 79 80 81 82 83 84 85
  # create wp-config.php file on VM
  ssh ${SSH_OWNER}@${_SERVER} '~/bin/create-settings '${NFS_WP_SETTINGS_SRC}' '${VM_CORE_PATH}'/'${RELATIVE_SETTINGS_SRC}'/wp-config.php '${ENVIRONMENT}' '${DEPLOY_INSTANCE}' '${SERVER};

  # create & symlink wp-config.php
  ssh ${SSH_OWNER}@${_SERVER} "ln -svf ${VM_CORE_PATH}/${RELATIVE_SETTINGS_SRC}/wp-config.php ${VM_CORE_PATH}/${WP_SETTINGS_PHP}";
  ssh ${SSH_OWNER}@${_SERVER} "chmod u+w ${VM_CORE_PATH}/${RELATIVE_DOCUMENT_ROOT} ${VM_CORE_PATH}/${WP_SETTINGS_PHP} || true";

  # create files symlink
  ssh ${SSH_OWNER}@${_SERVER} "chmod u+w ${VM_CORE_PATH}/${RELATIVE_DOCUMENT_ROOT} || true";
  ssh ${SSH_OWNER}@${_SERVER} "rm -rf ${VM_CORE_PATH}/${USER_FILES} || true";
  ssh ${SSH_OWNER}@${_SERVER} "ln -svf ${USER_FILES_SYMLINK} ${VM_CORE_PATH}/${USER_FILES}";

86 87 88
  if [ "${SITE_IS_SUBPATH}" = "YES" ] ; then
    echo "Update URL in wp-config for site :- ${PROJECT_NAME}";
    # Overwrite the site url in wp-config.php file
89
    ssh ${SSH_OWNER}@${_SERVER} "cd ${VM_CORE_PATH}/ && sed -E -i'' '/(WP_HOME|WP_SITEURL)/d' ${RELATIVE_SETTINGS_SRC}/wp-config.php && ~/bin/wp --path=${VM_CORE_PATH}/${RELATIVE_DOCUMENT_ROOT} config set WP_HOME '${WP_SITE_URL}' && ~/bin/wp --path=${VM_CORE_PATH}/${RELATIVE_DOCUMENT_ROOT} config set WP_SITEURL '${WP_SITE_URL}'";
90 91
 fi;

Sandeep Kadam's avatar
Sandeep Kadam committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  # stop apache, etc
  APACHEVER=$(ssh -tt ${SSH_APACHE}@${_SERVER} '[ -f /bin/systemctl ] && echo -n apache24 || echo -n apache22');
  if [ "${APACHEVER}" = "apache24" ] ; then
    ssh -tt ${SSH_APACHE}@${_SERVER} 'sudo /bin/systemctl stop httpd.service | tee /dev/null' || true;

    # start apache, etc
    ssh -tt ${SSH_APACHE}@${_SERVER} 'sudo /bin/systemctl start httpd.service | tee /dev/null' ||
      ssh -tt ${SSH_APACHE}@${_SERVER} 'sleep 5 && sudo /bin/systemctl start httpd.service | tee /dev/null';

    # check status of apache, etc
    ssh -tt ${SSH_APACHE}@${_SERVER} 'sudo /bin/systemctl status httpd.service | tee /dev/null' ||
      ssh -tt ${SSH_APACHE}@${_SERVER} 'sleep 5 && sudo /bin/systemctl status httpd.service | tee /dev/null';
  else
    # check apache config
    ssh -tt ${SSH_APACHE}@${_SERVER} '/etc/init.d/httpd configtest';

    # stop apache, etc
    ssh -tt ${SSH_APACHE}@${_SERVER} 'sudo /etc/init.d/httpd stop; sudo /etc/init.d/memcached stop; sudo /etc/init.d/rh-php56-php-fpm stop' || true;

    # start apache, etc
    ssh -tt ${SSH_APACHE}@${_SERVER} 'sudo /etc/init.d/httpd start; sudo /etc/init.d/memcached start; sudo /etc/init.d/rh-php56-php-fpm start' ||
      ssh -tt ${SSH_APACHE}@${_SERVER} 'sleep 5 && sudo /etc/init.d/httpd start; sudo /etc/init.d/memcached start; sudo /etc/init.d/rh-php56-php-fpm start';

    # check status of apache, etc
    ssh -tt ${SSH_APACHE}@${_SERVER} 'sudo /etc/init.d/httpd status && sudo /etc/init.d/memcached status && sudo /etc/init.d/rh-php56-php-fpm status' ||
      ssh -tt ${SSH_APACHE}@${_SERVER} 'sleep 5 && sudo /etc/init.d/httpd status && sudo /etc/init.d/memcached status && sudo /etc/init.d/rh-php56-php-fpm status';
  fi;
119 120 121 122 123 124 125 126

  # Changes to be executed only once & on one server
  if [ "$_SERVER" = "$MASTER_SERVER" ]; then
    #copy settings, conf, cron, bin folders to nfs in HH datacentre
    ssh ${SSH_OWNER}@ebi-cli '/nfs/public/rw/webdevel/projects/drupal/bin/rsync_dir_datacentres_owner';
    #copy files, tmp folders to nfs in HH datacentre
    ssh ${SSH_APACHE}@ebi-cli '/nfs/public/rw/webdevel/projects/drupal/bin/rsync_dir_datacentres_user';
  fi
Sandeep Kadam's avatar
Sandeep Kadam committed
127 128
done;

129