Skip to content

Commit

Permalink
Merge pull request #420 from stgraber/migrate
Browse files Browse the repository at this point in the history
lxd-to-incus: Stop using LXD client package
  • Loading branch information
freeekanayaka committed Jan 23, 2024
2 parents 6750f17 + aec187b commit e569070
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 365 deletions.
6 changes: 0 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ endif
CC="$(CC)" CGO_LDFLAGS_ALLOW="$(CGO_LDFLAGS_ALLOW)" $(GO) install -v -tags "$(TAG_SQLITE3)" $(DEBUG) ./...
CGO_ENABLED=0 $(GO) install -v -tags netgo ./cmd/incus-migrate
CGO_ENABLED=0 $(GO) install -v -tags agent,netgo ./cmd/incus-agent
cd cmd/lxd-to-incus && CC="$(CC)" CGO_LDFLAGS_ALLOW="$(CGO_LDFLAGS_ALLOW)" $(GO) install -v ./
@echo "Incus built successfully"

.PHONY: client
Expand Down Expand Up @@ -100,10 +99,6 @@ endif
$(GO) mod tidy --go=1.20
$(GO) get toolchain@none

cd cmd/lxd-to-incus && $(GO) get -t -v -d -u ./...
cd cmd/lxd-to-incus && $(GO) get github.com/canonical/lxd@lxd-5.19
cd cmd/lxd-to-incus && $(GO) mod tidy --go=1.20

cd test/mini-oidc && $(GO) get -t -v -d -u ./...
cd test/mini-oidc && $(GO) mod tidy --go=1.20
@echo "Dependencies updated"
Expand Down Expand Up @@ -253,7 +248,6 @@ dist: doc

# Download dependencies
(cd $(TMP)/incus-$(VERSION) ; $(GO) mod vendor)
(cd $(TMP)/incus-$(VERSION)/cmd/lxd-to-incus ; $(GO) mod vendor)

# Download the cowsql libraries
git clone --depth=1 https://github.com/cowsql/cowsql $(TMP)/incus-$(VERSION)/vendor/cowsql
Expand Down
2 changes: 1 addition & 1 deletion cmd/lxd-to-incus/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func lz4Uncompress(zfilename string) error {
}

if n != 4 {
return fmt.Errorf("Read only %n bytes from %q: %w", n, zfilename)
return fmt.Errorf("Read only %d bytes from %q", n, zfilename)
}

// Check the file magic, and return now if it's not an lz4 file.
Expand Down
55 changes: 0 additions & 55 deletions cmd/lxd-to-incus/go.mod

This file was deleted.

247 changes: 0 additions & 247 deletions cmd/lxd-to-incus/go.sum

This file was deleted.

69 changes: 56 additions & 13 deletions cmd/lxd-to-incus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@ package main

import (
"bufio"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"time"

"github.com/canonical/lxd/client"
lxdAPI "github.com/canonical/lxd/shared/api"
"github.com/spf13/cobra"
"golang.org/x/sys/unix"

"github.com/lxc/incus/client"
cli "github.com/lxc/incus/internal/cmd"
"github.com/lxc/incus/internal/linux"
"github.com/lxc/incus/internal/version"
incusAPI "github.com/lxc/incus/shared/api"
"github.com/lxc/incus/shared/api"
"github.com/lxc/incus/shared/subprocess"
"github.com/lxc/incus/shared/util"
)
Expand Down Expand Up @@ -82,7 +81,7 @@ func (c *cmdMigrate) Command() *cobra.Command {

func (c *cmdMigrate) Run(app *cobra.Command, args []string) error {
var err error
var srcClient lxd.InstanceServer
var srcClient incus.InstanceServer
var targetClient incus.InstanceServer

// Confirm that we're root.
Expand Down Expand Up @@ -158,6 +157,51 @@ func (c *cmdMigrate) Run(app *cobra.Command, args []string) error {
return fmt.Errorf("Failed to connect to the source: %w", err)
}

// Look for API incompatibility (bool in /1.0 config).
resp, _, err := srcClient.RawQuery("GET", "/1.0", nil, "")
if err != nil {
_, _ = logFile.WriteString(fmt.Sprintf("ERROR: %v\n", err))
return fmt.Errorf("Failed to get source server info: %w", err)
}

type lxdServer struct {
Config map[string]any `json:"config"`
}

s := lxdServer{}

err = json.Unmarshal(resp.Metadata, &s)
if err != nil {
_, _ = logFile.WriteString(fmt.Sprintf("ERROR: %v\n", err))
return fmt.Errorf("Failed to parse source server config: %w", err)
}

badEntries := []string{}
for k, v := range s.Config {
_, ok := v.(string)
if !ok {
badEntries = append(badEntries, k)
}
}

if len(badEntries) > 0 {
fmt.Println("")
fmt.Println("The source server (LXD) has the following configuration keys that are incompatible with Incus:")

for _, k := range badEntries {
fmt.Printf(" - %s\n", k)
}

fmt.Println("")
fmt.Println("The present migration tool cannot properly connect to the LXD server with those configuration keys present.")
fmt.Println("Please unset those configuration keys through the `lxc config unset` command and retry `lxd-to-incus`.")
fmt.Println("")

_, _ = logFile.WriteString(fmt.Sprintf("ERROR: Bad config keys: %v\n", badEntries))
return fmt.Errorf("Unable to interact with the source server")
}

// Get the source server info.
srcServerInfo, _, err := srcClient.GetServer()
if err != nil {
_, _ = logFile.WriteString(fmt.Sprintf("ERROR: %v\n", err))
Expand Down Expand Up @@ -208,7 +252,7 @@ func (c *cmdMigrate) Run(app *cobra.Command, args []string) error {
rewriteCommands := [][]string{}

if !c.flagClusterMember {
var storagePools []lxdAPI.StoragePool
var storagePools []api.StoragePool
if !clustered {
storagePools, err = srcClient.GetStoragePools()
if err != nil {
Expand Down Expand Up @@ -288,7 +332,7 @@ func (c *cmdMigrate) Run(app *cobra.Command, args []string) error {
return fmt.Errorf("Failed to get source server info: %w", err)
}

ovnNB, ok := srcServerInfo.Config["network.ovn.northbound_connection"].(string)
ovnNB, ok := srcServerInfo.Config["network.ovn.northbound_connection"]
if !ok && util.PathExists("/run/ovn/ovnnb_db.sock") {
ovnNB = "unix:/run/ovn/ovnnb_db.sock"
}
Expand Down Expand Up @@ -343,8 +387,7 @@ func (c *cmdMigrate) Run(app *cobra.Command, args []string) error {
fmt.Println(`
The migration is now ready to proceed.
At this point, the source server and all its instances will be stopped.
Instances will come back online once the migration is complete.
`)
Instances will come back online once the migration is complete.`)

ok, err := c.global.asker.AskBool("Proceed with the migration? [default=no]: ", "no")
if err != nil {
Expand All @@ -371,8 +414,7 @@ Manual action will be needed on each of the server prior to Incus being function
It will then convert the current server over to Incus and then wait for the other servers to be converted.
Do not attempt to manually run this tool on any of the other servers in the cluster.
Instead this tool will be providing specific commands for each of the servers.
`)
Instead this tool will be providing specific commands for each of the servers.`)

ok, err := c.global.asker.AskBool("Proceed with the migration? [default=no]: ", "no")
if err != nil {
Expand Down Expand Up @@ -406,7 +448,7 @@ Instead this tool will be providing specific commands for each of the servers.
fmt.Printf("==> Stopping all workloads on server %q\n", member.ServerName)
_, _ = logFile.WriteString(fmt.Sprintf("Stopping instances on server %qn\n", member.ServerName))

op, err := srcClient.UpdateClusterMemberState(member.ServerName, lxdAPI.ClusterMemberStatePost{Action: "evacuate", Mode: "stop"})
op, err := srcClient.UpdateClusterMemberState(member.ServerName, api.ClusterMemberStatePost{Action: "evacuate", Mode: "stop"})
if err != nil {
_, _ = logFile.WriteString(fmt.Sprintf("ERROR: %v\n", err))
return fmt.Errorf("Failed to stop workloads %q: %w", member.ServerName, err)
Expand Down Expand Up @@ -649,7 +691,8 @@ Instead this tool will be providing specific commands for each of the servers.
if !c.flagClusterMember {
_, _ = logFile.WriteString("Waiting for user to run command on other cluster members\n")

fmt.Println("=> Waiting for other cluster servers\n")
fmt.Println("=> Waiting for other cluster servers")
fmt.Println("")
fmt.Printf("Please run `lxd-to-incus --cluster-member` on all other servers in the cluster\n\n")
for {
ok, err := c.global.asker.AskBool("The command has been started on all other servers? [default=no]: ", "no")
Expand Down Expand Up @@ -751,7 +794,7 @@ Instead this tool will be providing specific commands for each of the servers.
fmt.Printf("==> Restoring workloads on server %q\n", member.ServerName)
_, _ = logFile.WriteString(fmt.Sprintf("Restoring workloads on %q\n", member.ServerName))

op, err := targetClient.UpdateClusterMemberState(member.ServerName, incusAPI.ClusterMemberStatePost{Action: "restore"})
op, err := targetClient.UpdateClusterMemberState(member.ServerName, api.ClusterMemberStatePost{Action: "restore"})
if err != nil {
_, _ = logFile.WriteString(fmt.Sprintf("ERROR: %v\n", err))
return fmt.Errorf("Failed to restore %q: %w", member.ServerName, err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/lxd-to-incus/sources.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package main

import (
"github.com/canonical/lxd/client"
"github.com/lxc/incus/client"
)

type Source interface {
Present() bool
Stop() error
Start() error
Purge() error
Connect() (lxd.InstanceServer, error)
Connect() (incus.InstanceServer, error)
Paths() (*DaemonPaths, error)
Name() string
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/lxd-to-incus/sources_copr.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package main

import (
"github.com/canonical/lxd/client"

"github.com/lxc/incus/client"
"github.com/lxc/incus/shared/subprocess"
"github.com/lxc/incus/shared/util"
)
Expand Down Expand Up @@ -42,8 +41,8 @@ func (s *srcCOPR) Purge() error {
return err
}

func (s *srcCOPR) Connect() (lxd.InstanceServer, error) {
return lxd.ConnectLXDUnix("/run/lxd.socket", nil)
func (s *srcCOPR) Connect() (incus.InstanceServer, error) {

Check failure on line 44 in cmd/lxd-to-incus/sources_copr.go

View workflow job for this annotation

GitHub Actions / Code

exported: exported method srcCOPR.Connect should have comment or be unexported (revive)
return incus.ConnectIncusUnix("/run/lxd.socket", &incus.ConnectionArgs{SkipGetServer: true})
}

func (s *srcCOPR) Paths() (*DaemonPaths, error) {
Expand Down
7 changes: 3 additions & 4 deletions cmd/lxd-to-incus/sources_deb.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package main

import (
"github.com/canonical/lxd/client"

"github.com/lxc/incus/client"
"github.com/lxc/incus/shared/subprocess"
"github.com/lxc/incus/shared/util"
)
Expand Down Expand Up @@ -41,8 +40,8 @@ func (s *srcDeb) Purge() error {
return err
}

func (s *srcDeb) Connect() (lxd.InstanceServer, error) {
return lxd.ConnectLXDUnix("/var/lib/lxd/unix.socket", nil)
func (s *srcDeb) Connect() (incus.InstanceServer, error) {

Check failure on line 43 in cmd/lxd-to-incus/sources_deb.go

View workflow job for this annotation

GitHub Actions / Code

exported: exported method srcDeb.Connect should have comment or be unexported (revive)
return incus.ConnectIncusUnix("/var/lib/lxd/unix.socket", &incus.ConnectionArgs{SkipGetServer: true})
}

func (s *srcDeb) Paths() (*DaemonPaths, error) {
Expand Down
7 changes: 3 additions & 4 deletions cmd/lxd-to-incus/sources_manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"net/http"
"time"

"github.com/canonical/lxd/client"

"github.com/lxc/incus/client"
"github.com/lxc/incus/shared/util"
)

Expand Down Expand Up @@ -53,8 +52,8 @@ func (s *srcManual) Purge() error {
return nil
}

func (s *srcManual) Connect() (lxd.InstanceServer, error) {
return lxd.ConnectLXDUnix("/var/lib/lxd/unix.socket", nil)
func (s *srcManual) Connect() (incus.InstanceServer, error) {

Check failure on line 55 in cmd/lxd-to-incus/sources_manual.go

View workflow job for this annotation

GitHub Actions / Code

exported: exported method srcManual.Connect should have comment or be unexported (revive)
return incus.ConnectIncusUnix("/var/lib/lxd/unix.socket", &incus.ConnectionArgs{SkipGetServer: true})
}

func (s *srcManual) Paths() (*DaemonPaths, error) {
Expand Down
7 changes: 3 additions & 4 deletions cmd/lxd-to-incus/sources_snap.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package main

import (
"github.com/canonical/lxd/client"

"github.com/lxc/incus/client"
"github.com/lxc/incus/shared/subprocess"
"github.com/lxc/incus/shared/util"
)
Expand Down Expand Up @@ -41,8 +40,8 @@ func (s *srcSnap) Purge() error {
return err
}

func (s *srcSnap) Connect() (lxd.InstanceServer, error) {
return lxd.ConnectLXDUnix("/var/snap/lxd/common/lxd/unix.socket", nil)
func (s *srcSnap) Connect() (incus.InstanceServer, error) {

Check failure on line 43 in cmd/lxd-to-incus/sources_snap.go

View workflow job for this annotation

GitHub Actions / Code

exported: exported method srcSnap.Connect should have comment or be unexported (revive)
return incus.ConnectIncusUnix("/var/snap/lxd/common/lxd/unix.socket", &incus.ConnectionArgs{SkipGetServer: true})
}

func (s *srcSnap) Paths() (*DaemonPaths, error) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/lxd-to-incus/targets_openrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *targetOpenRC) Start() error {
}

func (s *targetOpenRC) Connect() (incus.InstanceServer, error) {
return incus.ConnectIncusUnix("/var/lib/incus/unix.socket", nil)
return incus.ConnectIncusUnix("/var/lib/incus/unix.socket", &incus.ConnectionArgs{SkipGetServer: true})
}

func (s *targetOpenRC) Paths() (*DaemonPaths, error) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/lxd-to-incus/targets_systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *targetSystemd) Start() error {
}

func (s *targetSystemd) Connect() (incus.InstanceServer, error) {
return incus.ConnectIncusUnix("/var/lib/incus/unix.socket", nil)
return incus.ConnectIncusUnix("/var/lib/incus/unix.socket", &incus.ConnectionArgs{SkipGetServer: true})
}

func (s *targetSystemd) Paths() (*DaemonPaths, error) {
Expand Down
Loading

0 comments on commit e569070

Please sign in to comment.