X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=client.coffee;h=9e28b9b09f14d0de7c53cbf0103d6af417a13db2;hb=4b25ae43b769543dc9cb4c1c43178f9d43eb095f;hp=a378480a3b05cf13ba33389fbe24b06e05d04f98;hpb=d20d81a40aaf14801c8be6b94ff2e97dd54f15a1;p=af-coffee.git diff --git a/client.coffee b/client.coffee old mode 100644 new mode 100755 index a378480..9e28b9b --- a/client.coffee +++ b/client.coffee @@ -1,3 +1,5 @@ +#!/usr/bin/coffee + fs = require 'fs' async = require 'async' af = require './api.coffee' @@ -10,9 +12,140 @@ timeout = (ms, f) -> setTimeout f, ms # password if it goes stale. It re-tries API calls that fail due to # invalid/expired token +commands = {} +for k, v of af + commands[k] = v unless k is 'login' + +# this is the simple one that updates the files then restarts the app. +# to update without severing current connections or downtime, see app_publish_seamless +commands.app_publish = (token, app_name, zip_file, callback) -> + async.waterfall [ + (callback) => @api 'app_update_files', app_name, zip_file, callback + (res, callback) => @api 'app_restart', app_name, callback + ], callback + + +# This uses a pair of apps to get a seamless upgrades. ie the site is never +# down, and connections that are open at the time of upgrade are given 10 +# seconds to close. +# +# Requirements: +# +# 1. There must be two apps with the same name except one has an underscore at +# the end and the other doesn't. +# +# 2. One of these apps must be running, and must have the domain names mapped +# to it. +# +# 3. The other must be stopped, and must not have the domain names mapped to +# it. (just it's *.af.com address.) +# +# For now, pass only the name of the app which is currently stopped. +commands.app_publish_seamless = (token, app_name, zip_file, callback) -> + # FIXME auto-detect which one is new by checking app_info.urls.length + # (should happen for all api calls?) + # + # for now, we assume that the passed app_name is the currently dormant one + if app_name.substr(-1) is '_' + old_app = app_name.substr 0, app_name.length - 1 + new_app = app_name + else + old_app = app_name + '_' + new_app = app_name + + async.auto { + push: (cb) => @api 'app_update_files', new_app, zip_file, cb + # TODO find out if app_update_files increments app_info.version + # if not, drop new_info's dependency on push + old_info: (cb) => @api 'app_info', old_app, cb + new_info: ['push', (cb) => @api 'app_info', new_app, cb ] + start_new: ['push', 'old_info', 'new_info', (cb, args) => + args.new_info.state = 'STARTED' + for u in args.old_info.uris + args.new_info.uris.push u unless u.substr(-6) is '.af.cm' + @api 'app_set_info', new_app, args.new_info, cb + ] + hide_old: ['start_new', 'old_info', (cb, args) => + just_af = [] + for u in args.old_info.uris + just_af.push u if u.substr(-6) is '.af.cm' + args.old_info.uris = just_af + @api 'app_set_info', old_app, args.old_info, cb + ] + wait_for_old_connections: ['hide_old', (cb) => + seconds = 10 + log_id = @log_start "waiting #{seconds} seconds for connections to old instance to finish" + timeout seconds * 1000, => + @log_end(log_id) + cb() + ] + old_info_again: ['hide_old', (cb) => + @api 'app_info', old_app, cb + ] + stop_old: ['wait_for_old_connections', 'old_info_again', (cb, args) => + args.old_info_again.state = 'STOPPED' + @api 'app_set_info', old_app, args.old_info_again, cb + ] + # TODO when we get into sending manifests, we may need to update_files to old_app too + }, callback + +app_set_state = (token, app_name, state, callback) -> + async.waterfall [ + (callback) => + @api 'app_info', app_name, callback + (info, callback) => + info.state = state + @api 'app_set_info', app_name, info, callback + ], callback + +commands.app_start = (token, app_name, callback) -> + app_set_state.call this, token, app_name, 'STARTED', callback + +commands.app_stop = (token, app_name, callback) -> + app_set_state.call this, token, app_name, 'STOPPED', callback + + +commands.app_restart = (token, app_name, callback) -> + # Server requires you to fetch the app state before each call to change + # it, so there's no quicker way than just calling app_stop then app_start + async.waterfall [ + (callback) => @api 'app_stop', app_name, callback + (res, callback) => @api 'app_start', app_name, callback + ], callback + + + class Session constructor: -> @token = 0 + @verbose = true + @log_nest = 0 + @log_mid = 0 + @log_id = 0 + + log_whitespace: -> + out = '' + out += '\n' if @log_mid + for i in [0...@log_nest] + out += '\t' + return out + + log_start: (msg) -> + return unless @verbose + @log_id += 1 + process.stdout.write "#{@log_whitespace()}#{@log_id}: #{msg}" + @log_nest += 1 + @log_mid = @log_id + return @log_id + + log_end: (start_id) -> + return unless @verbose + @log_nest -= 1 + if @log_mid is start_id + process.stdout.write "... done\n" + else + process.stdout.write "#{@log_whitespace()}#{start_id} done\n" + @log_mid = 0 api: (call, args..., callback) -> async.waterfall [ @@ -21,12 +154,16 @@ class Session when 0 get_token callback when -1 - login callback + login.call this, callback else callback null, @token (token, callback) => @token = token - af[call] @token, args..., callback + log_id = @log_start [call, args...].join ' ' + # commands implemented in client.coffee need "this" pointing to the session + commands[call].call this, @token, args..., (err, the_rest...) => + @log_end(log_id) unless err? + callback err, the_rest... ], (err, result) => # eg /app/xxx/stats sometimes returns 404 with wrong auth token if err?.code is 403 or err?.code is 404 @@ -34,18 +171,19 @@ class Session @api(call, args..., callback) else callback err, result - -ask = (opts, callback) -> - process.stdout.write opts.prompt - process.stdin.setEncoding 'utf8' - process.stdin.resume() - process.stdin.once 'data', (line) -> - if opts.silent - # send ^[[A^[[2K to move the cursor up one line, then clear that line - process.stdout.write new Buffer [27, 91, 65, 27, 91, 50, 75] - process.stdout.write opts.prompt + "***\n" - process.stdin.pause() - callback null, (line.substr 0, line.length - 1) + + ask: (opts, callback) -> + process.stdout.write @log_whitespace() + opts.prompt + process.stdin.setEncoding 'utf8' + process.stdin.resume() + process.stdin.once 'data', (line) => + if opts.silent + # send ^[[A^[[2K to move the cursor up one line, then clear that line + process.stdout.write new Buffer [27, 91, 65, 27, 91, 50, 75] + process.stdout.write @log_whitespace() + opts.prompt + "***\n" + process.stdin.pause() + @log_mid = 0 + callback null, (line.substr 0, line.length - 1) get_token = (callback) -> fs.readFile token_file, 'utf8', (err, token) -> @@ -56,9 +194,9 @@ get_token = (callback) -> login = (callback) -> async.waterfall [ - (callback) -> async.series [ - (callback) -> ask prompt: 'username: ', callback - (callback) -> ask prompt: 'password: ', silent: true, callback + (callback) => async.series [ + (callback) => @ask prompt: 'username: ', callback + (callback) => @ask prompt: 'password: ', silent: true, callback ], callback ([username, password], callback) -> af.login username, password, callback @@ -66,7 +204,7 @@ login = (callback) -> # wait for file write so there's no race condition if get_token gets called soon fs.writeFile token_file, token, (err) -> if err - console.log "Warning: couldn't cache auth token in #{token_file}: ", err + process.stderr.write "Warning: couldn't cache auth token in #{token_file}: #{err}\n" # don't pass on error, it's ok if we can't cache it callback null, token ], callback @@ -74,3 +212,26 @@ login = (callback) -> exports.new_session = -> return new Session() + +usage = -> + process.stderr.write "usage: #{process.argv[0]} #{process.argv[1]} command [args...]\n" + process.stderr.write "valid commands are:\n\t#{(k for k, v of commands).join '\n\t'}\n" + +# parse and act on commandline arguments unless we were require()d as a module +if require.main is module + args = process.argv[2..] + if args.length is 0 + usage() + else if not commands[args[0]] + process.stderr.write "unknown command \"#{args[0]}\"\n" + usage() + else + session = new Session() + session.api args[0], args[1..]..., (err, result) -> + if err? + process.stderr.write "Error: #{JSON.stringify err}\n" + if result? + if typeof result is 'string' + process.stdout.write result + else + process.stdout.write JSON.stringify result