Ejemplo n.º 1
0
  }).then(result => {
    if (result.code !== 0) {
      throw new Error(result.stderr);
    }

    currentSsh.end();
    schemaStr = result.stdout;

    envSsh = new SshClient();

    return envSsh.connect({
      host: randomItem(toEnv.hosts),
      username: toEnv.ssh.username,
      privateKey: toEnv.ssh.privateKey || `${process.env.HOME}/.ssh/id_rsa`
    });
  }).then(() => {
Ejemplo n.º 2
0
router.post('/promote', (req, res, next) => {
  if (!req.body.keyspace || (req.body.currentEnv !== 'development' && req.body.currentEnv !== 'qa')) {
    return res.status(400).end();
  }

  var currentEnv = config[req.body.currentEnv];
  var toEnv = config[changeOrder[changeOrder.indexOf(req.body.currentEnv) + 1]];

  var currentSsh = new SshClient();
  var envSsh;
  var schemaStr;

  currentSsh.connect({
    host: randomItem(currentEnv.hosts),
    username: currentEnv.ssh.username,
    privateKey: currentEnv.ssh.privateKey || `${process.env.HOME}/.ssh/id_rsa`
  }).then(() => {
    return currentSsh.execCommand(`cqlsh -u ${currentEnv.cassandra.username} \
                                     -p ${currentEnv.cassandra.password} \
                                     -e "DESCRIBE KEYSPACE ${req.body.keyspace}"`)
  }).then(result => {
    if (result.code !== 0) {
      throw new Error(result.stderr);
    }

    currentSsh.end();
    schemaStr = result.stdout;

    envSsh = new SshClient();

    return envSsh.connect({
      host: randomItem(toEnv.hosts),
      username: toEnv.ssh.username,
      privateKey: toEnv.ssh.privateKey || `${process.env.HOME}/.ssh/id_rsa`
    });
  }).then(() => {
    return envSsh.execCommand(`cqlsh -u ${toEnv.cassandra.username} \
                                     -p ${toEnv.cassandra.password} \
                                     -e "${schemaStr}"`);
  }).then(result => {
    if (result.code !== 0) {
      throw new Error(result.stderr);
    }

    res.end();
  }).catch(next);
});
Ejemplo n.º 3
0
const initProcedure = [
  // Create directories
  `mkdir -p ${path.join(config.deployPath, 'current/web')}`,
  `mkdir -p ${path.join(config.deployPath, 'previous')}`,
  `mkdir -p ${path.join(config.deployPath, 'static/uploads')}`,
  deployEnv === 'production' ? `mkdir -p ${path.join(config.deployPath, 'static/cache')}` : false,
  // Create an empty .env config file
  `touch ${path.join(config.deployPath, 'static/.env')}`,
  // Create an index.php inside webroot
  `echo -e "<?php phpinfo();\n" > ${path.join(config.deployPath, 'current/web/index.php')}`
].filter(cmd => cmd).join(' && ')

const ssh = new NodeSSH()
console.log(`==> Preparing directories for deploy on: ${deployEnv}`)
ssh.connect(config.deploySSH)
  .then(() => {
    console.log(`==> Connected.`)
    return ssh.execCommand(initProcedure)
  })
  .then(() => {
    console.log(`==> Done. You still need to:`)
    console.log(`- Set up the web server with webroot in "${config.deployPath}/current/web".`)
    console.log(`- Configure ${config.deployPath}/static/.env.`)
    console.log(`- Make ${config.deployPath}/static/uploads writable for the PHP process group.`)
    if (deployEnv === 'production') {
      console.log(`- Make ${config.deployPath}/static/cache writable for the PHP process group and your SSH user.`)
    }
    ssh.dispose()
  })
  .catch(err => {
Ejemplo n.º 4
0
    })().then(function(){

      var d = Q.defer();

                ssh.connect({
                  host: server,
                  username: config.user,
                  password: config.password

                }).then(function(){

                  var z = Q.defer();

                  ssh.put(path.dirname(require.main.filename) + '/files/c++/'+ nameFile +'.cpp', 'files/c++/'+ nameFile +'.cpp').then(function() {
                      console.log('Uploaded file '+nameFile +'.cpp');
                      z.resolve();
                  }, function(error) {

                      console.log(error);
                      z.reject(error);

                  });

                  return z.promise;

                }).then(function(){

                    var z = Q.defer();

                  ssh.execCommand('g++ -std=c++11 files/c++/'+ nameFile +'.cpp -o files/c++/'+ nameFile ,{stream: 'both'}).then(function(result) {

                    if(result.stderr !== ''){

                      z.reject(result.stderr);
                    }else{
                      console.log("Archivo compilado");
                      z.resolve();
                    }

                    });

                  return z.promise;

                }).then(function(){

                  var z = Q.defer();

                  ssh.execCommand('./files/c++/'+ nameFile ,{stream: 'both'}).then(function(result) {
                    if(result.stderr !== ''){

                      z.reject(result.stderr);

                    }else{

                      if(result.stdout === '1'){
                          result.stdout = "true";
                      }

                      if(result.stdout === '0'){
                          result.stdout = "false";
                      }

                      console.log("Respuesta: "+ result.stdout);
                      res.json(JSON.stringify({"respuesta": result.stdout.trim()}));
                      z.resolve();
                    }

                  });

                return z.promise;

                }).then(function(){

                    var z = Q.defer();

                  ssh.execCommand('rm -f files/c++/'+ nameFile +'.cpp && rm -f files/c++/'+ nameFile).then(function(result) {
                    console.log('Borrado archivos en el servidor');
                    console.log('Borrando archivo local');
                    fs.unlink('./files/c++/'+ nameFile +'.cpp');
                    z.resolve();
                    d.resolve();
                  });

                  return z.promise;

                }).catch(function(e){

                  ssh.execCommand('rm -f files/c++/'+ nameFile +'.cpp && rm -f files/c++/'+ nameFile).then(function(result) {
                    console.log('Borrado archivos en el servidor');
                    console.log('Borrando archivo local');
                    fs.unlink('./files/c++/'+ nameFile +'.cpp');
                    z.resolve();
                    d.resolve();
                  });

                });

        return d.promise;

    }).fail(function(error){
Ejemplo n.º 5
0
let setupOnVm = function(ip, setupOnVmCb) {
  let ssh = new node_ssh();
  ssh.connect({
    host: ip,
    username: username,
    privateKey: localKey
  }).then(function() {
    async.waterfall([
      function(callback) {
        const failed = []
        const successful = []
        ssh.putDirectory(setupScript, '/home/' + username + '/grafana-setup', {
          recursive: true,
          validate: function(itemPath) {
            const baseName = path.basename(itemPath)
            return baseName.substr(0, 1) !== '.' && // do not allow dot files
              baseName !== 'node_modules' // do not allow node_modules
          },
          tick: function(localPath, remotePath, error) {
            if (error) {
              failed.push(localPath)
            } else {
              successful.push(localPath)
            }
          }
        }).then(function(status) {
          // console.log('the directory transfer was', status ? 'success' : 'failure')
          // console.log('failed transfers', failed.join(', '))
          // console.log('successful transfers', successful.join(', '))
          if(status) {
            console.log("Grafana-Setup scripts copied.");
            callback(null, 'success', ip);
          } else {
            console.log("Something's wrong when copying.");
            callback("Failed to copy scripts", null);
          }
        });
      },
      function(status, params, callback) {
        ssh.execCommand('sudo docker-compose stop', { cwd:'/home/cloud/grafana-setup' }).then(function(result) {
          console.log('ERROR: ' + result.stderr);
          console.log('STDOUT: ' + result.stdout);
          if(result.stderr.indexOf('Error response from daemon') === -1) {
            callback(null, status, params);
          } else {
            callback("Failed to docker-compose up", null);
          }
        })
      },
      function(status, params, callback) {
        ssh.execCommand('sudo docker-compose rm -f', { cwd:'/home/cloud/grafana-setup' }).then(function(result) {
          console.log('ERROR: ' + result.stderr);
          console.log('STDOUT: ' + result.stdout);
          if(result.stderr.indexOf('Error response from daemon') === -1) {
            callback(null, status, params);
          } else {
            callback("Failed to docker-compose up", null);
          }
        })
      },
      function(status, params, callback) {
        ssh.execCommand('sudo docker-compose up -d', { cwd:'/home/cloud/grafana-setup' }).then(function(result) {
          console.log('ERROR: ' + result.stderr);
          console.log('STDOUT: ' + result.stdout);
          if(result.stderr.indexOf('Error response from daemon') === -1) {
            callback(null, status, params);
          } else {
            callback("Failed to docker-compose up", null);
          }
        })
      }
    ], function (err, res) {
      setupOnVmCb(err, res, ip);
    });
  }).catch(function() {
    setupOnVmCb('Failed to SSH on required VM ' + ip + '.', null);
  });
};
Ejemplo n.º 6
0
    })().then(function(){

      var d = Q.defer();

              ssh.connect({
                host: server,
                username: config.user,
                password: config.password

              }).then(function(){

                var z = Q.defer();

                ssh.put(path.dirname(require.main.filename) + '/files/python/'+ nameFile +'.py', 'files/python/'+ nameFile +'.py').then(function() {
                    console.log('Uploaded File '+nameFile +'.py');
                    z.resolve();
                }, function(error) {

                    console.log(error);
                    z.reject(error);
                });

                return z.promise;

              }).then(function(){

                  var z = Q.defer();

                ssh.execCommand('python files/python/'+ nameFile +'.py',{stream: 'both'}).then(function(result) {

                  if(result.stderr !== ''){

                    z.reject(result.stderr);
                  }else{
                    console.log("Respuesta: "+ result.stdout);
                    res.json(JSON.stringify({"respuesta": result.stdout.trim()}));
                    z.resolve();
                  }

                });

                return z.promise;

              }).then(function(){

                  var z = Q.defer();

                ssh.execCommand('rm -f files/python/'+ nameFile +'.py').then(function(result) {
                  console.log('Borrado archivo en el servidor');
                  console.log('Borrando archivo local');
                  fs.unlink('./files/python/'+ nameFile +'.py');
                  z.resolve();
                  d.resolve();
                });

                return z.promise;

              }).catch(function(e){

                ssh.execCommand('rm -f files/python/'+ nameFile +'.py').then(function(result) {
                  console.log('Borrado archivo en el servidor');
                  console.log('Borrando archivo local');
                  fs.unlink('./files/python/'+ nameFile +'.py');
                  z.resolve();
                  d.resolve();
                });

                d.reject(e);

              });

            return d.promise;


    }).catch(function(e) {