diff --git a/go.mod b/go.mod index 2a6f1c1..49f3473 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,9 @@ module github.com/codemodify/systemkit-service -go 1.13 +go 1.14 require ( + github.com/codemodify/SystemKit v1.7.4 github.com/codemodify/systemkit-helpers v1.7.7 github.com/codemodify/systemkit-logging v1.7.6 golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd diff --git a/go.sum b/go.sum index 74b5b83..8b9349d 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,6 @@ +github.com/codemodify/SystemKit v1.7.4 h1:glqH7m+qxEQoUXWuLXhzHHLgV9sYYUPM1rRWVz66tRg= +github.com/codemodify/SystemKit v1.7.4/go.mod h1:KKn3HcCBZURVS8AbQanXxsyUbHsGhnIinVpAkSPVzXk= +github.com/codemodify/systemkit-cryptography v1.0.0/go.mod h1:xpC1R/KccPhPrmY9Vs5lcOrESMlAZfJ/xrbqrfuFve0= github.com/codemodify/systemkit-helpers v1.7.7 h1:ITFCHhS6hSxqNhqcqwVfpzKG9JOEcX3E+4AO7uo1yXI= github.com/codemodify/systemkit-helpers v1.7.7/go.mod h1:5jkrQySFtJYZnUnudYeuAYzbunPsAcDlQmW/kMa6rT0= github.com/codemodify/systemkit-logging v1.7.6 h1:B43qtBemRL85bwQPsdwtspOENYB1OgHPEnZIfhB/88I= @@ -5,14 +8,19 @@ github.com/codemodify/systemkit-logging v1.7.6/go.mod h1:CgHyiLfps+2ALdwXfTKTShM github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davidmz/go-pageant v1.0.1 h1:kzCt64aTc557QYvy5VBSN1pVWO/tNAcqzwlpUsZ8IEE= github.com/davidmz/go-pageant v1.0.1/go.mod h1:WWOKE/93DhgsPq15jaipH4fVY+MLKKWH4Yku5Ei92rE= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= +github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= +github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.6 h1:6Su7aK7lXmJ/U79bYtBjLNaha4Fs1Rg9plHpcH+vvnE= github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= @@ -20,19 +28,23 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pkg/sftp v1.11.0 h1:4Zv0OGbpkg4yNuUtH0s8rvoYxRCNyT29NVUo6pgPmxI= github.com/pkg/sftp v1.11.0/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/sfreiberg/simplessh v0.0.0-20180301191542-495cbb862a9c h1:7Q+2oF0uBoLEV+j13E3/xUkPkI7f+sFNPZOPo2jmrWk= github.com/sfreiberg/simplessh v0.0.0-20180301191542-495cbb862a9c/go.mod h1:sB7d6wQapoRM+qx5MgQYB6JVHtel4YHRr0NXXCkXiwQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200208060501-ecb85df21340/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59 h1:3zb4D3T4G8jdExgVU/95+vQXfpEPiMdCaZgmGVxjNHM= golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/readme.md b/readme.md index 070ee81..863b1c2 100644 --- a/readme.md +++ b/readme.md @@ -1,9 +1,22 @@ # ![](https://fonts.gstatic.com/s/i/materialicons/bookmarks/v4/24px.svg) Service -[![GoDoc](https://godoc.org/github.com/codemodify/SystemKit?status.svg)](https://godoc.org/github.com/codemodify/SystemKit) -[![0-License](https://img.shields.io/badge/license-0--license-brightgreen)](https://github.com/codemodify/TheFreeLicense) -[![Go Report Card](https://goreportcard.com/badge/github.com/codemodify/SystemKit)](https://goreportcard.com/report/github.com/codemodify/SystemKit) -[![Test Status](https://github.com/danawoodman/systemservice/workflows/Test/badge.svg)](https://github.com/danawoodman/systemservice/actions) -![code size](https://img.shields.io/github/languages/code-size/codemodify/SystemKit?style=flat-square) +[![](https://img.shields.io/github/v/release/codemodify/systemkit-service?style=flat-square)](https://github.com/codemodify/systemkit-service/releases/latest) +![](https://img.shields.io/github/languages/code-size/codemodify/systemkit-service?style=flat-square) +![](https://img.shields.io/github/last-commit/codemodify/systemkit-service?style=flat-square) +[![](https://img.shields.io/badge/license-0--license-brightgreen?style=flat-square)](https://github.com/codemodify/TheFreeLicense) + +![](https://img.shields.io/github/workflow/status/codemodify/systemkit-service/qa?style=flat-square) +![](https://img.shields.io/github/issues/codemodify/systemkit-service?style=flat-square) +[![](https://goreportcard.com/badge/github.com/codemodify/systemkit-service?style=flat-square)](https://goreportcard.com/report/github.com/codemodify/systemkit-service) + +[![](https://img.shields.io/badge/godoc-reference-brightgreen?style=flat-square)](https://godoc.org/github.com/codemodify/systemkit-service) +![](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square) +![](https://img.shields.io/gitter/room/codemodify/systemkit-service?style=flat-square) + +![](https://img.shields.io/github/contributors/codemodify/systemkit-service?style=flat-square) +![](https://img.shields.io/github/stars/codemodify/systemkit-service?style=flat-square) +![](https://img.shields.io/github/watchers/codemodify/systemkit-service?style=flat-square) +![](https://img.shields.io/github/forks/codemodify/systemkit-service?style=flat-square) + #### Robust Cross platform Create/Start/Stop/Delete system or user service @@ -12,4 +25,58 @@ # ![](https://fonts.gstatic.com/s/i/materialicons/bookmarks/v4/24px.svg) Install ```go go get github.com/codemodify/systemkit-service -``` \ No newline at end of file +``` + +# ![](https://fonts.gstatic.com/s/i/materialicons/bookmarks/v4/24px.svg) API + +  |   +--- | --- +service := Service.New() | Create a new system service +service.Install(false) | Install a new system service +service.Install(true) | Install a new system service and start +service.Start() | Start system service +service.Restart() | Restart system service +service.Status() | System service status +service.Stop() | Stop system service +service.Uninstall() | Uninstall system service + + +# ![](https://fonts.gstatic.com/s/i/materialicons/bookmarks/v4/24px.svg) Usage +```go +package main + +import ( + "fmt" + + "https://github.com/codemodify/systemkit-service/" +) + +func main() { + +// Create service definition + + usr, _ := user.Current() + service := Service.New(Service.Command{ + Name: "MY_SERVICE", + DisplayLabel: "My Service", + Description: "This service is a test service", + DocumentationURL: "", + Executable: usr.HomeDir + "/Downloads/service.sh"), + Args: []string{""}, + WorkingDirectory: usr.HomeDir, + }) + +// Instal and start + + err := service.Install(true) + if err != nil { + fmt.Println(err.Error()) + } +} + +// Done +``` + +--- + +> `IMPORTANT NOTE:`
If this is executed as `SUDO` then a system service will be created instead of user service diff --git a/samples/MacOSX/main.go b/samples/MacOSX/main.go new file mode 100644 index 0000000..7dfd01b --- /dev/null +++ b/samples/MacOSX/main.go @@ -0,0 +1,29 @@ +// +build darwin + +package main + +import ( + "fmt" + "os/user" + + "github.com/codemodify/SystemKit/Service" +) + +func main() { + usr, _ := user.Current() + + service := Service.New(Service.Command{ + Name: "MY_SERVICE", + DisplayLabel: "My Service", + Description: "This service is a test service", + DocumentationURL: "", + Executable: usr.HomeDir + "/Downloads/service.sh", + Args: []string{""}, + WorkingDirectory: usr.HomeDir, + }) + + err := service.Install(true) + if err != nil { + fmt.Println(err.Error()) + } +} diff --git a/samples/MacOSX/readme.md b/samples/MacOSX/readme.md new file mode 100644 index 0000000..f87e0a8 --- /dev/null +++ b/samples/MacOSX/readme.md @@ -0,0 +1,74 @@ +# ![](https://fonts.gstatic.com/s/i/materialicons/label_important/v4/24px.svg) Service +[![GoDoc](https://godoc.org/github.com/codemodify/SystemKit?status.svg)](https://godoc.org/github.com/codemodify/SystemKit) +[![0-License](https://img.shields.io/badge/license-0--license-brightgreen)](https://github.com/codemodify/TheFreeLicense) +[![Go Report Card](https://goreportcard.com/badge/github.com/codemodify/SystemKit)](https://goreportcard.com/report/github.com/codemodify/SystemKit) +[![Test Status](https://github.com/danawoodman/systemservice/workflows/Test/badge.svg)](https://github.com/danawoodman/systemservice/actions) +![code size](https://img.shields.io/github/languages/code-size/codemodify/SystemKit?style=flat-square) + +# Cross platform

`CREATE/START/STOP/UNINSTALL` SYSTEM / USER SERVICE +## How to use? + +# ![](https://fonts.gstatic.com/s/i/materialicons/label_important/v4/24px.svg) MacOS ![](https://img.icons8.com/ios-filled/30/000000/mac-os.png) + - Download a sample service definiton from here and save/extract to your `~/Downloads` folder: + +> `https://github.com/codemodify/systemkit-service/samples/service.sh` + +```go +package main + +import ( + "fmt" + + "https://github.com/codemodify/systemkit-service/" +) + +func main() { + +// Create service definition + + usr, _ := user.Current() + service := Service.New(Service.Command{ + Name: "MY_SERVICE", + DisplayLabel: "My Service", + Description: "This service is a test service", + DocumentationURL: "", + Executable: usr.HomeDir + "/Downloads/service.sh"), + Args: []string{""}, + WorkingDirectory: usr.HomeDir, + }) + +// Instal and start + + err := service.Install(true) + if err != nil { + fmt.Println(err.Error()) + } +} + +// Done +``` + +--- +#### `INSTALL WITHOUT START ` +```go + err := service.Install(false) + if err != nil { + fmt.Println(err.Error()) + +``` +--- +#### `STOP SERVICE ` +```go + err := service.Stop + if err != nil { + fmt.Println(err.Error()) +``` +#### `UNINSTALL SERVICE ` +```go + err := service.Uninstall + if err != nil { + fmt.Println(err.Error()) +``` +--- +#### `IMPORTANT NOTE` +### If this is executed as `SUDO` then a system service will be created instead of user service diff --git a/samples/MacOSX/service.sh b/samples/MacOSX/service.sh new file mode 100755 index 0000000..85697f1 --- /dev/null +++ b/samples/MacOSX/service.sh @@ -0,0 +1,6 @@ +#!/bin/bash +NEXT_WAIT_TIME=0 +until say 'done' && [ $NEXT_WAIT_TIME -eq 4 ]; do +$(( NEXT_WAIT_TIME++ )) +sleep 5s +done \ No newline at end of file diff --git a/system-service_darwin.go b/system-service_darwin.go index bae7666..856f0cf 100644 --- a/system-service_darwin.go +++ b/system-service_darwin.go @@ -106,7 +106,7 @@ func (thisRef MacOSService) Install(start bool) error { // Start - func (thisRef MacOSService) Start() error { - // 1. + output, _ := runLaunchCtlCommand("load", "-w", thisRef.FilePath()) if strings.Contains(output, "No such file or directory") { return ErrServiceDoesNotExist diff --git a/tests-as-root/main.go b/tests-as-root/main.go deleted file mode 100644 index 16f5c92..0000000 --- a/tests-as-root/main.go +++ /dev/null @@ -1,24 +0,0 @@ -package main - -import ( - "fmt" - - // helpersErrors "github.com/codemodify/systemkit-helpers" - "github.com/codemodify/systemkit-service/tests" -) - -func main() { - service := tests.CreateRemoteitService() - - // err := service.Stop() - // if helpersErrors.Is(err, Service.ErrServiceDoesNotExist) { - // // this is a good thing - // } else if err != nil { - // fmt.Println(err.Error()) - // } - - err := service.Uninstall() - if err != nil { - fmt.Println(err.Error()) - } -} diff --git a/tests/helpers_unix.go b/tests/helpers_unix.go index 785baf5..72751bb 100644 --- a/tests/helpers_unix.go +++ b/tests/helpers_unix.go @@ -38,17 +38,3 @@ func createRandomService() service.SystemService { RunAsUser: "user", }) } - -func CreateRemoteitService() service.SystemService { - return service.New(service.Command{ - Name: "it.remote.cli", - DisplayLabel: "it.remote.cli", - Description: "it.remote.cli", - DocumentationURL: "", - Executable: "/Users/nicolae/Downloads/remoteit_mac-osx_x86_64", - Args: []string{"watch", "-v", "-c", "/etc/remoteit/config.json"}, - WorkingDirectory: "", - StdOutPath: "null", - RunAsUser: "user", - }) -} diff --git a/tests/install_test.go b/tests/install_test.go index 39b644b..f0b6e34 100644 --- a/tests/install_test.go +++ b/tests/install_test.go @@ -5,7 +5,7 @@ import ( ) func Test_install(t *testing.T) { - service := CreateRemoteitService() + service := createService() err := service.Install(false) if err != nil { diff --git a/tests/restart_test.go b/tests/restart_test.go index 8bf316a..2efa03f 100644 --- a/tests/restart_test.go +++ b/tests/restart_test.go @@ -5,7 +5,7 @@ import ( ) func Test_restart(t *testing.T) { - service := CreateRemoteitService() + service := createService() err := service.Restart() if err != nil { diff --git a/tests/start_test.go b/tests/start_test.go index 74734f6..cd1e402 100644 --- a/tests/start_test.go +++ b/tests/start_test.go @@ -5,7 +5,7 @@ import ( ) func Test_start(t *testing.T) { - service := CreateRemoteitService() + service := createService() err := service.Start() if err != nil { diff --git a/tests/status_test.go b/tests/status_test.go index 9bd0d98..7f83ed0 100644 --- a/tests/status_test.go +++ b/tests/status_test.go @@ -6,7 +6,7 @@ import ( ) func Test_status(t *testing.T) { - service := CreateRemoteitService() + service := createService() serviceStatus := service.Status() if serviceStatus.Error != nil { diff --git a/tests/stop_test.go b/tests/stop_test.go index 52921d0..20cddc6 100644 --- a/tests/stop_test.go +++ b/tests/stop_test.go @@ -8,7 +8,7 @@ import ( ) func Test_stop(t *testing.T) { - systemService := CreateRemoteitService() + systemService := createService() err := systemService.Stop() if helpersErrors.Is(err, service.ErrServiceDoesNotExist) { diff --git a/tests/uninstall_test.go b/tests/uninstall_test.go index d0da1fe..d9198ef 100644 --- a/tests/uninstall_test.go +++ b/tests/uninstall_test.go @@ -5,7 +5,7 @@ import ( ) func Test_uninstall(t *testing.T) { - service := CreateRemoteitService() + service := createService() err := service.Uninstall() if err != nil {