-
Notifications
You must be signed in to change notification settings - Fork 3
/
jobd.go
124 lines (98 loc) · 2.49 KB
/
jobd.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
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"bufio"
"flag"
"os"
"path"
"strings"
"code.google.com/p/go9p/p"
"code.google.com/p/go9p/p/srv"
"github.com/golang/glog"
)
// jobsroot is the root of the jobd file hierarchy
var jobsroot *jobsdir
// jobsdb is the path to the jobs database
var jobsdb string
func main() {
flfsaddr := flag.String("fsaddr", "0.0.0.0:5640", "Address where job file service listens for connections")
fldbdir := flag.String("dbdir", "/var/lib/jobd", "Location of the jobd jobs database")
fldebug := flag.Bool("debug", false, "9p debugging to stderr")
flag.Parse()
var err error
jobsdb, err = mkjobdb(*fldbdir)
if err != nil {
os.Exit(1)
}
root, err := mkjobfs()
if err != nil {
os.Exit(1)
}
db, err := os.Open(jobsdb)
if err != nil {
os.Exit(1)
}
scanner := bufio.NewScanner(db)
for scanner.Scan() {
data := scanner.Text()
jdparts := strings.Split(data, ":")
if len(jdparts) != 3 {
glog.Errorf("jobdb corruption: invalid job definition (%v)", data)
os.Exit(1)
}
jd, err := mkJobDefinition(jdparts[0], jdparts[1], jdparts[2])
if err != nil {
glog.Errorf("unable to create job definition (%v)", err)
os.Exit(1)
}
if err := jobsroot.addJob(*jd); err != nil {
glog.Errorf("can't add job (%v)", err)
os.Exit(1)
}
}
s := srv.NewFileSrv(root)
s.Dotu = true
if *fldebug {
s.Debuglevel = 1
}
s.Start(s)
if err := s.StartNetListener("tcp", *flfsaddr); err != nil {
glog.Errorf("listener failed to start (%v)", err)
os.Exit(1)
}
os.Exit(0)
}
// mkjobdb checks to see if the specified path to the jobd database exists and creates it
// if necessary, it also creates an empty database if none exists and returns the full
// path to the jobs database
func mkjobdb(dbdir string) (string, error) {
if err := os.MkdirAll(dbdir, 0755); err != nil {
return "", err
}
dbpath := path.Join(dbdir, "jobs.db")
f, err := os.OpenFile(dbpath, os.O_CREATE|os.O_RDONLY, 0755)
if err != nil {
return "", err
}
f.Close()
return dbpath, nil
}
// mkjobfs creates the static portion of the jobd file hierarchy: the 'clone'
// file, and the 'jobs' directory at the root of the hierarchy.
func mkjobfs() (*srv.File, error) {
var err error
user := p.OsUsers.Uid2User(os.Geteuid())
root := new(srv.File)
err = root.Add(nil, "/", user, nil, p.DMDIR|0555, nil)
if err != nil {
return nil, err
}
err = mkCloneFile(root, user)
if err != nil {
return nil, err
}
jobsroot, err = mkJobsDir(root, user)
if err != nil {
return nil, err
}
return root, nil
}