From dc8c05ad76b3ada43b864afed94c07f38d453ca8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= Date: Sun, 29 Oct 2023 23:03:26 -0400 Subject: [PATCH] lxd-to-incus: Split the targets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Graber --- cmd/lxd-to-incus/targets.go | 92 ----------------------------- cmd/lxd-to-incus/targets_openrc.go | 53 +++++++++++++++++ cmd/lxd-to-incus/targets_systemd.go | 53 +++++++++++++++++ 3 files changed, 106 insertions(+), 92 deletions(-) create mode 100644 cmd/lxd-to-incus/targets_openrc.go create mode 100644 cmd/lxd-to-incus/targets_systemd.go diff --git a/cmd/lxd-to-incus/targets.go b/cmd/lxd-to-incus/targets.go index 0940e15970d..5ed94ba99c6 100644 --- a/cmd/lxd-to-incus/targets.go +++ b/cmd/lxd-to-incus/targets.go @@ -1,11 +1,7 @@ package main import ( - "time" - "github.com/lxc/incus/client" - "github.com/lxc/incus/shared/subprocess" - "github.com/lxc/incus/shared/util" ) type Target interface { @@ -17,91 +13,3 @@ type Target interface { } var targets = []Target{&targetSystemd{}, &targetOpenRC{}} - -type targetSystemd struct{} - -func (s *targetSystemd) Present() bool { - if !util.PathExists("/var/lib/incus/") { - return false - } - - _, err := subprocess.RunCommand("systemctl", "list-unit-files", "incus.service") - if err != nil { - return false - } - - return true -} - -func (s *targetSystemd) Stop() error { - _, err := subprocess.RunCommand("systemctl", "stop", "incus.service", "incus.socket") - return err -} - -func (s *targetSystemd) Start() error { - _, err := subprocess.RunCommand("systemctl", "start", "incus.service", "incus.socket") - if err != nil { - return err - } - - // Wait for the socket to become available. - time.Sleep(5 * time.Second) - - return nil -} - -func (s *targetSystemd) Connect() (incus.InstanceServer, error) { - return incus.ConnectIncusUnix("/var/lib/incus/unix.socket", nil) -} - -func (s *targetSystemd) Paths() (*DaemonPaths, error) { - return &DaemonPaths{ - Daemon: "/var/lib/incus/", - Logs: "/var/log/incus/", - Cache: "/var/cache/incus/", - }, nil -} - -type targetOpenRC struct{} - -func (s *targetOpenRC) Present() bool { - if !util.PathExists("/var/lib/incus/") { - return false - } - - _, err := subprocess.RunCommand("rc-service", "--exists", "incus") - if err != nil { - return false - } - - return true -} - -func (s *targetOpenRC) Stop() error { - _, err := subprocess.RunCommand("rc-service", "incus", "stop") - return err -} - -func (s *targetOpenRC) Start() error { - _, err := subprocess.RunCommand("rc-service", "incus", "start") - if err != nil { - return err - } - - // Wait for the socket to become available. - time.Sleep(5 * time.Second) - - return nil -} - -func (s *targetOpenRC) Connect() (incus.InstanceServer, error) { - return incus.ConnectIncusUnix("/var/lib/incus/unix.socket", nil) -} - -func (s *targetOpenRC) Paths() (*DaemonPaths, error) { - return &DaemonPaths{ - Daemon: "/var/lib/incus/", - Logs: "/var/log/incus/", - Cache: "/var/cache/incus/", - }, nil -} diff --git a/cmd/lxd-to-incus/targets_openrc.go b/cmd/lxd-to-incus/targets_openrc.go new file mode 100644 index 00000000000..78be0ebdfdc --- /dev/null +++ b/cmd/lxd-to-incus/targets_openrc.go @@ -0,0 +1,53 @@ +package main + +import ( + "time" + + "github.com/lxc/incus/client" + "github.com/lxc/incus/shared/subprocess" + "github.com/lxc/incus/shared/util" +) + +type targetOpenRC struct{} + +func (s *targetOpenRC) Present() bool { + if !util.PathExists("/var/lib/incus/") { + return false + } + + _, err := subprocess.RunCommand("rc-service", "--exists", "incus") + if err != nil { + return false + } + + return true +} + +func (s *targetOpenRC) Stop() error { + _, err := subprocess.RunCommand("rc-service", "incus", "stop") + return err +} + +func (s *targetOpenRC) Start() error { + _, err := subprocess.RunCommand("rc-service", "incus", "start") + if err != nil { + return err + } + + // Wait for the socket to become available. + time.Sleep(5 * time.Second) + + return nil +} + +func (s *targetOpenRC) Connect() (incus.InstanceServer, error) { + return incus.ConnectIncusUnix("/var/lib/incus/unix.socket", nil) +} + +func (s *targetOpenRC) Paths() (*DaemonPaths, error) { + return &DaemonPaths{ + Daemon: "/var/lib/incus/", + Logs: "/var/log/incus/", + Cache: "/var/cache/incus/", + }, nil +} diff --git a/cmd/lxd-to-incus/targets_systemd.go b/cmd/lxd-to-incus/targets_systemd.go new file mode 100644 index 00000000000..a3759a5d9fa --- /dev/null +++ b/cmd/lxd-to-incus/targets_systemd.go @@ -0,0 +1,53 @@ +package main + +import ( + "time" + + "github.com/lxc/incus/client" + "github.com/lxc/incus/shared/subprocess" + "github.com/lxc/incus/shared/util" +) + +type targetSystemd struct{} + +func (s *targetSystemd) Present() bool { + if !util.PathExists("/var/lib/incus/") { + return false + } + + _, err := subprocess.RunCommand("systemctl", "list-unit-files", "incus.service") + if err != nil { + return false + } + + return true +} + +func (s *targetSystemd) Stop() error { + _, err := subprocess.RunCommand("systemctl", "stop", "incus.service", "incus.socket") + return err +} + +func (s *targetSystemd) Start() error { + _, err := subprocess.RunCommand("systemctl", "start", "incus.service", "incus.socket") + if err != nil { + return err + } + + // Wait for the socket to become available. + time.Sleep(5 * time.Second) + + return nil +} + +func (s *targetSystemd) Connect() (incus.InstanceServer, error) { + return incus.ConnectIncusUnix("/var/lib/incus/unix.socket", nil) +} + +func (s *targetSystemd) Paths() (*DaemonPaths, error) { + return &DaemonPaths{ + Daemon: "/var/lib/incus/", + Logs: "/var/log/incus/", + Cache: "/var/cache/incus/", + }, nil +}