diff --git a/src/platform.js b/src/platform.js index ad3ad1a4f..679ec559e 100644 --- a/src/platform.js +++ b/src/platform.js @@ -4,9 +4,12 @@ exports = module.exports = { initialize: initialize }; -var config = require('./config.js'), +var apps = require('./apps.js'), + config = require('./config.js'), certificates = require('./certificates.js'), debug = require('debug')('box:platform'), + fs = require('fs'), + ini = require('ini'), path = require('path'), paths = require('./paths.js'), shell = require('./shell.js'); @@ -17,9 +20,51 @@ function initialize(callback) { if (process.env.BOX_ENV === 'test' && !process.env.CREATE_INFRA) return callback(); debug('initializing addon infrastructure'); + + var currentInfraData = fs.readFileSync(__dirname + '/INFRA_VERSION', 'utf8'); + var currentInfra = ini.parse(currentInfraData); + var existingInfra = { INFRA_VERSION: 'none' }; + if (fs.existsSync(paths.INFRA_VERSION_FILE)) { + existingInfra = ini.parse(fs.readFileSync(paths.INFRA_VERSION_FILE, 'utf8')); + } + + if (currentInfra.INFRA_VERSION === existingInfra.INFRA_VERSION) { + debug('platform is uptodate at version %s', currentInfra.INFRA_VERSION); + return callback(); + } + + debug('Updating infrastructure from %s to %s', existingInfra.INFRA_VERSION, currentInfra.INFRA_VERSION); + + stopContainersSync(); + + if (!existingInfra.INFRA_VERSION) removeImagesSync(); // a hack for --recreate-infra + certificates.getAdminCertificatePath(function (error, certFilePath, keyFilePath) { if (error) return callback(error); - shell.sudo('seutp_infra', [ SETUP_INFRA_CMD, paths.DATA_DIR, config.fqdn(), config.adminFqdn(), certFilePath, keyFilePath, config.database().name, config.database().password ], callback); + shell.sudo('seutp_infra', [ SETUP_INFRA_CMD, paths.DATA_DIR, config.fqdn(), config.adminFqdn(), certFilePath, keyFilePath ], function (error) { + if (error) return callback(error); + + var func = existingInfra ? apps.configureInstalledApps : apps.restoreInstalledApps; + + func(function (error) { + if (error) return callback(error); + + fs.writeFileSync(paths.INFRA_VERSION_FILE, currentInfraData); + + callback(); + }); + }); }); } + +function removeImagesSync() { + debug('removing existing images'); + shell.execSync('removeImagesSync', 'docker images -q | xargs --no-run-if-empty docker rmi -f'); +} + +function stopContainersSync() { + // TODO: be nice and stop addons cleanly (example, shutdown commands) + debug('stopping existing containers'); + shell.execSync('stopContainersSync', 'docker ps -qa | xargs --no-run-if-empty docker rm -f'); +} diff --git a/src/scripts/setup_infra.sh b/src/scripts/setup_infra.sh index f12070fca..27094dfd2 100755 --- a/src/scripts/setup_infra.sh +++ b/src/scripts/setup_infra.sh @@ -20,30 +20,6 @@ readonly fqdn="$2" readonly mail_fqdn="$3" readonly mail_tls_cert="$4" readonly mail_tls_key="$5" -readonly db_name="$6" -readonly db_password="$7" - -# removing containers ensures containers are launched with latest config updates -# restore code in appatask does not delete old containers -infra_version="none" -[[ -f "${data_dir}/INFRA_VERSION" ]] && infra_version=$(cat "${data_dir}/INFRA_VERSION") -if [[ "${infra_version}" == "${INFRA_VERSION}" ]]; then - echo "Infrastructure is upto date" - exit 0 -fi - -echo "Upgrading infrastructure from ${infra_version} to ${INFRA_VERSION}" - -# TODO: be nice and stop addons cleanly (example, shutdown commands) -existing_containers=$(docker ps -qa) -echo "Remove containers: ${existing_containers}" -echo "${existing_containers}" | xargs --no-run-if-empty docker rm -f - -# a hack to 'refresh' images when testing with hotfix --recreate-infra -if [[ -z "${infra_version}" ]]; then - echo "Removing existing images" - docker rmi "${BASE_IMAGE}" "${MYSQL_IMAGE}" "${POSTGRESQL_IMAGE}" "${MONGODB_IMAGE}" "${REDIS_IMAGE}" "${MAIL_IMAGE}" "${GRAPHITE_IMAGE}" || true -fi # graphite graphite_container_id=$(docker run --restart=always -d --name="graphite" \ @@ -143,15 +119,3 @@ fi if docker images "${REDIS_REPO}" | tail -n +2 | awk '{ print $1 ":" $2 }' | grep -v "${REDIS_IMAGE}" | xargs --no-run-if-empty docker rmi; then echo "Removed old redis images" fi - -# only touch apps in installed state. any other state is just resumed by the taskmanager -if [[ "${infra_version}" == "none" ]]; then - # if no existing infra was found (for new, upgraded and restored cloudons), download app backups - echo "Marking installed apps for restore" - mysql -u root --password="${db_password}" -e 'UPDATE apps SET installationState = "pending_restore", oldConfigJson = NULL WHERE installationState = "installed"' ${db_name} -else - # if existing infra was found, just mark apps for reconfiguration - mysql -u root --password="${db_password}" -e 'UPDATE apps SET installationState = "pending_configure", oldConfigJson = NULL WHERE installationState = "installed"' ${db_name} -fi - -echo -n "${INFRA_VERSION}" > "${data_dir}/INFRA_VERSION"