/
main.go
112 lines (92 loc) · 2.03 KB
/
main.go
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
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
log "github.com/sirupsen/logrus"
)
var db *gorm.DB
var p string
/*
TODOs:
- Add flag for dev
- test 8000
- test multiple remote
*/
func main() {
Worker := flag.Bool("worker", false, "Start remote worker")
Port := flag.String("p", "22", "Set SSH port")
DB := flag.Bool("db", false, "Remove db")
Debug := flag.Bool("debug", false, "debug")
flag.Parse()
p = *Port
if *Debug {
path, err := exec.LookPath("jupyter")
if err != nil {
log.Error(err)
}
ioutil.WriteFile("log", []byte(path), 0644)
fmt.Print(path)
}
if *DB {
os.Remove("flow.db")
}
initDB()
go flowWatcher()
r := server()
if *Worker {
go runJupyter()
log.Info("Bayesnote flow worker started")
r.Run(":9000")
} else {
go tunnelWatcher()
log.Info("Bayesnote flow core started")
// go serveFrontend()
r.Run(":9000")
}
}
//TODO: check if jupyter is already running
func runJupyter() {
path, err := lookJupyter()
if err != nil {
return
}
cmd := exec.Command("/bin/sh", "-c", path+" notebook --ip='127.0.0.1' --port='8888' --NotebookApp.token='' --NotebookApp.password='' --allow-root")
err = cmd.Run()
if err != nil {
log.Error(err)
}
}
//TODO: search all possible paths or let user input
func lookJupyter() (string, error) {
dir, err := os.UserHomeDir()
if err != nil {
log.Error(err)
}
condaPath := filepath.Join(dir, "conda3/bin")
minicondaPath := filepath.Join(dir, "miniconda3/bin")
_, err = os.Stat(condaPath)
if !os.IsNotExist(err) {
os.Setenv("PATH", condaPath+":$PATH")
}
_, err = os.Stat(minicondaPath)
if !os.IsNotExist(err) {
os.Setenv("PATH", minicondaPath+":$PATH")
}
path, err := exec.LookPath("jupyter") //TODO: not executable by current user
if err != nil {
log.Error(err)
}
if path != "" {
ioutil.WriteFile("flow.env", []byte(path), 0644)
return path, nil
}
log.Error("Jupyter not found in $PATH")
return "", errors.New("Jupyter not found")
}