-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
120 lines (98 loc) · 2.73 KB
/
config.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
// goforever - processes management
// Copyright (c) 2013 Garrett Woodworth (https://github.com/gwoo).
// sphere-director - Ninja processes management
// Copyright (c) 2014 Ninja Blocks Inc. (https://github.com/ninjablocks).
package main
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"github.com/davecgh/go-spew/spew"
nconfig "github.com/ninjasphere/go-ninja/config"
)
var homeDir = "./"
func init() {
if runtime.GOOS == "linux" {
homeDir = "/var/run/director/"
os.MkdirAll(homeDir, 0777)
}
}
type Config struct {
Port int
Username string
Password string
Daemonize bool
Paths []string
Processes map[string]*Process
}
type packageJson struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Version string `json:"version,omitempty"`
Description string `json:"description,omitempty"`
Main string `json:"main,omitempty"`
Author string `json:"author,omitempty"`
License string `json:"license,omitempty"`
MaxMemory int `json:"maxMemory,omitempty"`
PingTime string `json:"ping,omitempty"`
}
func (c Config) Keys() []string {
keys := []string{}
for k := range c.Processes {
keys = append(keys, k)
}
spew.Dump("Keys", keys)
return keys
}
func (c Config) Get(key string) *Process {
return c.Processes[key]
}
func (c Config) FindProcesses() {
for _, path := range c.Paths {
path = os.ExpandEnv(path)
log.Debugf("Finding processes in path %s", path)
files, err := ioutil.ReadDir(path)
if err == nil {
for _, file := range files {
if file.IsDir() {
//spew.Dump(file)
infoFile, err := ioutil.ReadFile(filepath.Join(path, file.Name(), "package.json"))
if err == nil {
var info packageJson
err = json.Unmarshal(infoFile, &info)
if err != nil || info.Main == "" {
log.Warningf("Could not read package.info for module %s : %s", file.Name(), err)
} else {
//spew.Dump(info)
process := &Process{
ID: file.Name(),
Info: info,
MaxMemory: info.MaxMemory,
Command: info.Main,
Path: filepath.Join(path, file.Name()),
PingTime: info.PingTime,
Pidfile: Pidfile(homeDir + file.Name() + ".pid"),
}
if process.PingTime == "" {
process.PingTime = "1m"
}
c.Processes[file.Name()] = process
}
}
}
}
}
}
//spew.Dump(c.Processes)
}
func LoadConfig() (*Config, error) {
return &Config{
Port: nconfig.MustInt("director", "port"),
Username: nconfig.MustString("director", "username"),
Password: nconfig.MustString("director", "password"),
Paths: nconfig.MustStringArray("director", "paths"),
Processes: make(map[string]*Process),
}, nil
}