forked from codeskyblue/gosuv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmds.go
140 lines (128 loc) · 2.97 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/goji/httpauth"
"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)
}
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(defaultConfigDir, "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)
}
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 {
err := checkServerStatus()
if err != nil {
log.Fatal(err)
} else {
log.Println("Server is running, OK.")
}
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 equinoxUpdate(c.String("channel"), 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
}