Skip to content

Commit

Permalink
Merge branch 'release/0.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
takama committed Aug 5, 2016
2 parents 9efa9f9 + 90f34e5 commit a16f14d
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func main() {
}
```

## Contributors
## Contributors (unsorted)

- [Igor Dolzhikov](https://github.com/takama)
- [Sheile](https://github.com/Sheile)
Expand All @@ -183,6 +183,7 @@ func main() {
- [Fatih Kaya](https://github.com/fatihky)
- [Jannick Fahlbusch](https://github.com/jannickfahlbusch)
- [TobyZXJ](https://github.com/tobyzxj)
- [Pichu Chen](https://github.com/PichuChen)

All the contributors are welcome. If you would like to be the contributor please accept some rules.
- The pull requests will be accepted only in "develop" branch
Expand Down
2 changes: 1 addition & 1 deletion daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// license that can be found in the LICENSE file.

/*
Package daemon 0.5.2 for use with Go (golang) services.
Package daemon 0.6.0 for use with Go (golang) services.
Package daemon provides primitives for daemonization of golang services.
This package is not provide implementation of user daemon,
Expand Down
3 changes: 3 additions & 0 deletions daemon_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (

// Get the daemon properly
func newDaemon(name, description string, dependencies []string) (Daemon, error) {
if _, err := os.Stat("/sbin/initctl"); err == nil {
return &upstartRecord{name, description, dependencies}, nil
}
if _, err := os.Stat("/run/systemd/system"); err == nil {
return &systemDRecord{name, description, dependencies}, nil
}
Expand Down
192 changes: 192 additions & 0 deletions daemon_linux_upstart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by
// license that can be found in the LICENSE file.

package daemon

import (
"os"
"os/exec"
"regexp"
"strings"
"text/template"
)

// upstartRecord - standard record (struct) for linux upstart version of daemon package
type upstartRecord struct {
name string
description string
dependencies []string
}

// Standard service path for systemV daemons
func (linux *upstartRecord) servicePath() string {
return "/etc/init/" + linux.name + ".conf"
}

// Is a service installed
func (linux *upstartRecord) isInstalled() bool {

if _, err := os.Stat(linux.servicePath()); err == nil {
return true
}

return false
}

// Check service is running
func (linux *upstartRecord) checkRunning() (string, bool) {
output, err := exec.Command("status", linux.name).Output()
if err == nil {
if matched, err := regexp.MatchString(linux.name+" start/running", string(output)); err == nil && matched {
reg := regexp.MustCompile("process ([0-9]+)")
data := reg.FindStringSubmatch(string(output))
if len(data) > 1 {
return "Service (pid " + data[1] + ") is running...", true
}
return "Service is running...", true
}
}

return "Service is stopped", false
}

// Install the service
func (linux *upstartRecord) Install(args ...string) (string, error) {
installAction := "Install " + linux.description + ":"

if ok, err := checkPrivileges(); !ok {
return installAction + failed, err
}

srvPath := linux.servicePath()

if linux.isInstalled() {
return installAction + failed, ErrAlreadyInstalled
}

file, err := os.Create(srvPath)
if err != nil {
return installAction + failed, err
}
defer file.Close()

execPatch, err := executablePath(linux.name)
if err != nil {
return installAction + failed, err
}

templ, err := template.New("upstatConfig").Parse(upstatConfig)
if err != nil {
return installAction + failed, err
}

if err := templ.Execute(
file,
&struct {
Name, Description, Path, Args string
}{linux.name, linux.description, execPatch, strings.Join(args, " ")},
); err != nil {
return installAction + failed, err
}

if err := os.Chmod(srvPath, 0755); err != nil {
return installAction + failed, err
}

return installAction + success, nil
}

// Remove the service
func (linux *upstartRecord) Remove() (string, error) {
removeAction := "Removing " + linux.description + ":"

if ok, err := checkPrivileges(); !ok {
return removeAction + failed, err
}

if !linux.isInstalled() {
return removeAction + failed, ErrNotInstalled
}

if err := os.Remove(linux.servicePath()); err != nil {
return removeAction + failed, err
}

return removeAction + success, nil
}

// Start the service
func (linux *upstartRecord) Start() (string, error) {
startAction := "Starting " + linux.description + ":"

if ok, err := checkPrivileges(); !ok {
return startAction + failed, err
}

if !linux.isInstalled() {
return startAction + failed, ErrNotInstalled
}

if _, ok := linux.checkRunning(); ok {
return startAction + failed, ErrAlreadyRunning
}

if err := exec.Command("start", linux.name).Run(); err != nil {
return startAction + failed, err
}

return startAction + success, nil
}

// Stop the service
func (linux *upstartRecord) Stop() (string, error) {
stopAction := "Stopping " + linux.description + ":"

if ok, err := checkPrivileges(); !ok {
return stopAction + failed, err
}

if !linux.isInstalled() {
return stopAction + failed, ErrNotInstalled
}

if _, ok := linux.checkRunning(); !ok {
return stopAction + failed, ErrAlreadyStopped
}

if err := exec.Command("stop", linux.name).Run(); err != nil {
return stopAction + failed, err
}

return stopAction + success, nil
}

// Status - Get service status
func (linux *upstartRecord) Status() (string, error) {

if ok, err := checkPrivileges(); !ok {
return "", err
}

if !linux.isInstalled() {
return "Status could not defined", ErrNotInstalled
}

statusAction, _ := linux.checkRunning()

return statusAction, nil
}

var upstatConfig = `# {{.Name}} {{.Description}}
description "{{.Description}}"
author "Pichu Chen <pichu@tih.tw>"
start on runlevel [2345]
stop on runlevel [016]
#kill timeout 5
exec {{.Path}} >> /var/log/{{.Name}}.log 2>> /var/log/{{.Name}}.err
`

0 comments on commit a16f14d

Please sign in to comment.