-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirector.go
150 lines (135 loc) · 3.1 KB
/
director.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// 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 (
"flag"
"fmt"
"os"
"github.com/gwoo/greq"
"github.com/mitchellh/osext"
"github.com/ninjasphere/go-ninja/logger"
)
var config *Config
var daemon *Process
var log = logger.GetLogger("Director")
var Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
usage := `
Commands
list List processes.
show [name] Show main proccess or named process.
start [name] Start main proccess or named process.
stop [name] Stop main proccess or named process.
restart [name] Restart main proccess or named process.
`
fmt.Fprintln(os.Stderr, usage)
}
func init() {
flag.Usage = Usage
flag.Parse()
setConfig()
// Make sure we use the same binary for the daemon
currentBinary, err := osext.Executable()
log.Infof("Binary: %s", currentBinary)
if err != nil {
currentBinary = "/opt/ninjablocks/bin/sphere-director"
log.Infof("Couldn't get current binary. ? Setting to '%s'. error: %s", currentBinary, err)
}
daemon = &Process{
ID: "sphere-director",
Info: packageJson{
Name: "Sphere Director",
Description: "Manages the processes that make up Ninja Sphere.",
},
Args: []string{},
Command: currentBinary,
Pidfile: Pidfile("/var/run/sphere-director.pid"),
Logfile: homeDir + "sphere-director.log",
Errfile: homeDir + "sphere-director.log",
}
}
func main() {
if len(flag.Args()) > 0 {
fmt.Printf("%s", Cli())
return
}
if len(flag.Args()) == 0 {
RunDaemon()
Mqtt()
HttpServer()
}
}
func Cli() string {
var o []byte
var err error
sub := flag.Arg(0)
name := flag.Arg(1)
req := greq.New(host(), true)
if sub == "list" {
o, _, err = req.Get("/")
}
if name == "" {
if sub == "start" {
daemon.Args = append(daemon.Args, os.Args[2:]...)
return daemon.start()
}
_, _, err = daemon.find()
if err != nil {
return fmt.Sprintf("Error: %s.\n", err)
}
if sub == "show" {
return fmt.Sprintf("%s.\n", daemon.String())
}
if sub == "stop" {
message := daemon.stop()
return message
}
if sub == "restart" {
ch, message := daemon.restart()
fmt.Print(message)
return fmt.Sprintf("%s\n", <-ch)
}
}
if name != "" {
path := fmt.Sprintf("/%s", name)
switch sub {
case "show":
o, _, err = req.Get(path)
case "start":
o, _, err = req.Post(path, nil)
case "stop":
o, _, err = req.Delete(path)
case "restart":
o, _, err = req.Put(path, nil)
}
}
if err != nil {
fmt.Printf("Process error: %s", err)
}
return fmt.Sprintf("%s\n", o)
}
func RunDaemon() {
daemon.children = config.Processes
//daemon.run()
}
func setConfig() {
var err error
config, err = LoadConfig()
if err != nil {
log.Fatalf("%s", err)
return
}
config.FindProcesses()
}
func host() string {
scheme := "https"
if isHttps() == false {
scheme = "http"
}
return fmt.Sprintf("%s://%s:%s@:%d",
scheme, config.Username, config.Password, config.Port,
)
}