在遠程主機上執行命令的Node.js庫:Flightplan

jopen 10年前發布 | 21K 次閱讀 Node.js 開發 Flightplan

Flightplan 這是一個可以在本地或者遠程主機上運行一序列命令的 Node.js 庫。用于執行應用發布和系統管理任務,類似 Python 的 Fabric。

安裝和使用

# install the cli tool
$ npm install -g flightplan

# use it in your project
$ npm install flightplan --save-dev

# run a flightplan (`fly --help` for more information)
$ fly <destination> [--plan flightplan.js]

簡單的示例flightplan.js

// flightplan.js
var Flightplan = require('flightplan');

var tmpDir = 'pstadler-sh-' + new Date().getTime();

var plan = new Flightplan();

// configuration
plan.briefing({
    debug: false,
    destinations: {
        'staging': {
            host: 'staging.pstadler.sh',
            username: 'pstadler',
            agent: process.env.SSH_AUTH_SOCK
        },
        'production': [
            {
                host: 'www1.pstadler.sh',
                username: 'pstadler',
                agent: process.env.SSH_AUTH_SOCK
            },
            {
                host: 'www2.pstadler.sh',
                username: 'pstadler',
                agent: process.env.SSH_AUTH_SOCK
            }
        ]
    }
});

// run commands on localhost
plan.local(function(local) {
    local.log('Run build');
    local.exec('gulp build');

    local.log('Copy files to remote host');
    var filesToCopy = '(git ls-files -z;find assets/public -type f -print0)';
    local.exec(filesToCopy + '|rsync --files-from - -avz0 --rsh="ssh"'
                + ' ./ pstadler@pstadler.sh:/tmp/' + tmpDir);
});

// run commands on remote hosts (destinations)
plan.remote(function(remote) {
    remote.log('Move folder to web root');
    remote.sudo('cp -R /tmp/' + tmpDir + ' ~', { user: 'www' });
    remote.rm('-rf /tmp/' + tmpDir);

    remote.log('Install dependencies');
    remote.sudo('npm --production --silent --prefix ~/'
                    + tmpDir + ' install ~/' + tmpDir, { user: 'www' });

    remote.log('Reload application');
    remote.sudo('ln -snf ~/' + tmpDir + ' ~/pstadler-sh', { user: 'www' });
    remote.sudo('pm2 reload pstadler-sh', { user: 'www' });
});

// run more commands on localhost afterwards
plan.local(function(local) { /* ... */ });
// ...or on remote hosts
plan.remote(function(remote) { /* ... */ });

// executed if flightplan succeeded
plan.success(function() {
});

// executed if flightplan failed
plan.disaster(function() {
});

// always executed after flightplan finished
plan.debriefing(function() {
});

項目主頁:http://www.baiduhome.net/lib/view/home/1392514760491

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!