JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
add .editorconfig
[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 # 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) ->
46         async.auto {
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
53                         else
54                                 cb null, new: args.info2, old: args.info1
55                 ]
56                 push: ['infos', (cb, args) =>
57                         @api 'app_update_files', args.infos.new.name, zip_file, cb
58                 ]
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
65                 ]
66                 new_info_again: ['copy_uris', 'infos', (cb, args) =>
67                         @api 'app_info', args.infos.new.name, cb
68                 ]
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
72                 ]
73                 hide_old: ['start_new', 'infos', (cb, args) =>
74                         just_af = []
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
79                 ]
80                 wait_for_old_connections: ['hide_old', (cb) =>
81                         seconds = 10
82                         log_id = @log_start "waiting #{seconds} seconds for connections to old instance to finish"
83                         timeout seconds * 1000, =>
84                                 @log_end(log_id)
85                                 cb()
86                 ]
87                 old_info_again: ['hide_old', 'infos', (cb, args) =>
88                         @api 'app_info', args.infos.old.name, cb
89                 ]
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
93                 ]
94                 # TODO when we get into sending manifests, we may need to update_files to infos.old.name too
95         }, callback
96
97 app_set_state = (token, app_name, state, callback) ->
98         async.waterfall [
99                 (callback) =>
100                         @api 'app_info', app_name, callback
101                 (info, callback) =>
102                         info.state = state
103                         @api 'app_set_info', app_name, info, callback
104         ], callback
105
106 commands.app_start = (token, app_name, callback) ->
107         app_set_state.call this, token, app_name, 'STARTED', callback
108
109 commands.app_stop = (token, app_name, callback) ->
110         app_set_state.call this, token, app_name, 'STOPPED', callback
111
112
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
116         async.waterfall [
117                 (callback) =>      @api 'app_stop',  app_name, callback
118                 (res, callback) => @api 'app_start', app_name, callback
119         ], callback
120
121
122
123 class Session
124         constructor: ->
125                 @token = 0
126                 @verbose = true
127                 @log_nest = 0
128                 @log_mid = 0
129                 @log_id = 0
130
131         log_whitespace: ->
132                 out = ''
133                 out += '\n' if @log_mid
134                 for i in [0...@log_nest]
135                         out += '\t'
136                 return out
137
138         log_start: (msg) ->
139                 return unless @verbose
140                 @log_id += 1
141                 process.stdout.write "#{@log_whitespace()}#{@log_id}: #{msg}"
142                 @log_nest += 1
143                 @log_mid = @log_id
144                 return @log_id
145
146         log_end: (start_id) ->
147                 return unless @verbose
148                 @log_nest -= 1
149                 if @log_mid is start_id
150                         process.stdout.write "... done\n"
151                 else
152                         process.stdout.write "#{@log_whitespace()}#{start_id} done\n"
153                 @log_mid = 0
154
155         api: (call, args..., callback) ->
156                 async.waterfall [
157                         (callback) =>
158                                 switch @token
159                                         when 0
160                                                 get_token callback
161                                         when -1
162                                                 login.call this, callback
163                                         else
164                                                 callback null, @token
165                         (token, callback) =>
166                                 @token = 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...
172                 ], (err, result) =>
173                         # eg /app/xxx/stats sometimes returns 404 with wrong auth token
174                         if err?.code is 403 or err?.code is 404
175                                 @token = -1
176                                 @api(call, args..., callback)
177                         else
178                                 callback err, result
179
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) =>
185                         if opts.silent
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()
190                         @log_mid = 0
191                         callback null, (line.substr 0, line.length - 1)
192
193 get_token = (callback) ->
194         fs.readFile token_file, 'utf8', (err, token) ->
195                 if err?
196                         login callback
197                 else
198                         callback null, token
199
200 login_callbacks = []
201 login = (real_callback) ->
202         login_callbacks.push real_callback
203         return if login_callbacks.length > 1
204
205         callback = (err, token) ->
206                 while login_callbacks.length > 0
207                         login_callbacks.shift()(err, token)
208
209         async.waterfall [
210                 (callback) => async.series [
211                         (callback) => @ask prompt: 'username: ', callback
212                         (callback) => @ask prompt: 'password: ', silent: true, callback
213                 ], callback
214                 ([username, password], callback) ->
215                         af.login username, password, callback
216                 (token, 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) ->
219                                 if 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
222                                 callback null, token
223         ], callback
224
225
226 exports.new_session = ->
227         return new Session()
228
229 usage = ->
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"
232
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..]
236         if args.length is 0
237                 usage()
238         else if not commands[args[0]]
239                 process.stderr.write "unknown command \"#{args[0]}\"\n"
240                 usage()
241         else
242                 session = new Session()
243                 session.api args[0], args[1..]..., (err, result) ->
244                         if err?
245                                 process.stderr.write "Error: #{JSON.stringify err}\n"
246                         if result?
247                                 if typeof result is 'string'
248                                         process.stdout.write result
249                                 else
250                                         process.stdout.write JSON.stringify result