From c9dc4a38cdb3801f7b477e2e3635a2e992f22858 Mon Sep 17 00:00:00 2001 From: Brent Baude Date: Tue, 31 Oct 2023 16:12:37 -0500 Subject: [PATCH] Removing unneeded code many of the things in cmd/ were not needed after development of the library was done. also, the code used to build a machine was never used. Signed-off-by: Brent Baude --- cmd/dumpsummary/main.go | 33 ----------- cmd/dumpvms/main.go | 58 +++++++++++++++++--- cmd/dumpvs/main.go | 34 ------------ cmd/keycreate/main.go | 79 --------------------------- cmd/wmigen/main.go | 52 ------------------ pkg/machine/machine.go | 118 ---------------------------------------- 6 files changed, 49 insertions(+), 325 deletions(-) delete mode 100644 cmd/dumpsummary/main.go delete mode 100644 cmd/dumpvs/main.go delete mode 100644 cmd/keycreate/main.go delete mode 100644 cmd/wmigen/main.go delete mode 100644 pkg/machine/machine.go diff --git a/cmd/dumpsummary/main.go b/cmd/dumpsummary/main.go deleted file mode 100644 index 767ccae..0000000 --- a/cmd/dumpsummary/main.go +++ /dev/null @@ -1,33 +0,0 @@ -//go:build windows -// +build windows - -package main - -import ( - "encoding/json" - "fmt" - "os" - - "github.com/containers/libhvee/pkg/hypervctl" -) - -func main() { - var err error - - vmms := hypervctl.VirtualMachineManager{} - - summs, err := vmms.GetSummaryInformation(hypervctl.SummaryRequestNearAll) - if err != nil { - fmt.Printf("Could not retrieve virtual machine summaries: %s\n", err.Error()) - os.Exit(1) - } - - b, err := json.MarshalIndent(summs, "", "\t") - - if err != nil { - fmt.Println("Failed to generate output") - os.Exit(1) - } - - fmt.Println(string(b)) -} \ No newline at end of file diff --git a/cmd/dumpvms/main.go b/cmd/dumpvms/main.go index c29ac3b..355bed0 100644 --- a/cmd/dumpvms/main.go +++ b/cmd/dumpvms/main.go @@ -11,23 +11,63 @@ import ( "github.com/containers/libhvee/pkg/hypervctl" ) -func main() { - var err error +const ( + summary = "summary" + vms = "vms" +) +func getVms() (string, error) { vmms := hypervctl.VirtualMachineManager{} - vms, err := vmms.GetAll() if err != nil { - fmt.Printf("Could not retrieve virtual machines : %s\n", err.Error()) - os.Exit(1) + return "", fmt.Errorf("Could not retrieve virtual machines : %s\n", err.Error()) } - b, err := json.MarshalIndent(vms, "", "\t") + if err != nil { + return "", err + } + return string(b), nil +} +func dumpSummary() (string, error) { + vmms := hypervctl.VirtualMachineManager{} + summs, err := vmms.GetSummaryInformation(hypervctl.SummaryRequestNearAll) if err != nil { - fmt.Println("Failed to generate output") - os.Exit(1) + return "", fmt.Errorf("Could not retrieve virtual machine summaries: %v\n", err) + } + b, err := json.MarshalIndent(summs, "", "\t") + if err != nil { + return "", err } + return string(b), nil +} + +func printHelp() { + fmt.Printf("argument must be one of %q or %q", summary, vms) +} - fmt.Println(string(b)) +func main() { + var ( + err error + result string + ) + args := os.Args + if len(args) != 2 { + printHelp() + os.Exit(1) + } + if arg := args[1]; arg != summary && arg != vms { + printHelp() + os.Exit(1) + } + if args[1] == summary { + result, err = dumpSummary() + } else { + result, err = getVms() + } + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Println(result) } diff --git a/cmd/dumpvs/main.go b/cmd/dumpvs/main.go deleted file mode 100644 index d50f49a..0000000 --- a/cmd/dumpvs/main.go +++ /dev/null @@ -1,34 +0,0 @@ -//go:build windows -// +build windows - -package main - -import ( - "encoding/json" - "fmt" - "os" - - "github.com/containers/libhvee/pkg/hypervctl" -) - -func main() { - var err error - - vmms := hypervctl.VirtualMachineManager{} - - vms, err := vmms.GetAll() - if err != nil { - fmt.Printf("Could not retrieve virtual machines : %s\n", err.Error()) - os.Exit(1) - } - - b, err := json.MarshalIndent(vms, "", "\t") - - if err != nil { - fmt.Println("Failed to generate output") - os.Exit(1) - } - - fmt.Println(string(b)) - -} diff --git a/cmd/keycreate/main.go b/cmd/keycreate/main.go deleted file mode 100644 index 3f7ce83..0000000 --- a/cmd/keycreate/main.go +++ /dev/null @@ -1,79 +0,0 @@ -//go:build windows -// +build windows - -package main - -import ( - "fmt" - "time" - - "github.com/containers/libhvee/pkg/wmiext" -) - -func main() { - var service *wmiext.Service - var err error - - if service, err = wmiext.NewLocalService("root\\virtualization\\v2"); err != nil { - panic(err) - } - defer service.Close() - - item, err := service.SpawnInstance("Msvm_KvpExchangeDataItem") - if err != nil { - panic(err) - } - defer item.Close() - - _ = item.Put("Name", "jkey-"+fmt.Sprintf("%d", time.Now().Unix())) - _ = item.Put("Data", "jval-"+fmt.Sprintf("%d", time.Now().Unix())) - _ = item.Put("Source", 0) - - itemStr := item.GetCimText() - fmt.Println(itemStr) - - vmms, err := service.GetSingletonInstance("Msvm_VirtualSystemManagementService") - if err != nil { - panic(err) - } - - defer vmms.Close() - const wql = "Select * From Msvm_ComputerSystem Where ElementName='%s'" - - computerSystem, err := service.FindFirstInstance(fmt.Sprintf(wql, "New Virtual Machine")) - if err != nil { - panic(err) - } - defer computerSystem.Close() - - var job *wmiext.Instance - - e := vmms.BeginInvoke("AddKvpItems"). - In("TargetSystem", computerSystem). - In("DataItems", []string{itemStr}). - Execute() - - fmt.Printf("pre-get %v\n", e) - - fmt.Printf("Pointer -> %d", &job) - - e.Out("Job", &job) - - _ = e.End() - if err != nil { - panic(err) - } - - for { - status, _, _, err := job.GetAsAny("JobState") - if err != nil { - panic(err) - } - fmt.Printf("%v\n", status) - time.Sleep(100 * time.Millisecond) - job, _ = service.RefetchObject(job) - if status.(int32) == 7 { - break - } - } -} diff --git a/cmd/wmigen/main.go b/cmd/wmigen/main.go deleted file mode 100644 index 899e0a1..0000000 --- a/cmd/wmigen/main.go +++ /dev/null @@ -1,52 +0,0 @@ -package main - -import ( - "bufio" - "fmt" - "os" - "regexp" - "strings" -) - -func main() { - r := regexp.MustCompile(`^\s*([a-zA-Z0-9_]+)\s+(REF)?\s*([a-zA-Z0-9_-]+)(\[\])?\s*([^;:]*=[^;]*)?;?$`) - - fmt.Println("Paste MWI/CIM model definition and type Ctrl-D") - - var lines []string - scanner := bufio.NewScanner(os.Stdin) - for scanner.Scan() { - lines = append(lines, scanner.Text()) - } - - fmt.Printf("\nTransformed:\n") - fmt.Println("struct {") - for _, line := range lines { - match := r.FindStringSubmatch(line) - if match == nil { - continue - } - - val := match[1] - switch strings.ToLower(val) { - case "boolean": - val = "bool" - case "datetime": - val = "time.Time" - } - - var ref string - if match[2] == "REF" { - val = "string" - ref = "REF to " + match[1] - } - - suffix := match[5] - if len(suffix) > 0 || len(ref) > 0 { - suffix = " // " + ref + suffix - } - - fmt.Printf("\t%s %s%s%s\n", match[3], match[4], val, suffix) - } - fmt.Println("}") -} diff --git a/pkg/machine/machine.go b/pkg/machine/machine.go deleted file mode 100644 index 1866d66..0000000 --- a/pkg/machine/machine.go +++ /dev/null @@ -1,118 +0,0 @@ -//go:build windows -// +build windows - -//nolint:unused -package hypervctl - -import ( - "errors" - "fmt" - "strings" - - "github.com/containers/libhvee/pkg/hypervctl" -) - -// Machine represents the information needed to be a -// podman machine -type Machine struct { - diskSize uint64 - ignitionPath string - imagePath string - memory int64 - name string - processors uint - timezone string - username string - vm *hypervctl.VirtualMachine - volumes []hyperVVolume -} - -// hyperVVolume is a typed string representation of a mount -type hyperVVolume struct { - combinedPath string - guestPath string - hostPath string -} - -var ErrNotImplemented = errors.New("function not implemented") - -func newHyperVVolume(path string) (*hyperVVolume, error) { - vol := &hyperVVolume{combinedPath: path} - return vol.validate() -} - -func (v *hyperVVolume) validate() (*hyperVVolume, error) { - // todo, check each side of the split starts with / ? - // todo, should we check the host side to make sure it exists? - split := strings.Split(v.combinedPath, ":") - if len(split) != 2 { - return nil, fmt.Errorf("volume path %s is invalid", v.combinedPath) - } - return v, nil -} - -func NewMachine(name string) (*Machine, error) { - // TODO Check to make sure name is not used, return error - h := Machine{name: name} - return &h, nil -} - -func (m *Machine) withDiskSize(size uint64) *Machine { - m.diskSize = size - return m -} - -func (m *Machine) withIgnitionPath(path string) *Machine { - m.ignitionPath = path - return m -} - -func (m *Machine) withImagePath(path string) *Machine { - m.imagePath = path - return m -} - -func (m *Machine) withMemory(size int64) *Machine { - m.memory = size - return m -} - -func (m *Machine) withProcessors(processorCount uint) *Machine { - m.processors = processorCount - return m -} - -func (m *Machine) withTimeZone(tz string) *Machine { - m.timezone = tz - return m -} - -func (m *Machine) withUsername(name string) *Machine { - m.username = name - return m -} - -func (m *Machine) withVolume(vol hyperVVolume) *Machine { - m.volumes = append(m.volumes, vol) - return m -} - -// Create uses the builder to actually create the new virtual machine in hyperv -func (m *Machine) Create() (*hypervctl.VirtualMachine, error) { - return nil, ErrNotImplemented -} - -// ChangeDiskSize alters the size of machine's disk -func (m *Machine) ChangeDiskSize(newSize uint64) error { - return ErrNotImplemented -} - -// ChangeMemorySize changes the amount of memory allocated to the machine -func (m *Machine) ChangeMemorySize(newsize uint64) error { - return ErrNotImplemented -} - -// ChangeProcesorCounteranges the number of CPUs assigned to the machine -func (m *Machine) ChangeProcesorCounter(processorCount uint) error { - return ErrNotImplemented -}