Skip to content

Commit

Permalink
Add --layer flag for Ubuntu container layer (abiosoft#281)
Browse files Browse the repository at this point in the history
* core: add ubuntu layer

Signed-off-by: Abiola Ibrahim <[email protected]>

* core: layer: preserve home dir

Signed-off-by: Abiola Ibrahim <[email protected]>

* core: layer: preserve chroot exec dir

Signed-off-by: Abiola Ibrahim <[email protected]>

* core: layer: combine chroot scripts into one

Signed-off-by: Abiola Ibrahim <[email protected]>

* net: fix interface gateway

Signed-off-by: Abiola Ibrahim <[email protected]>

* core: layer: more fixes

Signed-off-by: Abiola Ibrahim <[email protected]>

* core: layer: refactor and rename

Signed-off-by: Abiola Ibrahim <[email protected]>

* chore: refactor logger

Signed-off-by: Abiola Ibrahim <[email protected]>

* volume: revert default mount type to sshfs

* log: enable trace level log

* ci: add layer to integration test
  • Loading branch information
abiosoft committed May 13, 2022
1 parent c42a673 commit acb4cd0
Show file tree
Hide file tree
Showing 28 changed files with 809 additions and 317 deletions.
50 changes: 40 additions & 10 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,17 @@ jobs:
- name: Stop
run: colima stop

- name: Kubernetes
run: colima start --runtime docker --kubernetes
- name: Start Colima with Layer
run: colima start --runtime docker --layer

- name: Delay
run: sleep 20
run: sleep 10

- name: Validate Kubernetes
run: kubectl cluster-info && kubectl version && kubectl get nodes -o wide
- name: Validate Layer
run: colima ssh -- cat /etc/os-release

- name: Stop
run: colima stop

- name: Teardown
run: colima delete -f
Expand Down Expand Up @@ -142,6 +145,18 @@ jobs:
- name: Stop
run: colima stop

- name: Start Colima with Layer
run: colima start --runtime docker --arch aarch64 --layer

- name: Delay
run: sleep 10

- name: Validate Layer
run: colima ssh -- cat /etc/os-release

- name: Stop
run: colima stop

- name: Teardown
run: colima delete -f

Expand Down Expand Up @@ -177,14 +192,17 @@ jobs:
- name: Stop
run: colima stop

- name: Kubernetes
run: colima start --runtime containerd --kubernetes
- name: Start Colima with Layer
run: colima start --runtime containerd --layer

- name: Delay
run: sleep 20
run: sleep 10

- name: Validate Kubernetes
run: kubectl cluster-info && kubectl version && kubectl get nodes -o wide
- name: Validate Layer
run: colima ssh -- cat /etc/os-release

- name: Stop
run: colima stop

- name: Teardown
run: colima delete -f
Expand Down Expand Up @@ -221,5 +239,17 @@ jobs:
- name: Stop
run: colima stop

- name: Start Colima with Layer
run: colima start --runtime containerd --arch aarch64 --layer

- name: Delay
run: sleep 10

- name: Validate Layer
run: colima ssh -- cat /etc/os-release

- name: Stop
run: colima stop

- name: Teardown
run: colima delete -f
58 changes: 54 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package app
import (
"context"
"fmt"
"os"
"strings"

"github.com/abiosoft/colima/cli"
"github.com/abiosoft/colima/config"
"github.com/abiosoft/colima/config/configmanager"
"github.com/abiosoft/colima/environment"
"github.com/abiosoft/colima/environment/container/docker"
"github.com/abiosoft/colima/environment/container/kubernetes"
"github.com/abiosoft/colima/environment/container/ubuntu"
"github.com/abiosoft/colima/environment/host"
"github.com/abiosoft/colima/environment/vm/lima"
log "github.com/sirupsen/logrus"
Expand All @@ -19,7 +23,7 @@ type App interface {
Start(config.Config) error
Stop(force bool) error
Delete() error
SSH(...string) error
SSH(layer bool, args ...string) error
Status() error
Version() error
Runtime() (string, error)
Expand Down Expand Up @@ -64,14 +68,22 @@ func (c colimaApp) Start(conf config.Config) error {
}
containers = append(containers, env)
}
// kubernetes should come last
// kubernetes should come after required runtime
if conf.Kubernetes.Enabled {
env, err := c.containerEnvironment(kubernetes.Name)
if err != nil {
return err
}
containers = append(containers, env)
}
// ubuntu layer should come last
if conf.Layer {
env, err := c.containerEnvironment(ubuntu.Name)
if err != nil {
return err
}
containers = append(containers, env)
}

// the order for start is:
// vm start -> container runtime provision -> container runtime start
Expand All @@ -83,9 +95,12 @@ func (c colimaApp) Start(conf config.Config) error {

// provision and start container runtimes
for _, cont := range containers {
log := log.WithField("context", cont.Name())
log.Println("provisioning ...")
if err := cont.Provision(ctx); err != nil {
return fmt.Errorf("error provisioning %s: %w", cont.Name(), err)
}
log.Println("starting ...")
if err := cont.Start(ctx); err != nil {
return fmt.Errorf("error starting %s: %w", cont.Name(), err)
}
Expand Down Expand Up @@ -117,6 +132,10 @@ func (c colimaApp) Stop(force bool) error {
// stop happens in reverse of start
for i := len(containers) - 1; i >= 0; i-- {
cont := containers[i]

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

if err := cont.Stop(ctx); err != nil {
// failure to stop a container runtime is not fatal
// it is only meant for graceful shutdown.
Expand Down Expand Up @@ -154,6 +173,10 @@ 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 ...")

if err := cont.Teardown(ctx); err != nil {
// failure here is not fatal
log.Warnln(fmt.Errorf("error during teardown of %s: %w", cont.Name(), err))
Expand All @@ -175,12 +198,34 @@ func (c colimaApp) Delete() error {
return nil
}

func (c colimaApp) SSH(args ...string) error {
func (c colimaApp) SSH(layer bool, args ...string) error {
if !c.guest.Running() {
return fmt.Errorf("%s not running", config.Profile().DisplayName)
}

return c.guest.RunInteractive(args...)
cmdArgs, inLayer, err := lima.ShowSSH(config.Profile().ID, layer, "args")
if err != nil {
return fmt.Errorf("error getting ssh config: %w", err)
}

if !layer || !inLayer {
return c.guest.RunInteractive(args...)
}

wd, err := os.Getwd()
if err != nil {
log.Debug(fmt.Errorf("cannot get working dir: %w", err))
}

if len(args) > 0 {
args = append([]string{"-q", "-t", "127.0.0.1", "--"}, args...)
} else if wd != "" {
args = []string{"-q", "-t", "127.0.0.1", "--", "cd " + wd + " 2> /dev/null; bash --login"}
}

args = append(strings.Fields(cmdArgs), args...)
return cli.CommandInteractive("ssh", args...).Run()

}

func (c colimaApp) Status() error {
Expand Down Expand Up @@ -277,6 +322,11 @@ func (c colimaApp) currentContainerEnvironments() ([]environment.Container, erro
containers = append(containers, k)
}

// detect and add ubuntu layer
if u, err := c.containerEnvironment(ubuntu.Name); err == nil && u.Running() {
containers = append(containers, u)
}

return containers, nil
}

Expand Down
22 changes: 16 additions & 6 deletions cli/chain.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package cli

import (
"context"
"fmt"
"io/ioutil"
"time"

log "github.com/sirupsen/logrus"
)

// CtxKeyQuiet is the context key to mute the chain.
var CtxKeyQuiet = struct{ quiet bool }{quiet: true}

// New creates a new runner instance.
func New(name string) CommandChain {
return &namedCommandChain{
Expand All @@ -23,9 +28,9 @@ type cFunc struct {
// commands are executed in order.
type CommandChain interface {
// Init initiates a new runner using the current instance.
Init() *ActiveCommandChain
Init(ctx context.Context) *ActiveCommandChain
// Logger returns the instance logger.
Logger() *log.Entry
Logger(ctx context.Context) *log.Entry
}

var _ CommandChain = (*namedCommandChain)(nil)
Expand All @@ -35,16 +40,21 @@ type namedCommandChain struct {
log *log.Entry
}

func (n namedCommandChain) Logger() *log.Entry {
func (n namedCommandChain) Logger(ctx context.Context) *log.Entry {
if quiet, _ := ctx.Value(CtxKeyQuiet).(bool); quiet {
l := log.New()
l.SetOutput(ioutil.Discard)
return l.WithContext(ctx)
}
if n.log == nil {
n.log = log.WithField("context", n.name)
n.log = log.WithField("context", n.name).WithContext(ctx)
}
return n.log
}

func (n namedCommandChain) Init() *ActiveCommandChain {
func (n namedCommandChain) Init(ctx context.Context) *ActiveCommandChain {
return &ActiveCommandChain{
log: n.Logger(),
log: n.Logger(ctx),
}
}

Expand Down
17 changes: 17 additions & 0 deletions cli/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"fmt"
"os"
"os/exec"
"strconv"

log "github.com/sirupsen/logrus"
)

var runner commandRunner = &defaultCommandRunner{}
Expand Down Expand Up @@ -35,6 +38,9 @@ func (d defaultCommandRunner) Command(command string, args ...string) *exec.Cmd
cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

log.Trace("cmd ", quotedArgs(cmd.Args))

return cmd
}

Expand All @@ -43,9 +49,20 @@ func (d defaultCommandRunner) CommandInteractive(command string, args ...string)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

log.Trace("cmd int ", quotedArgs(cmd.Args))

return cmd
}

func quotedArgs(args []string) string {
var q []string
for _, s := range args {
q = append(q, strconv.Quote(s))
}
return fmt.Sprintf("%v", q)
}

// Prompt prompts for input with a question. It returns true only if answer is y or Y.
func Prompt(question string) bool {
fmt.Print(question)
Expand Down
2 changes: 1 addition & 1 deletion cmd/nerdctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ It is recommended to specify '--' to differentiate from Colima flags.
}

nerdctlArgs := append([]string{"sudo", "nerdctl"}, args...)
return app.SSH(nerdctlArgs...)
return app.SSH(false, nerdctlArgs...)
},
}

Expand Down
21 changes: 14 additions & 7 deletions cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ func Cmd() *cobra.Command {

// rootCmdArgs holds all flags configured in root Cmd
var rootCmdArgs struct {
Profile string
Verbose bool
Profile string
Verbose bool
VeryVerbose bool
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -60,17 +61,23 @@ func Execute() {

func init() {
rootCmd.PersistentFlags().BoolVarP(&rootCmdArgs.Verbose, "verbose", "v", rootCmdArgs.Verbose, "enable verbose log")
rootCmd.PersistentFlags().BoolVar(&rootCmdArgs.VeryVerbose, "very-verbose", rootCmdArgs.VeryVerbose, "enable more verbose log")
rootCmd.PersistentFlags().StringVarP(&rootCmdArgs.Profile, "profile", "p", "default", "profile name, for multiple instances")
}

func initLog() error {
// general log output
log.SetOutput(logrus.New().Writer())
log.SetFlags(0)

if rootCmdArgs.Verbose {
cli.Settings.Verbose = rootCmdArgs.Verbose
cli.Settings.Verbose = true
logrus.SetLevel(logrus.DebugLevel)
}
if rootCmdArgs.VeryVerbose {
cli.Settings.Verbose = true
logrus.SetLevel(logrus.TraceLevel)
}

// general log output
log.SetOutput(logrus.StandardLogger().Writer())
log.SetFlags(0)

return nil
}
10 changes: 9 additions & 1 deletion cmd/ssh-config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"fmt"

"github.com/abiosoft/colima/cmd/root"
"github.com/abiosoft/colima/config"
"github.com/abiosoft/colima/environment/vm/lima"
Expand All @@ -14,16 +16,22 @@ var sshConfigCmd = &cobra.Command{
Long: `Show configuration of the SSH connection to the VM.`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return lima.ShowSSH(config.Profile().ID, sshConfigCmdArgs.format)
out, _, err := lima.ShowSSH(config.Profile().ID, sshConfigCmdArgs.layer, sshConfigCmdArgs.format)
if err == nil {
fmt.Println(out)
}
return err
},
}

var sshConfigCmdArgs struct {
format string
layer bool
}

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

sshConfigCmd.Flags().StringVarP(&sshConfigCmdArgs.format, "format", "f", "config", "format (config, cmd)")
sshConfigCmd.Flags().BoolVarP(&sshCmdArgs.layer, "layer", "l", true, "config for the Ubuntu layer (if enabled)")
}
Loading

0 comments on commit acb4cd0

Please sign in to comment.