Skip to content

Commit

Permalink
add basic service support (#163)
Browse files Browse the repository at this point in the history
* add basic service support

* macos cannot write to cwd

* improve service
  • Loading branch information
ChristopherHX authored Dec 4, 2023
1 parent 91e4838 commit 3fa1c65
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/kardianos/service v1.2.2 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.15.12 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/kardianos/service v1.2.2 h1:ZvePhAHfvo0A7Mftk/tEzqEZ7Q4lgnR8sGz4xu1YX60=
github.com/kardianos/service v1.2.2/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
Expand Down Expand Up @@ -297,6 +299,7 @@ golang.org/x/sys v0.0.0-20191115151921-52ab43148777/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
138 changes: 137 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/ChristopherHX/github-act-runner/protocol"
"github.com/ChristopherHX/github-act-runner/runnerconfiguration"
runnerCompat "github.com/ChristopherHX/github-act-runner/runnerconfiguration/compat"
"github.com/kardianos/service"
"github.com/nektos/act/pkg/container"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -127,6 +128,10 @@ func (run *RunRunner) Run() int {
}
}
}()
return run.RunWithContext(listenerctx, ctx)
}

func (run *RunRunner) RunWithContext(listenerctx context.Context, ctx context.Context) int {
var settings *runnerconfiguration.RunnerSettings
var err error
if run.JITConfig != "" {
Expand Down Expand Up @@ -171,6 +176,43 @@ func (i *interactive) GetMultiSelectInput(prompt string, options []string) []str
return GetMultiSelectInput(prompt, options)
}

type RunRunnerSvc struct {
stop func()
wait chan error
}

func (svc *RunRunnerSvc) Start(s service.Service) error {
runner := &RunRunner{}

ctx, cancel := context.WithCancel(context.Background())
listenerctx, cancelListener := context.WithCancel(context.Background())
svc.stop = func() {
cancelListener()
}
svc.wait = make(chan error)
go func() {
defer cancelListener()
defer cancel()
defer close(svc.wait)
code := runner.RunWithContext(listenerctx, ctx)
if code != 0 {
svc.wait <- fmt.Errorf("runner failed with exit code %v", code)
} else {
svc.wait <- nil
}
s.Stop()
}()
return nil
}

func (svc *RunRunnerSvc) Stop(s service.Service) error {
svc.stop()
if err, ok := <-svc.wait; ok && err != nil {
return err
}
return nil
}

func main() {
config := &runnerconfiguration.ConfigureRunner{}
run := &RunRunner{}
Expand Down Expand Up @@ -351,11 +393,105 @@ func main() {
<-ccontext.Done()
},
}
var cmdSvc = &cobra.Command{
Use: "svc",
}
wd, _ := os.Getwd()
svcConfig := &service.Config{
Name: "github-act-runner",
DisplayName: "GitHub Act Runner",
Description: "Cross platform GitHub Actions Runner.",
Arguments: []string{"svc", "run", "--working-directory", wd},
}
if runtime.GOOS == "darwin" {
if path, ok := os.LookupEnv("PATH"); ok {
svcConfig.EnvVars = map[string]string{
"PATH": path,
}
}
svcConfig.Option = service.KeyValue{
"KeepAlive": true,
"RunAtLoad": true,
"UserService": os.Getuid() != 0,
}
}
svcRun := &cobra.Command{
Use: "run",
RunE: func(cmd *cobra.Command, args []string) error {
err := os.Chdir(wd)
if err != nil {
return err
}
stdOut, err := os.OpenFile("github-act-runner-log.txt", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0777)
if err == nil {
os.Stdout = stdOut
defer os.Stdout.Close()
}
stdErr, err := os.OpenFile("github-act-runner-log-error.txt", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0777)
if err == nil {
os.Stderr = stdErr
defer os.Stderr.Close()
}

svc, err := service.New(&RunRunnerSvc{}, svcConfig)

if err != nil {
return err
}
return svc.Run()
},
}
svcRun.Flags().StringVar(&wd, "working-directory", wd, "path to the working directory of the runner config")
svcInstall := &cobra.Command{
Use: "install",
RunE: func(cmd *cobra.Command, args []string) error {
svc, err := service.New(&RunRunnerSvc{}, svcConfig)

if err != nil {
return err
}
return svc.Install()
},
}
svcUninstall := &cobra.Command{
Use: "uninstall",
RunE: func(cmd *cobra.Command, args []string) error {
svc, err := service.New(&RunRunnerSvc{}, svcConfig)

if err != nil {
return err
}
return svc.Uninstall()
},
}
svcStart := &cobra.Command{
Use: "start",
RunE: func(cmd *cobra.Command, args []string) error {
svc, err := service.New(&RunRunnerSvc{}, svcConfig)

if err != nil {
return err
}
return svc.Start()
},
}
svcStop := &cobra.Command{
Use: "stop",
RunE: func(cmd *cobra.Command, args []string) error {
svc, err := service.New(&RunRunnerSvc{}, svcConfig)

if err != nil {
return err
}
return svc.Stop()
},
}
cmdSvc.AddCommand(svcInstall, svcStart, svcStop, svcRun, svcUninstall)

var rootCmd = &cobra.Command{
Use: "github-act-runner",
Version: version,
}
rootCmd.AddCommand(cmdConfigure, cmdRun, cmdRemove, cmdWorker)
rootCmd.AddCommand(cmdConfigure, cmdRun, cmdRemove, cmdWorker, cmdSvc)
rootCmd.Execute()
}

0 comments on commit 3fa1c65

Please sign in to comment.