Skip to content

Commit

Permalink
go rewrite: checkpoint - app state management
Browse files Browse the repository at this point in the history
Signed-off-by: Abiola Ibrahim <[email protected]>
  • Loading branch information
abiosoft committed Sep 22, 2021
1 parent 0af2073 commit 525f02b
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 50 deletions.
4 changes: 3 additions & 1 deletion cmd/colima/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import "github.com/abiosoft/colima/cmd"
import (
"github.com/abiosoft/colima/cmd"
)

func main() {
cmd.Execute()
Expand Down
4 changes: 2 additions & 2 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
// deleteCmd represents the delete command
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete and teardown the VM and settings",
Long: `Delete and teardown Colima VM and all settings.
Short: "Delete and teardown Colima",
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.
Expand Down
8 changes: 3 additions & 5 deletions cmd/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ var kubernetesStopCmd = &cobra.Command{
var kubernetesDashboardCmd = &cobra.Command{
Use: "dashboard",
Aliases: []string{"d"},
Short: "Enable the Kubernetes dashboard",
Long: `Enable the Kubernetes dashboard.
Short: "Enable the Kubernetes dashboard and print dashboard url",
Long: `Enable the Kubernetes dashboard and print dashboard url.
This may take a while on first run, the dashboard is not enabled by default.
The dashboard url is printed afterwards`,
This may take a while on first run, the dashboard is not enabled by default.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("kubernetes called")
},
Expand Down
16 changes: 8 additions & 8 deletions cmd/nerdctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ var nerdctlConf struct {
var nerdctlCmd = &cobra.Command{
Use: "nerdctl",
Aliases: []string{"nerd", "n"},
Short: "Run nerdctl (requires --runtime=containerd)",
Short: "Run nerdctl (requires containerd runtime)",
Long: `Run nerdctl to interact with containerd.
This requires containerd runtime (--runtime=containerd).
This requires containerd runtime.
It is recommended to specify '--' to differentiate from colima flags.
It is recommended to specify '--' to differentiate from Colima flags.
`,
Run: func(cmd *cobra.Command, args []string) {
nerdctlArgs := append([]string{"sudo", "nerdctl"}, args...)
Expand All @@ -29,13 +29,13 @@ It is recommended to specify '--' to differentiate from colima flags.

// nerdctlLink represents the nerdctl command
var nerdctlLink = &cobra.Command{
Use: "link",
Short: "Link nerdctl binary for easy access",
Long: `Link nerdctl binary for easy access from the host.
This installs the binary in /usr/local/bin/nerdctl.`,
Use: "install",
Short: "Install nerdctl binary on the host",
Long: `Install nerdctl binary on the host.
The binary will be installed at /usr/local/bin/nerdctl.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(os.Args)
fmt.Println("nerdctl link")
fmt.Println("nerdctl install")
},
}

Expand Down
19 changes: 11 additions & 8 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ import (
// TODO replace $HOME env var.
var startCmd = &cobra.Command{
Use: "start",
Short: "Start colima VM",
Long: `Start (and/or provision) the VM with docker (and kubernetes
if --with-kubernetes is passed).
Short: "Start Colima",
Long: `Start Colima with the specified container runtime (and kubernetes if --with-kubernetes is passed).
Kubernetes requires at least 2 CPUs and 2.3GiB memory.
For verbose output, tail the log file "~/Library/Application Support/colima/out.log".
For verbose output, tail the log file "$HOME/Library/Caches/colima/out.log".
`,
Run: func(cmd *cobra.Command, args []string) {
cobra.CheckErr(app.Start())
Expand All @@ -36,18 +35,22 @@ const (
var appConfig config.Config

func init() {
runtimes := container.Names()
runtimes := strings.Join(container.Names(), ", ")

rootCmd.AddCommand(startCmd)
startCmd.Flags().BoolVarP(&appConfig.Kubernetes, "with-kubernetes", "k", false, "start VM with Kubernetes")
startCmd.Flags().StringVarP(&appConfig.Runtime, "runtime", "r", docker.Name, "container runtime, one of ["+strings.Join(runtimes, ", ")+"]")
startCmd.Flags().StringVarP(&appConfig.Runtime, "runtime", "r", docker.Name, "container runtime, one of ["+runtimes+"]")
startCmd.Flags().IntVarP(&appConfig.VM.CPU, "cpu", "c", defaultCPU, "number of CPUs")
startCmd.Flags().IntVarP(&appConfig.VM.Memory, "memory", "m", defaultMemory, "memory in GiB")
startCmd.Flags().IntVarP(&appConfig.VM.Disk, "disk", "d", defaultDisk, "disk size in GiB")
startCmd.Flags().IPSliceVarP(&appConfig.VM.DNS, "dns", "n", nil, "DNS nameservers for the VM")
startCmd.Flags().StringToStringVarP(&appConfig.VM.Env, "env", "e", nil, "environment variables for the VM")
startCmd.Flags().IPSliceVarP(&appConfig.VM.DNS, "dns", "n", nil, "DNS servers for the VM")

// internal
startCmd.Flags().IntVar(&appConfig.VM.SSHPort, "ssh-port", defaultSSHPort, "SSH port for the VM")
startCmd.Flags().MarkHidden("ssh-port")

// not sure of the usefulness of env vars for now considering that interactions will be with the containers, not the VM.
// leaving it undocumented until there is a need.
startCmd.Flags().StringToStringVarP(&appConfig.VM.Env, "env", "e", nil, "environment variables for the VM")
startCmd.Flags().MarkHidden("env")
}
4 changes: 2 additions & 2 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
// statusCmd represents the status command
var statusCmd = &cobra.Command{
Use: "status",
Short: "Show the status of the VM",
Long: `Show the status of the VM`,
Short: "Show the status of Colima",
Long: `Show the status of Colima`,
Run: func(cmd *cobra.Command, args []string) {
cobra.CheckErr(app.Status())
},
Expand Down
5 changes: 2 additions & 3 deletions cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
// stopCmd represents the stop command
var stopCmd = &cobra.Command{
Use: "stop",
Short: "Stop the VM",
Long: `Stop stops the VM to free up resources.
Short: "Stop Colima",
Long: `Stop stops Colima to free up resources.
The state of the VM is persisted at stop. A start afterwards
should return it back to its previous state.`,
Expand All @@ -19,5 +19,4 @@ should return it back to its previous state.`,

func init() {
rootCmd.AddCommand(stopCmd)

}
37 changes: 32 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package config

import (
"fmt"
"github.com/abiosoft/colima/util"
"gopkg.in/yaml.v3"
"log"
"net"
"os"
Expand Down Expand Up @@ -61,6 +63,29 @@ func init() {
}
}

const configFileName = "colima.yaml"

func configFile() string {
return filepath.Join(configDir, configFileName)
}

// Save saves the config.
func Save(c Config) error {
return util.WriteYAML(c, configFile())
}

// Load loads the config.
func Load() (Config, error) {
var c Config
b, err := os.ReadFile(configFile())
if err != nil {
return c, fmt.Errorf("could not load previous settings: %w", err)
}

err = yaml.Unmarshal(b, &c)
return c, err
}

// Config is the application config.
type Config struct {
// Virtual Machine
Expand All @@ -75,11 +100,13 @@ type Config struct {

// VM is virtual machine configuration.
type VM struct {
CPU int `yaml:"cpu"`
Disk int `yaml:"disk"`
Memory int `yaml:"memory"`
DNS []net.IP `yaml:"dns"` // DNS nameservers
Env map[string]string `yaml:"env"` // environment variables
CPU int `yaml:"cpu"`
Disk int `yaml:"disk"`
Memory int `yaml:"memory"`

// do not persist. i.e. discarded on VM shutdown
DNS []net.IP `yaml:"-"` // DNS nameservers
Env map[string]string `yaml:"-"` // environment variables

// internal use
SSHPort int `yaml:"-"`
Expand Down
6 changes: 4 additions & 2 deletions runtime/container/docker/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package docker

import (
"fmt"
"github.com/abiosoft/colima/util"
)

func (d dockerRuntime) setupSocketSymlink() error {
Expand Down Expand Up @@ -46,8 +45,11 @@ func (d dockerRuntime) setupInVM() error {
}

func (d dockerRuntime) fixUserPermission() error {
err := d.guest.Run("sudo", "usermod", "-aG", "docker", util.User())
user, err := d.vmUser()
if err != nil {
return fmt.Errorf("error retrieving user in the VM: %w", err)
}
if err := d.guest.Run("sudo", "usermod", "-aG", "docker", user); err != nil {
return fmt.Errorf("error fixing user permission: %w", err)
}
return nil
Expand Down
4 changes: 4 additions & 0 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ type GuestActions interface {
Start() error
// Stop shuts down the VM
Stop() error
// Created returns if the VM has been previously created.
Created() bool
// Running returns if the VM is currently running.
Running() bool
// Env retrieves environment variable in the VM.
Env(string) (string, error)
}

// Dependencies are dependencies that must exist on the host.
Expand Down
20 changes: 14 additions & 6 deletions runtime/vm/lima/lima.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lima

import (
_ "embed"
"fmt"
"github.com/abiosoft/colima/cli"
"github.com/abiosoft/colima/config"
"github.com/abiosoft/colima/runtime"
Expand Down Expand Up @@ -34,11 +35,6 @@ func limaConfDir() string {
return filepath.Join(home, ".lima", config.AppName())
}

func isConfigured() bool {
stat, err := os.Stat(limaConfDir())
return err == nil && stat.IsDir()
}

var _ vm.VM = (*limaVM)(nil)

type limaVM struct {
Expand All @@ -56,7 +52,7 @@ func (l limaVM) Dependencies() []string {
func (l limaVM) Start() error {
r := l.Init()

if isConfigured() {
if l.Created() {
return l.resume()
}

Expand Down Expand Up @@ -211,3 +207,15 @@ func (l limaVM) RunOutput(args ...string) (out string, err error) {
func (l limaVM) Host() runtime.HostActions {
return l.host
}

func (l limaVM) Env(s string) (string, error) {
if !l.Running() {
return "", fmt.Errorf("not running")
}
return l.RunOutput("echo", "$"+s)
}

func (l limaVM) Created() bool {
stat, err := os.Stat(limaConfDir())
return err == nil && stat.IsDir()
}
8 changes: 0 additions & 8 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,3 @@ func HomeDir() string {
}
return home
}

func User() string {
user := os.Getenv("USER")
if user == "" {
log.Fatal("could not retrieve OS user")
}
return user
}

0 comments on commit 525f02b

Please sign in to comment.