0.3.0 • Published 5 years ago

rmtcmd v0.3.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

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
0.3.0

5 years ago

0.2.0

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.0

5 years ago