feat: Add support for service on Windows, Linux, and macOS

Introduce functionality to support the execution of the `act_runner` daemon as a service on various operating systems, including Windows, Linux, and macOS. This enhancement includes the ability to set the working directory using the `--working-directory` flag.

Details:
- The daemon can be installed and enabled with the command `act_runner daemon install`.
- The service can be stopped and uninstalled using `act_runner daemon uninstall`.
- The default working directory is set to the directory containing the `act_runner` executable.
- During the installation process (`act_runner daemon install`), the service checks for the existence of `.runner` and `config.yaml` files in the same directory. If found, these files are loaded into the service.

Note: Prior to running `act_runner daemon install`, ensure registration of `act_runner` with the command `act_runner register` to generate the required `.runner` file.
This commit is contained in:
cachito-worker
2023-11-28 01:33:27 +00:00
parent 91bfe4c186
commit c9ec99c632
5 changed files with 292 additions and 111 deletions

View File

@@ -6,7 +6,9 @@ package cmd
import (
"context"
"fmt"
"gitea.com/gitea/act_runner/internal/pkg/helpers"
"os"
"os/user"
"github.com/spf13/cobra"
@@ -23,9 +25,16 @@ func Execute(ctx context.Context) {
Version: ver.Version(),
SilenceUsage: true,
}
configFile := ""
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "Config file path")
workingDirectory := ""
rootCmd.PersistentFlags().StringVarP(&workingDirectory, "working-directory", "d", helpers.GetCurrentWorkingDirectory(), "Specify custom root directory where all data are stored")
daemonUser := ""
rootCmd.PersistentFlags().StringVarP(&daemonUser, "user", "u", getCurrentUserName(), "Specify user-name to run the daemon service as")
// ./act_runner register
var regArgs registerArgs
registerCmd := &cobra.Command{
@@ -45,11 +54,33 @@ func Execute(ctx context.Context) {
daemonCmd := &cobra.Command{
Use: "daemon",
Short: "Run as a runner daemon",
Args: cobra.MaximumNArgs(1),
RunE: runDaemon(ctx, &configFile),
Args: cobra.MaximumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
return daemon(cmd, ctx, &configFile, &workingDirectory, &daemonUser)
},
}
rootCmd.AddCommand(daemonCmd)
// ./act_runner daemon install
installDaemonCmd := &cobra.Command{
Use: "install",
Short: "Install the daemon",
RunE: func(cmd *cobra.Command, args []string) error {
return daemon(cmd, ctx, &configFile, &workingDirectory, &daemonUser)
},
}
daemonCmd.AddCommand(installDaemonCmd)
// ./act_runner daemon uninstall
uninstallDaemonCmd := &cobra.Command{
Use: "uninstall",
Short: "Uninstall the daemon",
RunE: func(cmd *cobra.Command, args []string) error {
return daemon(cmd, ctx, &configFile, &workingDirectory, &daemonUser)
},
}
daemonCmd.AddCommand(uninstallDaemonCmd)
// ./act_runner exec
rootCmd.AddCommand(loadExecCmd(ctx))
@@ -83,3 +114,11 @@ func Execute(ctx context.Context) {
os.Exit(1)
}
}
func getCurrentUserName() string {
user, _ := user.Current()
if user != nil {
return user.Username
}
return ""
}