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 # Pass the app name without the underscore at the end, and app_publish_seamless
44 # will figure out which is which
45 commands.app_publish_seamless = (token, app_name, zip_file, callback) ->
47 info1: (cb) => @api 'app_info', app_name, cb
48 info2: (cb) => @api 'app_info', app_name + '_', cb
49 infos: ['info1', 'info2', (cb, args) ->
50 # FIXME check for other requirements and bail if not met
51 if args.info1.state is 'STOPPED'
52 cb null, new: args.info1, old: args.info2
54 cb null, new: args.info2, old: args.info1
56 push: ['infos', (cb, args) =>
57 @api 'app_update_files', args.infos.new.name, zip_file, cb
59 copy_uris: ['push', 'infos', (cb, args) =>
60 # There's a bug in the server where you can't set new uris and
61 # start the app in the same app_set_info call
62 for u in args.infos.old.uris
63 args.infos.new.uris.push u unless u.substr(-6) is '.af.cm'
64 @api 'app_set_info', args.infos.new.name, args.infos.new, cb
66 new_info_again: ['copy_uris', 'infos', (cb, args) =>
67 @api 'app_info', args.infos.new.name, cb
69 start_new: ['new_info_again', (cb, args) =>
70 args.new_info_again.state = 'STARTED'
71 @api 'app_set_info', args.new_info_again.name, args.new_info_again, cb
73 hide_old: ['start_new', 'infos', (cb, args) =>
75 for u in args.infos.old.uris
76 just_af.push u if u.substr(-6) is '.af.cm'
77 args.infos.old.uris = just_af
78 @api 'app_set_info', args.infos.old.name, args.infos.old, cb
80 wait_for_old_connections: ['hide_old', (cb) =>
82 log_id = @log_start "waiting #{seconds} seconds for connections to old instance to finish"
83 timeout seconds * 1000, =>
87 old_info_again: ['hide_old', 'infos', (cb, args) =>
88 @api 'app_info', args.infos.old.name, cb
90 stop_old: ['wait_for_old_connections', 'old_info_again', (cb, args) =>
91 args.old_info_again.state = 'STOPPED'
92 @api 'app_set_info', args.old_info_again.name, args.old_info_again, cb
94 # TODO when we get into sending manifests, we may need to update_files to infos.old.name too
97 app_set_state = (token, app_name, state, callback) ->
100 @api 'app_info', app_name, callback
103 @api 'app_set_info', app_name, info, callback
106 commands.app_start = (token, app_name, callback) ->
107 app_set_state.call this, token, app_name, 'STARTED', callback
109 commands.app_stop = (token, app_name, callback) ->
110 app_set_state.call this, token, app_name, 'STOPPED', callback
113 commands.app_restart = (token, app_name, callback) ->
114 # Server requires you to fetch the app state before each call to change
115 # it, so there's no quicker way than just calling app_stop then app_start
117 (callback) => @api 'app_stop', app_name, callback
118 (res, callback) => @api 'app_start', app_name, callback
133 out += '\n' if @log_mid
134 for i in [0...@log_nest]
139 return unless @verbose
141 process.stdout.write "#{@log_whitespace()}#{@log_id}: #{msg}"
146 log_end: (start_id) ->
147 return unless @verbose
149 if @log_mid is start_id
150 process.stdout.write "... done\n"
152 process.stdout.write "#{@log_whitespace()}#{start_id} done\n"
155 api: (call, args..., callback) ->
162 login.call this, callback
164 callback null, @token
167 log_id = @log_start [call, args...].join ' '
168 # commands implemented in client.coffee need "this" pointing to the session
169 commands[call].call this, @token, args..., (err, the_rest...) =>
170 @log_end(log_id) unless err?
171 callback err, the_rest...
173 # eg /app/xxx/stats sometimes returns 404 with wrong auth token
174 if err?.code is 403 or err?.code is 404
176 @api(call, args..., callback)
180 ask: (opts, callback) ->
181 process.stdout.write @log_whitespace() + opts.prompt
182 process.stdin.setEncoding 'utf8'
183 process.stdin.resume()
184 process.stdin.once 'data', (line) =>
186 # send ^[[A^[[2K to move the cursor up one line, then clear that line
187 process.stdout.write new Buffer [27, 91, 65, 27, 91, 50, 75]
188 process.stdout.write @log_whitespace() + opts.prompt + "***\n"
189 process.stdin.pause()
191 callback null, (line.substr 0, line.length - 1)
193 get_token = (callback) ->
194 fs.readFile token_file, 'utf8', (err, token) ->
201 login = (real_callback) ->
202 login_callbacks.push real_callback
203 return if login_callbacks.length > 1
205 callback = (err, token) ->
206 while login_callbacks.length > 0
207 login_callbacks.shift()(err, token)
210 (callback) => async.series [
211 (callback) => @ask prompt: 'username: ', callback
212 (callback) => @ask prompt: 'password: ', silent: true, callback
214 ([username, password], callback) ->
215 af.login username, password, callback
217 # wait for file write so there's no race condition if get_token gets called soon
218 fs.writeFile token_file, token, (err) ->
220 process.stderr.write "Warning: couldn't cache auth token in #{token_file}: #{err}\n"
221 # don't pass on error, it's ok if we can't cache it
226 exports.new_session = ->
230 process.stderr.write "usage: #{process.argv[0]} #{process.argv[1]} command [args...]\n"
231 process.stderr.write "valid commands are:\n\t#{(k for k, v of commands).join '\n\t'}\n"
233 # parse and act on commandline arguments unless we were require()d as a module
234 if require.main is module
235 args = process.argv[2..]
238 else if not commands[args[0]]
239 process.stderr.write "unknown command \"#{args[0]}\"\n"
242 session = new Session()
243 session.api args[0], args[1..]..., (err, result) ->
245 process.stderr.write "Error: #{JSON.stringify err}\n"
247 if typeof result is 'string'
248 process.stdout.write result
250 process.stdout.write JSON.stringify result