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 push: (cb) => @api 'app_update_files', new_app, zip_file, cb
58 # TODO find out if app_update_files increments app_info.version
59 # if not, drop new_info's dependency on push
60 old_info: (cb) => @api 'app_info', old_app, cb
61 new_info: ['push', (cb) => @api 'app_info', new_app, cb ]
62 start_new: ['push', 'old_info', 'new_info', (cb, args) =>
63 args.new_info.state = 'STARTED'
64 for u in args.old_info.uris
65 args.new_info.uris.push u unless u.substr(-6) is '.af.cm'
66 @api 'app_set_info', new_app, args.new_info, cb
68 hide_old: ['start_new', 'old_info', (cb, args) =>
70 for u in args.old_info.uris
71 just_af.push u if u.substr(-6) is '.af.cm'
72 args.old_info.uris = just_af
73 @api 'app_set_info', old_app, args.old_info, cb
75 wait_for_old_connections: ['hide_old', (cb) =>
77 log_id = @log_start "waiting #{seconds} seconds for connections to old instance to finish"
78 timeout seconds * 1000, =>
82 old_info_again: ['hide_old', (cb) =>
83 @api 'app_info', old_app, cb
85 stop_old: ['wait_for_old_connections', 'old_info_again', (cb, args) =>
86 args.old_info_again.state = 'STOPPED'
87 @api 'app_set_info', old_app, args.old_info_again, cb
89 # TODO when we get into sending manifests, we may need to update_files to old_app too
92 app_set_state = (token, app_name, state, callback) ->
95 @api 'app_info', app_name, callback
98 @api 'app_set_info', app_name, info, callback
101 commands.app_start = (token, app_name, callback) ->
102 app_set_state.call this, token, app_name, 'STARTED', callback
104 commands.app_stop = (token, app_name, callback) ->
105 app_set_state.call this, token, app_name, 'STOPPED', callback
108 commands.app_restart = (token, app_name, callback) ->
109 # Server requires you to fetch the app state before each call to change
110 # it, so there's no quicker way than just calling app_stop then app_start
112 (callback) => @api 'app_stop', app_name, callback
113 (res, callback) => @api 'app_start', app_name, callback
128 out += '\n' if @log_mid
129 for i in [0...@log_nest]
134 return unless @verbose
136 process.stdout.write "#{@log_whitespace()}#{@log_id}: #{msg}"
141 log_end: (start_id) ->
142 return unless @verbose
144 if @log_mid is start_id
145 process.stdout.write "... done\n"
147 process.stdout.write "#{@log_whitespace()}#{start_id} done\n"
150 api: (call, args..., callback) ->
157 login.call this, callback
159 callback null, @token
162 log_id = @log_start [call, args...].join ' '
163 # commands implemented in client.coffee need "this" pointing to the session
164 commands[call].call this, @token, args..., (err, the_rest...) =>
165 @log_end(log_id) unless err?
166 callback err, the_rest...
168 # eg /app/xxx/stats sometimes returns 404 with wrong auth token
169 if err?.code is 403 or err?.code is 404
171 @api(call, args..., callback)
175 ask: (opts, callback) ->
176 process.stdout.write @log_whitespace() + opts.prompt
177 process.stdin.setEncoding 'utf8'
178 process.stdin.resume()
179 process.stdin.once 'data', (line) =>
181 # send ^[[A^[[2K to move the cursor up one line, then clear that line
182 process.stdout.write new Buffer [27, 91, 65, 27, 91, 50, 75]
183 process.stdout.write @log_whitespace() + opts.prompt + "***\n"
184 process.stdin.pause()
186 callback null, (line.substr 0, line.length - 1)
188 get_token = (callback) ->
189 fs.readFile token_file, 'utf8', (err, token) ->
195 login = (callback) ->
197 (callback) => async.series [
198 (callback) => @ask prompt: 'username: ', callback
199 (callback) => @ask prompt: 'password: ', silent: true, callback
201 ([username, password], callback) ->
202 af.login username, password, callback
204 # wait for file write so there's no race condition if get_token gets called soon
205 fs.writeFile token_file, token, (err) ->
207 process.stderr.write "Warning: couldn't cache auth token in #{token_file}: #{err}\n"
208 # don't pass on error, it's ok if we can't cache it
213 exports.new_session = ->
217 process.stderr.write "usage: #{process.argv[0]} #{process.argv[1]} command [args...]\n"
218 process.stderr.write "valid commands are:\n\t#{(k for k, v of commands).join '\n\t'}\n"
220 # parse and act on commandline arguments unless we were require()d as a module
221 if require.main is module
222 args = process.argv[2..]
225 else if not commands[args[0]]
226 process.stderr.write "unknown command \"#{args[0]}\"\n"
229 session = new Session()
230 session.api args[0], args[1..]..., (err, result) ->
232 process.stderr.write "Error: #{JSON.stringify err}\n"
234 if typeof result is 'string'
235 process.stdout.write result
237 process.stdout.write JSON.stringify result