Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39384
1
###
2
Some convenient command-line shortcuts. If you're working on the command line, do
3
4
require('./c.coffee')
5
6
The functions below in some cases return things, and in some cases set global variables! Read docs.
7
8
COPYRIGHT : (c) 2017 SageMath, Inc.
9
LICENSE : AGPLv3
10
###
11
12
async = require('async')
13
14
# disable incredibly verbose DB debugging, which makes interactive use hard
15
require('smc-hub/postgres-base').DEBUG = false
16
17
global.misc = require('smc-util/misc')
18
global.done = misc.done
19
global.done1 = misc.done1
20
global.done2 = misc.done2
21
22
db = undefined
23
get_db = (cb) ->
24
if db?
25
cb?(undefined, db) # HACK -- might not really be initialized yet!
26
return db
27
else
28
db = require('./smc-hub/postgres').db()
29
db.connect(cb:cb)
30
return db
31
32
# get a connection to the db
33
global.db = (cb) ->
34
global.db = get_db(cb)
35
return
36
console.log("db() -- sets global variable db to a database")
37
38
global.gcloud = ->
39
global.g = require('./smc-hub/smc_gcloud.coffee').gcloud(db:get_db())
40
console.log("setting global variable g to a gcloud interface")
41
42
console.log("gcloud() -- sets global variable g to gcloud instance")
43
44
global.vms = () ->
45
get_db (err) ->
46
global.g = require('./smc-hub/smc_gcloud.coffee').gcloud(db:db)
47
global.vms = global.g.vm_manager(manage:false)
48
console.log("setting global variable g to a gcloud interface and vms to vm manager")
49
50
console.log("vms() -- sets vms to gcloud VM manager (and g to gcloud interface)")
51
52
# make the global variable s be the compute server
53
global.compute_server = () ->
54
return require('smc-hub/compute-client').compute_server
55
cb : (e,s)->
56
global.s = s
57
console.log("compute_server() -- sets global variable s to compute server")
58
59
# make the global variable p be the project with given id and the global variable s be the compute server
60
global.proj = global.project = (id) ->
61
require('smc-hub/compute-client').compute_server
62
cb : (e,s)->
63
global.s=s
64
s.project
65
project_id:id
66
cb:(e,p)->global.p=p
67
68
console.log("project 'project_id' -- set p = project, s = compute server")
69
70
global.activity = (opts={}) ->
71
opts.cb = (err, a) ->
72
if err
73
console.log("failed to initialize activity")
74
else
75
console.log('initialized activity')
76
global.activity = a
77
require('smc-hub/storage').activity(opts)
78
79
console.log("activity() -- makes activity the activity monitor object")
80
81
global.delete_account = (email) ->
82
get_db (err) ->
83
if err
84
done("FAIL -- #{err}")
85
return
86
db.mark_account_deleted
87
email_address : email
88
cb : (err) ->
89
if err
90
done("FAIL -- #{err}")
91
else
92
done("SUCCESS!")
93
94
console.log("delete_account '[email protected]' -- marks an account deleted")
95
96
DEFAULT_CLOSE_DAYS = 35
97
98
global.close_unused_projects = (host, cb) ->
99
cb ?= done()
100
require('smc-hub/compute-client').compute_server
101
cb : (err, s)->
102
if err
103
cb("FAIL -- #{err}")
104
return
105
s.close_open_unused_projects
106
dry_run : false
107
min_age_days : DEFAULT_CLOSE_DAYS
108
max_age_days : 1000
109
threads : 1
110
host : host
111
cb : (err) -> cb?(err)
112
113
console.log("close_unused_projects('hostname') -- closes all projects on that host not used in the last #{DEFAULT_CLOSE_DAYS} days")
114
115
global.close_unused_free_projects = () ->
116
free = [0..3].map((n) -> "compute#{n}-us")
117
async.mapSeries(free, global.close_unused_projects, done())
118
119
console.log("close_unused_free_projects() -- closes all projects on all free hosts not used in the last #{DEFAULT_CLOSE_DAYS} days")
120
121
global.active_students = (cb) ->
122
cb ?= done()
123
get_db (err) ->
124
if err
125
cb("FAIL -- #{err}")
126
return
127
db.get_active_student_stats
128
cb : (err, stats) ->
129
if err
130
console.log("FAILED")
131
cb(err)
132
else
133
console.log(stats)
134
cb()
135
return
136
137
138
console.log("active_students() -- stats about student course projects during the last 30 days")
139
140
global.save = (obj, filename) ->
141
if filename.slice(filename.length - 5) != '.json'
142
filename += '.json'
143
fs.writeFileSync(filename, JSON.stringify(obj))
144
145
global.load = (filename) ->
146
if filename.slice(filename.length - 5) != '.json'
147
filename += '.json'
148
JSON.parse(fs.readFileSync(filename))
149
150
global.stripe = (account_id) ->
151
get_db (err, db) ->
152
db.stripe_update_customer(account_id:account_id,cb:done())
153
console.log 'stripe [account_id] -- update stripe info about user'
154
155