Code Monkey home page Code Monkey logo

rmtcmd's Introduction

rmtcmd

CircleCI

simple execute commands local and remotely over SSH

./example.js -u userName -h 192.168.11.1

✔ [sudo] Password: … ****
#!/usr/bin/env node

const rmtcmd = require('rmtcmd')

// execute commands
const deploy = async ({ config, local, remote }) => {
  await local('ls -la ./dist', { cwd: __dirname })

  const target = `/home/${config.username}/rmtcmd`

  await remote(`sudo mkdir -p ${target}`)
  await remote(`sudo chmod 775 ${target}`)
  await remote(`sudo chown ${config.username}:${config.username} ${target}`)

  const src = path.join(__dirname, 'dist', 'src')

  await local(
    [
      `rsync -av`,
      `--exclude='node_modules'`,
      `-e 'ssh -i ${config.privateKeyPath}'`,
      `${src}/`,
      `${config.username}@${config.host}:${target}`
    ].join(' '),
    {
      cwd: __dirname
    }
  )

  await remote(`ls -la ${target}`)
}

;(async () => {
  const { host, username, privateKeyPath, sudoPassword } = await rmtcmd.cli.getArgs()

  const privateKey = require('fs').readFileSync(privateKeyPath)

  await rmtcmd.connect({
    host,
    username,
    privateKey,
    sudoPassword,
    task: deploy
  })
})().catch(console.error)

API

connect(config)

wrapper ssh2.client.connect

TaskFunction

async function

  • args

    • config <opject> connect config
    • local <LocalCmd> executer async function for local command
    • remote <RemoteCmd> executer async function for remote command
  • returns Promise<void>

LocalCmd

wrapper child_process.exec

  • args

    • cmd <string> command
    • options
  • returns Promise<{ stdout, stderr }>

RemoteCmd

  • args

    • cmd <string> command

    • options

      • stdout
        • WritableStream
        • default: process.stdout
      • stderr
        • WritableStream
        • default: process.stderr
  • returns Promise<{ code: number, signal: boolean, stdout: string }>

cli

getArgs()

  • returns
    • host <string> remote host.
      • option: -h or --host
    • port <number> ssh port
      • option: --port (optional)
      • default: 22
    • username <string> ssh user name.
      • option: -u or --username (optional)
      • default: whoami
    • sudoPassword <string> ssh sudo password.
      • option: --sudo-password (optional)
      • default: prompt
    • privateKeyPath <string> private key path.
      • option: -k or --private-key-path. (optional)
      • default: ~/.ssh/id_rsa

rmtcmd's People

Contributors

koh110 avatar

Watchers

James Cloos avatar

rmtcmd's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.