-
Notifications
You must be signed in to change notification settings - Fork 212
/
message.coffee
2015 lines (1750 loc) · 66.5 KB
/
message.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###############################################################################
#
# CoCalc: Collaborative Calculation in the Cloud
#
# Copyright (C) 2016, Sagemath Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
###
#
# Library for working with JSON messages for Salvus.
#
# We use functions to work with messages to ensure some level of
# consistency, defaults, and avoid errors from typos, etc.
#
###
doc_intro = """
## About the API
### Purpose
The purpose of the CoCalc API (application programming interface) is to make
essential operations within the CoCalc platform available to automated
clients. This allows embedding of CoCalc services within other products
and customizing the external look and feel of the application.
### Protocol and Data Format
Each API command is invoked using an HTTPS PUT request.
All commands support request parameters in JSON format, with request header
`Content-Type: application/json`. Many commands (those that do not
require lists or objects as parameters)
also accept request parameters as key-value pairs, i.e.
`Content-Type: application/x-www-form-urlencoded`.
Responses are formatted as JSON strings.
Note that it is possible for a request to fail and return
a response code of 200. In that case, the response
string may contain helpful information on the nature of
the failure. In other cases, if the request cannnot
be completed, a response code other than 200 may be
returned, and the response body may be a
generic HTML message rather than a JSON string.
### Authentication
A valid API key is required on all API requests.
To obtain a key, log into
CoCalc and click on Settings (gear icon next to user name at upper
right), and look under `Account Settings`.
With the `API key` dialogue, you can create a key,
view a previously assigned key, generate a replacement key,
and delete your key entirely.
Your API key carries access privileges, just like your
login and password.
__Keep it secret.__
Do not share your API key with others or post it in publicly accessible
forums.
### Additional References
- The CoCalc PostgreSQL schema definition
[src/smc-util/db-schema.coffee]
(https://github.com/sagemathinc/cocalc/blob/master/src/smc-util/db-schema.coffee)
has information on tables and fields used with the API `query` request.
- The API test suite
[src/smc-hub/test/api/]
(https://github.com/sagemathinc/cocalc/tree/master/src/smc-hub/test/api)
contains mocha unit tests for the API messages.
- The CoCalc message definition file
[src/smc-util/message.coffee]
(https://github.com/sagemathinc/cocalc/blob/master/src/smc-util/message.coffee)
contains the source for this guide.
### API Message Reference
The remainder of this guide explains the individual API endpoints.
Each API request definition begins with the path of the
URL used to invoke the request,
for example `/api/v1/change_email_address`.
The path name ends with the name of the request,
for example, `change_email_address`.
Following the path is the list of options.
After options are one or more sample invocations
illustrating format of the request as made with the `curl`
command, and the format of the response.
The following two options appear on all API messages
(request parameters are often referred to
as 'options' in the guide):
- **event**: the command to be executed, for example "ping"
- **id**: uuid for the API call, returned in response in most cases.
If id is not provided in the API message, a random id will be
generated and returned in the response.
"""
misc = require('./misc')
defaults = misc.defaults
required = defaults.required
_ = require('underscore')
message = (obj) ->
exports[obj.event] = (opts={}, strict=false) ->
if opts.event?
throw Error("ValueError: must not define 'event' when calling message creation function (opts=#{JSON.stringify(opts)}, obj=#{JSON.stringify(obj)})")
defaults(opts, obj, false, strict)
return obj
# message2 for "version 2" of the message definitions
# TODO document it, for now just search for "message2" to see examples
message2 = (obj) ->
mk_desc = (val) ->
desc = val.desc
if val.init == required
desc += ' (required)'
else if val.init?
desc += " (default: #{misc.to_json(val.init)})"
return desc
# reassembling a version 1 message from a version 2 message
mesg_v1 = _.mapObject(obj.fields, ((val) -> val.init))
mesg_v1.event = obj.event
# extracting description for the documentation
fdesc = _.mapObject(obj.fields, mk_desc)
exports.documentation.events[obj.event] =
description : obj.desc ? ''
fields : fdesc
# ... and the examples
exports.examples[obj.event] = obj.examples
# wrapped version 1 message
message(mesg_v1)
return obj
# messages that can be used by the HTTP api. {'event':true, ...}
exports.api_messages = {}
# this holds the documentation for the message protocol
exports.documentation =
intro : doc_intro
events : {}
# holds all the examples: list of expected in/out objects for each message
exports.examples = {}
API = (obj) ->
# obj could be message version 1 or 2!
exports.api_messages[obj.event] = true
############################################
# Compute server messages
#############################################
message
event : 'compute_server_status'
status : undefined
# Message for actions using a compute server
message
event : 'compute'
project_id : undefined
action : required # open, save, ...
args : undefined
param : undefined # deprecate
id : undefined
message
event : 'project_state_update'
project_id : required
state : required
time : required
state_error : undefined # error if there was one transitioning to this state
############################################
# Sage session management; executing code
#############################################
# hub --> sage_server&console_server, etc. and browser --> hub
message
event : 'start_session'
type : required # "sage", "console"; later this could be "R", "octave", etc.
# TODO: project_id should be required
project_id : undefined # the project that this session will start in
session_uuid : undefined # set by the hub -- client setting this will be ignored.
params : undefined # extra parameters that control the type of session
id : undefined
limits : undefined
# hub --> browser
message
event : 'session_started'
id : undefined
session_uuid : undefined
limits : undefined
data_channel : undefined # The data_channel is a single UTF-16
# character; this is used for
# efficiently sending and receiving
# non-JSON data (except channel
# '\u0000', which is JSON).
# hub --> client
message
event : 'session_reconnect'
session_uuid : undefined # at least one of session_uuid or data_channel must be defined
data_channel : undefined
#
# client --> hub
message
event : 'connect_to_session'
id : undefined
type : required
project_id : required
session_uuid : required
params : undefined # extra parameters that control the type of session -- if we have to create a new one
message
event : 'session_connected'
id : undefined
session_uuid : required
data_channel : undefined # used for certain types of sessions
history : undefined # used for console/terminal sessions
# sage_server&console_server --> hub
message
event : 'session_description'
pid : required
limits : undefined
# client <----> hub <--> sage_server
message
event : 'terminate_session'
project_id : undefined
session_uuid : undefined
reason : undefined
done : undefined
message
event : 'execute_code'
id : undefined
code : required
data : undefined
session_uuid : undefined
cell_id : undefined # optional extra useful information about which cells is being executed
preparse : true
allow_cache : true
# Output resulting from evaluating code that is displayed by the browser.
# sage_server --> local hub --> hubs --> clients
message
event : 'output'
id : undefined # the id for this particular computation
stdout : undefined # plain text stream
stderr : undefined # error text stream -- colored to indicate an error
html : undefined # arbitrary html stream
md : undefined # github flavored markdown
tex : undefined # tex/latex stream -- is an object {tex:..., display:...}
d3 : undefined # d3 data document, e.g,. {d3:{viewer:'graph', data:{...}}}
hide : undefined # 'input' or 'output'; hide display of given component of cell
show : undefined # 'input' or 'output'; show display of given component of cell
auto : undefined # true or false; sets whether or not cell auto-executess on process restart
javascript : undefined # javascript code evaluation stream (see also 'execute_javascript' to run code directly in browser that is not part of the output stream).
interact : undefined # create an interact layout defined by a JSON object
obj : undefined # used for passing any JSON-able object along as output; this is used, e.g., by interact.
file : undefined # used for passing a file -- is an object {filename:..., uuid:..., show:true}; the file is at https://cloud.sagemath.com/blobs/filename?uuid=[the uuid]
raw_input : undefined # used for getting blocking input from client -- {raw_input:{prompt:'input stuff?', value:'', submitted:false}}
done : false # the sequences of messages for a given code evaluation is done.
session_uuid : undefined # the uuid of the session that produced this output
once : undefined # if given, message is transient; it is not saved by the worksheet, etc.
clear : undefined # if true, clears all output of the current cell before rendering message.
events : undefined # {'event_name':'name of Python callable to call', ...} -- only for images right now
# This message tells the client to execute the given Javascript code
# in the browser. (For safety, the client may choose to ignore this
# message.) If coffeescript==true, then the code is assumed to be
# coffeescript and is first compiled to Javascript. This message is
# "out of band", i.e., not meant to be part of any particular output
# cell. That is why there is no id key.
# sage_server --> hub --> client
message
event : 'execute_javascript'
session_uuid : undefined # set by the hub, since sage_server doesn't (need to) know the session_uuid.
code : required
obj : undefined
coffeescript : false
cell_id : undefined # if set, eval scope contains an object cell that refers to the cell in the worksheet with this id.
############################################
# Information about several projects or accounts
#############################################
API message2
event : 'get_usernames'
fields:
id:
init : undefined
desc : 'A unique UUID for the query'
account_ids:
init : required
desc : 'list of account_ids'
desc : """
Get first and last names for a list of account ids.
Note: Options for the `get_usernames` API message must be sent as JSON object.
Example:
```
curl -u sk_abcdefQWERTY090900000000: -H "Content-Type: application/json" \\
-d '{"account_ids":["cc3cb7f1-14f6-4a18-a803-5034af8c0004","9b896055-920a-413c-9172-dfb4007a8e7f"]}' \\
https://cocalc.com/api/v1/get_usernames
==> {"event":"usernames",
"id":"32b485a8-f214-4fda-a622-4dbfe0db2b9c",
"usernames": {
"cc3cb7f1-14f6-4a18-a803-5034af8c0004":{"first_name":"John","last_name":"Smith"},
"9b896055-920a-413c-9172-dfb4007a8e7f":{"first_name":"Jane","last_name":"Doe"}}}
```
"""
message
event : 'usernames'
id : undefined
usernames : required
############################################
# Account Management
#############################################
# client --> hub
API message2
event : 'create_account'
fields:
id:
init : undefined
desc : 'A unique UUID for the query'
first_name:
init : required
last_name:
init : required
email_address:
init : required
password:
init : required
desc : 'must be between 6 and 64 characters in length'
agreed_to_terms:
init : required
desc : 'must be true for request to succeed'
token:
init : undefined # only required when token is set.
desc : 'account creation token - see src/dev/docker/README.md'
desc : """
Examples:
Create a new account:
```
curl -u sk_abcdefQWERTY090900000000: \\
-d first_name=John00 \\
-d last_name=Doe00 \\
-d email_address=jd@some_email \\
-d password=xyzabc09090 \\
-d agreed_to_terms=true https://cocalc.com/api/v1/create_account
```
Option `agreed_to_terms` must be present and specified as true.
Account creation fails if there is already an account using the
given email address, if `email_address` is improperly formatted,
and if password is fewer than 6 or more than 64 characters.
Attempting to create the same account a second time results in an error:
```
curl -u sk_abcdefQWERTY090900000000: \\
-d first_name=John00 \\
-d last_name=Doe00 \\
-d email_address=jd@some_email \\
-d password=xyzabc09090 \\
-d agreed_to_terms=true https://cocalc.com/api/v1/create_account
==> {"event":"account_creation_failed",
"id":"2332be03-aa7d-49a6-933a-cd9824b7331a",
"reason":{"email_address":"This e-mail address is already taken."}}
```
"""
message
event : 'account_created'
id : undefined
account_id : required
# hub --> client
message
event : 'account_creation_failed'
id : undefined
reason : required
# client --> hub
API message2
event : 'delete_account'
fields:
id:
init : undefined
desc : 'A unique UUID for the query'
account_id:
init : required
desc : 'account_id for account to be deleted'
desc : """
Example:
Delete an existing account:
```
curl -u sk_abcdefQWERTY090900000000: \\
-d account_id=99ebde5c-58f8-4e29-b6e4-b55b8fd71a1b \\
https://cocalc.com/api/v1/delete_account
==> {"event":"account_deleted","id":"9e8b68ac-08e8-432a-a853-398042fae8c9"}
```
Event `account_deleted` is also returned if the account was already
deleted before the API call, or if the account never existed.
After successful `delete_account`, the owner of the deleted account
will not be able to login, but will still be listed as collaborator
or owner on projects which the user collaborated on or owned
respectively.
"""
# hub --> client
message
event : 'account_deleted'
id : undefined
error : undefined
# client --> hub
message
id : undefined
event : 'sign_in'
email_address : required
password : required
remember_me : false
message
id : undefined
event : 'sign_in_using_auth_token'
auth_token : required
# hub --> client
message
id : undefined
event : 'remember_me_failed'
reason : required
# client --> hub
message
id : undefined
event : 'sign_in_failed'
email_address : required
reason : required
# hub --> client; sent in response to either create_account or log_in
message
event : 'signed_in'
id : undefined # message uuid
remember_me : required # true if sign in accomplished via remember_me cookie; otherwise, false.
hub : required # ip address (on vpn) of hub user connected to.
account_id : required # uuid of user's account
email_address : undefined # email address they signed in under
first_name : undefined
last_name : undefined
# client --> hub
message
event : 'sign_out'
everywhere : false
id : undefined
# hub --> client
message
event : 'signed_out'
id : undefined
# client --> hub
API message2
event : 'change_password'
fields:
id:
init : undefined
desc : 'A unique UUID for the query'
email_address:
init : required
desc : 'email address for account whose password is changed'
old_password:
init : ""
desc : ''
new_password:
init : required
desc : 'must be between 6 and 64 characters in length'
desc : """
Given email address and old password for an account, set a new password.
Example:
```
curl -u sk_abcdefQWERTY090900000000: \\
-d email_address=... \\
-d old_password=... \\
-d new_password=... \\
https://cocalc.com/api/v1/change_password
==> {"event":"changed_password","id":"41ff89c3-348e-4361-ad1d-372b55e1544a"}
```
"""
# hub --> client
# if error is true, that means the password was not changed; would
# happen if password is wrong (message:'invalid password').
message
event : 'changed_password'
id : undefined
error : undefined
# client --> hub: "please send a password reset email"
API message2
event : "forgot_password"
fields:
id:
init : undefined
desc : 'A unique UUID for the query'
email_address:
init : required
desc : 'email address for account requesting password reset'
desc : """
Given the email address of an existing account, send password reset email.
Example:
```
curl -u sk_abcdefQWERTY090900000000: \\
-d email_address=... \\
https://cocalc.com/api/v1/forgot_password
==> {"event":"forgot_password_response",
"id":"26ed294b-922b-47e1-8f3f-1e54d8c8e558",
"error":false}
```
"""
# hub --> client "a password reset email was sent, or there was an error"
message
event : "forgot_password_response"
id : undefined
error : false
# client --> hub: "reset a password using this id code that was sent in a password reset email"
API message2
event : "reset_forgot_password"
fields:
id:
init : undefined
desc : 'A unique UUID for the query'
reset_code:
init : required
desc : 'id code that was sent in a password reset email'
new_password:
init : required
desc : 'must be between 6 and 64 characters in length'
desc : """
Reset password, given reset code.
Example:
```
curl -u sk_abcdefQWERTY090900000000: \\
-d reset_code=35a0eea6-370a-45c3-ab2f-3210df68748f \\
-d new_password=qjqhddfsfj \\
https://cocalc.com/api/v1/reset_forgot_password
==> {"event":"reset_forgot_password_response","id":"85bd6027-644d-4859-9e17-5e835bd47570","error":false}
```
"""
message
event : "reset_forgot_password_response"
id : undefined
error : false
# client --> hub
API message2
event : 'change_email_address'
fields:
id:
init : undefined
desc : 'A unique UUID for the query'
account_id:
init : required
desc : 'account_id for account whose email address is changed'
old_email_address:
init : ""
desc : 'ignored -- deprecated'
new_email_address:
init : required
desc : ''
password:
init :""
desc : ''
desc:"""
Given the `account_id` for an account, set a new email address.
Examples:
Successful change of email address.
```
curl -u sk_abcdefQWERTY090900000000: \\
-d account_id=99ebde5c-58f8-4e29-b6e4-b55b8fd71a1b \\
-d password=secret_password \\
-d new_email_address=new@email.com \\
https://cocalc.com/api/v1/change_email_address
==> {"event":"changed_email_address",
"id":"8f68f6c4-9851-4b88-bd65-37cb983298e3",
"error":false}
```
Fails if new email address is already in use.
```
curl -u sk_abcdefQWERTY090900000000: \\
-d account_id=99ebde5c-58f8-4e29-b6e4-b55b8fd71a1b \\
-d password=secret_password \\
-d new_email_address=used@email.com \\
https://cocalc.com/api/v1/change_email_address
==> {"event":"changed_email_address",
"id":"4501f022-a57c-4aaf-9cd8-af0eb05ebfce",
"error":"email_already_taken"}
```
**Note:** `account_id` and `password` must match the `id` of the current login.
"""
# hub --> client
message
event : 'changed_email_address'
id : undefined
error : false # some other error
ttl : undefined # if user is trying to change password too often, this is time to wait
# Unlink a passport auth for this account.
# client --> hub
API message2
event : 'unlink_passport'
fields:
strategy:
init : required
desc : 'passport strategy'
id:
init : required
desc : 'numeric id for user and passport strategy'
desc:"""
Unlink a passport auth for the account.
Strategies are defined in the database and may be viewed at [/auth/strategies](https://cocalc.com/auth/strategies).
Example:
Get passport id for some strategy for current user.
```
curl -u sk_abcdefQWERTY090900000000: \\
-H "Content-Type: application/json" \\
-d '{"query":{"accounts":{"account_id":"e6993694-820d-4f78-bcc9-10a8e336a88d","passports":null}}}' \\
https://cocalc.com/api/v1/query
==> {"query":{"accounts":{"account_id":"e6993694-820d-4f78-bcc9-10a8e336a88d",
"passports":{"facebook-14159265358":{"id":"14159265358",...}}}},
"multi_response":false,
"event":"query",
"id":"a2554ec8-665b-495b-b0e2-8e248b54eb94"}
```
Unlink passport for that strategy and id.
```
curl -u sk_abcdefQWERTY090900000000: \\
-d strategy=facebook \\
-d id=14159265358 \\
https://cocalc.com/api/v1/unlink_passport
==> {"event":"success",
"id":"14159265358"}
```
Note that success is returned regardless of whether or not passport was linked
for the given strategy and id before issuing the API command.
"""
message
event : 'error'
id : undefined
error : undefined
message
event : 'success'
id : undefined
# You need to reconnect.
message
event : 'reconnect'
id : undefined
reason : undefined # optional to make logs more informative
######################################################################################
# This is a message that goes
# hub --> client
# In response, the client grabs "/cookies?id=...,set=...,get=..." via an AJAX call.
# During that call the server can get/set HTTP-only cookies.
# (Note that the /cookies url gets customized by base_url.)
######################################################################################
message
event : 'cookies'
id : required
url : "/cookies"
get : undefined # name of a cookie to get
set : undefined # name of a cookie to set
value : undefined # value to set cookie to
###
Project Server <---> Hub interaction
These messages are mainly focused on working with individual projects.
Architecture:
* The database stores a files object (with the file tree), logs
(of each branch) and a sequence of git bundles that when
combined together give the complete history of the repository.
Total disk usage per project is limited by hard/soft disk quota,
and includes the space taken by the revision history (the .git
directory).
* A project should only be opened by at most one project_server at
any given time (not implemented: if this is violated then we'll
merge the resulting conflicting repo's.)
* Which project_server that has a project opened is stored in the
database. If a hub cannot connect to a given project server,
the hub assigns a new project_server for the project and opens
the project on the new project_server. (The error also gets
logged to the database.) All hubs will use this new project
server henceforth.
###
# The open_project message causes the project_server to create a new
# project or prepare to receive one (as a sequence of blob messages)
# from a hub.
#
# hub --> project_server
message
event : 'open_project'
id : required
project_id : required # uuid of the project, which impacts
# where project is extracted, etc.
quota : required # Maximum amount of disk space/inodes this
# project can use. This is an object
# {disk:{soft:megabytes, hard:megabytes}, inode:{soft:num, hard:num}}
idle_timeout : required # A time in seconds; if the project_server
# does not receive any messages related
# to this project for this many seconds,
# then it does the same thing as when
# receiving a 'close_project' message.
ssh_public_key: required # ssh key of the one UNIX user that is allowed to access this account (this is running the hub).
# A project_server sends the project_opened message to the hub once
# the project_server has received and unbundled all bundles that
# define a project.
# project_server --> hub
message
event : 'project_opened'
id : required
######################################################################
# Execute a shell command in a given project
######################################################################
# client --> project
API message2
event : 'project_exec'
fields:
id:
init : undefined
desc : 'A unique UUID for the query'
project_id:
init : required
desc : 'id of project where command is to be executed'
path:
init : ''
desc : 'path of working directory for the command'
command:
init : required
desc : 'command to be executed'
args:
init : []
desc : 'command line options for the command'
timeout:
init : 10
desc : 'maximum allowed time, in seconds'
max_output:
init : undefined
desc : 'maximum number of characters in the output'
bash:
init : false
desc : 'if true, args are ignored and command is run as a bash command'
err_on_exit:
init : true
desc : 'if exit code is nonzero send error return message instead of the usual output'
desc: """
Execute a shell command in a given project.
Examples:
Simple built-in shell command.
```
curl -u sk_abcdefQWERTY090900000000: \\
-d command=pwd \\
-d project_id=e49e86aa-192f-410b-8269-4b89fd934fba \\
https://cocalc.com/api/v1/project_exec
==> {"event":"project_exec_output",
"id":"8a78a37d-b2fb-4e29-94ae-d66acdeac949",
"stdout":"/projects/e49e86aa-192f-410b-8269-4b89fd934fba\\n","stderr":"","exit_code":0}
```
Shell command with different working directory.
```
curl -u sk_abcdefQWERTY090900000000: \\
-d command=pwd \\
-d path=Private \\
-d project_id=e49e86aa-192f-410b-8269-4b89fd934fba \\
https://cocalc.com/api/v1/project_exec
==> {"event":"project_exec_output",
"id":"8a78a37d-b2fb-4e29-94ae-d66acdeac949",
"stdout":"/projects/e49e86aa-192f-410b-8269-4b89fd934fba/Private\\n","stderr":"","exit_code":0}
```
Command line arguments specified by 'args' option. Note JSON format for request parameters.
```
curl -u sk_abcdefQWERTY090900000000: \\
-H 'Content-Type: application/json' \\
-d '{"command":"echo","args":["xyz","abc"],"project_id":"e49e86aa-192f-410b-8269-4b89fd934fba"}' \\
https://cocalc.com/api/v1/project_exec
==> {"event":"project_exec_output",
"id":"39289ba7-0333-48ad-984e-b25c8b8ffa0e",
"stdout":"xyz abc\\n",
"stderr":"",
"exit_code":0}
```
Limiting output of the command to 3 characters.
```
curl -u sk_abcdefQWERTY090900000000: \\
-H 'Content-Type: application/json' \\
-d '{"command":"echo","args":["xyz","abc"],"max_output":3,"project_id":"e49e86aa-192f-410b-8269-4b89fd934fba"}' \\
https://cocalc.com/api/v1/project_exec
==> {"event":"project_exec_output",
"id":"02feab6c-a743-411a-afca-8a23b58988a9",
"stdout":"xyz (truncated at 3 characters)",
"stderr":"",
"exit_code":0}
```
Setting a timeout for the command.
```
curl -u sk_abcdefQWERTY090900000000: \\
-H 'Content-Type: application/json' \\
-d '{"command":"sleep 5","timeout":2,"project_id":"e49e86aa-192f-410b-8269-4b89fd934fba"}' \\
https://cocalc.com/api/v1/project_exec
==> {"event":"error",
"id":"86fea3f0-6a90-495b-a541-9c14a25fbe58",
"error":"Error executing command 'sleep 5' with args '' -- killed command 'bash /tmp/f-11757-1677-8ei2z0.t4fex0qkt9', , "}
```
Notes:
- Argument `command` may invoke an executable file or a built-in shell command. It may include
a path and command line arguments.
- If option `args` is provided, options must be sent as a JSON object.
- Argument `path` is optional. When provided, `path` is relative to home directory in target project
and specifies the working directory in which the command will be run.
"""
# project --> client
message
event : 'project_exec_output'
id : required
stdout : required
stderr : required
exit_code : required
######################################################################
# Jupyter server
######################################################################
# starts jupyter hub server and reports the port it is running on
# hub <--> project
API message
event : 'jupyter_port'
port : undefined # gets set in response
id : undefined
mathjax_url : undefined # e.g. '/static/mathjax-2.6.1/MathJax.js'
#############################################################################
# The read_file_from_project message is sent by the hub to request
# that the project_server read a file from a project and send it back
# to the hub as a blob. Also sent by client to hub to request a file
# or directory. If path is a directory, the optional archive field
# specifies how to create a single file archive, with supported
# options including: 'tar', 'tar.bz2', 'tar.gz', 'zip', '7z'.
#
# client --> hub --> project_server
message
event : 'read_file_from_project'
id : undefined
project_id : required
path : required
archive : 'tar.bz2'
# The file_read_from_project message is sent by the project_server
# when it finishes reading the file from disk.
# project_server --> hub
message
event : 'file_read_from_project'
id : required
data_uuid : required # The project_server will send the raw data of the file as a blob with this uuid.
archive : undefined # if defined, means that file (or directory) was archived (tarred up) and this string was added to end of filename.
# hub --> client
message
event : 'temporary_link_to_file_read_from_project'
id : required
url : required
# The client sends this message to the hub in order to write (or
# create) a plain text file (binary files not allowed, since sending
# them via JSON makes no sense).
# client --> hub
API message2
event : 'read_text_file_from_project'
fields:
id:
init : undefined
desc : 'A unique UUID for the query'
project_id:
init : required
desc : 'id of project containing file to be read'
path:
init : required
desc : 'path to file to be read in target project'
desc: """
Read a text file in the project whose id is supplied.
User must be owner or collaborator in the target project.
Argument 'path' is relative to home directory in target project.
Unix user in the target project must have permissions to read file
and containing directories if they do not already exist.
Example:
Read a text file.
```
curl -u sk_abcdefQWERTY090900000000: \\
-d project_id=e49e86aa-192f-410b-8269-4b89fd934fba \\
-d path=Assignments/A1/h1.txt \\
https://cocalc.com/api/v1/read_text_file_from_project
==> {"event":"text_file_read_from_project",
"id":"481d6055-5609-450f-a229-480e518b2f84",
"content":"hello"}
```
"""
# hub --> client
message
event : 'text_file_read_from_project'
id : required
content : required
# The write_file_to_project message is sent from the hub to the
# project_server to tell the project_server to write a file to a