JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
add .editorconfig
[af-coffee.git] / client.coffee
index 84fff91..02d28c1 100755 (executable)
@@ -16,14 +16,85 @@ commands = {}
 for k, v of af
        commands[k] = v unless k is 'login'
 
-# this is the friendly one that updates the files then restarts the app
+# 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
 
-commands.app_set_state = (token, app_name, state, 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.)
+#
+# Pass the app name without the underscore at the end, and app_publish_seamless
+# will figure out which is which
+commands.app_publish_seamless = (token, app_name, zip_file, callback) ->
+       async.auto {
+               info1: (cb) => @api 'app_info', app_name, cb
+               info2: (cb) => @api 'app_info', app_name + '_', cb
+               infos: ['info1', 'info2', (cb, args) ->
+                       # FIXME check for other requirements and bail if not met
+                       if args.info1.state is 'STOPPED'
+                               cb null, new: args.info1, old: args.info2
+                       else
+                               cb null, new: args.info2, old: args.info1
+               ]
+               push: ['infos', (cb, args) =>
+                       @api 'app_update_files', args.infos.new.name, zip_file, cb
+               ]
+               copy_uris: ['push', 'infos', (cb, args) =>
+                       # There's a bug in the server where you can't set new uris and
+                       # start the app in the same app_set_info call
+                       for u in args.infos.old.uris
+                               args.infos.new.uris.push u unless u.substr(-6) is '.af.cm'
+                       @api 'app_set_info', args.infos.new.name, args.infos.new, cb
+               ]
+               new_info_again: ['copy_uris', 'infos', (cb, args) =>
+                       @api 'app_info', args.infos.new.name, cb
+               ]
+               start_new: ['new_info_again', (cb, args) =>
+                       args.new_info_again.state = 'STARTED'
+                       @api 'app_set_info', args.new_info_again.name, args.new_info_again, cb
+               ]
+               hide_old: ['start_new', 'infos', (cb, args) =>
+                       just_af = []
+                       for u in args.infos.old.uris
+                               just_af.push u if u.substr(-6) is '.af.cm'
+                       args.infos.old.uris = just_af
+                       @api 'app_set_info', args.infos.old.name, args.infos.old, 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', 'infos', (cb, args) =>
+                       @api 'app_info', args.infos.old.name, cb
+               ]
+               stop_old: ['wait_for_old_connections', 'old_info_again', (cb, args) =>
+                       args.old_info_again.state = 'STOPPED'
+                       @api 'app_set_info', args.old_info_again.name, args.old_info_again, cb
+               ]
+               # TODO when we get into sending manifests, we may need to update_files to infos.old.name too
+       }, callback
+
+app_set_state = (token, app_name, state, callback) ->
        async.waterfall [
                (callback) =>
                        @api 'app_info', app_name, callback
@@ -33,10 +104,10 @@ commands.app_set_state = (token, app_name, state, callback) ->
        ], callback
 
 commands.app_start = (token, app_name, callback) ->
-       @api 'app_set_state', app_name, 'STARTED', callback
+       app_set_state.call this, token, app_name, 'STARTED', callback
 
 commands.app_stop = (token, app_name, callback) ->
-       @api 'app_set_state', app_name, 'STOPPED', callback
+       app_set_state.call this, token, app_name, 'STOPPED', callback
 
 
 commands.app_restart = (token, app_name, callback) ->
@@ -52,6 +123,34 @@ commands.app_restart = (token, app_name, 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 [
@@ -60,13 +159,16 @@ class Session
                                        when 0
                                                get_token callback
                                        when -1
-                                               login callback
+                                               login.call this, callback
                                        else
                                                callback null, @token
                        (token, callback) =>
                                @token = token
+                               log_id = @log_start [call, args...].join ' '
                                # commands implemented in client.coffee need "this" pointing to the session
-                               commands[call].call this, @token, args..., callback
+                               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
@@ -75,17 +177,18 @@ class Session
                        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) ->
@@ -94,11 +197,19 @@ get_token = (callback) ->
                else
                        callback null, token
 
-login = (callback) ->
+login_callbacks = []
+login = (real_callback) ->
+       login_callbacks.push real_callback
+       return if login_callbacks.length > 1
+
+       callback = (err, token) ->
+               while login_callbacks.length > 0
+                       login_callbacks.shift()(err, token)
+
        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