Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env node
- const fs = require('fs');
- const path = require('path');
- const util = require('util');
- const exec = util.promisify(require('child_process').exec);
- const {promisify} = require('util');
- const readFileAsync = promisify(fs.readFile);
- const filePath = `${path.join(__dirname, '.')}/vhost.conf`;
- __NGINX_REMOTE_CONF_PATH="/etc/nginx/conf.d/vhost.conf";
- __WEB_CONTAINER_NAME="startcasa_web_1";
- __DB_CONTAINER_NAME="startcasa_db_1"
- __DOCKER_SERVICE_NAME="web"
- async function configExists() {
- try {
- const { stdout } = await exec(`docker exec -i ${__WEB_CONTAINER_NAME} ls ${__NGINX_REMOTE_CONF_PATH}`);
- console.log('stdout', stdout);
- return stdout
- }
- catch(error) {
- return '';
- }
- }
- async function backupConfigExists() {
- try {
- const { stdout } = await exec(`docker exec -i ${__WEB_CONTAINER_NAME} ls ${__NGINX_REMOTE_CONF_PATH}.orig`);
- console.log('stdout', stdout);
- return stdout;
- }
- catch(error) {
- return '';
- }
- }
- async function backupOldConfig() {
- try {
- const { stdout } = await exec(`docker exec -i ${__WEB_CONTAINER_NAME} cp ${__NGINX_REMOTE_CONF_PATH} ${__NGINX_REMOTE_CONF_PATH}.orig`);
- console.log('stdout', stdout);
- return stdout;
- }
- catch(error) {
- console.log(error.stderr);
- }
- }
- async function copyNewConfig() {
- try {
- const MY_NGINX_CONFIG = await readFileAsync(filePath, {encoding: 'utf8'});
- const something=`$(cat << END_HEREDOC
- echo '${MY_NGINX_CONFIG}' > ${__NGINX_REMOTE_CONF_PATH}
- )
- `
- // console.log(`${path.join(__dirname, '.')}/vhost.conf`);
- const {stdout} = await exec(`docker exec -i ${__WEB_CONTAINER_NAME} bash -c "${something}"`);
- // const { stdout } = await exec(`docker cp ${path.join(__dirname, '/')}vhost.conf ${__WEB_CONTAINER_NAME}:/etc/nginx/conf.d/vhost.conf`);
- // console.log('stdout', stdout);
- // return stdout;
- }
- catch(error) {
- console.log(error);
- }
- }
- // async function commitChanges() {
- // try {
- // const { stdout} = await exec(`docker commit ${__WEB_CONTAINER_NAME}`);
- // console.log('stdout', stdout);
- // return stdout;
- // }
- // catch(error) {
- // console.log(error.stderr);
- // }
- // }
- async function restartContainer() {
- try {
- const {stdout} = await exec(`docker-compose restart ${__DOCKER_SERVICE_NAME}`);
- console.log('stdout', stdout);
- return stdout;
- } catch (error) {
- console.log(error.stdout);
- }
- }
- async function tearUpContainers() {
- try {
- const {stdout} = await exec (`docker-compose up -d`);
- console.info('STARTING COTAINERS!');
- console.log(stdout);
- console.info('CONTAINERS STARTED');
- } catch (error) {
- console.log(error.stderr)
- }
- }
- async function migrateDb() {
- try {
- DATABASE_NAME="startcasa"
- TABLE_CONTAINS="wp_"
- USERNAME="startcasa"
- PASSWORD="startcasa"
- HOST="localhost"
- const SQL_SCRIPT = await readFileAsync(`${path.join(__dirname, '.')}/${DATABASE_NAME}.sql`, {encoding: 'utf8'});
- SQL=`$(
- cat << END_HEREDOC
- mysql -u ${USERNAME} -p${PASSWORD} -h ${HOST} -e "USE ${DATABASE_NAME}; SHOW TABLES" 2> /dev/null |grep -i ${TABLE_CONTAINS} | head -2 | tail -1
- )`
- SQL2=`
- $(
- cat << END_HEREDOC
- mysql -u ${USERNAME} -p${PASSWORD} -h ${HOST} ${DATABASE_NAME} < /home/mysql/${DATABASE_NAME}.sql
- )
- `
- const {stdout: TABLE} = await exec(`docker exec -i ${__DB_CONTAINER_NAME} bash -c "${SQL}"`);
- if (TABLE && TABLE !='' && TABLE != ' ') {
- console.log(`${TABLE} exists. Database already populated`);
- } else {
- console.log('Populating the database');
- const { stdout: copy_db_output } = await exec(`docker cp ${path.join(__dirname, '.')}/${DATABASE_NAME}.sql ${__DB_CONTAINER_NAME}:/home/mysql/${DATABASE_NAME}.sql`);
- const { stdout: populate_output} = await exec (`docker exec -i ${__DB_CONTAINER_NAME} bash -c "${SQL2}"`);
- console.log(copy_db_output+populate_output);
- console.log('finished');
- }
- } catch(error) {
- console.error(error);
- }
- }
- function timeout(ms) {
- return new Promise(resolve => setTimeout(resolve, ms));
- }
- async function main() {
- try {
- await tearUpContainers();
- await timeout(10000);
- await migrateDb();
- const config = (await configExists()).trim();
- const backup = (await backupConfigExists()).trim();
- console.log('LOGGGG', config);
- console.log('__NGINX_REMOTE_CONF_PATH', __NGINX_REMOTE_CONF_PATH);
- console.log('config', config);
- if (__NGINX_REMOTE_CONF_PATH == config) {
- if (`${__NGINX_REMOTE_CONF_PATH}.orig` != backup) {
- await backupOldConfig();
- console.info('The original vhost config was backed up');
- }
- console.info('Copying local vhost conf into the container');
- copyNewConfig();
- console.info('File copy successful');
- // commitChanges();
- // console.info('Changes to the container have been committed');
- console.info('Restarting container');
- restartContainer();
- console.info('Done');
- } else {
- console.error('The path is not valid anymore or some error was thrown');
- }
- } catch(error) {
- console.log(error);
- }
- }
- copyNewConfig();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement