Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39538
1
###
2
Projects
3
###
4
5
winston = require('winston')
6
7
postgres = require('./postgres')
8
local_hub_connection = require('./local_hub_connection')
9
message = require('smc-util/message')
10
11
misc = require('smc-util/misc')
12
misc_node = require('smc-util-node/misc_node')
13
{defaults, required} = misc
14
15
# Create a project object that is connected to a local hub (using
16
# appropriate port and secret token), login, and enhance socket
17
# with our message protocol.
18
19
_project_cache = {}
20
exports.new_project = (project_id, database, compute_server) ->
21
P = _project_cache[project_id]
22
if not P?
23
P = new Project(project_id, database, compute_server)
24
_project_cache[project_id] = P
25
return P
26
27
class Project
28
constructor: (@project_id, @database, @compute_server) ->
29
@dbg("instantiating Project class")
30
@local_hub = local_hub_connection.new_local_hub(@project_id, @database, @compute_server)
31
# we always look this up and cache it
32
@get_info()
33
34
dbg: (m) =>
35
winston.debug("project(#{@project_id}): #{m}")
36
37
_fixpath: (obj) =>
38
if obj? and @local_hub?
39
if obj.path?
40
if obj.path[0] != '/'
41
obj.path = @local_hub.path+ '/' + obj.path
42
else
43
obj.path = @local_hub.path
44
45
owner: (cb) =>
46
if not @database?
47
cb('need database in order to determine owner')
48
return
49
@database.get_project
50
project_id : @project_id
51
columns : ['account_id']
52
cb : (err, result) =>
53
if err
54
cb(err)
55
else
56
cb(err, result[0])
57
58
# get latest info about project from database
59
get_info: (cb) =>
60
if not @database?
61
cb('need database in order to determine owner')
62
return
63
@database.get_project
64
project_id : @project_id
65
columns : postgres.PROJECT_COLUMNS
66
cb : (err, result) =>
67
if err
68
cb?(err)
69
else
70
@cached_info = result
71
cb?(undefined, result)
72
73
call: (opts) =>
74
opts = defaults opts,
75
mesg : required
76
multi_response : false
77
timeout : 15
78
cb : undefined
79
#@dbg("call")
80
@_fixpath(opts.mesg)
81
opts.mesg.project_id = @project_id
82
@local_hub.call(opts)
83
84
jupyter_port: (opts) =>
85
opts = defaults opts,
86
cb : required
87
@dbg("jupyter_port")
88
@call
89
mesg : message.jupyter_port(mathjax_url : misc_node.MATHJAX_URL)
90
timeout : 30
91
cb : (err, resp) =>
92
if err
93
opts.cb(err)
94
else
95
@dbg("jupyter_port -- #{resp.port}")
96
opts.cb(undefined, resp.port)
97
98
move_project: (opts) =>
99
opts = defaults opts,
100
target : undefined # optional prefered target
101
cb : undefined
102
@dbg("move_project")
103
@local_hub.move(opts)
104
105
read_file: (opts) =>
106
@dbg("read_file")
107
@_fixpath(opts)
108
opts.project_id = @project_id
109
@local_hub.read_file(opts)
110
111
write_file: (opts) =>
112
@dbg("write_file")
113
@_fixpath(opts)
114
opts.project_id = @project_id
115
@local_hub.write_file(opts)
116
117
console_session: (opts) =>
118
@dbg("console_session")
119
@_fixpath(opts.params)
120
opts.project_id = @project_id
121
@local_hub.console_session(opts)
122
123
terminate_session: (opts) =>
124
opts = defaults opts,
125
session_uuid : required
126
cb : undefined
127
@dbg("terminate_session")
128
opts.project_id = @project_id
129
@local_hub.terminate_session(opts)
130
131