Advertisement
Guest User

Untitled

a guest
Jun 7th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env node
  2.  
  3. const fs = require('fs');
  4. const path =  require('path');
  5. const util = require('util');
  6. const exec = util.promisify(require('child_process').exec);
  7. const {promisify} = require('util');
  8. const readFileAsync = promisify(fs.readFile);
  9. const filePath = `${path.join(__dirname, '.')}/vhost.conf`;
  10. __NGINX_REMOTE_CONF_PATH="/etc/nginx/conf.d/vhost.conf";
  11. __WEB_CONTAINER_NAME="startcasa_web_1";
  12. __DB_CONTAINER_NAME="startcasa_db_1"
  13. __DOCKER_SERVICE_NAME="web"
  14. async function configExists() {
  15.     try {
  16.         const { stdout } = await exec(`docker exec -i ${__WEB_CONTAINER_NAME} ls ${__NGINX_REMOTE_CONF_PATH}`);
  17.         console.log('stdout', stdout);
  18.         return stdout
  19.     }
  20.     catch(error) {
  21.         return '';
  22.     }
  23. }
  24.  
  25. async function backupConfigExists() {
  26.     try {
  27.         const { stdout } = await exec(`docker exec -i ${__WEB_CONTAINER_NAME} ls ${__NGINX_REMOTE_CONF_PATH}.orig`);
  28.         console.log('stdout', stdout);
  29.         return stdout;
  30.     }
  31.     catch(error) {
  32.         return '';
  33.     }
  34. }
  35.  
  36. async function backupOldConfig() {
  37.     try {
  38.      const { stdout } = await exec(`docker exec -i ${__WEB_CONTAINER_NAME} cp ${__NGINX_REMOTE_CONF_PATH} ${__NGINX_REMOTE_CONF_PATH}.orig`);
  39.      console.log('stdout', stdout);
  40.      return stdout;
  41.     }
  42.     catch(error) {
  43.         console.log(error.stderr);
  44.     }
  45. }
  46.  
  47. async function copyNewConfig() {
  48.     try {
  49.         const MY_NGINX_CONFIG = await readFileAsync(filePath, {encoding: 'utf8'});
  50.         const something=`$(cat << END_HEREDOC
  51.                 echo '${MY_NGINX_CONFIG}' > ${__NGINX_REMOTE_CONF_PATH}
  52.                
  53.                 )
  54.         `  
  55.         // console.log(`${path.join(__dirname, '.')}/vhost.conf`);
  56.         const {stdout} = await exec(`docker exec -i ${__WEB_CONTAINER_NAME} bash -c "${something}"`);
  57.         // const { stdout } = await exec(`docker cp ${path.join(__dirname, '/')}vhost.conf ${__WEB_CONTAINER_NAME}:/etc/nginx/conf.d/vhost.conf`);
  58.  
  59.         // console.log('stdout', stdout);
  60.         // return stdout;
  61.  
  62.     }
  63.     catch(error) {
  64.         console.log(error);
  65.     }
  66. }
  67.  
  68. // async function commitChanges() {
  69. //  try {
  70. //      const { stdout} = await exec(`docker commit ${__WEB_CONTAINER_NAME}`);
  71. //      console.log('stdout', stdout);
  72. //      return stdout;
  73. //  }
  74. //  catch(error) {
  75. //      console.log(error.stderr);
  76. //  }
  77. // }
  78.  
  79. async function restartContainer() {
  80.     try {
  81.         const {stdout} = await exec(`docker-compose restart ${__DOCKER_SERVICE_NAME}`);
  82.         console.log('stdout', stdout);
  83.         return stdout;
  84.     } catch (error) {
  85.         console.log(error.stdout);
  86.     }
  87. }
  88.  
  89. async function tearUpContainers() {
  90.     try {
  91.         const {stdout} = await exec (`docker-compose up -d`);
  92.         console.info('STARTING COTAINERS!');
  93.         console.log(stdout);
  94.         console.info('CONTAINERS STARTED');
  95.  
  96.     } catch (error) {
  97.         console.log(error.stderr)
  98.     }
  99. }
  100.  
  101. async function migrateDb() {
  102.     try {
  103.         DATABASE_NAME="startcasa"
  104.         TABLE_CONTAINS="wp_"
  105.         USERNAME="startcasa"
  106.         PASSWORD="startcasa"
  107.         HOST="localhost"
  108.         const SQL_SCRIPT = await readFileAsync(`${path.join(__dirname, '.')}/${DATABASE_NAME}.sql`, {encoding: 'utf8'});
  109.         SQL=`$(
  110.             cat << END_HEREDOC
  111.             mysql -u ${USERNAME} -p${PASSWORD} -h ${HOST} -e "USE ${DATABASE_NAME}; SHOW TABLES" 2> /dev/null |grep -i ${TABLE_CONTAINS} | head -2 | tail -1   
  112.         )`
  113.  
  114.         SQL2=`
  115.             $(
  116.             cat << END_HEREDOC
  117.             mysql -u ${USERNAME} -p${PASSWORD} -h ${HOST} ${DATABASE_NAME} < /home/mysql/${DATABASE_NAME}.sql
  118.             )
  119.         `
  120.  
  121.         const {stdout: TABLE} = await exec(`docker exec -i ${__DB_CONTAINER_NAME} bash -c "${SQL}"`);
  122.         if (TABLE && TABLE !='' && TABLE != ' ') {
  123.             console.log(`${TABLE} exists. Database already populated`);    
  124.         } else {
  125.             console.log('Populating the database');
  126.             const { stdout: copy_db_output } = await exec(`docker cp ${path.join(__dirname, '.')}/${DATABASE_NAME}.sql ${__DB_CONTAINER_NAME}:/home/mysql/${DATABASE_NAME}.sql`);
  127.             const { stdout: populate_output} = await exec (`docker exec -i ${__DB_CONTAINER_NAME} bash -c "${SQL2}"`); 
  128.             console.log(copy_db_output+populate_output);
  129.             console.log('finished');
  130.  
  131.         }
  132.     } catch(error) {
  133.         console.error(error);
  134.     }
  135. }
  136. function timeout(ms) {
  137.     return new Promise(resolve => setTimeout(resolve, ms));
  138. }
  139. async function main() {
  140.     try {
  141.         await tearUpContainers();
  142.         await timeout(10000);
  143.         await migrateDb();
  144.         const config =  (await configExists()).trim();
  145.         const backup = (await backupConfigExists()).trim();
  146.         console.log('LOGGGG', config);
  147.         console.log('__NGINX_REMOTE_CONF_PATH', __NGINX_REMOTE_CONF_PATH);
  148.         console.log('config', config);
  149.         if (__NGINX_REMOTE_CONF_PATH == config) {
  150.             if (`${__NGINX_REMOTE_CONF_PATH}.orig` != backup) {
  151.                 await backupOldConfig();
  152.                 console.info('The original vhost config was backed up');
  153.             }
  154.             console.info('Copying local vhost conf into the container');   
  155.             copyNewConfig();
  156.             console.info('File copy successful');
  157.             // commitChanges();
  158.             // console.info('Changes to the container have been committed');
  159.             console.info('Restarting container');
  160.             restartContainer();
  161.             console.info('Done');
  162.         } else {
  163.             console.error('The path is not valid anymore or some error was thrown');
  164.         }
  165.     } catch(error) {
  166.         console.log(error);    
  167.     }
  168. }
  169.  
  170. copyNewConfig();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement