CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/hub/projects.coffee
Views: 687
1
#########################################################################
2
# This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
# License: MS-RSL – see LICENSE.md for details
4
#########################################################################
5
6
###
7
Projects
8
###
9
10
winston = require('./logger').getLogger('projects')
11
12
postgres = require('@cocalc/database')
13
local_hub_connection = require('./local_hub_connection')
14
message = require('@cocalc/util/message')
15
{callback2} = require('@cocalc/util/async-utils')
16
misc = require('@cocalc/util/misc')
17
misc_node = require('@cocalc/backend/misc_node')
18
{defaults, required} = misc
19
20
# Create a project object that is connected to a local hub (using
21
# appropriate port and secret token), login, and enhance socket
22
# with our message protocol.
23
24
_project_cache = {}
25
exports.new_project = (project_id, database, projectControl) ->
26
P = _project_cache[project_id]
27
if not P?
28
P = new Project(project_id, database, projectControl)
29
_project_cache[project_id] = P
30
return P
31
32
class Project
33
constructor: (@project_id, @database, @projectControl) ->
34
@dbg("instantiating Project class")
35
@local_hub = local_hub_connection.new_local_hub(@project_id, @database, @projectControl)
36
# we always look this up and cache it
37
@get_info()
38
39
dbg: (m) =>
40
winston.debug("project(#{@project_id}): #{m}")
41
42
_fixpath: (obj) =>
43
if obj? and @local_hub?
44
if obj.path?
45
if obj.path[0] != '/'
46
obj.path = @local_hub.path+ '/' + obj.path
47
else
48
obj.path = @local_hub.path
49
50
owner: (cb) =>
51
if not @database?
52
cb('need database in order to determine owner')
53
return
54
@database.get_project
55
project_id : @project_id
56
columns : ['account_id']
57
cb : (err, result) =>
58
if err
59
cb(err)
60
else
61
cb(err, result[0])
62
63
# get latest info about project from database
64
get_info: (cb) =>
65
if not @database?
66
cb('need database in order to determine owner')
67
return
68
@database.get_project
69
project_id : @project_id
70
columns : postgres.PROJECT_COLUMNS
71
cb : (err, result) =>
72
if err
73
cb?(err)
74
else
75
@cached_info = result
76
cb?(undefined, result)
77
78
call: (opts) =>
79
opts = defaults opts,
80
mesg : required
81
multi_response : false
82
timeout : 15
83
cb : undefined
84
#@dbg("call")
85
@_fixpath(opts.mesg)
86
opts.mesg.project_id = @project_id
87
@local_hub.call(opts)
88
89
# async function
90
named_server_port: (name) =>
91
@dbg("named_server_port(name=#{name})")
92
resp = await callback2(@call, {mesg : message.named_server_port(name:name), timeout : 30})
93
@dbg("named_server_port #{resp.port}")
94
return resp.port
95
96
read_file: (opts) =>
97
@dbg("read_file")
98
@_fixpath(opts)
99
opts.project_id = @project_id
100
@local_hub.read_file(opts)
101
102
write_file: (opts) =>
103
@dbg("write_file")
104
@_fixpath(opts)
105
opts.project_id = @project_id
106
@local_hub.write_file(opts)
107
108
109