Skip to content

Commit

Permalink
Merge branch 'release/v1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
takama committed Jul 23, 2020
2 parents eee3878 + af3497b commit 496d691
Show file tree
Hide file tree
Showing 17 changed files with 138 additions and 56 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016 The Go Authors
Copyright (c) 2020 The Go Authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Go Daemon

A daemon package for use with Go (golang) services with no dependencies
A daemon package for use with Go (golang) services

[![GoDoc](https://godoc.org/github.com/takama/daemon?status.svg)](https://godoc.org/github.com/takama/daemon)

Expand All @@ -19,7 +19,7 @@ import (
)

func main() {
service, err := daemon.New("name", "description")
service, err := daemon.New("name", "description", daemon.SystemDaemon)
if err != nil {
log.Fatal("Error: ", err)
}
Expand Down Expand Up @@ -159,7 +159,7 @@ func init() {
}

func main() {
srv, err := daemon.New(name, description, dependencies...)
srv, err := daemon.New(name, description, daemon.SystemDaemon, dependencies...)
if err != nil {
errlog.Println("Error: ", err)
os.Exit(1)
Expand All @@ -183,13 +183,13 @@ Windows). Template will be a default Go Template(`"text/template"`).
If `SetTemplate` is not called, default template content will be used
while creating service.

|Variable | Description|
|---------|------------|
|Description| Description for service |
|Dependencies|Service dependencies|
|Name|Service name|
|Path|Path of service executable|
|Args|Arguments for service executable|
| Variable | Description |
| ------------ | -------------------------------- |
| Description | Description for service |
| Dependencies | Service dependencies |
| Name | Service name |
| Path | Path of service executable |
| Args | Arguments for service executable |

#### Example template(for linux systemv)

Expand All @@ -215,7 +215,6 @@ See `examples/cron/cron_job.go`

## Contributors (unsorted)

- [Igor Dolzhikov](https://github.com/takama)
- [Sheile](https://github.com/Sheile)
- [Nguyen Trung Loi](https://github.com/loint)
- [Donny Prasetyobudi](https://github.com/donnpebe)
Expand All @@ -235,10 +234,12 @@ See `examples/cron/cron_job.go`
- [AlgorathDev](https://github.com/AlgorathDev)
- [Alexis Camilleri](https://github.com/krysennn)
- [neverland4u](https://github.com/neverland4u)
- [Rustam](https://github.com/rusq)
- [King'ori Maina](https://github.com/itskingori)

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
- The pull requests will be accepted only in `develop` branch
- All modifications or additions should be tested
- Sorry, We will not accept code with any dependency, only standard library

Expand Down
73 changes: 63 additions & 10 deletions daemon.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Copyright 2020 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 v0.12.0 for use with Go (golang) services.
Package daemon v1.0.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,
accordingly must have root rights to install/remove service.
In the current implementation is only supported Linux and Mac Os X daemon.
Package daemon provides primitives for daemonization of golang services. In the
current implementation the only supported operating systems are macOS, FreeBSD,
Linux and Windows. Also to note, for global daemons one must have root rights to
install or remove the service. The only exception is macOS where there is an
implementation of a user daemon that can installed or removed by the current
user.
Example:
Expand Down Expand Up @@ -137,7 +139,7 @@ Example:
}
func main() {
srv, err := daemon.New(name, description, dependencies...)
srv, err := daemon.New(name, description, daemon.SystemDaemon, dependencies...)
if err != nil {
errlog.Println("Error: ", err)
os.Exit(1)
Expand All @@ -155,7 +157,11 @@ Go daemon
*/
package daemon

import "strings"
import (
"errors"
"runtime"
"strings"
)

// Status constants.
const (
Expand Down Expand Up @@ -199,11 +205,58 @@ type Executable interface {
Run()
}

// Kind is type of the daemon
type Kind string

const (
// UserAgent is a user daemon that runs as the currently logged in user and
// stores its property list in the user’s individual LaunchAgents directory.
// In other words, per-user agents provided by the user. Valid for macOS only.
UserAgent Kind = "UserAgent"

// GlobalAgent is a user daemon that runs as the currently logged in user and
// stores its property list in the users' global LaunchAgents directory. In
// other words, per-user agents provided by the administrator. Valid for macOS
// only.
GlobalAgent Kind = "GlobalAgent"

// GlobalDaemon is a system daemon that runs as the root user and stores its
// property list in the global LaunchDaemons directory. In other words,
// system-wide daemons provided by the administrator. Valid for macOS only.
GlobalDaemon Kind = "GlobalDaemon"

// SystemDaemon is a system daemon that runs as the root user. In other words,
// system-wide daemons provided by the administrator. Valid for FreeBSD, Linux
// and Windows only.
SystemDaemon Kind = "SystemDaemon"
)

// New - Create a new daemon
//
// name: name of the service
//
// description: any explanation, what is the service, its purpose
func New(name, description string, dependencies ...string) (Daemon, error) {
return newDaemon(strings.Join(strings.Fields(name), "_"), description, dependencies)
//
// kind: what kind of daemon to create
func New(name, description string, kind Kind, dependencies ...string) (Daemon, error) {
switch runtime.GOOS {
case "darwin":
if kind == SystemDaemon {
return nil, errors.New("Invalid daemon kind specified")
}
case "freebsd":
if kind != SystemDaemon {
return nil, errors.New("Invalid daemon kind specified")
}
case "linux":
if kind != SystemDaemon {
return nil, errors.New("Invalid daemon kind specified")
}
case "windows":
if kind != SystemDaemon {
return nil, errors.New("Invalid daemon kind specified")
}
}

return newDaemon(strings.Join(strings.Fields(name), "_"), description, kind, dependencies)
}
37 changes: 28 additions & 9 deletions daemon_darwin.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by
// license that can be found in the LICENSE file.

Expand All @@ -8,6 +8,7 @@ package daemon
import (
"os"
"os/exec"
"os/user"
"path/filepath"
"regexp"
"text/template"
Expand All @@ -17,17 +18,30 @@ import (
type darwinRecord struct {
name string
description string
kind Kind
dependencies []string
}

func newDaemon(name, description string, dependencies []string) (Daemon, error) {
func newDaemon(name, description string, kind Kind, dependencies []string) (Daemon, error) {

return &darwinRecord{name, description, dependencies}, nil
return &darwinRecord{name, description, kind, dependencies}, nil
}

// Standard service path for system daemons
func (darwin *darwinRecord) servicePath() string {
return "/Library/LaunchDaemons/" + darwin.name + ".plist"
var path string

switch darwin.kind {
case UserAgent:
usr, _ := user.Current()
path = usr.HomeDir + "/Library/LaunchAgents/" + darwin.name + ".plist"
case GlobalAgent:
path = "/Library/LaunchAgents/" + darwin.name + ".plist"
case GlobalDaemon:
path = "/Library/LaunchDaemons/" + darwin.name + ".plist"
}

return path
}

// Is a service installed
Expand Down Expand Up @@ -66,7 +80,8 @@ func (darwin *darwinRecord) checkRunning() (string, bool) {
func (darwin *darwinRecord) Install(args ...string) (string, error) {
installAction := "Install " + darwin.description + ":"

if ok, err := checkPrivileges(); !ok {
ok, err := checkPrivileges()
if !ok && darwin.kind != UserAgent {
return installAction + failed, err
}

Expand Down Expand Up @@ -109,7 +124,8 @@ func (darwin *darwinRecord) Install(args ...string) (string, error) {
func (darwin *darwinRecord) Remove() (string, error) {
removeAction := "Removing " + darwin.description + ":"

if ok, err := checkPrivileges(); !ok {
ok, err := checkPrivileges()
if !ok && darwin.kind != UserAgent {
return removeAction + failed, err
}

Expand All @@ -128,7 +144,8 @@ func (darwin *darwinRecord) Remove() (string, error) {
func (darwin *darwinRecord) Start() (string, error) {
startAction := "Starting " + darwin.description + ":"

if ok, err := checkPrivileges(); !ok {
ok, err := checkPrivileges()
if !ok && darwin.kind != UserAgent {
return startAction + failed, err
}

Expand All @@ -151,7 +168,8 @@ func (darwin *darwinRecord) Start() (string, error) {
func (darwin *darwinRecord) Stop() (string, error) {
stopAction := "Stopping " + darwin.description + ":"

if ok, err := checkPrivileges(); !ok {
ok, err := checkPrivileges()
if !ok && darwin.kind != UserAgent {
return stopAction + failed, err
}

Expand All @@ -173,7 +191,8 @@ func (darwin *darwinRecord) Stop() (string, error) {
// Status - Get service status
func (darwin *darwinRecord) Status() (string, error) {

if ok, err := checkPrivileges(); !ok {
ok, err := checkPrivileges()
if !ok && darwin.kind != UserAgent {
return "", err
}

Expand Down
9 changes: 7 additions & 2 deletions daemon_freebsd.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2020 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 (
Expand All @@ -15,6 +19,7 @@ import (
type bsdRecord struct {
name string
description string
kind Kind
dependencies []string
}

Expand Down Expand Up @@ -66,8 +71,8 @@ func (bsd *bsdRecord) getCmd(cmd string) string {
}

// Get the daemon properly
func newDaemon(name, description string, dependencies []string) (Daemon, error) {
return &bsdRecord{name, description, dependencies}, nil
func newDaemon(name, description string, kind Kind, dependencies []string) (Daemon, error) {
return &bsdRecord{name, description, kind, dependencies}, nil
}

func execPath() (name string, err error) {
Expand Down
10 changes: 5 additions & 5 deletions daemon_linux.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by
// license that can be found in the LICENSE file.

Expand All @@ -10,15 +10,15 @@ import (
)

// Get the daemon properly
func newDaemon(name, description string, dependencies []string) (Daemon, error) {
func newDaemon(name, description string, kind Kind, dependencies []string) (Daemon, error) {
// newer subsystem must be checked first
if _, err := os.Stat("/run/systemd/system"); err == nil {
return &systemDRecord{name, description, dependencies}, nil
return &systemDRecord{name, description, kind, dependencies}, nil
}
if _, err := os.Stat("/sbin/initctl"); err == nil {
return &upstartRecord{name, description, dependencies}, nil
return &upstartRecord{name, description, kind, dependencies}, nil
}
return &systemVRecord{name, description, dependencies}, nil
return &systemVRecord{name, description, kind, dependencies}, nil
}

// Get executable path
Expand Down
3 changes: 2 additions & 1 deletion daemon_linux_systemd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by
// license that can be found in the LICENSE file.

Expand All @@ -16,6 +16,7 @@ import (
type systemDRecord struct {
name string
description string
kind Kind
dependencies []string
}

Expand Down
3 changes: 2 additions & 1 deletion daemon_linux_systemv.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by
// license that can be found in the LICENSE file.

Expand All @@ -16,6 +16,7 @@ import (
type systemVRecord struct {
name string
description string
kind Kind
dependencies []string
}

Expand Down
3 changes: 2 additions & 1 deletion daemon_linux_upstart.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by
// license that can be found in the LICENSE file.

Expand All @@ -16,6 +16,7 @@ import (
type upstartRecord struct {
name string
description string
kind Kind
dependencies []string
}

Expand Down
Loading

0 comments on commit 496d691

Please sign in to comment.