Skip to content

Commit 60b7924

Browse files
committed
chore: code cleanups
Signed-off-by: Abiola Ibrahim <git@abiosoft.com>
1 parent 049b1d8 commit 60b7924

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

environment/container/incus/incus.go

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ var configDir = func() string { return config.CurrentProfile().ConfigDir() }
3232
// HostSocketFile returns the path to the containerd socket on host.
3333
func HostSocketFile() string { return filepath.Join(configDir(), "incus.sock") }
3434

35-
const Name = "incus"
35+
const (
36+
Name = "incus"
37+
diskName = "/dev/vdb"
38+
poolName = "default"
39+
storageDriver = "zfs"
40+
)
3641

3742
func init() {
3843
environment.RegisterContainer(Name, newRuntime, false)
@@ -55,15 +60,6 @@ func (c *incusRuntime) Dependencies() []string {
5560
func (c *incusRuntime) Provision(ctx context.Context) error {
5661
log := c.Logger(ctx)
5762

58-
// ensure that the systemd socket file is created
59-
if err := c.guest.RunQuiet("sudo", "systemctl", "start", "incus.socket"); err != nil {
60-
return fmt.Errorf("error starting incus socket: %w", err)
61-
}
62-
63-
if err := c.guest.RunQuiet("sudo", "systemctl", "start", "incus.service"); err != nil {
64-
return fmt.Errorf("error starting incus service: %w", err)
65-
}
66-
6763
if err := c.guest.RunQuiet("ip", "addr", "show", incusBridgeInterface); err == nil {
6864
// already provisioned
6965
return nil
@@ -83,7 +79,7 @@ func (c *incusRuntime) Provision(ctx context.Context) error {
8379
Interface string
8480
SetStorage bool
8581
}
86-
value.Disk = "/dev/vdb"
82+
value.Disk = diskName
8783
value.Interface = incusBridgeInterface
8884
value.SetStorage = emptyDisk // set only when the disk is empty
8985

@@ -349,20 +345,19 @@ func (c *incusRuntime) Update(ctx context.Context) (bool, error) {
349345
// DataDirs represents the data disk for the container runtime.
350346
func DataDisk() environment.DataDisk {
351347
return environment.DataDisk{
352-
FSType: "zfs",
353-
PreMount: []string{
354-
"systemctl stop incus.service",
355-
"systemctl stop incus.socket",
356-
},
348+
FSType: storageDriver,
357349
}
358350
}
359351

360352
func (c *incusRuntime) hasExistingPool() bool {
361-
return c.guest.RunQuiet("sh", "-c", "sudo zpool import | grep -A 2 'pool: default' | grep 'state: ONLINE'") == nil
353+
script := strings.NewReplacer(
354+
"{pool_name}", poolName,
355+
).Replace("sudo zpool import | grep -A 2 'pool: {pool_name}' | grep 'state: ONLINE'")
356+
return c.guest.RunQuiet("sh", "-c", script) == nil
362357
}
363358

364359
func (c *incusRuntime) importExistingPool() error {
365-
if err := c.guest.RunQuiet("sudo", "zpool", "import", "default"); err != nil {
360+
if err := c.guest.RunQuiet("sudo", "zpool", "import", poolName); err != nil {
366361
return fmt.Errorf("error importing existing zpool: %w", err)
367362
}
368363

@@ -376,21 +371,21 @@ func (c *incusRuntime) recoverDisk(ctx context.Context) error {
376371
log.Println("Running 'incus admin recover' ...")
377372
log.Println()
378373
log.Println("Use the following values for the prompts")
379-
log.Println(" name of storage pool: default")
380-
log.Println(" name of storage backend: zfs")
381-
log.Println(" source of storage pool: /dev/vdb")
374+
log.Println(" name of storage pool: " + poolName)
375+
log.Println(" name of storage backend: " + storageDriver)
376+
log.Println(" source of storage pool: " + diskName)
382377
log.Println()
383378

384379
if err := c.guest.RunInteractive("sudo", "incus", "admin", "recover"); err != nil {
385380
return fmt.Errorf("error recovering storage pool: %w", err)
386381
}
387382

388-
out, err := c.guest.RunOutput("sudo", "incus", "storage", "list", "name=default", "-c", "n", "--format", "compact,noheader")
383+
out, err := c.guest.RunOutput("sudo", "incus", "storage", "list", "name="+poolName, "-c", "n", "--format", "compact,noheader")
389384
if err != nil {
390385
return err
391386
}
392387

393-
if out != "default" {
388+
if out != poolName {
394389
return fmt.Errorf("storage pool recovery failure")
395390
}
396391

@@ -399,19 +394,19 @@ func (c *incusRuntime) recoverDisk(ctx context.Context) error {
399394

400395
func (c *incusRuntime) wipeDisk(wipeZpool bool) error {
401396
if wipeZpool {
402-
if err := c.guest.RunQuiet("sudo", "zpool", "destroy", "default"); err != nil {
397+
if err := c.guest.RunQuiet("sudo", "zpool", "destroy", poolName); err != nil {
403398
return fmt.Errorf("cannot resetting pool data: %w", err)
404399
}
405400
} else {
406-
if err := c.guest.RunQuiet("sudo", "sfdisk", "--delete", "/dev/vdb", "1"); err != nil {
401+
if err := c.guest.RunQuiet("sudo", "sfdisk", "--delete", diskName, "1"); err != nil {
407402
return fmt.Errorf("error resetting pool data: %w", err)
408403
}
409404
}
410405

411406
// prepare directory
412-
if err := c.guest.RunQuiet("sudo", "rm", "-rf", "/var/lib/incus/storage-pools/default"); err != nil {
407+
if err := c.guest.RunQuiet("sudo", "rm", "-rf", "/var/lib/incus/storage-pools/"+poolName); err != nil {
413408
return fmt.Errorf("error preparing storage pools directory: %w", err)
414409
}
415410

416-
return c.guest.RunQuiet("sudo", "incus", "storage", "create", "default", "zfs", "source=/dev/vdb")
411+
return c.guest.RunQuiet("sudo", "incus", "storage", "create", poolName, storageDriver, "source="+diskName)
417412
}

0 commit comments

Comments
 (0)