Skip to content

Commit

Permalink
Add sample app initing/starting a podman-machine VM
Browse files Browse the repository at this point in the history
  • Loading branch information
cfergeau committed Jun 17, 2024
1 parent 923f926 commit 668e6e8
Show file tree
Hide file tree
Showing 1,109 changed files with 503,517 additions and 2 deletions.
187 changes: 186 additions & 1 deletion cmd/macadam/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,200 @@
package main

import (
"errors"
"fmt"
"log/slog"
"os"

"github.com/crc-org/macadam/pkg/cmdline"

_ "github.com/containers/podman/v5/pkg/machine"
"github.com/containers/common/pkg/config"
ldefine "github.com/containers/podman/v5/libpod/define"

"github.com/containers/podman/v5/pkg/machine"
"github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/podman/v5/pkg/machine/env"
provider2 "github.com/containers/podman/v5/pkg/machine/provider"
"github.com/containers/podman/v5/pkg/machine/shim"
"github.com/containers/podman/v5/pkg/machine/vmconfigs"

_ "github.com/spf13/cobra"
)

var (
initOpts = define.InitOptions{}
defaultMachineName = machine.DefaultMachineName
)

type PodmanMachine struct {
initOpts define.InitOptions
provider vmconfigs.VMProvider
config *vmconfigs.MachineConfig
}

func startMachine(m *PodmanMachine) error {

machineName := m.config.Name
dirs, err := env.GetMachineDirs(m.provider.VMType())
if err != nil {
return err
}
/*
mc, err := vmconfigs.LoadMachineByName(machineName, dirs)
if err != nil {
return err
}
*/

fmt.Printf("Starting machine %q\n", machineName)

startOpts := machine.StartOptions{
NoInfo: false,
Quiet: false,
}
if err := shim.Start(m.config, m.provider, dirs, startOpts); err != nil {
return err
}
fmt.Printf("Machine %q started successfully\n", machineName)
//newMachineEvent(events.Start, events.Event{Name: vmName})
return nil
}

func initMachine(initOpts define.InitOptions) (*PodmanMachine, error) {
machine := PodmanMachine{}
provider, err := provider2.Get()
if err != nil {
return nil, err
}
machine.provider = provider

// The vmtype names need to be reserved and cannot be used for podman machine names
if _, err := define.ParseVMType(initOpts.Name, define.UnknownVirt); err == nil {
return nil, fmt.Errorf("cannot use %q for a machine name", initOpts.Name)
}

if !ldefine.NameRegex.MatchString(initOpts.Username) {
return nil, fmt.Errorf("invalid username %q: %w", initOpts.Username, ldefine.RegexError)
}

// Check if machine already exists
vmConfig, exists, err := shim.VMExists(initOpts.Name, []vmconfigs.VMProvider{provider})
if err != nil {
return nil, err
}
machine.config = vmConfig

// machine exists, return error
if exists {
return &machine, fmt.Errorf("%s: %w", initOpts.Name, define.ErrVMAlreadyExists)
}

/*
// check if a system connection already exists
cons, err := registry.PodmanConfig().ContainersConfDefaultsRO.GetAllConnections()
if err != nil {
return err
}
for _, con := range cons {
if con.ReadWrite {
for _, connection := range []string{initOpts.Name, fmt.Sprintf("%s-root", initOpts.Name)} {
if con.Name == connection {
return fmt.Errorf("system connection %q already exists. consider a different machine name or remove the connection with `podman system connection rm`", connection)
}
}
}
}
*/

for idx, vol := range initOpts.Volumes {
initOpts.Volumes[idx] = os.ExpandEnv(vol)
}

// TODO need to work this back in
// if finished, err := vm.Init(initOpts); err != nil || !finished {
// // Finished = true, err = nil - Success! Log a message with further instructions
// // Finished = false, err = nil - The installation is partially complete and podman should
// // exit gracefully with no error and no success message.
// // Examples:
// // - a user has chosen to perform their own reboot
// // - reexec for limited admin operations, returning to parent
// // Finished = *, err != nil - Exit with an error message
// return err
// }

err = shim.Init(initOpts, provider)
if err != nil {
return nil, err
}

/*
newMachineEvent(events.Init, events.Event{Name: initOpts.Name})
*/
fmt.Println("Machine init complete")

vmConfig, exists, err = shim.VMExists(initOpts.Name, []vmconfigs.VMProvider{provider})
if err != nil {
return nil, err
}
machine.config = vmConfig

/*
now := false
if now {
return startMachine(initOpts.Name, provider)
}
*/
extra := ""

if initOpts.Name != defaultMachineName {
extra = " " + initOpts.Name
}
fmt.Printf("To start your machine run:\n\n\tpodman machine start%s\n\n", extra)
return &machine, err
}

func main() {
slog.Info(fmt.Sprintf("macadam version %s", cmdline.Version()))

defaultConfig, err := config.New(&config.Options{
SetDefault: true, // This makes sure that following calls to config.Default() return this config
})
if err != nil {
os.Exit(1)
}

// defaults from cmd/podman/machine/init.go
initOpts.Name = defaultMachineName

initOpts.CPUS = defaultConfig.Machine.CPUs
initOpts.DiskSize = defaultConfig.Machine.DiskSize
initOpts.Memory = defaultConfig.Machine.Memory
defaultTz := defaultConfig.TZ()
if len(defaultTz) < 1 {
defaultTz = "local"
}
initOpts.TimeZone = defaultTz
initOpts.ReExec = false
initOpts.Username = defaultConfig.Machine.User
initOpts.Image = defaultConfig.Machine.Image
initOpts.Volumes = defaultConfig.Machine.Volumes.Get()
initOpts.USBs = []string{}
initOpts.VolumeDriver = ""
initOpts.IgnitionPath = ""
initOpts.Rootful = false
userModeNetworking := false
initOpts.UserModeNetworking = &userModeNetworking
// user-mode networking

machine, err := initMachine(initOpts)
if err != nil && !errors.Is(err, define.ErrVMAlreadyExists) {
slog.Error(err.Error())
}
if err := startMachine(machine); err != nil {
slog.Error(err.Error())
}
/*
if err != nil || errors.Is(err, define.ErrVMAlreadyExists)
{
*/
}
28 changes: 27 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
)

require github.com/containers/common v0.59.1

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
Expand All @@ -19,34 +21,46 @@ require (
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/bytedance/sonic v1.10.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/cilium/ebpf v0.11.0 // indirect
github.com/containerd/cgroups/v3 v3.0.3 // indirect
github.com/containerd/containerd v1.7.17 // indirect
github.com/containerd/errdefs v0.1.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.15.1 // indirect
github.com/containerd/typeurl/v2 v2.1.1 // indirect
github.com/containers/buildah v1.36.0 // indirect
github.com/containers/common v0.59.1 // indirect
github.com/containers/gvisor-tap-vsock v0.7.4-0.20240408151405-d744d71db363 // indirect
github.com/containers/image/v5 v5.31.0 // indirect
github.com/containers/libhvee v0.7.1 // indirect
github.com/containers/libtrust v0.0.0-20230121012942-c1716e8a8d01 // indirect
github.com/containers/ocicrypt v1.1.10 // indirect
github.com/containers/psgo v1.9.0 // indirect
github.com/containers/storage v1.54.0 // indirect
github.com/containers/winquit v1.1.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.1-0.20231103132048-7d375ecc2b09 // indirect
github.com/crc-org/crc/v2 v2.36.0 // indirect
github.com/crc-org/vfkit v0.5.1 // indirect
github.com/cyberphone/json-canonicalization v0.0.0-20231217050601-ba74d44ecf5f // indirect
github.com/cyphar/filepath-securejoin v0.2.5 // indirect
github.com/digitalocean/go-libvirt v0.0.0-20220804181439-8648fbde413e // indirect
github.com/digitalocean/go-qemu v0.0.0-20230711162256-2e3d0186973e // indirect
github.com/disiqueira/gotree/v3 v3.0.2 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker v26.1.3+incompatible // indirect
github.com/docker/docker-credential-helpers v0.8.1 // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fsouza/go-dockerclient v1.11.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect
github.com/go-jose/go-jose/v3 v3.0.3 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand All @@ -61,6 +75,11 @@ require (
github.com/go-openapi/strfmt v0.23.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-openapi/validate v0.24.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.17.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/godbus/dbus/v5 v5.1.1-0.20230522191255-76236955d466 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
Expand All @@ -70,10 +89,13 @@ require (
github.com/gorilla/mux v1.8.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/jinzhu/copier v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.8 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/letsencrypt/boulder v0.0.0-20230907030200-6d76a0f91e1e // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
Expand Down Expand Up @@ -104,6 +126,7 @@ require (
github.com/opencontainers/selinux v1.11.0 // indirect
github.com/openshift/imagebuilder v1.2.9 // indirect
github.com/ostreedev/ostree-go v0.0.0-20210805093236-719684c64e4f // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/proglottis/gpgme v0.1.3 // indirect
Expand All @@ -122,6 +145,8 @@ require (
github.com/titanous/rocacheck v0.0.0-20171023193734-afe73141d399 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
github.com/vbatts/tar-split v0.11.5 // indirect
github.com/vbauerster/mpb/v8 v8.7.3 // indirect
Expand All @@ -133,6 +158,7 @@ require (
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
golang.org/x/net v0.25.0 // indirect
Expand Down
Loading

0 comments on commit 668e6e8

Please sign in to comment.