UNPKG

af

Version:

a command line interface for interacting with AppFog API. This should eventually be able to replace the af gem.

250 lines (200 loc) 7.02 kB
#! /usr/bin/node var afapi = require("appfog-api"), util = require("util"), fs = require("fs"), path = require("path"); /** * Setting Class constructor */ function Settings() { this.token = ""; this.user = ""; this.endpoint = "https://api.appfog.com"; this.load = function() { var pkg = require(__dirname+"/package.json"); pkg = pkg.config || {}; pkg = pkg.appfog || {}; this.token = pkg.token || ""; this.user = pkg.user || ""; this.endpoint = pkg.endpoint || this.endpoint; }; this.save = function() { var pkg = require(__dirname+"/package.json"); pkg.config = pkg.config || {}; pkg.config.appfog = pkg.config.appfog || {}; pkg.config.appfog.token = this.token; pkg.config.appfog.user = this.user; fs.writeFileSync(__dirname+"/package.json", JSON.stringify(pkg, null, 2)); }; //trigger load on creation of object this.load(); } var sessionSettings = new Settings(); afapi.settings = sessionSettings; // ditch node & node bin location var args = process.argv.slice(2); switch (args[0]) { case 'login': login(args[1], args[2]); break; case 'info': info(); break; case 'apps': apps(); break; case 'services': services(); break; case 'app': app(); break; case 'stats': stats(args[1]); break; default: console.log(' '); console.log(' Error: Unrecognized command '); console.log(' '); printHelp(); } function printHelp() { //Thanks : http://patorjk.com/software/taag/#p=display&h=1&v=0&c=c&f=Banner3&t=Dont%20Panic console.log(' '); console.log(' ###### ###### '); console.log(' # # #### # # ##### # # ## # # # #### '); console.log(' # # # # ## # # # # # # ## # # # # '); console.log(' # # # # # # # # ###### # # # # # # # '); console.log(' # # # # # # # # # ###### # # # # # '); console.log(' # # # # # ## # # # # # ## # # # '); console.log(' ###### #### # # # # # # # # # #### '); console.log(' '); console.log(' '); console.log(' Usage '); console.log(' ============================================================='); console.log(' af info '); console.log(' af login '); console.log(' af infras '); console.log(' af apps '); console.log(' af app <app-name> '); console.log(' af stats <app-name> '); } function login(eml, psswrd) { //if no username prompt for input //if no password prompt for input if(!eml || !psswrd){ console.error('Usage: login <email> <password>'); return; } afapi.login({email:eml, password:psswrd}, function (err, token) { if(err){ console.log(err); }else{ console.log('Sucessfull login'); sessionSettings.token = token; sessionSettings.save(); } }); } function info() { afapi.info({}, function (error, info) { if(error) { printHelp(); }else{ prettyPrintInfo(info); } }); } function prettyPrintInfo(info){ console.log(' '); console.log(' Current User: %s',info.user); console.log(' ---------------------------------------------------------------'); console.log(' %s Applications using %s/%sMb of mem. %s SSL endpoints left', info.usage.apps, info.usage.memory, info.limits.memory, info.limits.ssl); console.log(' ---------------------------------------------------------------'); console.log(' FrameWorks avaliable '); console.log(' ---------------------------------------------------------------'); for(var key in info.frameworks) { console.log(' Name: %s', key); } } function app() { afapi.app({}, function (error, app) { if(error) { printHelp(); }else{ JSON.stringify(app, null, 2); } }); } function apps() { afapi.apps({}, function (error, apps) { if(error) { printHelp(); }else{ prettyPrintApps(apps); } }); } /** * Example app object, returned in a greater object with keys 0-x * * { * "name": "quotebook-dev", * "staging": { * "model": "node", * "stack": "node06" * }, * "uris": [ * "quotebook-dev.aws.af.cm" * ], * "instances": 1, * "runningInstances": null, * "resources": { * "memory": 128, * "disk": 1024, * "fds": 256 * }, * "state": "STOPPED", * "services": [], * "version": "c1adc9f2d9834c25daa39a0799e4937f5bd739df-1", * "env": [], * "meta": { * "debug": null, * "console": null, * "version": 17, * "created": 1367579153 * }, * "infra": { * "provider": "aws", * "name": "aws" * } * } */ function prettyPrintApps(apps) { console.log(' '); console.log(' Stack:App:Instances - state - URL:Infra - mem disk '); console.log(' ==============================================================='); console.log(' '); var app; for(var key in apps){ app = apps[key]; console.log(' %s:%s:%s - %s - %s', app.staging.stack, app.name, app.instances, app.state, app.infra.name); } } function services() { afapi.services({}, function (error, services) { if(error) { printHelp(); } if(services.length === 0){ console.log(' No services installed'); return; } console.log(JSON.stringify(services, null, 2)); }); } function stats(appname) { afapi.stats({'appname':appname}, function (error, appstats) { console.log(JSON.stringify(appstats, null, 2)); }) }