index.ts 5.9 KB
Newer Older
1 2
#!/usr/bin/env node

P
Peter Pan 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/**
 * Copyright 2020 Baidu Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

19 20
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable no-console */
21 22

import ora, {Ora} from 'ora';
P
Peter Pan 已提交
23

24 25 26 27 28
import ecosystem from '@visualdl/server/ecosystem.config';
import pm2 from 'pm2';

const app = ecosystem.apps[0];

29 30 31 32
const argv = require('yargs')
    .usage('Usage: $0 <command> [options]')
    .command('start', 'Start VisualDL server')
    .command('stop', 'Stop VisualDL server')
33 34 35 36
    .example(
        '$0 start --backend="http://172.17.0.82:8040"',
        'Start VisualDL server with backend address http://172.17.0.82:8040'
    )
37 38 39 40 41 42
    .alias('p', 'port')
    .nargs('p', 1)
    .nargs('port', 1)
    .describe('p', 'Port of server')
    .nargs('host', 1)
    .describe('host', 'Host of server')
43 44 45 46
    .alias('b', 'backend')
    .nargs('b', 1)
    .nargs('backend', 1)
    .describe('b', 'Backend API address')
P
Peter Pan 已提交
47 48
    .boolean('demo')
    .describe('demo', 'Run in demo mode')
49 50 51 52
    .boolean('open')
    .describe('open', 'Open browser when server is ready')
    .help('h')
    .alias('h', 'help')
53
    .epilog('Visit https://github.com/PaddlePaddle/VisualDL for more information.').argv;
54 55 56 57 58 59 60 61

const command = argv._[0];

const exit = () => {
    console.log('Command not found, use -h or --help for help');
    process.exit(1);
};

62
const exitIfError = (err?: Error, exitCode = 1, spinner?: Ora) => {
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    if (!err) {
        return;
    }
    if (spinner) {
        spinner.fail('Error!');
    }
    console.error(err);
    process.exit(exitCode);
};

if (!command) {
    exit();
}

const banner = `

█████   █████  ███                               ████  ██████████   █████
░░███   ░░███  ░░░                               ░░███ ░░███░░░░███ ░░███
 ░███    ░███  ████   █████  █████ ████  ██████   ░███  ░███   ░░███ ░███
 ░███    ░███ ░░███  ███░░  ░░███ ░███  ░░░░░███  ░███  ░███    ░███ ░███
 ░░███   ███   ░███ ░░█████  ░███ ░███   ███████  ░███  ░███    ░███ ░███
  ░░░█████░    ░███  ░░░░███ ░███ ░███  ███░░███  ░███  ░███    ███  ░███      █
    ░░███      █████ ██████  ░░████████░░████████ █████ ██████████   ███████████
     ░░░      ░░░░░ ░░░░░░    ░░░░░░░░  ░░░░░░░░ ░░░░░ ░░░░░░░░░░   ░░░░░░░░░░░



`;

pm2.connect(err => {
    exitIfError(err, 2);

    pm2.list((err, list) => {
        exitIfError(err, 2);

        const host = argv.host || 'localhost';
        const port = Number.parseInt(argv.port, 10) || 8999;
        const url = `http://${host}:${port}`;

        if (command === 'start') {
            if (list.find(item => item.name === app.name)) {
104
                exitIfError(new Error('VisualDL server is already running'), 1);
105 106 107 108 109 110
            }
            const spinner = ora('Starting VisualDL server...').start();
            pm2.start(
                {
                    ...app,
                    env: {
111
                        ...app.env,
112
                        HOST: host,
113
                        PORT: port + '',
P
Peter Pan 已提交
114 115
                        BACKEND: argv.backend,
                        DEMO: argv.demo ? '1' : ''
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
                    }
                },
                err => {
                    pm2.disconnect();
                    exitIfError(err, 2, spinner);
                    spinner.succeed('Starting VisualDL server... Done');
                    console.log(banner);
                    console.log(`> VisualDL server is running at ${url}`);
                    if (argv.open) {
                        console.log('  Opening your browser for you...');
                        const open = require('open');
                        open(url);
                    }
                }
            );
        } else if (command === 'stop') {
            if (!list.find(item => item.name === app.name)) {
133
                exitIfError(new Error('VisualDL server is not running'), 1);
134 135 136 137
            }
            const spinner = ora('Stopping VisualDL server...').start();
            pm2.delete(app.name, err => {
                exitIfError(err, 2);
138
                const end = (err?: Error) => {
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
                    pm2.disconnect();
                    exitIfError(err, 2);

                    spinner.succeed('Stopping VisualDL server... Done');
                    console.log('> VisualDL server stopped');
                    console.log('  See you next time');
                    process.exit(0);
                };
                pm2.list((err, newList) => {
                    exitIfError(err, 2);
                    if (!newList.length) {
                        pm2.killDaemon(end);
                    } else {
                        end();
                    }
                });
            });
        } else {
            exit();
        }
    });
});