SHARE
TWEET

Untitled

a guest Aug 19th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Docker = require('dockerode');
  2.  
  3. const names = ['redis', 'nats', 'postgres'];
  4.  
  5. if (!names.includes(process.argv[2])) process.exit(1);
  6.  
  7. const data = {
  8.   redis: {
  9.     port: '6379',
  10.     image: process.env.IMAGE_NAME || 'redis:5-alpine',
  11.     name: 'redis',
  12.   },
  13.   nats: {
  14.     port: '4222',
  15.     image: process.env.IMAGE_NAME || 'nats:1.4.1-linux',
  16.     name: 'nats',
  17.   },
  18.   postgres: {
  19.     port: '5432',
  20.     image: process.env.IMAGE_NAME || 'postgres:9.6.11-alpine',
  21.     name: 'postgres',
  22.     password: process.env.POSTGRES_PASSWORD || '12345678'
  23.   },
  24. };
  25.  
  26. (async () => {
  27.   const host = process.env.DOCKER_HOST;
  28.  
  29.   let dockerOptions;
  30.   if (host) dockerOptions = { host };
  31.   const docker = new Docker(dockerOptions || { socketPath: '/var/run/docker.sock' });
  32.  
  33.   const params = data[process.argv[2]];
  34.  
  35.   const stage = process.argv[3];
  36.   if (!stage) throw new Error('Env stage does not specified as argument.');
  37.  
  38.   const containerName = `fixpoint.${stage}.${params.name}`;
  39.   const containersList = await docker.listContainers({ all: true });
  40.   let container = containersList.find(container => container.Names.includes(`/${containerName}`));
  41.  
  42.   let Env = [];
  43.   if (params.name === 'postgres') {
  44.     Env = [`POSTGRES_PASSWORD=${params.password}`];
  45.   }
  46.  
  47.   try {
  48.     if (!container) {
  49.       container = await docker.createContainer({
  50.         name: containerName,
  51.         Image: params.image,
  52.         AttachStdin: false,
  53.         AttachStdout: true,
  54.         AttachStderr: true,
  55.         HostConfig: {
  56.           PortBindings: {
  57.             [`${params.port}/tcp`]: [{
  58.               'HostPort': '0',
  59.             }],
  60.           },
  61.         },
  62.         Env
  63.       });
  64.       await container.start();
  65.     } else if (!container.Status.includes('Up')) {
  66.       container = docker.getContainer(containerName);
  67.       await container.start();
  68.     }
  69.     const { NetworkSettings: networkSettings } = await docker.getContainer(containerName).inspect();
  70.     const json = JSON.stringify({ port: networkSettings.Ports[`${params.port}/tcp`][0].HostPort });
  71.     console.log(json);
  72.   } catch (err) {
  73.     console.log(err.stack);
  74.     process.exit(1);
  75.   }
  76. })();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top