Skip to content
Snippets Groups Projects
Commit 69fa6eda authored by Kamil Trzcinski's avatar Kamil Trzcinski
Browse files

Make it possible to run on not supported system by providing SimpleService implementation

parent 909b5b6b
No related branches found
No related tags found
No related merge requests found
...@@ -55,6 +55,11 @@ test: ...@@ -55,6 +55,11 @@ test:
# Running tests... # Running tests...
go test ./... -cover go test ./... -cover
mocks: FORCE
go get github.com/vektra/mockery/.../
rm -rf mocks/
mockery -dir=$(GOPATH)/src/github.com/ayufan/golang-kardianos-service -name=Interface
test-docker: test-docker:
make test-docker-image IMAGE=centos:6 TYPE=rpm make test-docker-image IMAGE=centos:6 TYPE=rpm
make test-docker-image IMAGE=centos:7 TYPE=rpm make test-docker-image IMAGE=centos:7 TYPE=rpm
......
// +build linux darwin // +build linux darwin freebsd
package commands package commands
......
...@@ -18,6 +18,7 @@ import ( ...@@ -18,6 +18,7 @@ import (
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common" "gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers" "gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers"
"math" "math"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers/service"
) )
type RunnerHealth struct { type RunnerHealth struct {
...@@ -388,7 +389,7 @@ func (c *RunCommand) Execute(context *cli.Context) { ...@@ -388,7 +389,7 @@ func (c *RunCommand) Execute(context *cli.Context) {
Arguments: []string{"run"}, Arguments: []string{"run"},
} }
service, err := service.New(c, svcConfig) service, err := service_helpers.New(c, svcConfig)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
......
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers" "gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers"
"os" "os"
"runtime" "runtime"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers/service"
) )
const ( const (
...@@ -135,7 +136,7 @@ func RunServiceControl(c *cli.Context) { ...@@ -135,7 +136,7 @@ func RunServiceControl(c *cli.Context) {
svcConfig.Arguments = append(svcConfig.Arguments, "--syslog") svcConfig.Arguments = append(svcConfig.Arguments, "--syslog")
s, err := service.New(&NullService{}, svcConfig) s, err := service_helpers.New(&NullService{}, svcConfig)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
......
...@@ -26,6 +26,11 @@ wget https://storage.googleapis.com/golang/go1.4.2.darwin-amd64-osx10.8.pkg ...@@ -26,6 +26,11 @@ wget https://storage.googleapis.com/golang/go1.4.2.darwin-amd64-osx10.8.pkg
open go*-*.pkg open go*-*.pkg
``` ```
### For FreeBSD
```
pkg install go-1.4.2
```
## 2. Configure Go ## 2. Configure Go
Add to `.profile` or `.bash_profile`: Add to `.profile` or `.bash_profile`:
......
package service_helpers
import (
log "github.com/Sirupsen/logrus"
"github.com/ayufan/golang-kardianos-service"
)
func New(i service.Interface, c *service.Config) (service.Service, error) {
s, err := service.New(i, c)
if err == service.ErrNoServiceSystemDetected {
log.Warningln("No service system detected. Some features may not work!")
return &SimpleService{
i: i,
c: c,
}, nil
}
return s, err
}
package service_helpers
import (
service "github.com/ayufan/golang-kardianos-service"
"os"
"syscall"
"os/signal"
"errors"
)
var (
// ErrNotSupported is returned when specific feature is not supported.
ErrNotSupported = errors.New("Not supported.")
)
type SimpleService struct {
i service.Interface
c *service.Config
}
// Run should be called shortly after the program entry point.
// After Interface.Stop has finished running, Run will stop blocking.
// After Run stops blocking, the program must exit shortly after.
func (s *SimpleService) Run() (err error) {
err = s.i.Start(s)
if err != nil {
return err
}
sigChan := make(chan os.Signal, 3)
signal.Notify(sigChan, syscall.SIGTERM, os.Interrupt)
<-sigChan
return s.i.Stop(s)
}
// Start signals to the OS service manager the given service should start.
func (s *SimpleService) Start() error {
return service.ErrNoServiceSystemDetected
}
// Stop signals to the OS service manager the given service should stop.
func (s *SimpleService) Stop() error {
return ErrNotSupported
}
// Restart signals to the OS service manager the given service should stop then start.
func (s *SimpleService) Restart() error {
return ErrNotSupported
}
// Install setups up the given service in the OS service manager. This may require
// greater rights. Will return an error if it is already installed.
func (s *SimpleService) Install() error {
return ErrNotSupported
}
// Uninstall removes the given service from the OS service manager. This may require
// greater rights. Will return an error if the service is not present.
func (s *SimpleService) Uninstall() error {
return ErrNotSupported
}
// Opens and returns a system logger. If the user program is running
// interactively rather then as a service, the returned logger will write to
// os.Stderr. If errs is non-nil errors will be sent on errs as well as
// returned from Logger's functions.
func (s *SimpleService) Logger(errs chan<- error) (service.Logger, error) {
return service.ConsoleLogger, nil
}
// SystemLogger opens and returns a system logger. If errs is non-nil errors
// will be sent on errs as well as returned from Logger's functions.
func (s *SimpleService) SystemLogger(errs chan<- error) (service.Logger, error) {
return nil, ErrNotSupported
}
// String displays the name of the service. The display name if present,
// otherwise the name.
func (s *SimpleService) String() string {
return "SimpleService"
}
package service_helpers
import (
"testing"
"github.com/golang/mock/gomock"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/mocks"
"errors"
"github.com/stretchr/testify/assert"
)
var ExampleError = errors.New("example error")
func TestStart(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mi := &mocks.Interface{}
s := &SimpleService{i: mi}
mi.On("Start", s).Return(ExampleError)
err := s.Run()
assert.Equal(t, err, ExampleError)
mi.AssertExpectations(t)
}
package mocks
import "github.com/ayufan/golang-kardianos-service"
import "github.com/stretchr/testify/mock"
type Interface struct {
mock.Mock
}
func (m *Interface) Start(s service.Service) error {
ret := m.Called(s)
r0 := ret.Error(0)
return r0
}
func (m *Interface) Stop(s service.Service) error {
ret := m.Called(s)
r0 := ret.Error(0)
return r0
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment