Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"strings"

"github.com/abiosoft/colima/cli"
"github.com/abiosoft/colima/config"
"github.com/abiosoft/colima/config/configmanager"
"github.com/abiosoft/colima/environment"
Expand All @@ -20,6 +21,7 @@ import (
"github.com/abiosoft/colima/environment/host"
"github.com/abiosoft/colima/environment/vm/lima"
"github.com/abiosoft/colima/environment/vm/lima/limautil"
"github.com/abiosoft/colima/store"
"github.com/abiosoft/colima/util"
"github.com/docker/go-units"
log "github.com/sirupsen/logrus"
Expand All @@ -29,7 +31,7 @@ type App interface {
Active() bool
Start(config.Config) error
Stop(force bool) error
Delete() error
Delete(data, force bool) error
SSH(args ...string) error
Status(extended bool, json bool) error
Version() error
Expand Down Expand Up @@ -196,7 +198,29 @@ func (c colimaApp) Stop(force bool) error {
return nil
}

func (c colimaApp) Delete() error {
func (c colimaApp) Delete(data, force bool) error {
confirmContainerDestruction := func() bool {
return cli.Prompt("\033[31m\033[1mthis will delete ALL container data. Are you sure you want to continue")
}

s, _ := store.Load()
diskInUse := s.DiskFormatted

if !force {
y := cli.Prompt("are you sure you want to delete " + config.CurrentProfile().DisplayName + " and all settings")
if !y {
return nil
}

// runtime disk not in use or data deletion is requested,
// deletion deletes all data, warn accordingly.
if !diskInUse || data {
if y := confirmContainerDestruction(); !y {
return nil
}
}
}

ctx := context.Background()
log.Println("deleting", config.CurrentProfile().DisplayName)

Expand All @@ -214,7 +238,6 @@ func (c colimaApp) Delete() error {
log.Warnln(fmt.Errorf("error retrieving runtimes: %w", err))
}
for _, cont := range containers {

log := log.WithField("context", cont.Name())
log.Println("deleting ...")

Expand All @@ -235,6 +258,14 @@ func (c colimaApp) Delete() error {
return fmt.Errorf("error deleting configs: %w", err)
}

// delete runtime disk if disk in use and data deletion is requested
if diskInUse && data {
log.Println("deleting container data")
if err := limautil.DeleteDisk(); err != nil {
return fmt.Errorf("error deleting container data: %w", err)
}
}

log.Println("done")

if err := generateSSHConfig(false); err != nil {
Expand Down
21 changes: 4 additions & 17 deletions cmd/delete.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package cmd

import (
"github.com/abiosoft/colima/cli"
"github.com/abiosoft/colima/cmd/root"
"github.com/abiosoft/colima/config"
"github.com/spf13/cobra"
)

var deleteCmdArgs struct {
force bool
data bool
}

// deleteCmd represents the delete command
Expand All @@ -18,28 +17,16 @@ var deleteCmd = &cobra.Command{
Long: `Delete and teardown Colima and all settings.

Use with caution. This deletes everything and a startup afterwards is like the
initial startup of Colima.

If you simply want to reset the Kubernetes cluster, run 'colima kubernetes reset'.`,
initial startup of Colima.`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if !deleteCmdArgs.force {
y := cli.Prompt("are you sure you want to delete " + config.CurrentProfile().DisplayName + " and all settings")
if !y {
return nil
}
yy := cli.Prompt("\033[31m\033[1mthis will delete ALL container data. Are you sure you want to continue")
if !yy {
return nil
}
}

return newApp().Delete()
return newApp().Delete(deleteCmdArgs.data, deleteCmdArgs.force)
},
}

func init() {
root.Cmd().AddCommand(deleteCmd)

deleteCmd.Flags().BoolVarP(&deleteCmdArgs.force, "force", "f", false, "do not prompt for yes/no")
deleteCmd.Flags().BoolVarP(&deleteCmdArgs.data, "data", "d", false, "delete container runtime data")
}
26 changes: 2 additions & 24 deletions config/configmanager/configmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ package configmanager
import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/abiosoft/colima/cli"
"github.com/abiosoft/colima/config"
"github.com/abiosoft/colima/util"
"github.com/abiosoft/colima/util/yamlutil"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)

Expand All @@ -33,13 +30,6 @@ func SaveToFile(c config.Config, file string) error {
return yamlutil.Save(c, file)
}

// oldConfigFile returns the path to config file of versions <0.4.0.
// TODO: remove later, only for backward compatibility
func oldConfigFile() string {
_, configFileName := filepath.Split(config.CurrentProfile().File())
return filepath.Join(os.Getenv("HOME"), "."+config.CurrentProfile().ID, configFileName)
}

// LoadFrom loads config from file.
func LoadFrom(file string) (config.Config, error) {
var c config.Config
Expand Down Expand Up @@ -96,22 +86,10 @@ func ValidateConfig(c config.Config) error {
// Load loads the config.
// Error is only returned if the config file exists but could not be loaded.
// No error is returned if the config file does not exist.
func Load() (config.Config, error) {
func Load() (c config.Config, err error) {
f := config.CurrentProfile().File()
if _, err := os.Stat(f); err != nil {
oldF := oldConfigFile()

// config file does not exist, check older version for backward compatibility
if _, err := os.Stat(oldF); err != nil {
return config.Config{}, nil
}

// older version exists
logrus.Infof("settings from older %s version detected and copied", config.AppName)
if err := cli.Command("cp", oldF, f).Run(); err != nil {
logrus.Warn(fmt.Errorf("error copying config: %w, proceeding with defaults", err))
return config.Config{}, nil
}
return c, nil
}

return LoadFrom(f)
Expand Down
10 changes: 10 additions & 0 deletions config/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ var (
return filepath.Join(dir, "_lima"), nil
},
}

storeDir = requiredDir{
dir: func() (string, error) {
dir, err := configBaseDir.dir()
if err != nil {
return "", err
}
return filepath.Join(dir, "_store"), nil
},
}
)

// CacheDir returns the cache directory.
Expand Down
7 changes: 7 additions & 0 deletions config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func (p *Profile) StateFile() string {
return filepath.Join(p.LimaInstanceDir(), configFileName)
}

func (p *Profile) StoreFile() string {
return filepath.Join(storeDir.Dir(), p.ID+".json")
}

var _ ProfileInfo = (*Profile)(nil)

// ProfileInfo is the information about a profile.
Expand All @@ -102,4 +106,7 @@ type ProfileInfo interface {

// StateFile returns the path to the state file.
StateFile() string

// StoreFile returns the path to the store file.
StoreFile() string
}
6 changes: 6 additions & 0 deletions environment/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ type Container interface {
Dependencies
}

// DataDir is the container runtime data directory
type DataDir struct {
Name string
Path string
}

// NewContainer creates a new container environment.
func NewContainer(runtime string, host HostActions, guest GuestActions) (Container, error) {
if _, ok := containerRuntimes[runtime]; !ok {
Expand Down
10 changes: 10 additions & 0 deletions environment/container/containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,13 @@ func (c containerdRuntime) Version(ctx context.Context) string {
func (c *containerdRuntime) Update(ctx context.Context) (bool, error) {
return false, fmt.Errorf("update not supported for the %s runtime", Name)
}

// DataDirs returns the list of directories that are used for storing container runtime data.
func DataDirs() []environment.DataDir {
return []environment.DataDir{
{Name: "containerd", Path: "/var/lib/containerd"},
{Name: "buildkit", Path: "/var/lib/buildkit"},
{Name: "rancher", Path: "/var/lib/rancher"},
{Name: "cni", Path: "/var/lib/cni"},
}
}
10 changes: 10 additions & 0 deletions environment/container/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,13 @@ func (d *dockerRuntime) Update(ctx context.Context) (bool, error) {

return debutil.UpdateRuntime(ctx, d.guest, d, packages...)
}

// DataDirs returns the list of directories that are used for storing container runtime data.
func DataDirs() []environment.DataDir {
return []environment.DataDir{
{Name: "docker", Path: "/var/lib/docker"},
{Name: "containerd", Path: "/var/lib/containerd"},
{Name: "rancher", Path: "/var/lib/rancher"},
{Name: "cni", Path: "/var/lib/cni"},
}
}
11 changes: 11 additions & 0 deletions environment/container/incus/incus.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func (c *incusRuntime) Provision(ctx context.Context) error {
return fmt.Errorf("error parsing incus config template: %w", err)
}

if err := c.guest.RunQuiet("sudo", "systemctl", "restart", "incus.socket"); err != nil {
return fmt.Errorf("error starting incus socket: %w", err)
}

stdin := bytes.NewReader(buf)
if err := c.guest.RunWith(stdin, nil, "sudo", "incus", "admin", "init", "--preseed"); err != nil {
return fmt.Errorf("error setting up incus: %w", err)
Expand Down Expand Up @@ -291,3 +295,10 @@ func (c *incusRuntime) Update(ctx context.Context) (bool, error) {

return debutil.UpdateRuntime(ctx, c.guest, c, packages...)
}

// DataDirs returns the list of directories that are used for storing container runtime data.
func DataDirs() []environment.DataDir {
return []environment.DataDir{
{Name: "incus", Path: "/var/lib/incus"},
}
}
8 changes: 8 additions & 0 deletions environment/container/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,11 @@ func (c kubernetesRuntime) Version(context.Context) string {
func (c *kubernetesRuntime) Update(ctx context.Context) (bool, error) {
return false, fmt.Errorf("update not supported for the %s runtime", Name)
}

// DataDirs returns the list of directories that are used for storing container runtime data.
func DataDirs() []environment.DataDir {
return []environment.DataDir{
{Name: "rancher", Path: "/var/lib/rancher"},
{Name: "cni", Path: "/var/lib/cni"},
}
}
Loading