This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 181
/
cmds.go
218 lines (200 loc) · 4.67 KB
/
cmds.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
// "github.com/franela/goreq"
"github.com/goji/httpauth"
"github.com/imroc/req"
"github.com/urfave/cli"
)
func postForm(pathname string, data url.Values) (r JSONResponse, err error) {
resp, err := http.PostForm(cfg.Client.ServerURL+pathname, data)
if err != nil {
return r, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return r, err
}
err = json.Unmarshal(body, &r)
if err != nil {
return r, fmt.Errorf("POST %v %v", strconv.Quote(pathname), string(body))
}
return r, nil
}
func actionStartServer(c *cli.Context) error {
suv, hdlr, err := newSupervisorHandler()
if err != nil {
log.Fatal(err)
}
if c.Bool("foreground") {
if err = newDistributed(suv, hdlr); err != nil {
log.Fatal(err)
}
}
auth := cfg.Server.HttpAuth
if auth.Enabled {
hdlr = httpauth.SimpleBasicAuth(auth.User, auth.Password)(hdlr)
}
http.Handle("/", hdlr)
addr := cfg.Server.Addr
if c.Bool("foreground") {
suv.AutoStartPrograms()
log.Printf("server listen on %v", addr)
log.Fatal(http.ListenAndServe(addr, nil))
} else {
if checkServerStatus() == nil {
fmt.Println("server is already running")
return nil
}
logPath := filepath.Join(defaultGosuvDir, "gosuv.log")
logFd, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
log.Fatalf("create file %s failed: %v", logPath, err)
}
cmd := exec.Command(os.Args[0], "start-server", "-f")
cmd.Stdout = logFd
cmd.Stderr = logFd
err = cmd.Start()
if err != nil {
log.Fatal(err)
}
if err := ioutil.WriteFile("/var/run/gosuv.pid", []byte(strconv.Itoa(cmd.Process.Pid)), 0644); err != nil {
log.Fatalln(err)
}
select {
case err = <-GoFunc(cmd.Wait):
log.Fatalf("server started failed, %v", err)
case <-time.After(200 * time.Millisecond):
showAddr := addr
if strings.HasPrefix(addr, ":") {
showAddr = "0.0.0.0" + addr
}
fmt.Printf("server started, listening on %s\n", showAddr)
}
}
return nil
}
func actionStatus(c *cli.Context) error {
// res, err := goreq.Request{
// Uri: cfg.Client.ServerURL + "/api/programs",
// }.Do()
res, err := req.Get(cfg.Client.ServerURL + "/api/programs")
if err != nil {
return err
}
var programs = make([]struct {
Program Program `json:"program"`
Status string `json:"status"`
}, 0)
// if err := res.Body.FromJsonTo(&programs); err != nil {
// return err
// }
if err := res.ToJSON(&programs); err != nil {
return err
}
format := "%-23s\t%-8s\n"
fmt.Printf(format, "PROGRAM NAME", "STATUS")
for _, p := range programs {
fmt.Printf(format, p.Program.Name, p.Status)
}
return nil
}
// cmd: <start|stop>
func programOperate(cmd, name string) (err error, success bool) {
// res, err := goreq.Request{
// Method: "POST",
// Uri: cfg.Client.ServerURL + "/api/programs/" + name + "/" + cmd,
// }.Do()
res, err := req.Post(cfg.Client.ServerURL + "/api/programs/" + name + "/" + cmd)
if err != nil {
return
}
var v = struct {
Status int `json:"status"`
}{}
// if err = res.Body.FromJsonTo(&v); err != nil {
// return
// }
if err = res.ToJSON(&v); err != nil {
return
}
success = v.Status == 0
return
}
func actionStart(c *cli.Context) (err error) {
name := c.Args().First()
err, success := programOperate("start", name)
if err != nil {
return
}
if success {
fmt.Println("Started")
} else {
fmt.Println("Start failed")
}
return nil
}
func actionStop(c *cli.Context) (err error) {
name := c.Args().First()
err, success := programOperate("stop", name)
if err != nil {
return
}
if !success {
fmt.Println("Stop failed")
}
return nil
}
func actionShutdown(c *cli.Context) error {
restart := c.Bool("restart")
if restart {
log.Fatal("Restart not implemented.")
}
ret, err := postForm("/api/shutdown", nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(ret.Value)
return nil
}
func actionUpdateSelf(c *cli.Context) error {
return githubUpdate(c.Bool("yes"))
}
func actionEdit(c *cli.Context) error {
cmd := exec.Command("vim", filepath.Join(os.Getenv("HOME"), ".gosuv/programs.yml"))
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
return cmd.Run()
}
func actionVersion(c *cli.Context) error {
fmt.Printf("gosuv version %s\n", version)
return nil
}
func actionReload(c *cli.Context) error {
ret, err := postForm("/api/reload", nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(ret.Value)
return nil
}
func actionConfigTest(c *cli.Context) error {
if _, _, err := newSupervisorHandler(); err != nil {
log.Fatal(err)
}
log.Println("test is successful")
return nil
}