Skip to content

Commit

Permalink
lxd-to-incus: Port to current Incus
Browse files Browse the repository at this point in the history
Closes #133

Signed-off-by: Stéphane Graber <stgraber@stgraber.org>
  • Loading branch information
stgraber committed Oct 7, 2023
1 parent 7181b0f commit ae360e8
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 25 deletions.
7 changes: 4 additions & 3 deletions cmd/lxd-to-incus/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (

"github.com/pierrec/lz4/v4"

"github.com/lxc/incus/shared"
internalIO "github.com/lxc/incus/internal/io"
internalUtil "github.com/lxc/incus/internal/util"
)

// Uncompress the raft snapshot files in the given database directory.
Expand All @@ -18,7 +19,7 @@ import (
func migrateDatabase(dir string) error {
global := filepath.Join(dir, "global")

err := shared.DirCopy(global, global+".bak")
err := internalUtil.DirCopy(global, global+".bak")
if err != nil {
return fmt.Errorf("Failed to backup database directory %q: %w", global, err)
}
Expand Down Expand Up @@ -97,7 +98,7 @@ func lz4Uncompress(zfilename string) error {
// use the same mode for the output file
mode := zinfo.Mode()

_, uid, gid := shared.GetOwnerMode(zinfo)
_, uid, gid := internalIO.GetOwnerMode(zinfo)

filename := zfilename + ".uncompressed"
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode)
Expand Down
4 changes: 3 additions & 1 deletion cmd/lxd-to-incus/go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module github.com/lxc/incus/cmd/lxd-to-incus

replace github.com/lxc/incus => ../../

go 1.20

require (
github.com/canonical/lxd v0.0.0-20230925202117-188c290d29fc
github.com/lxc/incus v0.0.0-20230926032748-f97f7f9d68ac
github.com/lxc/incus v0.0.0-20231007153324-7181b0fd8650
github.com/pierrec/lz4/v4 v4.1.18
github.com/spf13/cobra v1.7.0
)
Expand Down
10 changes: 4 additions & 6 deletions cmd/lxd-to-incus/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/lxc/incus v0.0.0-20230926032748-f97f7f9d68ac h1:XOCgvOXwM4skYY9BSo/+UO7lVhqD2WNnc8UbeWDX6Vs=
github.com/lxc/incus v0.0.0-20230926032748-f97f7f9d68ac/go.mod h1:yhHdpW6Uzxn10at7nNMPQRVr7qQA0gS+EVT32mi+9/U=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
Expand Down Expand Up @@ -109,7 +107,7 @@ github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/fastuuid v1.2.0 h1:Ppwyp6VYCF1nvBTXL3trRso7mXMlRrw9ooo375wvi2s=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rs/cors v1.10.0 h1:62NOS1h+r8p1mW6FM0FSB0exioXLhd/sh15KpjWBZ+8=
github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
Expand All @@ -127,9 +125,9 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zitadel/oidc/v2 v2.11.0 h1:Am4/yQr4iiM5bznRgF3FOp+wLdKx2gzSU73uyI9vvBE=
github.com/zitadel/oidc/v2 v2.11.0/go.mod h1:enFSVBQI6aE0TEB1ntjXs9r6O6DEosxX4uhEBLBVD8o=
go.opentelemetry.io/otel v1.17.0 h1:MW+phZ6WZ5/uk2nd93ANk/6yJ+dVrvNWUjGhnnFU5jM=
go.opentelemetry.io/otel/metric v1.17.0 h1:iG6LGVz5Gh+IuO0jmgvpTB6YVrCGngi8QGm+pMd8Pdc=
go.opentelemetry.io/otel/trace v1.17.0 h1:/SWhSRHmDPOImIAetP1QAeMnZYiQXrTy4fMMYOdSKWQ=
go.opentelemetry.io/otel v1.19.0 h1:MuS/TNf4/j4IXsZuJegVzI1cwut7Qc00344rgH7p8bs=
go.opentelemetry.io/otel/metric v1.19.0 h1:aTzpGtV0ar9wlV4Sna9sdJyII5jTVJEvKETPiOKwvpE=
go.opentelemetry.io/otel/trace v1.19.0 h1:DFVQmlVbfVeOuBRrwdtaehRrWiL1JoVs9CPIQ1Dzxpg=
golang.org/x/crypto v0.0.0-20180723164146-c126467f60eb/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
18 changes: 12 additions & 6 deletions cmd/lxd-to-incus/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bufio"
"fmt"
"os"
"path/filepath"
Expand All @@ -9,7 +10,7 @@ import (
lxdAPI "github.com/canonical/lxd/shared/api"
"github.com/spf13/cobra"

"github.com/lxc/incus/internal/cmd"
cli "github.com/lxc/incus/internal/cmd"
"github.com/lxc/incus/internal/version"
incusAPI "github.com/lxc/incus/shared/api"
"github.com/lxc/incus/shared/subprocess"
Expand All @@ -19,15 +20,17 @@ var minLXDVersion = &version.DottedVersion{4, 0, 0}
var maxLXDVersion = &version.DottedVersion{5, 18, 0}

type cmdGlobal struct {
asker cli.Asker

flagHelp bool
flagVersion bool
}

func main() {
// Setup command line parser.
daemonCmd := cmdMigrate{}
migrateCmd := cmdMigrate{}

app := daemonCmd.Command()
app := migrateCmd.Command()
app.Use = "lxd-to-incus"
app.Short = "LXD to Incus migration tool"
app.Long = `Description:
Expand All @@ -39,7 +42,8 @@ func main() {
app.CompletionOptions = cobra.CompletionOptions{DisableDefaultCmd: true}

// Global flags.
globalCmd := cmdGlobal{}
globalCmd := cmdGlobal{asker: cli.NewAsker(bufio.NewReader(os.Stdin))}
migrateCmd.global = globalCmd
app.PersistentFlags().BoolVar(&globalCmd.flagVersion, "version", false, "Print version number")
app.PersistentFlags().BoolVarP(&globalCmd.flagHelp, "help", "h", false, "Print help")

Expand All @@ -55,6 +59,8 @@ func main() {
}

type cmdMigrate struct {
global cmdGlobal

flagYes bool
}

Expand Down Expand Up @@ -456,7 +462,7 @@ At this point, the source server and all its instances will be stopped.
Instances will come back online once the migration is complete.
`)

ok, err := cmd.AskBool("Proceed with the migration? [default=no]: ", "no")
ok, err := c.global.asker.AskBool("Proceed with the migration? [default=no]: ", "no")
if err != nil {
return err
}
Expand Down Expand Up @@ -585,7 +591,7 @@ Instances will come back online once the migration is complete.

// Confirm uninstall.
if !c.flagYes {
ok, err := cmd.AskBool("Uninstall the LXD package? [default=no]: ", "no")
ok, err := c.global.asker.AskBool("Uninstall the LXD package? [default=no]: ", "no")
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/lxd-to-incus/sources_deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ package main
import (
"github.com/canonical/lxd/client"

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

type srcDeb struct{}

func (s *srcDeb) Present() bool {
// Validate that the Debian package is installed.
if !shared.PathExists("/var/lib/dpkg/info/lxd.list") {
if !util.PathExists("/var/lib/dpkg/info/lxd.list") {
return false
}

if !shared.PathExists("/var/lib/lxd") {
if !util.PathExists("/var/lib/lxd") {
return false
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/lxd-to-incus/sources_snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ package main
import (
"github.com/canonical/lxd/client"

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

type srcSnap struct{}

func (s *srcSnap) Present() bool {
// Validate that the snap is installed.
if !shared.PathExists("/snap/lxd") {
if !util.PathExists("/snap/lxd") {
return false
}

if !shared.PathExists("/var/snap/lxd") {
if !util.PathExists("/var/snap/lxd") {
return false
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/lxd-to-incus/targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"time"

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

type Target interface {
Expand All @@ -21,11 +21,11 @@ var targets = []Target{&targetSystemd{}}
type targetSystemd struct{}

func (s *targetSystemd) Present() bool {
if !shared.PathExists("/var/lib/incus/") {
if !util.PathExists("/var/lib/incus/") {
return false
}

if !shared.PathExists("/lib/systemd/system/incus.service") {
if !util.PathExists("/lib/systemd/system/incus.service") {
return false
}

Expand Down

0 comments on commit ae360e8

Please sign in to comment.