1
0
mirror of https://github.com/lxc/incus.git synced 2026-02-05 09:46:19 +01:00

Added Windows agent install scripts.

Signed-off-by: Marc Olivier Bergeron <mbergeron28@proton.me>
This commit is contained in:
Marc Olivier Bergeron
2025-11-16 00:08:20 -05:00
parent 00e14e8d12
commit 221cddae4c
9 changed files with 268 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ package main
import (
"os"
"runtime"
"github.com/spf13/cobra"
@@ -11,6 +12,7 @@ import (
type cmdGlobal struct {
flagVersion bool
flagHelp bool
flagService bool
flagLogVerbose bool
flagLogDebug bool
@@ -33,6 +35,9 @@ func main() {
app.PersistentFlags().BoolVarP(&globalCmd.flagHelp, "help", "h", false, "Print help")
app.PersistentFlags().BoolVarP(&globalCmd.flagLogVerbose, "verbose", "v", false, "Show all information messages")
app.PersistentFlags().BoolVarP(&globalCmd.flagLogDebug, "debug", "d", false, "Show all debug messages")
if runtime.GOOS == "windows" {
app.PersistentFlags().BoolVar(&globalCmd.flagService, "service", false, "Start as a system service")
}
// Version handling
app.SetVersionTemplate("{{.Version}}\n")

View File

@@ -46,6 +46,11 @@ func (c *cmdAgent) Command() *cobra.Command {
}
func (c *cmdAgent) Run(cmd *cobra.Command, args []string) error {
if c.global.flagService {
err := runService("Incus-Agent", c)
return err
}
// Setup logger.
err := logger.InitLogger("", "", c.global.flagLogVerbose, c.global.flagLogDebug, nil)
if err != nil {

View File

@@ -38,6 +38,10 @@ func parseBytes(b []byte) string {
return string(b[:n])
}
func runService(name string, agentCmd *cmdAgent) error {
return errors.New("Not implemented.")
}
func osGetEnvironment() (*api.ServerEnvironment, error) {
uname := unix.Utsname{}
err := unix.Uname(&uname)

View File

@@ -52,6 +52,10 @@ var (
osGuestAPISupport = true
)
func runService(name string, agentCmd *cmdAgent) error {
return errors.New("Not implemented.")
}
func osGetEnvironment() (*api.ServerEnvironment, error) {
uname, err := linux.Uname()
if err != nil {

View File

@@ -4,16 +4,21 @@ package main
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"runtime"
"sort"
"strings"
"time"
"github.com/shirou/gopsutil/v4/disk"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/debug"
"golang.org/x/sys/windows/svc/eventlog"
"github.com/lxc/incus/v6/internal/server/metrics"
"github.com/lxc/incus/v6/internal/version"
@@ -21,8 +26,88 @@ import (
"github.com/lxc/incus/v6/shared/logger"
)
// https://dev.to/cosmic_predator/writing-a-windows-service-in-go-1d1m
var osBaseWorkingDirectory = "C:\\"
// Start of Windows service code block
// Inspired of https://github.com/golang/sys/blob/master/windows/svc/example/service.go
var elog debug.Log
type incusAgentService struct {
agentCmd *cmdAgent
}
func (m *incusAgentService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
tick := time.Tick(2 * time.Second)
changes <- svc.Status{State: svc.StartPending}
d := newDaemon(m.agentCmd.global.flagLogDebug, m.agentCmd.global.flagLogVerbose)
// Start the server.
err := startHTTPServer(d, m.agentCmd.global.flagLogDebug)
if err != nil {
changes <- svc.Status{State: svc.StopPending}
elog.Error(1, fmt.Sprintf("Failed to start HTTP server: %s", err))
return
}
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
loop:
for {
select {
case <-tick:
case c := <-r:
switch c.Cmd {
case svc.Interrogate:
changes <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
break loop
default:
elog.Error(1, fmt.Sprintf("Unexpected control request #%d", c))
}
}
}
changes <- svc.Status{State: svc.StopPending}
return
}
func runService(name string, agentCmd *cmdAgent) error {
var err error
if agentCmd.global.flagLogDebug {
elog = debug.New(name)
} else {
elog, err = eventlog.Open(name)
if err != nil {
return err
}
}
defer elog.Close()
elog.Info(1, fmt.Sprintf("Starting %s service", name))
run := svc.Run
if agentCmd.global.flagLogDebug {
run = debug.Run
}
err = run(name, &incusAgentService{agentCmd: agentCmd})
if err != nil {
elog.Error(1, fmt.Sprintf("%s service failed: %v", name, err))
return err
}
elog.Info(1, fmt.Sprintf("%s service stopped", name))
return nil
}
// End of Windows service code block
func osGetEnvironment() (*api.ServerEnvironment, error) {
serverName, err := os.Hostname()
if err != nil {