Skip to content

Commit

Permalink
Merge pull request containers#17494 from ashley-cui/osapply
Browse files Browse the repository at this point in the history
Introduce podman machine os apply
  • Loading branch information
openshift-merge-robot authored Feb 15, 2023
2 parents d63d916 + 9abe8c7 commit 7b590d4
Show file tree
Hide file tree
Showing 16 changed files with 493 additions and 43 deletions.
9 changes: 0 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -396,15 +396,6 @@ bin/rootlessport: $(SOURCES) go.mod go.sum
.PHONY: rootlessport
rootlessport: bin/rootlessport

.PHONY: podman-remote-experimental
podman-remote-experimental: $(SRCBINDIR)/experimental/podman$(BINSFX)
$(SRCBINDIR)/experimental/podman$(BINSFX): $(SOURCES) go.mod go.sum | $(SRCBINDIR)
$(GOCMD) build \
$(BUILDFLAGS) \
$(GO_LDFLAGS) '$(LDFLAGS_PODMAN)' \
-tags "${REMOTETAGS} experimental" \
-o $@ ./cmd/podman

###
### Secondary binary-build targets
###
Expand Down
7 changes: 3 additions & 4 deletions cmd/podman/machine/os.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//go:build (amd64 || arm64) && experimental
//go:build amd64 || arm64
// +build amd64 arm64
// +build experimental

package machine

Expand All @@ -13,8 +12,8 @@ import (
var (
OSCmd = &cobra.Command{
Use: "os",
Short: "Manage a virtual machine's os",
Long: "Manage a virtual machine's operating system",
Short: "Manage a Podman virtual machine's OS",
Long: "Manage a Podman virtual machine's operating system",
PersistentPreRunE: validate.NoOp,
RunE: validate.SubCommandExists,
}
Expand Down
42 changes: 32 additions & 10 deletions cmd/podman/machine/os/apply.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,60 @@
//go:build (amd64 || arm64) && experimental
//go:build amd64 || arm64
// +build amd64 arm64
// +build experimental

package machineos
package os

import (
"fmt"

"github.com/containers/podman/v4/cmd/podman/common"
"github.com/containers/podman/v4/cmd/podman/machine"
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/cmd/podman/validate"
"github.com/containers/podman/v4/pkg/machine/os"
"github.com/spf13/cobra"
)

var (
applyCmd = &cobra.Command{
Use: "apply",
Short: "Apply OCI image to existing VM",
Long: "Apply custom layers from a containerized Fedora CoreOS image on top of an existing VM",
Use: "apply [options] IMAGE [NAME]",
Short: "Apply an OCI image to a Podman Machine's OS",
Long: "Apply custom layers from a containerized Fedora CoreOS OCI image on top of an existing VM",
PersistentPreRunE: validate.NoOp,
Args: cobra.RangeArgs(1, 2),
RunE: apply,
ValidArgsFunction: common.AutocompleteImages,
Example: `podman machine os apply myimage`,
}
)

var restart bool

func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Command: applyCmd,
Parent: machine.OSCmd,
})
flags := applyCmd.Flags()

restartFlagName := "restart"
flags.BoolVar(&restart, restartFlagName, false, "Restart VM to apply changes")
}

func apply(cmd *cobra.Command, args []string) error {
fmt.Println("Applying..")
return nil
vmName := ""
if len(args) == 2 {
vmName = args[1]
}
managerOpts := ManagerOpts{
VMName: vmName,
CLIArgs: args,
Restart: restart,
}
osManager, err := NewOSManager(managerOpts)
if err != nil {
return err
}

applyOpts := os.ApplyOptions{
Image: args[0],
}
return osManager.Apply(args[0], applyOpts)
}
91 changes: 91 additions & 0 deletions cmd/podman/machine/os/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//go:build amd64 || arm64
// +build amd64 arm64

package os

import (
"bufio"
"errors"
"os"
"strings"

machineconfig "github.com/containers/common/pkg/machine"
"github.com/containers/podman/v4/cmd/podman/machine"
pkgMachine "github.com/containers/podman/v4/pkg/machine"
pkgOS "github.com/containers/podman/v4/pkg/machine/os"
)

type ManagerOpts struct {
VMName string
CLIArgs []string
Restart bool
}

// NewOSManager creates a new OSManager depending on the mode of the call
func NewOSManager(opts ManagerOpts) (pkgOS.Manager, error) {
// If a VM name is specified, then we know that we are not inside a
// Podman VM, but rather outside of it.
if machineconfig.IsPodmanMachine() && opts.VMName == "" {
return guestOSManager()
}
return machineOSManager(opts)
}

// guestOSManager returns an OSmanager for inside-VM operations
func guestOSManager() (pkgOS.Manager, error) {
dist := GetDistribution()
switch {
case dist.Name == "fedora" && dist.Variant == "coreos":
return &pkgOS.OSTree{}, nil
default:
return nil, errors.New("unsupported OS")
}
}

// machineOSManager returns an os manager that manages outside the VM.
func machineOSManager(opts ManagerOpts) (pkgOS.Manager, error) {
vmName := opts.VMName
if opts.VMName == "" {
vmName = pkgMachine.DefaultMachineName
}
provider := machine.GetSystemDefaultProvider()
vm, err := provider.LoadVMByName(vmName)
if err != nil {
return nil, err
}
return &pkgOS.MachineOS{
VM: vm,
Args: opts.CLIArgs,
VMName: vmName,
Restart: opts.Restart,
}, nil
}

type Distribution struct {
Name string
Variant string
}

// GetDistribution checks the OS distribution
func GetDistribution() Distribution {
dist := Distribution{
Name: "unknown",
Variant: "unknown",
}
f, err := os.Open("/etc/os-release")
if err != nil {
return dist
}
defer f.Close()

l := bufio.NewScanner(f)
for l.Scan() {
if strings.HasPrefix(l.Text(), "ID=") {
dist.Name = strings.TrimPrefix(l.Text(), "ID=")
}
if strings.HasPrefix(l.Text(), "VARIANT_ID=") {
dist.Variant = strings.Trim(strings.TrimPrefix(l.Text(), "VARIANT_ID="), "\"")
}
}
return dist
}
7 changes: 7 additions & 0 deletions cmd/podman/machine/os/os_unsupported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !amd64 && !arm64
// +build !amd64,!arm64

package os

// init do not register _podman machine os_ command on unsupported platforms
func init() {}
1 change: 1 addition & 0 deletions cmd/podman/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
_ "github.com/containers/podman/v4/cmd/podman/images"
_ "github.com/containers/podman/v4/cmd/podman/kube"
_ "github.com/containers/podman/v4/cmd/podman/machine"
_ "github.com/containers/podman/v4/cmd/podman/machine/os"
_ "github.com/containers/podman/v4/cmd/podman/manifest"
_ "github.com/containers/podman/v4/cmd/podman/networks"
_ "github.com/containers/podman/v4/cmd/podman/pods"
Expand Down
8 changes: 0 additions & 8 deletions cmd/podman/main_experimental.go

This file was deleted.

2 changes: 1 addition & 1 deletion docs/source/markdown/podman-machine-inspect.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
podman\-machine\-inspect - Inspect one or more virtual machines

## SYNOPSIS
**podman machine inspect** [*options] *name* ...
**podman machine inspect** [*options*] *name* ...

## DESCRIPTION

Expand Down
41 changes: 41 additions & 0 deletions docs/source/markdown/podman-machine-os-apply.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
% podman-machine-os-apply 1

## NAME
podman\-machine\-os\-apply - Apply an OCI image to a Podman Machine's OS

## SYNOPSIS
**podman machine os apply** [*options*] *image* [vm]

## DESCRIPTION

Apply machine OS changes from an OCI image.

VM's that use OS's that use rpm-ostreee have the capability to rebase itself from the content of an OCI image.
`podman machine image apply` takes an OCI image with container native ostree functionality and rebases itself on that image.

By default, Podman machines on Mac and Linux use an rpm-ostree based distrubition (Fedora CoreOS).

For more information, please see the [rpm-ostree docs](https://coreos.github.io/rpm-ostree/container/).

## OPTIONS

#### **--help**

Print usage statement.

#### **--restart**

Restart VM after applying changes.

## EXAMPLES

```
$ podman machine os apply quay.io/podman_next
$ podman machine os apply quay.io/podman_next podman-machine-defualt
```

## SEE ALSO
**[podman(1)](podman.1.md)**, **[podman-machine(1)](podman-machine.1.md)**, **[podman-machine-os(1)](podman-machine-os.1.md)**

## HISTORY
February 2023, Originally compiled by Ashley Cui <[email protected]>
22 changes: 22 additions & 0 deletions docs/source/markdown/podman-machine-os.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
% podman-machine-os 1

## NAME
podman\-machine\-os - Manage a Podman virtual machine's OS

## SYNOPSIS
**podman machine os** *subcommand*

## DESCRIPTION
`podman machine os` is a set of subcommands that manage a Podman virtual machine's operating system.

## SUBCOMMANDS

| Command | Man Page | Description |
|---------|--------------------------------------------------------------|----------------------------------------------|
| apply | [podman-machine-os-apply(1)](podman-machine-os-apply.1.md) | Apply an OCI image to a Podman Machine's OS |

## SEE ALSO
**[podman(1)](podman.1.md)**, **[podman-machine(1)](podman-machine.1.md)**, **[podman-machine-os-apply(1)](podman-machine-os-apply.1.md)**

## HISTORY
February 2023, Originally compiled by Ashley Cui <[email protected]>
23 changes: 12 additions & 11 deletions docs/source/markdown/podman-machine.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,21 @@ environment variable while the machines are running can lead to unexpected behav

## SUBCOMMANDS

| Command | Man Page | Description |
|---------|------------------------------------------------------|-----------------------------------|
| info | [podman-machine-info(1)](podman-machine-info.1.md) | Display machine host info |
| init | [podman-machine-init(1)](podman-machine-init.1.md) | Initialize a new virtual machine |
| Command | Man Page | Description |
|---------|-----------------------------------------------------------|--------------------------------------|
| info | [podman-machine-info(1)](podman-machine-info.1.md) | Display machine host info |
| init | [podman-machine-init(1)](podman-machine-init.1.md) | Initialize a new virtual machine |
| inspect | [podman-machine-inspect(1)](podman-machine-inspect.1.md) | Inspect one or more virtual machines |
| list | [podman-machine-list(1)](podman-machine-list.1.md) | List virtual machines |
| rm | [podman-machine-rm(1)](podman-machine-rm.1.md) | Remove a virtual machine |
| set | [podman-machine-set(1)](podman-machine-set.1.md) | Sets a virtual machine setting |
| ssh | [podman-machine-ssh(1)](podman-machine-ssh.1.md) | SSH into a virtual machine |
| start | [podman-machine-start(1)](podman-machine-start.1.md) | Start a virtual machine |
| stop | [podman-machine-stop(1)](podman-machine-stop.1.md) | Stop a virtual machine |
| list | [podman-machine-list(1)](podman-machine-list.1.md) | List virtual machines |
| os | [podman-machine-os(1)](podman-machine-os.1.md) | Manage a Podman virtual machine's OS |
| rm | [podman-machine-rm(1)](podman-machine-rm.1.md) | Remove a virtual machine |
| set | [podman-machine-set(1)](podman-machine-set.1.md) | Sets a virtual machine setting |
| ssh | [podman-machine-ssh(1)](podman-machine-ssh.1.md) | SSH into a virtual machine |
| start | [podman-machine-start(1)](podman-machine-start.1.md) | Start a virtual machine |
| stop | [podman-machine-stop(1)](podman-machine-stop.1.md) | Stop a virtual machine |

## SEE ALSO
**[podman(1)](podman.1.md)**, **[podman-machine-info(1)](podman-machine-info.1.md)**, **[podman-machine-init(1)](podman-machine-init.1.md)**, **[podman-machine-list(1)](podman-machine-list.1.md)**, **[podman-machine-rm(1)](podman-machine-rm.1.md)**, **[podman-machine-ssh(1)](podman-machine-ssh.1.md)**, **[podman-machine-start(1)](podman-machine-start.1.md)**, **[podman-machine-stop(1)](podman-machine-stop.1.md)**, **[podman-machine-inspect(1)](podman-machine-inspect.1.md)**
**[podman(1)](podman.1.md)**, **[podman-machine-info(1)](podman-machine-info.1.md)**, **[podman-machine-init(1)](podman-machine-init.1.md)**, **[podman-machine-list(1)](podman-machine-list.1.md)**, **[podman-machine-os(1)](podman-machine-os.1.md)**, **[podman-machine-rm(1)](podman-machine-rm.1.md)**, **[podman-machine-ssh(1)](podman-machine-ssh.1.md)**, **[podman-machine-start(1)](podman-machine-start.1.md)**, **[podman-machine-stop(1)](podman-machine-stop.1.md)**, **[podman-machine-inspect(1)](podman-machine-inspect.1.md)**

## HISTORY
March 2021, Originally compiled by Ashley Cui <[email protected]>
27 changes: 27 additions & 0 deletions pkg/machine/e2e/config_os_apply_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package e2e_test

// type applyMachineOS struct {
// restart bool

// cmd []string
// }

// func (a *applyMachineOS) buildCmd(m *machineTestBuilder) []string {
// cmd := []string{"machine", "os", "apply"}
// if a.restart {
// cmd = append(cmd, "--restart")
// }

// a.cmd = cmd
// return cmd
// }

// func (a *applyMachineOS) withRestart() *applyMachineOS {
// a.restart = true
// return a
// }

// func (a *applyMachineOS) args(cmd []string) *applyMachineOS {
// a.cmd = cmd
// return a
// }
Loading

0 comments on commit 7b590d4

Please sign in to comment.