Skip to content

Commit 4fa7543

Browse files
committed
prearing for rc.d support
1 parent ff5569a commit 4fa7543

File tree

10 files changed

+410
-412
lines changed

10 files changed

+410
-412
lines changed

.helper-files/Vagrantfile

Lines changed: 0 additions & 34 deletions
This file was deleted.

cli/build.sh

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/bin/bash
2+
3+
# Remove unused modules from "go.mod / go.sum"
4+
go mod tidy
5+
6+
# Clean up
7+
export OUTPUTFOLDER=./temp-build
8+
rm -rf $OUTPUTFOLDER
9+
10+
# Set compiler flags
11+
export LDFLAGS="-w -s"
12+
13+
SUPPORTED_TARGETS=(
14+
"darwin,amd64"
15+
"linux,386"
16+
"linux,amd64"
17+
"linux,arm"
18+
"linux,arm64"
19+
"linux,mips"
20+
"linux,mipsle"
21+
"linux,mips64,true"
22+
"linux,mips64le,true"
23+
"windows,amd64"
24+
"dragonfly,amd64"
25+
"freebsd,386,true"
26+
"freebsd,amd64,true"
27+
"freebsd,arm,true"
28+
"netbsd,amd64,true"
29+
"netbsd,arm,true"
30+
"openbsd,amd64,true"
31+
"openbsd,arm,true"
32+
"openbsd,arm64,true"
33+
"netbsd,386,true"
34+
"openbsd,386,true"
35+
)
36+
37+
clear
38+
echo ""
39+
echo "Build PARAMS"
40+
echo " -> OUTPUTFOLDER : ${OUTPUTFOLDER}"
41+
echo ""
42+
43+
for target in "${SUPPORTED_TARGETS[@]}"; do
44+
45+
# get the OS/Arch array
46+
IFS=',' read -ra targ <<<"$target"
47+
os="${targ[0]}"
48+
arch="${targ[1]}"
49+
ext=""
50+
51+
if [ $os = "windows" ]; then
52+
ext=".exe"
53+
fi
54+
55+
# build
56+
UPX=YES
57+
if [ "${targ[2]}" = "true" ]; then
58+
UPX=NO
59+
fi
60+
61+
printf "Building %-22s : UPX=$UPX" ${os}.${arch}
62+
echo ""
63+
64+
fullOutputFilePath="$OUTPUTFOLDER/systemkit-service-cli.${os}.${arch}${ext}"
65+
GOOS=${os} GOARCH=${arch} go build -ldflags "${LDFLAGS}" -o ${fullOutputFilePath} .
66+
67+
if [ $UPX = YES ]; then
68+
upx ${fullOutputFilePath} 1>/dev/null
69+
fi
70+
71+
done
72+
73+
echo ""
74+
75+
SUPPORTED_TARGETS_NOT=(
76+
"darwin,386"
77+
"linux,ppc64"
78+
"linux,ppc64le"
79+
"linux,s390x,true"
80+
"illumos,amd64"
81+
"windows,386"
82+
"aix,ppc64"
83+
"android,386"
84+
"android,amd64"
85+
"android,arm"
86+
"android,arm64"
87+
"darwin,arm"
88+
"darwin,arm64"
89+
"js,wasm"
90+
"plan9,386"
91+
"plan9,amd64"
92+
"plan9,arm"
93+
"solaris,amd64"
94+
)

cli/go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/codemodify/systemkit-service-cli
2+
3+
go 1.13
4+
5+
require (
6+
github.com/codemodify/systemkit-clicmdflags v1.0.2
7+
github.com/codemodify/systemkit-service v1.8.2
8+
)

cli/go.sum

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
github.com/codemodify/systemkit-clicmdflags v1.0.2 h1:VTTJ+omSb3EFHwtWqSnhINTIJeHzmenS0T2KQ5moHas=
2+
github.com/codemodify/systemkit-clicmdflags v1.0.2/go.mod h1:MD95bEMV5Gnn11H+gFSnerkWdQLLENRgptIBzf48b/E=
3+
github.com/codemodify/systemkit-helpers-conv v1.7.9 h1:a3MRS88pBnOauCX82xuZEnHeJSfULQm6mVl9mIfC23Q=
4+
github.com/codemodify/systemkit-helpers-conv v1.7.9/go.mod h1:yqHcvZcVmd9RPIIi49biNO9ALSY3CFTQeUnOVc1GjDY=
5+
github.com/codemodify/systemkit-helpers-files v1.7.9 h1:Dx3eYA0SlN2WHmfoaqt4l3JbIUqWPxNIY2LCiWxNZFQ=
6+
github.com/codemodify/systemkit-helpers-files v1.7.9/go.mod h1:SW9fzAddqJChY39y+yTxzMUqWBzqjtkAmwFrQXoL4Xc=
7+
github.com/codemodify/systemkit-helpers-guid v1.7.9/go.mod h1:PBY47zBxTK7To13nvanJ516EUiSrpPxqDRXS0JKyNyc=
8+
github.com/codemodify/systemkit-helpers-os v1.7.9 h1:Z1EgPCkhj/zoZ9sKlgsx1lLw/eRvwU2h5mMWGvvyyQU=
9+
github.com/codemodify/systemkit-helpers-os v1.7.9/go.mod h1:2j8txlmkBgd4jjTz5h2nAvRPeK2D5ibE2i2g3wrOnLk=
10+
github.com/codemodify/systemkit-helpers-reflection v1.7.9 h1:vPsYvbOiEZHT+rpoXlRx14IvzsKEAkYy7Aw2QEjF/G0=
11+
github.com/codemodify/systemkit-helpers-reflection v1.7.9/go.mod h1:HunZSrygIcl5IBmnWAYoDoqvFRVu7GIUvC0/scCfPzY=
12+
github.com/codemodify/systemkit-logging v1.8.4 h1:nkbj0VpnRnpI4PE4w9HNUv/ySO3A/1Y9LVeT9j+ZvGM=
13+
github.com/codemodify/systemkit-logging v1.8.4/go.mod h1:29Zc9P/MnY2bgenJU9aoDszUef+Xf40b3ajtusTBC1Q=
14+
github.com/codemodify/systemkit-service v1.8.2 h1:g6YNnXpAwi6ZyIIcTjC5QDIbIma5qvBVdtXAqS0reNE=
15+
github.com/codemodify/systemkit-service v1.8.2/go.mod h1:O6kOiZyerrlpS47w5udym+XqUSmt9ha4bUAz9wz/1os=
16+
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
17+
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
18+
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
19+
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
20+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
21+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

cli/main.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"os"
6+
"path/filepath"
7+
8+
clicmdflags "github.com/codemodify/systemkit-clicmdflags"
9+
service "github.com/codemodify/systemkit-service"
10+
)
11+
12+
type cmdFlags struct {
13+
Name string `flagName:"name" flagRequired:"true" flagDescription:"Service name"`
14+
Description string `flagName:"description" flagRequired:"false" flagDescription:"Service description"`
15+
Executable string `flagName:"executable" flagRequired:"true" flagDescription:"Service executable"`
16+
Args string `flagName:"args" flagRequired:"false" flagDescription:"Executable args"`
17+
JSON bool `flagName:"json" flagDefault:"false" flagDescription:"Enables JSON output"`
18+
Verbose bool `flagName:"verbose" flagDefault:"false" flagDescription:"Enables verbose output"`
19+
}
20+
21+
func main() {
22+
var cmd = &clicmdflags.Command{
23+
Name: filepath.Base(os.Args[0]),
24+
Description: "Create a system service",
25+
Examples: []string{
26+
filepath.Base(os.Args[0]) + " -json",
27+
filepath.Base(os.Args[0]) + " -json true",
28+
},
29+
Flags: cmdFlags{},
30+
Handler: handler,
31+
}
32+
33+
if err := cmd.Execute(); err != nil {
34+
log.Fatal(err)
35+
}
36+
}
37+
38+
func handler(command *clicmdflags.Command) {
39+
service.NewServiceFromConfig(service.Config{
40+
Name: "systemkit-test-service",
41+
Description: "SystemKit Test Service",
42+
Executable: "/bin/sleep",
43+
Args: []string{"40"},
44+
WorkingDirectory: "/tmp",
45+
Restart: true,
46+
DelayBeforeRestart: 10,
47+
StdOut: service.LogConfig{
48+
Disable: true,
49+
},
50+
StdErr: service.LogConfig{
51+
Disable: true,
52+
},
53+
})
54+
}

service_darwin-launchd.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/groob/plist"
2121
)
2222

23-
var logTag = "LAUNCHD-SERVICE"
23+
var logTag = "LaunchD-SERVICE"
2424

2525
type launchdService struct {
2626
config Config
@@ -266,30 +266,30 @@ func (thisRef launchdService) fileContentFromConfig() ([]byte, error) {
266266
<plist version='1.0'>
267267
<dict>
268268
<key>Label</key>
269-
<string>{{ .Name }}</string>
269+
<string>{{.Name}}</string>
270270
271271
<key>ProgramArguments</key>
272-
<array>{{ range $arg := .Args }}
273-
<string>{{ $arg }}</string>{{ end }}
272+
<array>{{ range $arg := .Args}}
273+
<string>{{ $arg}}</string>{{ end}}
274274
</array>
275275
276276
{{ if eq .StdOut.Disable false}}
277277
<key>StandardOutPath</key>
278-
<string>{{ .StdOut.Value }}</string>
279-
{{ end }}
278+
<string>{{.StdOut.Value}}</string>
279+
{{ end}}
280280
281281
{{ if eq .StdErr.Disable false}}
282282
<key>StandardErrorPath</key>
283-
<string>{{ .StdErr.Value }}</string>
284-
{{ end }}
283+
<string>{{.StdErr.Value}}</string>
284+
{{ end}}
285285
286286
<key>KeepAlive</key>
287-
<{{ .Restart }}/>
287+
<{{.Restart}}/>
288288
<key>RunAtLoad</key>
289289
<true/>
290290
291291
<key>WorkingDirectory</key>
292-
<string>{{ .WorkingDirectory }}</string>
292+
<string>{{.WorkingDirectory}}</string>
293293
</dict>
294294
</plist>
295295
`))

service_unix-systemd.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
logging "github.com/codemodify/systemkit-logging"
2222
)
2323

24-
var logTagSystemD = "SYSTEMD-SERVICE"
24+
var logTagSystemD = "SystemD-SERVICE"
2525

2626
type systemdService struct {
2727
config Config
@@ -316,25 +316,25 @@ func (thisRef systemdService) fileContentFromConfig() ([]byte, error) {
316316

317317
fileTemplate := template.Must(template.New("systemdFile").Parse(`
318318
[Unit]
319-
After=$DependsOn$
320-
Description={{ .Description }}
321-
Documentation={{ .Documentation }}
322-
StartLimitIntervalSec={{ .DelayBeforeRestart }}
319+
After=@DependsOn@
320+
Description={{.Description}}
321+
Documentation={{.Documentation}}
322+
StartLimitIntervalSec={{.DelayBeforeRestart}}
323323
StartLimitBurst=0
324324
StartLimitAction=none
325325
326326
[Service]
327-
ExecStart={{ .Executable }}
328-
WorkingDirectory={{ .WorkingDirectory }}
329-
Restart=$Restart$
330-
RestartSec={{ .DelayBeforeRestart }}
327+
ExecStart={{.Executable}}
328+
WorkingDirectory={{.WorkingDirectory}}
329+
Restart=@Restart@
330+
RestartSec={{.DelayBeforeRestart}}
331331
Type=simple
332332
333-
{{ if eq .StdOut.Disable false}}StandardOutput={{ .StdOut.Value }}{{ end }}
334-
{{ if eq .StdErr.Disable false}}StandardError={{ .StdErr.Value }}{{ end }}
333+
{{ if eq .StdOut.Disable false}}StandardOutput={{.StdOut.Value}}{{ end}}
334+
{{ if eq .StdErr.Disable false}}StandardError={{.StdErr.Value}}{{ end}}
335335
336-
{{ if .RunAsUser }}User={{ .RunAsUser }}{{ end }}
337-
{{ if .RunAsGroup }}Group={{ .RunAsGroup }}{{ end }}
336+
{{ if .RunAsUser}}User={{.RunAsUser}}{{ end}}
337+
{{ if .RunAsGroup}}Group={{.RunAsGroup}}{{ end}}
338338
339339
[Install]
340340
WantedBy=multi-user.target
@@ -348,21 +348,21 @@ WantedBy=multi-user.target
348348
fileTemplateAsString := buffer.String()
349349
fileTemplateAsString = strings.Replace(
350350
fileTemplateAsString,
351-
"$DependsOn$",
351+
"@DependsOn@",
352352
strings.Join(thisRef.config.DependsOn, " "),
353353
1,
354354
)
355355
if thisRef.config.Restart {
356356
fileTemplateAsString = strings.Replace(
357357
fileTemplateAsString,
358-
"$Restart$",
358+
"@Restart@",
359359
"always",
360360
1,
361361
)
362362
} else {
363363
fileTemplateAsString = strings.Replace(
364364
fileTemplateAsString,
365-
"$Restart$",
365+
"@Restart@",
366366
"on-failure",
367367
1,
368368
)

0 commit comments

Comments
 (0)