UNPKG

baidubce-cli

Version:

baidu cloud engine command line tools

289 lines (247 loc) 9.82 kB
/** * Copyright (c) 2014 Baidu.com, 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. * * @file src/media/cli.js * @author leeight */ var fs = require('fs'); var path = require('path'); var u = require('underscore'); var Q = require('q'); var moment = require('moment'); var bce = require('baidubce-sdk'); var debug = require('debug')('bce-media-cli'); var kPattern = /^bos:\/\/([^\/]+)(\/.*)?/; var cmdConfig = require('../config'); var argv = require('yargs') .usage('Usage: $0 media') .boolean('h', {alias: 'help', describe: 'Show this help message'}) .options('c', {alias: 'config', describe: 'Manage the baidubce configuration', 'boolean': true}) .options('pn', {alias: 'pipeline-name', describe: 'The pipeline name', requiresArg: true}) .options('pc', {alias: 'pipeline-config', describe: 'The associated configuration while creating a pipeline', requiresArg: true}) .options('rn', {alias: 'preset-name', describe: 'The preset name', requiresArg: true}) .options('ds', {alias: 'description', describe: 'The description', requiresArg: true}) .options('sb', {alias: 'source-bucket', describe: 'The source bucket', requiresArg: true}) .options('sk', {alias: 'source-key', describe: 'The source key', requiresArg: true}) .options('tb', {alias: 'target-bucket', describe: 'The target bucket', requiresArg: true}) .options('tk', {alias: 'target-key', describe: 'The target key', requiresArg: true}) .options('id', {describe: 'The id', requiresArg: true}) .options('create-pipeline', {describe: 'Create a pipeline.', 'boolean': true}) .example('baidubce media --create-pipeline --pn name --pc \'{"capacity": 5}\' ' + '--ds \'description.\' --sb bucket1 --tb bucket2', 'Create a pipeline.') .options('get-pipeline', {describe: 'Get a pipeline', 'boolean': true}) .example('baidubce media --get-pipeline --pn name', 'Get the specified pipeline detail') .options('get-all-pipelines', {describe: 'Get all pipelines', 'boolean': true}) .example('baidubce media --get-all-pipelines', 'Get all pipeline') .options('delete-pipeline', {describe: 'Delete a pipeline', 'boolean': true}) .example('baidubce media --delete-pipeline --pn name', 'Delete the specified pipeline') .options('create-job', {describe: 'Create a job', 'boolean': true}) .example('baidubce media --create-job --pn name --rn preset --sk key1 --tk key2', 'Create a job') .options('get-all-jobs', {describe: 'Get all jobs in the specified pipeline', 'boolean': true}) .example('baidubce media --get-all-jobs --pn name') .options('get-job', {describe: 'Get job by id', 'boolean': true}) .example('baidubce media --get-job --id jobId123') .options('get-mediainfo', {describe: 'Get the mediainfo', requiresArg: true}) .example('baidubce media --get-mediainfo bos://my-bucket/the_big_buck_bunny.avi', 'Get the mediainfo') // .options('put-object', {describe: 'Upload a regular file', requiresArg: true}) // .example('baidubce bos --put-object hello.js bos://my-bucket/world.js', 'Upload a regular file') // .options('delete-objects', {describe: 'Delete objects.', 'boolean': true}) // .example('baidubce bos --delete-objects my-bucket-1 hello.js world.js', 'Delete objects') ; exports.run = function () { var args = argv.argv; if (args.h === true || args.help === true) { argv.showHelp(); return; } else if (process.argv.length <= 2) { argv.showHelp(); return; } if (args.c) { if (!args._.length) { cmdConfig.dump(); } else { cmdConfig.set(args._); } return; } var actionMap = { 'create-pipeline': createPipeline, 'get-pipeline': getPipeline, 'get-all-pipelines': getAllPipelines, 'delete-pipeline': deletePipeline, 'create-job': createJob, 'get-all-jobs': getAllJobs, 'get-job': getJob, 'get-mediainfo': getMediaInfo }; for (var ak in actionMap) { if (args[ak]) { return actionMap[ak](args); } } }; /** * @return {bce.MediaClient} */ function getClient() { var ak = cmdConfig.get('media.ak'); var sk = cmdConfig.get('media.sk'); var region = cmdConfig.get('media.region'); var endpoint = cmdConfig.get('media.endpoint'); if (!ak || !sk) { throw new Error('Please set ak, sk.'); } return new bce.MediaClient({ credentials: {ak: ak, sk: sk}, region: region, endpoint: endpoint }); } function getJob(args) { debug('getJob args = %j', args); var jobId = args.id; getClient().getJob(jobId) .then(function (response) { console.log(JSON.stringify(response.body, null, 2)); }) .catch(function (error) { console.error(error); }); } function getAllJobs(args) { debug('getAllJobs args = %j', args); var pipelineName = args.pn; getClient().getAllJobs(pipelineName) .then(function (response) { var table = require('text-table'); var rows = [['No.', 'Job Id', 'Preset Name', 'Source', 'Target', 'Status', 'Start Time', 'End Time']]; u.each(response.body.jobs, function (job, idx) { rows.push([ idx + 1, job.jobId, job.presetName, job.source.sourceKey, job.target.targetKey, job.jobStatus, moment(job.startTime).format('YYYY-MM-DD hh:mm:ss'), moment(job.endTime).format('YYYY-MM-DD hh:mm:ss') ]); }); console.log(table(rows, {stringLength: stringLength})); }) .catch(function (error) { console.error(error); }); } function createJob(args) { debug('createJob args = %j', args); var pipelineName = args.pn; var presetName = args.rn; var source = {sourceKey: args.sk}; var target = {targetKey: args.tk}; getClient().createJob(pipelineName, source, target, presetName) .then(function (response) { console.log(JSON.stringify(response.body, null, 2)); }) .catch(function (error) { console.error(error); }); } function getMediaInfo(args) { debug('getMediaInfo args = %j', args); var bos = args['get-mediainfo']; var match = kPattern.exec(bos); if (!match) { console.error('Invalid Parameters, please check `baidubce media -h` for more info.'); return; } getClient().getMediainfo(match[1], match[2]) .then(function (response) { console.log(JSON.stringify(response.body, null, 2)); }) .catch(function (error) { console.error(error); }); } function deletePipeline(args) { debug('deletePipeline args = %j', args); var pipelineName = args.pn; getClient().deletePipeline(pipelineName) .then(function (response) { console.log(JSON.stringify(response.body, null, 2)); }) .catch(function (error) { console.error(error); }); } function getAllPipelines(args) { debug('getAllPipelines args = %j', args); getClient().getAllPipelines() .then(function (response) { var table = require('text-table'); var rows = [['No.', 'Name', 'Source Bucket', 'Target Bucket', 'Status', 'Config', 'CreateTime']]; u.each(response.body.pipelines, function (pipeline, idx) { rows.push([ idx + 1, pipeline.pipelineName, pipeline.sourceBucket, pipeline.targetBucket, pipeline.state, JSON.stringify(pipeline.config), moment(pipeline.createTime).format('YYYY-MM-DD hh:mm:ss') ]); }); console.log(table(rows, {stringLength: stringLength})); }) .catch(function (error) { console.error(error); }); } function getPipeline(args) { debug('getPipeline args = %j', args); var pipelineName = args.pn; getClient().getPipeline(pipelineName) .then(function (response) { console.log(JSON.stringify(response.body, null, 2)); }) .catch(function (error) { console.error(error); }); } function createPipeline(args) { debug('createPipeline args = %j', args); var pipelineName = args.pn; var sourceBucket = args.sb; var targetBucket = args.tb; var description = args.ds; var config = safeParse(args.pc); getClient().createPipeline(pipelineName, sourceBucket, targetBucket, config, description) .then(function (response) { console.log(JSON.stringify(response.body, null, 2)); }) .catch(function (error) { console.error(error); }); } function safeParse(text) { try { return JSON.parse(text); } catch (ex) { return null; } } function stringLength(s) { var a = String(s).length; // Non-ASCII character will produce double width. var b = String(s).replace(/[\x00-\xff]/g, '').length; return a + b; } /* vim: set ts=4 sw=4 sts=4 tw=120: */