util.scaffold
TypeScript icon, indicating that this package has built-in type declarations

0.0.38 • Public • Published

util.scaffold

A library to simplify SSH interactions with a remote server. It can be used to run commands, change config, create files, etc.

build analysis code style: prettier testing NPM

This module contains functions for simplifying SSH interactions with the remote production server. It can be used to run commands, change config, create files, etc.

Each of the calls to run, sudo, etc, are sent to a command list queue. Once the commands are queued up, then the go function is called to process the commands in order. This class is used to group together dependent functions. Each grouping of commands should be a separate instantiation of the Scaffold class. e.g.

let scaffold = new Scaffold(config.ssh);

scaffold
    .run('uname -a')
    .sudo('env | sort')
    .go();

Note that coverage is difficult with this module. It requires a host that can be used connected to via SSH and that the user has full control over that host.

Installation

This module uses yarn to manage dependencies and run scripts for development.

To install as an application dependency:

$ yarn add util.scaffold

To build the app and run all tests:

$ yarn run all

Usages

To connect to a remote server and execute a set of commands:

const Scaffold = require('util.scaffold').Scaffold;

let config = {
    "hostname": "example.com",
    "host": "192.168.1.42",
    "port": 22,
    "username": "centos",
    "privateKeyFile": "~/.ssh/id_rsa",
    "publicKeyFile": "~/.ssh/id_rsa.pub"
};

new Scaffold(config)
    .run('uname -a')
    .sudo('env | sort')
    .mkdir('/var/log/myapp', {mode: '700', owner: 'root', group: 'docker'})
    .put('/tmp/file1.txt', '/tmp/file2.txt')
    .go((err) => {
        if (err) {
            console.error(err);
            return;
        }

        console.log('Done.');
    })

An instance of the Scaffold class is created. Methods can be chained to this instance to queue commands. The first command above runs the uname command on the remote server. It runs it as the centos user. The second command dumps a sorted list of environment variables using sudo. This assumes that the SSH user centos has sudo permissions (or this command will fail). The third chained command creates a directory named /var/log/myapp and sets the permissions/ownership of the directory. The fourth command takes a file on the local machine and moves it to the remote machine. It works with text files only. All four of these commands are added to a queue. Execution starts when the go command is called. It processes each command in order. When it finishes a callback is executed. The callback uses the "error first callback" convention in node.

API

The module is composed of one class named Scaffold. It has the following public functions:

  • copy - copies a file on the remote machine from one location to another on the remote machine.
  • go - starts the processing of the command queue.
  • mkdir - creates a directory on the remote host.
  • put - takes a file from the local host and puts it on the remote host.
  • run - runs a command on the remote host.
  • sudo - runs a command on the remote host using sudo.

Readme

Keywords

none

Package Sidebar

Install

npm i util.scaffold

Weekly Downloads

1

Version

0.0.38

License

MIT

Unpacked Size

41.3 kB

Total Files

7

Last publish

Collaborators

  • jmquigley