4 async = require 'async'
5 af = require './api.coffee'
7 token_file = "#{process.env.HOME}/.af-coffee-token"
9 timeout = (ms, f) -> setTimeout f, ms
11 # a session caches the api access token and prompts for the username and
12 # password if it goes stale. It re-tries API calls that fail due to
13 # invalid/expired token
17 commands[k] = v unless k is 'login'
19 # this is the simple one that updates the files then restarts the app.
20 # to update without severing current connections or downtime, see app_publish_seamless
21 commands.app_publish = (token, app_name, zip_file, callback) ->
23 (callback) => @api 'app_update_files', app_name, zip_file, callback
24 (res, callback) => @api 'app_restart', app_name, callback
28 # This uses a pair of apps to get a seamless upgrades. ie the site is never
29 # down, and connections that are open at the time of upgrade are given 10
34 # 1. There must be two apps with the same name except one has an underscore at
35 # the end and the other doesn't.
37 # 2. One of these apps must be running, and must have the domain names mapped
40 # 3. The other must be stopped, and must not have the domain names mapped to
41 # it. (just it's *.af.com address.)
43 # For now, pass only the name of the app which is currently stopped.
44 commands.app_publish_seamless = (token, app_name, zip_file, callback) ->
45 # FIXME auto-detect which one is new by checking app_info.urls.length
46 # (should happen for all api calls?)
48 # for now, we assume that the passed app_name is the currently dormant one
49 if app_name.substr(-1) is '_'
50 old_app = app_name.substr 0, app_name.length - 1
53 old_app = app_name + '_'
57 old_info: (cb) => @api 'app_info', old_app, cb
58 new_info: (cb) => @api 'app_info', new_app, cb
59 push: (cb) => @api 'app_update_files', new_app, zip_file, cb
60 copy_uris: ['push', 'old_info', 'new_info', (cb, args) =>
61 # There's a bug in the server where you can't set new uris and
62 # start the app in the same app_set_info call
63 for u in args.old_info.uris
64 args.new_info.uris.push u unless u.substr(-6) is '.af.cm'
65 @api 'app_set_info', new_app, args.new_info, cb
67 new_info_again: ['copy_uris', (cb, args) =>
68 @api 'app_info', new_app, cb
70 start_new: ['new_info_again', (cb, args) =>
71 args.new_info_again.state = 'STARTED'
72 @api 'app_set_info', new_app, args.new_info_again, cb
74 hide_old: ['start_new', 'old_info', (cb, args) =>
76 for u in args.old_info.uris
77 just_af.push u if u.substr(-6) is '.af.cm'
78 args.old_info.uris = just_af
79 @api 'app_set_info', old_app, args.old_info, cb
81 wait_for_old_connections: ['hide_old', (cb) =>
83 log_id = @log_start "waiting #{seconds} seconds for connections to old instance to finish"
84 timeout seconds * 1000, =>
88 old_info_again: ['hide_old', (cb) =>
89 @api 'app_info', old_app, cb
91 stop_old: ['wait_for_old_connections', 'old_info_again', (cb, args) =>
92 args.old_info_again.state = 'STOPPED'
93 @api 'app_set_info', old_app, args.old_info_again, cb
95 # TODO when we get into sending manifests, we may need to update_files to old_app too
98 app_set_state = (token, app_name, state, callback) ->
101 @api 'app_info', app_name, callback
104 @api 'app_set_info', app_name, info, callback
107 commands.app_start = (token, app_name, callback) ->
108 app_set_state.call this, token, app_name, 'STARTED', callback
110 commands.app_stop = (token, app_name, callback) ->
111 app_set_state.call this, token, app_name, 'STOPPED', callback
114 commands.app_restart = (token, app_name, callback) ->
115 # Server requires you to fetch the app state before each call to change
116 # it, so there's no quicker way than just calling app_stop then app_start
118 (callback) => @api 'app_stop', app_name, callback
119 (res, callback) => @api 'app_start', app_name, callback
134 out += '\n' if @log_mid
135 for i in [0...@log_nest]
140 return unless @verbose
142 process.stdout.write "#{@log_whitespace()}#{@log_id}: #{msg}"
147 log_end: (start_id) ->
148 return unless @verbose
150 if @log_mid is start_id
151 process.stdout.write "... done\n"
153 process.stdout.write "#{@log_whitespace()}#{start_id} done\n"
156 api: (call, args..., callback) ->
163 login.call this, callback
165 callback null, @token
168 log_id = @log_start [call, args...].join ' '
169 # commands implemented in client.coffee need "this" pointing to the session
170 commands[call].call this, @token, args..., (err, the_rest...) =>
171 @log_end(log_id) unless err?
172 callback err, the_rest...
174 # eg /app/xxx/stats sometimes returns 404 with wrong auth token
175 if err?.code is 403 or err?.code is 404
177 @api(call, args..., callback)
181 ask: (opts, callback) ->
182 process.stdout.write @log_whitespace() + opts.prompt
183 process.stdin.setEncoding 'utf8'
184 process.stdin.resume()
185 process.stdin.once 'data', (line) =>
187 # send ^[[A^[[2K to move the cursor up one line, then clear that line
188 process.stdout.write new Buffer [27, 91, 65, 27, 91, 50, 75]
189 process.stdout.write @log_whitespace() + opts.prompt + "***\n"
190 process.stdin.pause()
192 callback null, (line.substr 0, line.length - 1)
194 get_token = (callback) ->
195 fs.readFile token_file, 'utf8', (err, token) ->
201 login = (callback) ->
203 (callback) => async.series [
204 (callback) => @ask prompt: 'username: ', callback
205 (callback) => @ask prompt: 'password: ', silent: true, callback
207 ([username, password], callback) ->
208 af.login username, password, callback
210 # wait for file write so there's no race condition if get_token gets called soon
211 fs.writeFile token_file, token, (err) ->
213 process.stderr.write "Warning: couldn't cache auth token in #{token_file}: #{err}\n"
214 # don't pass on error, it's ok if we can't cache it
219 exports.new_session = ->
223 process.stderr.write "usage: #{process.argv[0]} #{process.argv[1]} command [args...]\n"
224 process.stderr.write "valid commands are:\n\t#{(k for k, v of commands).join '\n\t'}\n"
226 # parse and act on commandline arguments unless we were require()d as a module
227 if require.main is module
228 args = process.argv[2..]
231 else if not commands[args[0]]
232 process.stderr.write "unknown command \"#{args[0]}\"\n"
235 session = new Session()
236 session.api args[0], args[1..]..., (err, result) ->
238 process.stderr.write "Error: #{JSON.stringify err}\n"
240 if typeof result is 'string'
241 process.stdout.write result
243 process.stdout.write JSON.stringify result