JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
9e28b9b09f14d0de7c53cbf0103d6af417a13db2
[af-coffee.git] / client.coffee
1 #!/usr/bin/coffee
2
3 fs = require 'fs'
4 async = require 'async'
5 af = require './api.coffee'
6
7 token_file = "#{process.env.HOME}/.af-coffee-token"
8
9 timeout = (ms, f) -> setTimeout f, ms
10
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
14
15 commands = {}
16 for k, v of af
17         commands[k] = v unless k is 'login'
18
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) ->
22         async.waterfall [
23                 (callback) =>      @api 'app_update_files', app_name, zip_file, callback
24                 (res, callback) => @api 'app_restart',      app_name, callback
25         ], callback
26
27
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
30 # seconds to close.
31 #
32 # Requirements:
33 #
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.
36 #
37 # 2.    One of these apps must be running, and must have the domain names mapped
38 #       to it.
39 #
40 # 3.    The other must be stopped, and must not have the domain names mapped  to
41 #       it. (just it's *.af.com address.)
42 #
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?)
47         #
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
51                 new_app = app_name
52         else
53                 old_app = app_name + '_'
54                 new_app = app_name
55
56         async.auto {
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
67                 ]
68                 hide_old: ['start_new', 'old_info', (cb, args) =>
69                         just_af = []
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
74                 ]
75                 wait_for_old_connections: ['hide_old', (cb) =>
76                         seconds = 10
77                         log_id = @log_start "waiting #{seconds} seconds for connections to old instance to finish"
78                         timeout seconds * 1000, =>
79                                 @log_end(log_id)
80                                 cb()
81                 ]
82                 old_info_again: ['hide_old', (cb) =>
83                         @api 'app_info', old_app, cb
84                 ]
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
88                 ]
89                 # TODO when we get into sending manifests, we may need to update_files to old_app too
90         }, callback
91
92 app_set_state = (token, app_name, state, callback) ->
93         async.waterfall [
94                 (callback) =>
95                         @api 'app_info', app_name, callback
96                 (info, callback) =>
97                         info.state = state
98                         @api 'app_set_info', app_name, info, callback
99         ], callback
100
101 commands.app_start = (token, app_name, callback) ->
102         app_set_state.call this, token, app_name, 'STARTED', callback
103
104 commands.app_stop = (token, app_name, callback) ->
105         app_set_state.call this, token, app_name, 'STOPPED', callback
106
107
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
111         async.waterfall [
112                 (callback) =>      @api 'app_stop',  app_name, callback
113                 (res, callback) => @api 'app_start', app_name, callback
114         ], callback
115
116
117
118 class Session
119         constructor: ->
120                 @token = 0
121                 @verbose = true
122                 @log_nest = 0
123                 @log_mid = 0
124                 @log_id = 0
125
126         log_whitespace: ->
127                 out = ''
128                 out += '\n' if @log_mid
129                 for i in [0...@log_nest]
130                         out += '\t'
131                 return out
132
133         log_start: (msg) ->
134                 return unless @verbose
135                 @log_id += 1
136                 process.stdout.write "#{@log_whitespace()}#{@log_id}: #{msg}"
137                 @log_nest += 1
138                 @log_mid = @log_id
139                 return @log_id
140
141         log_end: (start_id) ->
142                 return unless @verbose
143                 @log_nest -= 1
144                 if @log_mid is start_id
145                         process.stdout.write "... done\n"
146                 else
147                         process.stdout.write "#{@log_whitespace()}#{start_id} done\n"
148                 @log_mid = 0
149
150         api: (call, args..., callback) ->
151                 async.waterfall [
152                         (callback) =>
153                                 switch @token
154                                         when 0
155                                                 get_token callback
156                                         when -1
157                                                 login.call this, callback
158                                         else
159                                                 callback null, @token
160                         (token, callback) =>
161                                 @token = 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...
167                 ], (err, result) =>
168                         # eg /app/xxx/stats sometimes returns 404 with wrong auth token
169                         if err?.code is 403 or err?.code is 404
170                                 @token = -1
171                                 @api(call, args..., callback)
172                         else
173                                 callback err, result
174
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) =>
180                         if opts.silent
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()
185                         @log_mid = 0
186                         callback null, (line.substr 0, line.length - 1)
187
188 get_token = (callback) ->
189         fs.readFile token_file, 'utf8', (err, token) ->
190                 if err?
191                         login callback
192                 else
193                         callback null, token
194
195 login = (callback) ->
196         async.waterfall [
197                 (callback) => async.series [
198                         (callback) => @ask prompt: 'username: ', callback
199                         (callback) => @ask prompt: 'password: ', silent: true, callback
200                 ], callback
201                 ([username, password], callback) ->
202                         af.login username, password, callback
203                 (token, 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) ->
206                                 if 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
209                                 callback null, token
210         ], callback
211
212
213 exports.new_session = ->
214         return new Session()
215
216 usage = ->
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"
219
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..]
223         if args.length is 0
224                 usage()
225         else if not commands[args[0]]
226                 process.stderr.write "unknown command \"#{args[0]}\"\n"
227                 usage()
228         else
229                 session = new Session()
230                 session.api args[0], args[1..]..., (err, result) ->
231                         if err?
232                                 process.stderr.write "Error: #{JSON.stringify err}\n"
233                         if result?
234                                 if typeof result is 'string'
235                                         process.stdout.write result
236                                 else
237                                         process.stdout.write JSON.stringify result