Allow to explicitly setup system logging for the service

parent 01804eb8
......@@ -8,6 +8,7 @@ import (
"github.com/ayufan/golang-kardianos-service"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"gitlab.com/gitlab-org/gitlab-runner/common"
"gitlab.com/gitlab-org/gitlab-runner/helpers"
"gitlab.com/gitlab-org/gitlab-runner/helpers/service"
......@@ -115,7 +116,11 @@ func getServiceArguments(c *cli.Context) (arguments []string) {
arguments = append(arguments, "--service", sn)
}
arguments = append(arguments, "--syslog")
syslog := !c.IsSet("syslog") || c.Bool("syslog")
if syslog {
arguments = append(arguments, "--syslog")
}
return
}
......@@ -183,16 +188,18 @@ func RunServiceControl(c *cli.Context) {
}
}
func init() {
flags := []cli.Flag{
func getFlags() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
Name: "service, n",
Value: defaultServiceName,
Usage: "Specify service name to use",
},
}
}
installFlags := flags
func getInstallFlags() []cli.Flag {
installFlags := getFlags()
installFlags = append(installFlags, cli.StringFlag{
Name: "working-directory, d",
Value: helpers.GetCurrentWorkingDirectory(),
......@@ -203,6 +210,10 @@ func init() {
Value: getDefaultConfigFile(),
Usage: "Specify custom config file",
})
installFlags = append(installFlags, cli.BoolFlag{
Name: "syslog",
Usage: "Setup system logging integration",
})
if runtime.GOOS == "windows" {
installFlags = append(installFlags, cli.StringFlag{
......@@ -223,6 +234,13 @@ func init() {
})
}
return installFlags
}
func init() {
flags := getFlags()
installFlags := getInstallFlags()
common.RegisterCommand(cli.Command{
Name: "install",
Usage: "install service",
......
package commands
import (
"fmt"
"os"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
"gitlab.com/gitlab-org/gitlab-runner/helpers"
"gitlab.com/gitlab-org/gitlab-runner/helpers/service/mocks"
)
......@@ -32,3 +36,105 @@ func TestServiceLogHook(t *testing.T) {
mockServiceLogger.AssertExpectations(t)
}
func newTestGetServiceArgumentsCommand(t *testing.T, expectedArgs []string) func(*cli.Context) {
return func(c *cli.Context) {
arguments := getServiceArguments(c)
for _, arg := range expectedArgs {
assert.Contains(t, arguments, arg)
}
}
}
func testServiceCommandRun(t *testing.T, command func(*cli.Context), args ...string) {
app := cli.NewApp()
app.Commands = []cli.Command{
{
Name: "test-command",
Action: command,
Flags: getInstallFlags(),
},
}
args = append([]string{"binary", "test-command"}, args...)
app.Run(args)
}
type getServiceArgumentsTestCase struct {
cliFlags []string
expectedArgs []string
}
func TestGetServiceArguments(t *testing.T) {
tests := []getServiceArgumentsTestCase{
{
expectedArgs: []string{
"--working-directory", helpers.GetCurrentWorkingDirectory(),
"--config", getDefaultConfigFile(),
"--service", "gitlab-runner",
"--syslog",
},
},
{
cliFlags: []string{
"--config", "/tmp/config.toml",
},
expectedArgs: []string{
"--working-directory", helpers.GetCurrentWorkingDirectory(),
"--config", "/tmp/config.toml",
"--service", "gitlab-runner",
"--syslog",
},
},
{
cliFlags: []string{
"--working-directory", "/tmp",
},
expectedArgs: []string{
"--working-directory", "/tmp",
"--config", getDefaultConfigFile(),
"--service", "gitlab-runner",
"--syslog",
},
},
{
cliFlags: []string{
"--service", "gitlab-runner-service-name",
},
expectedArgs: []string{
"--working-directory", helpers.GetCurrentWorkingDirectory(),
"--config", getDefaultConfigFile(),
"--service", "gitlab-runner-service-name",
"--syslog",
},
},
{
cliFlags: []string{
"--syslog=true",
},
expectedArgs: []string{
"--working-directory", helpers.GetCurrentWorkingDirectory(),
"--config", getDefaultConfigFile(),
"--service", "gitlab-runner",
"--syslog",
},
},
{
cliFlags: []string{
"--syslog=false",
},
expectedArgs: []string{
"--working-directory", helpers.GetCurrentWorkingDirectory(),
"--config", getDefaultConfigFile(),
"--service", "gitlab-runner",
},
},
}
for id, testCase := range tests {
t.Run(fmt.Sprintf("case-%d", id), func(t *testing.T) {
testServiceCommandRun(t, newTestGetServiceArgumentsCommand(t, testCase.expectedArgs), testCase.cliFlags...)
})
}
}
......@@ -306,10 +306,10 @@ service. Use them to install, uninstall, start and stop the runner service.
All service related commands accept these arguments:
| Parameter | Default | Description |
|-----------|---------|-------------|
| `--service` | `gitlab-runner` | Specify custom service name |
| `--config` | See the [configuration file](#configuration-file) | Specify a custom configuration file to use |
| Parameter | Default | Description |
|-------------|---------------------------------------------------|-------------|
| `--service` | `gitlab-runner` | Specify custom service name |
| `--config` | See the [configuration file](#configuration-file) | Specify a custom configuration file to use |
### gitlab-runner install
......@@ -319,12 +319,12 @@ arguments depending on which system it's run on.
When run on **Windows** or as super-user, it accepts the `--user` flag which
allows you to drop privileges of builds run with the **shell** executor.
| Parameter | Default | Description |
|-----------|---------|-------------|
| `--service` | `gitlab-runner` | Specify a custom name for the Runner |
| Parameter | Default | Description |
|-----------------------|-----------------------|-------------|
| `--syslog` | `true` | Specify if the service should integrate with system logging service |
| `--working-directory` | the current directory | Specify the root directory where all data will be stored when builds will be run with the **shell** executor |
| `--user` | `root` | Specify the user which will be used to execute builds |
| `--password` | none | Specify the password for the user that will be used to execute the builds |
| `--user` | `root` | Specify the user which will be used to execute builds |
| `--password` | none | Specify the password for the user that will be used to execute the builds |
### gitlab-runner uninstall
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment