0.1.1 • Published 2 years ago

cloud-buddy v0.1.1

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

Cloud Buddy

NodeJS script for local or CI execution, helper scripts to upload and execute stuff on servers.

With heavy duty done by:

  • node-fetch
  • simple-ssh
  • ssh2-sftp-client
  • tar

Contents:

execSSH

Execute SSH commands on a remote server.

import {execSSH} from 'cloud-buddy/execSSH'

const target = {
   username: targetUsername,
   host: targetHost,
   port: targetPort,
}

getSshSocket()
    .then((sshSocketAgent) =>
        execSSH(
            {
                ...target,
                agent: sshSocketAgent,
            },
            `sudo -su devops echo $(whoami)`,
        ),
    )
    .then(() => {
        console.log('✓ Finished echo')
    })
    .catch((e) => {
        console.log('X Failed echo')
        process.exit(1)
    })

getSshSocket

Automatically finds open ssh-agents, e.g. to use with sftpUpload or execSSH

import {getSshSocket} from 'cloud-buddy/getSshSocket'

getTerraOutput

Fetches output from terraform states, uses JSON, must have terraform in path.

import {getTerraOutput} from 'cloud-buddy/getTerraOutput'

packAndUpload

Pack a folder into .tgz (tar with gzip) archive, save that into local tmp, upload it to the server and extract it there.

import {packAndUpload} from 'cloud-buddy/packAndUpload'

packAndUpload({
    host: targetHost,
    username: targetUsername,
    port: targetPort,
    from: path.join(__dirname, 'build'),
    to: '/var/www/files',
    // creates a subfolder with a hash & time stamp based naming
    makeFolder: true,
    // optional callback to execute afterwards with the same context values
    execAfter: async ({
                    host, username, port, agent,
                    from, to, fileName, fileNameHash,
                }) => {},
    tmpFolder: path.resolve(__dirname, 'tmp'),
})
    .then()
    .catch()

packer

Functions to pack a folder into a .tgz archive, named using a hash and timestamp.

  • packIntoTmp and packer excludes files and folders matching: - ['.git', '.idea', '_dev', '_ci', ] - can not be changed atm.
import path from 'path'
import {scanner, packer, packIntoTmp} from 'cloud-buddy/packer';

packIntoTmp(path.join(__dirname, 'build'), path.join(__dirname, 'tmp'))
    .then(({
               fileNameHash, fileName, filePath,
               fileStats, timestamp,
          }) => {})
    .catch()

packer(path.join(__dirname, 'build'), 'archive-name', path.join(__dirname, 'tmp'))
    .then(fileStats => {})

scanner(path.join(__dirname, 'build'), (err, fileNames) => {
    if(err) {
        console.error(err)
        return
    }
    fileNames = fileNames.map(fileName => {})
})

sftpUpload

Upload something with sftp using an SSH agent.

import {sftpUpload} from 'cloud-buddy/sftpUpload'

sftpUpload(host, username, to, filePath, sshSocketAgent, port)
    .then(filename => {})
    .catch()

License

MIT License

Copyright (c) 2021 Michael Becker

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.