File: service_procd_linux.go

package info (click to toggle)
golang-github-kardianos-service 1.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 324 kB
  • sloc: makefile: 40; sh: 3
file content (175 lines) | stat: -rw-r--r-- 3,808 bytes parent folder | download | duplicates (2)
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
// Copyright 2015 Daniel Theophanes.
// Use of this source code is governed by a zlib-style
// license that can be found in the LICENSE file.

package service

import (
	"fmt"
	"os"
	"os/exec"
	"strings"
	"text/template"
	"time"
)

func isProcd() bool {
	if _, err := exec.LookPath("procd"); err == nil {
		return true
	}
	return false
}

type procd struct {
	*sysv
	scriptPath string
}

func newProcdService(i Interface, platform string, c *Config) (Service, error) {
	sv := &sysv{
		i:        i,
		platform: platform,
		Config:   c,
	}

	p := &procd{
		sysv:       sv,
		scriptPath: "/etc/init.d/" + sv.Name,
	}
	return p, nil
}

func (p *procd) template() *template.Template {
	customScript := p.Option.string(optionSysvScript, "")

	if customScript != "" {
		return template.Must(template.New("").Funcs(tf).Parse(customScript))
	}
	return template.Must(template.New("").Funcs(tf).Parse(procdScript))
}

func (p *procd) Install() error {
	confPath, err := p.configPath()
	if err != nil {
		return err
	}
	_, err = os.Stat(confPath)
	if err == nil {
		return fmt.Errorf("init already exists: %q", confPath)
	}

	f, err := os.Create(confPath)
	if err != nil {
		return err
	}
	defer f.Close()

	path, err := p.execPath()
	if err != nil {
		return err
	}

	var to = &struct {
		*Config
		Path         string
		LogDirectory string
	}{
		p.Config,
		path,
		p.Option.string(optionLogDirectory, defaultLogDirectory),
	}

	err = p.template().Execute(f, to)
	if err != nil {
		return err
	}

	if err = os.Chmod(confPath, 0755); err != nil {
		return err
	}

	if err = os.Symlink(confPath, "/etc/rc.d/S50"+p.Name); err != nil {
		return err
	}
	if err = os.Symlink(confPath, "/etc/rc.d/K02"+p.Name); err != nil {
		return err
	}

	return nil
}

func (p *procd) Uninstall() error {
	if err := run(p.scriptPath, "disable"); err != nil {
		return err
	}
	cp, err := p.configPath()
	if err != nil {
		return err
	}
	if err := os.Remove(cp); err != nil {
		return err
	}
	return nil
}

func (p *procd) Status() (Status, error) {
	_, out, err := runWithOutput(p.scriptPath, "status")
	if err != nil && !(err.Error() == "exit status 3") {
		return StatusUnknown, err
	}

	switch {
	case strings.HasPrefix(out, "running"):
		return StatusRunning, nil
	case strings.HasPrefix(out, "inactive"):
		return StatusStopped, nil
	default:
		return StatusUnknown, ErrNotInstalled
	}
}

func (p *procd) Start() error {
	return run(p.scriptPath, "start")
}

func (p *procd) Stop() error {
	return run(p.scriptPath, "stop")
}

func (p *procd) Restart() error {
	err := p.Stop()
	if err != nil {
		return err
	}
	time.Sleep(50 * time.Millisecond)
	return p.Start()
}

const procdScript = `#!/bin/sh /etc/rc.common
USE_PROCD=1
# After network starts
START=21
# Before network stops
STOP=89
cmd="{{.Path}}{{range .Arguments}} {{.|cmd}}{{end}}"
name="{{.Name}}"
pid_file="/var/run/${name}.pid"

start_service() {
    echo "Starting ${name}"
    procd_open_instance
    procd_set_param command ${cmd}

    # respawn automatically if something died, be careful if you have an alternative process supervisor
    # if process exits sooner than respawn_threshold, it is considered crashed and after 5 retries the service is stopped
    # if process finishes later than respawn_threshold, it is restarted unconditionally, regardless of error code
    # notice that this is literal respawning of the process, no in a respawn-on-failure sense
    procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}

    procd_set_param stdout 1             # forward stdout of the command to logd
    procd_set_param stderr 1             # same for stderr
    procd_set_param pidfile ${pid_file}  # write a pid file on instance start and remove it on stop
    procd_close_instance
    echo "${name} has been started"
}
`