Skip to content

Commit

Permalink
lxd-to-incus: Split the targets
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Graber <[email protected]>
  • Loading branch information
stgraber committed Oct 30, 2023
1 parent 9a7b4cc commit dc8c05a
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 92 deletions.
92 changes: 0 additions & 92 deletions cmd/lxd-to-incus/targets.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
53 changes: 53 additions & 0 deletions cmd/lxd-to-incus/targets_openrc.go
Original file line number Diff line number Diff line change
@@ -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
}
53 changes: 53 additions & 0 deletions cmd/lxd-to-incus/targets_systemd.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit dc8c05a

Please sign in to comment.