forked from containers/podman
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request containers#17494 from ashley-cui/osapply
Introduce podman machine os apply
- Loading branch information
Showing
16 changed files
with
493 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
// } |
Oops, something went wrong.