-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
- Loading branch information
Showing
8 changed files
with
419 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
inlets-svc.exe | ||
/bin/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2012 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build windows | ||
|
||
package main | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
// BUG(brainman): MessageBeep Windows api is broken on Windows 7, | ||
// so this example does not beep when runs as service on Windows 7. | ||
|
||
var ( | ||
beepFunc = syscall.MustLoadDLL("user32.dll").MustFindProc("MessageBeep") | ||
) | ||
|
||
func beep() { | ||
beepFunc.Call(0xffffffff) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/inlets/inlets-svc | ||
|
||
go 1.13 | ||
|
||
require golang.org/x/sys v0.0.0-20210317091845-390168757d9c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
golang.org/x/sys v0.0.0-20210317091845-390168757d9c h1:WGyvPg8lhdtSkb8BiYWdtPlLSommHOmJHFvzWODI7BQ= | ||
golang.org/x/sys v0.0.0-20210317091845-390168757d9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2012 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build windows | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"golang.org/x/sys/windows/svc/eventlog" | ||
"golang.org/x/sys/windows/svc/mgr" | ||
) | ||
|
||
func exePath() (string, error) { | ||
prog := os.Args[0] | ||
p, err := filepath.Abs(prog) | ||
if err != nil { | ||
return "", err | ||
} | ||
fi, err := os.Stat(p) | ||
if err == nil { | ||
if !fi.Mode().IsDir() { | ||
return p, nil | ||
} | ||
err = fmt.Errorf("%s is directory", p) | ||
} | ||
if filepath.Ext(p) == "" { | ||
p += ".exe" | ||
fi, err := os.Stat(p) | ||
if err == nil { | ||
if !fi.Mode().IsDir() { | ||
return p, nil | ||
} | ||
err = fmt.Errorf("%s is directory", p) | ||
} | ||
} | ||
return "", err | ||
} | ||
|
||
func installService(name, desc string) error { | ||
exepath, err := exePath() | ||
if err != nil { | ||
return err | ||
} | ||
m, err := mgr.Connect() | ||
if err != nil { | ||
return err | ||
} | ||
defer m.Disconnect() | ||
s, err := m.OpenService(name) | ||
if err == nil { | ||
s.Close() | ||
return fmt.Errorf("service %s already exists", name) | ||
} | ||
s, err = m.CreateService(name, exepath, mgr.Config{DisplayName: desc}, "is", "auto-started") | ||
if err != nil { | ||
return err | ||
} | ||
defer s.Close() | ||
err = eventlog.InstallAsEventCreate(name, eventlog.Error|eventlog.Warning|eventlog.Info) | ||
if err != nil { | ||
s.Delete() | ||
return fmt.Errorf("SetupEventLogSource() failed: %s", err) | ||
} | ||
return nil | ||
} | ||
|
||
func removeService(name string) error { | ||
m, err := mgr.Connect() | ||
if err != nil { | ||
return err | ||
} | ||
defer m.Disconnect() | ||
s, err := m.OpenService(name) | ||
if err != nil { | ||
return fmt.Errorf("service %s is not installed", name) | ||
} | ||
defer s.Close() | ||
err = s.Delete() | ||
if err != nil { | ||
return err | ||
} | ||
err = eventlog.Remove(name) | ||
if err != nil { | ||
return fmt.Errorf("RemoveEventLogSource() failed: %s", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright 2012 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build windows | ||
|
||
// Example service program that beeps. | ||
// | ||
// The program demonstrates how to create Windows service and | ||
// install / remove it on a computer. It also shows how to | ||
// stop / start / pause / continue any service, and how to | ||
// write to event log. It also shows how to use debug | ||
// facilities available in debug package. | ||
// | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"strings" | ||
|
||
"golang.org/x/sys/windows/svc" | ||
) | ||
|
||
const svcName = "inlets-client" | ||
const svcDesc = "Inlets client" | ||
|
||
func usage(errmsg string) { | ||
fmt.Fprintf(os.Stderr, | ||
"%s\n\n"+ | ||
"usage: %s <command>\n"+ | ||
" where <command> is one of\n"+ | ||
" install, remove, debug, start, stop, pause or continue.\n", | ||
errmsg, os.Args[0]) | ||
os.Exit(2) | ||
} | ||
|
||
func main() { | ||
|
||
inService, err := svc.IsWindowsService() | ||
if err != nil { | ||
log.Fatalf("failed to determine if we are running in service: %v", err) | ||
} | ||
if inService { | ||
runService(svcName, false) | ||
return | ||
} | ||
|
||
if len(os.Args) < 2 { | ||
usage("no command specified") | ||
} | ||
|
||
cmd := strings.ToLower(os.Args[1]) | ||
switch cmd { | ||
case "debug": | ||
runService(svcName, true) | ||
return | ||
case "install": | ||
err = installService(svcName, svcDesc) | ||
case "remove": | ||
err = removeService(svcName) | ||
case "start": | ||
err = startService(svcName) | ||
case "stop": | ||
err = controlService(svcName, svc.Stop, svc.Stopped) | ||
case "pause": | ||
err = controlService(svcName, svc.Pause, svc.Paused) | ||
case "continue": | ||
err = controlService(svcName, svc.Continue, svc.Running) | ||
default: | ||
usage(fmt.Sprintf("invalid command %s", cmd)) | ||
} | ||
if err != nil { | ||
log.Fatalf("failed to %s %s: %v", cmd, svcName, err) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2012 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build windows | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"golang.org/x/sys/windows/svc" | ||
"golang.org/x/sys/windows/svc/mgr" | ||
) | ||
|
||
func startService(name string) error { | ||
m, err := mgr.Connect() | ||
if err != nil { | ||
return err | ||
} | ||
defer m.Disconnect() | ||
s, err := m.OpenService(name) | ||
if err != nil { | ||
return fmt.Errorf("could not access service: %v", err) | ||
} | ||
defer s.Close() | ||
err = s.Start("is", "manual-started") | ||
if err != nil { | ||
return fmt.Errorf("could not start service: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
func controlService(name string, c svc.Cmd, to svc.State) error { | ||
m, err := mgr.Connect() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer m.Disconnect() | ||
s, err := m.OpenService(name) | ||
if err != nil { | ||
return fmt.Errorf("could not access service: %v", err) | ||
} | ||
defer s.Close() | ||
status, err := s.Control(c) | ||
if err != nil { | ||
return fmt.Errorf("could not send control=%d: %v", c, err) | ||
} | ||
timeout := time.Now().Add(10 * time.Second) | ||
for status.State != to { | ||
if timeout.Before(time.Now()) { | ||
return fmt.Errorf("timeout waiting for service to go to state=%d", to) | ||
} | ||
time.Sleep(300 * time.Millisecond) | ||
status, err = s.Query() | ||
if err != nil { | ||
return fmt.Errorf("could not retrieve service status: %v", err) | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.