-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
227 lines (216 loc) · 8.49 KB
/
main.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
219
220
221
222
223
224
225
226
227
package main
import (
"flag"
"fmt"
"os"
"sync"
"github.com/charmbracelet/lipgloss"
"github.com/pioz/god/runner"
"golang.org/x/exp/slices"
)
var availableCommands = []string{"install", "uninstall", "start", "stop", "restart", "status", "show-service"}
func init() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [OPTIONS...] {COMMAND} ...\n", os.Args[0])
flag.PrintDefaults()
fmt.Fprintln(flag.CommandLine.Output())
fmt.Fprintln(flag.CommandLine.Output(), lipgloss.NewStyle().Bold(true).Render("Commands:"))
fmt.Fprintln(flag.CommandLine.Output(), lipgloss.NewStyle().Width(120).Render("After each command you can specify one or more services. If you do not specify any, all services in the YAML configuration file will be selected."))
fmt.Fprintln(flag.CommandLine.Output())
commands := [][]string{
{"install SERVICE...", "Install one or more services on the remote host."},
{"uninstall SERVICE...", "Uninstall one or more services on the remote host."},
{"start SERVICE...", "Start one or more services."},
{"stop SERVICE...", "Stop one or more services."},
{"restart SERVICE...", "Restart one or more services."},
{"status SERVICE...", "Show runtime status of one or more services."},
{"show-service SERVICE...", "Print systemd unit service file of one or more services."},
}
for _, command := range commands {
fmt.Fprintln(
flag.CommandLine.Output(),
lipgloss.JoinHorizontal(
lipgloss.Top,
lipgloss.NewStyle().Width(30).Render(command[0]),
lipgloss.NewStyle().Width(90).Render(command[1]),
),
)
}
fmt.Fprintln(flag.CommandLine.Output())
fmt.Fprintln(flag.CommandLine.Output(), lipgloss.NewStyle().Bold(true).Render("Configuration YAML file options:"))
confOptions := [][]string{
{"user", "User to log in with on the remote machine. (default current user)"},
{"host", "Hostname to log in for executing commands on the remote host. (required)"},
{"port", "Port to connect to on the remote host. (default 22)"},
{"private_key_path", "Local path of the private key used to authenticate on the remote host. (default '~/.ssh/id_rsa')"},
{"go_exec_path", "Remote path of the Go binary executable. (default '$GOBIN/go')"},
{"go_bin_directory", "The directory where 'go install' will install the service executable. (default '$GOBIN')"},
{"go_install", "Go package to install on the remote host. Package path must refer to main packages and must have the version suffix, ex: @latest. (required)"},
{"go_private", "Set GOPRIVATE environment variable to be used when run 'go install' to install from private sources."},
{"netrc_machine", "Add in remote .netrc file the machine name to be used to access private repository."},
{"netrc_login", "Add in remote .netrc file the login name to be used to access private repository."},
{"netrc_password", "Add in remote .netrc file the password or access token to be used to access private repository."},
{"systemd_path", "Remote path of systemd binary executable. (default 'systemd')"},
{"systemd_services_directory", "Remote directory where to save user instance systemd unit service configuration file. (default '~/.config/systemd/user/')"},
{"systemd_linger_directory", "Remote directory where to find the lingering user list. If lingering is enabled for a specific user, a user manager is spawned for the user at boot and kept around after logouts. (default '/var/lib/systemd/linger/')"},
{"exec_start", "Command with its arguments that are executed when this service is started."},
{"working_directory", "Sets the remote working directory for executed processes. (default: '~/')"},
{"environment", "Sets environment variables for executed process. Takes a space-separated list of variable assignments."},
{"log_path", "Sets the remote file path where executed processes will redirect its standard output and standard error."},
{"run_after_service", "Ensures that the service is started after the listed unit finished starting up."},
{"start_limit_burst", "Configure service start rate limiting. Services which are started more than burst times within an interval time interval are not permitted to start any more. Use 'start_limit_interval_sec' to configure the checking interval."},
{"start_limit_interval_sec", "Configure the checking interval used by 'start_limit_burst'."},
{"restart_sec", "Configures the time to sleep before restarting a service. Takes a unit-less value in seconds."},
{"copy_files", "[Array] Copy files to the remote working directory."},
{"ignore", "If a command is called without any service name, all services in the YAML configuration file will be selected, except those with ignore set to true. (default false)"},
}
for _, option := range confOptions {
fmt.Fprintln(
flag.CommandLine.Output(),
lipgloss.JoinHorizontal(
lipgloss.Top,
lipgloss.NewStyle().Width(30).Render(option[0]),
lipgloss.NewStyle().Width(90).Render(option[1]),
),
)
}
fmt.Fprintln(flag.CommandLine.Output(), lipgloss.NewStyle().Width(120).Render("\nAll previous configuration options can be overridden with environment variables in the form <SERVICE_NAME>_<OPTION_NAME>. For example, the option netrc_password can be overridden with the environment variable MY_SERVICE_NAME_NETRC_PASSWORD."))
}
}
func main() {
var createWorkingDirectory, help, quiet bool
var confFilePath string
flag.StringVar(&confFilePath, "f", ".god.yml", "Configuration YAML file path.")
flag.BoolVar(&createWorkingDirectory, "c", false, "Creates the remote service working directory if not exists. With uninstall command, removes log files and the remote working directory if empty.")
flag.BoolVar(&quiet, "q", false, "Disable printing.")
flag.BoolVar(&help, "h", false, "Print this help.")
flag.Parse()
if help {
flag.Usage()
os.Exit(0)
}
args := flag.Args()
if len(args) == 0 {
flag.Usage()
os.Exit(1)
}
command, services := args[0], args[1:]
if !slices.Contains(availableCommands, command) {
flag.Usage()
os.Exit(1)
}
r, err := runner.MakeRunner(confFilePath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
r.QuietMode = quiet
if len(services) == 0 {
services = r.GetServiceNames()
}
go r.StartPrintOutput(services)
defer r.StopPrintOutput()
var wg sync.WaitGroup
wg.Add(len(services))
switch command {
case "install":
{
for _, serviceName := range services {
go func(serviceName string) {
defer wg.Done()
s, err := r.MakeService(serviceName)
if err != nil {
r.SendMessage(serviceName, err.Error(), runner.MessageError)
} else {
s.Install(createWorkingDirectory)
}
}(serviceName)
}
}
case "uninstall":
{
for _, serviceName := range services {
go func(serviceName string) {
defer wg.Done()
s, err := r.MakeService(serviceName)
if err != nil {
r.SendMessage(serviceName, err.Error(), runner.MessageError)
} else {
s.Uninstall(createWorkingDirectory)
}
}(serviceName)
}
}
case "start":
{
for _, serviceName := range services {
go func(serviceName string) {
defer wg.Done()
s, err := r.MakeService(serviceName)
if err != nil {
r.SendMessage(serviceName, err.Error(), runner.MessageError)
} else {
s.StartService()
}
}(serviceName)
}
}
case "stop":
{
for _, serviceName := range services {
go func(serviceName string) {
defer wg.Done()
s, err := r.MakeService(serviceName)
if err != nil {
r.SendMessage(serviceName, err.Error(), runner.MessageError)
} else {
s.StopService()
}
}(serviceName)
}
}
case "restart":
{
for _, serviceName := range services {
go func(serviceName string) {
defer wg.Done()
s, err := r.MakeService(serviceName)
if err != nil {
r.SendMessage(serviceName, err.Error(), runner.MessageError)
} else {
s.RestartService()
}
}(serviceName)
}
}
case "status":
{
for _, serviceName := range services {
go func(serviceName string) {
defer wg.Done()
s, err := r.MakeService(serviceName)
if err != nil {
r.SendMessage(serviceName, err.Error(), runner.MessageError)
} else {
s.StatusService()
}
}(serviceName)
}
}
case "show-service":
{
for _, serviceName := range services {
go func(serviceName string) {
defer wg.Done()
s, err := r.MakeService(serviceName)
if err != nil {
r.SendMessage(serviceName, err.Error(), runner.MessageError)
} else {
s.ShowServiceFile()
}
}(serviceName)
}
}
}
wg.Wait()
}