-
Notifications
You must be signed in to change notification settings - Fork 570
/
service.go
116 lines (100 loc) · 2.6 KB
/
service.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
package main
import (
"fmt"
"github.com/kardianos/service"
log "github.com/sirupsen/logrus"
)
// ServiceCommand install/uninstall/start/stop supervisord service
type ServiceCommand struct {
}
var serviceCommand ServiceCommand
type program struct{}
// Start supervised service
func (p *program) Start(s service.Service) error {
go p.run()
return nil
}
func (p *program) run() {}
// Stop supervised service
func (p *program) Stop(s service.Service) error {
// Stop should not block. Return with a few seconds.
return nil
}
// Execute implement Execute() method defined in flags.Commander interface, executes the given command
func (sc ServiceCommand) Execute(args []string) error {
if len(args) == 0 {
showUsage()
return nil
}
serviceArgs := make([]string, 0)
if options.Configuration != "" {
serviceArgs = append(serviceArgs, "--configuration="+options.Configuration)
}
if options.EnvFile != "" {
serviceArgs = append(serviceArgs, "--env-file="+options.EnvFile)
}
svcConfig := &service.Config{
Name: "go-supervisord",
DisplayName: "go-supervisord",
Description: "Supervisord service in golang",
Arguments: serviceArgs,
}
prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Error("service init failed", err)
return err
}
action := args[0]
switch action {
case "install":
err := s.Install()
if err != nil {
log.Error("Failed to install service go-supervisord: ", err)
fmt.Println("Failed to install service go-supervisord: ", err)
return err
} else {
fmt.Println("Succeed to install service go-supervisord")
}
case "uninstall":
s.Stop()
err := s.Uninstall()
if err != nil {
log.Error("Failed to uninstall service go-supervisord: ", err)
fmt.Println("Failed to uninstall service go-supervisord: ", err)
return err
} else {
fmt.Println("Succeed to uninstall service go-supervisord")
}
case "start":
err := s.Start()
if err != nil {
log.Error("Failed to start service: ", err)
fmt.Println("Failed to start service: ", err)
return err
} else {
fmt.Println("Succeed to start service go-supervisord")
}
case "stop":
err := s.Stop()
if err != nil {
log.Error("Failed to stop service: ", err)
fmt.Println("Failed to stop service: ", err)
return err
} else {
fmt.Println("Succeed to stop service go-supervisord")
}
default:
showUsage()
}
return nil
}
func showUsage() {
fmt.Println("usage: supervisord service install/uninstall/start/stop")
}
func init() {
parser.AddCommand("service",
"install/uninstall/start/stop service",
"install/uninstall/start/stop service",
&serviceCommand)
}