Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove old qemu create and update #324

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/command/create/guest/create-guest.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func createGuest(args []string, IDtype string) (err error) {
if err != nil {
return
}
err = config.CreateVm(vmr, c)
err = config.Create(vmr, c)
}
if err != nil {
return
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"github.com/joho/godotenv"
)

type AppConfig struct {

Check failure on line 22 in main.go

View workflow job for this annotation

GitHub Actions / audit

exported type AppConfig should have comment or be unexported
APIURL string
HTTPHeaders string
User string
Expand Down Expand Up @@ -194,7 +194,7 @@
failError(err)
vmr = proxmox.NewVmRef(vmid)
vmr.SetNode(flag.Args()[2])
failError(config.CreateVm(vmr, c))
failError(config.Create(vmr, c))
log.Println("Complete")

case "createLxc":
Expand Down Expand Up @@ -224,7 +224,7 @@
vmr.SetNode(flag.Args()[1])
log.Printf("Creating node %s: \n", mode)
log.Println(vmr)
failError(config.CreateVm(vmr, c))
failError(config.Create(vmr, c))
_, err = c.StartVm(vmr)
failError(err)

Expand Down Expand Up @@ -283,7 +283,8 @@
}

failError(config.CloneVm(sourceVmr, vmr, c))
failError(config.UpdateConfig(vmr, c))
_, err = config.Update(true, vmr, c)
failError(err)
log.Println("Complete")

case "createQemuSnapshot":
Expand Down Expand Up @@ -516,7 +517,7 @@
//Users
case "getUser":
var config interface{}
userId, err := proxmox.NewUserID(flag.Args()[1])

Check failure on line 520 in main.go

View workflow job for this annotation

GitHub Actions / audit

var userId should be userID
failError(err)
config, err = proxmox.NewConfigUserFromApi(userId, c)
failError(err)
Expand All @@ -539,7 +540,7 @@
log.Printf("Error: Userid and Password required")
os.Exit(1)
}
userId, err := proxmox.NewUserID(flag.Args()[1])

Check failure on line 543 in main.go

View workflow job for this annotation

GitHub Actions / audit

var userId should be userID
failError(err)
err = proxmox.ConfigUser{
Password: proxmox.UserPassword(flag.Args()[2]),
Expand All @@ -552,7 +553,7 @@
var password proxmox.UserPassword
config, err := proxmox.NewConfigUserFromJson(GetConfig(*fConfigFile))
failError(err)
userId, err := proxmox.NewUserID(flag.Args()[1])

Check failure on line 556 in main.go

View workflow job for this annotation

GitHub Actions / audit

var userId should be userID
failError(err)
if len(flag.Args()) > 2 {
password = proxmox.UserPassword(flag.Args()[2])
Expand All @@ -565,7 +566,7 @@
log.Printf("Error: userId required")
os.Exit(1)
}
userId, err := proxmox.NewUserID(flag.Args()[1])

Check failure on line 569 in main.go

View workflow job for this annotation

GitHub Actions / audit

var userId should be userID
failError(err)
err = proxmox.ConfigUser{User: userId}.DeleteUser(c)
failError(err)
Expand Down Expand Up @@ -975,9 +976,9 @@
failError(fmt.Errorf("error: invoke with <vmID> <node> <diskID [<forceRemoval: false|true>]"))
}

vmIdUnparsed := flag.Args()[1]

Check failure on line 979 in main.go

View workflow job for this annotation

GitHub Actions / audit

var vmIdUnparsed should be vmIDUnparsed
node := flag.Args()[2]
vmId, err := strconv.Atoi(vmIdUnparsed)

Check failure on line 981 in main.go

View workflow job for this annotation

GitHub Actions / audit

var vmId should be vmID
if err != nil {
failError(fmt.Errorf("failed to convert vmId: %s to a string, error: %+v", vmIdUnparsed, err))
}
Expand Down
316 changes: 0 additions & 316 deletions proxmox/config_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,130 +103,6 @@ func (config ConfigQemu) Create(vmr *VmRef, client *Client) (err error) {
return
}

// DEPRECATED use ConfigQemu{}.Create Instead.
//
// CreateVm - Tell Proxmox API to make the VM
func (config ConfigQemu) CreateVm(vmr *VmRef, client *Client) (err error) {
if config.HasCloudInit() {
return fmt.Errorf("cloud-init parameters only supported on clones or updates")
}
vmr.SetVmType("qemu")

params := map[string]interface{}{
"vmid": vmr.vmId,
"name": config.Name,
"startup": config.Startup,
"agent": config.Agent,
"ostype": config.QemuOs,
"sockets": config.QemuSockets,
"cores": config.QemuCores,
"cpu": config.QemuCpu,
"hotplug": config.Hotplug,
"memory": config.Memory,
"boot": config.Boot,
"description": config.Description,
"tags": config.Tags,
"machine": config.Machine,
"args": config.Args,
}

if config.QemuNuma != nil {
params["numa"] = *config.QemuNuma
}

if config.QemuKVM != nil {
params["kvm"] = *config.QemuKVM
}

if config.Tablet != nil {
params["tablet"] = *config.Tablet
}

if config.Onboot != nil {
params["onboot"] = *config.Onboot
}

if config.QemuIso != "" {
params["ide2"] = config.QemuIso + ",media=cdrom"
}

if config.Bios != "" {
params["bios"] = config.Bios
}

if config.Balloon >= 1 {
params["balloon"] = config.Balloon
}

if config.QemuVcpus >= 1 {
params["vcpus"] = config.QemuVcpus
}
if config.Boot != "" {
params["boot"] = config.Boot
}
if config.BootDisk != "" {
params["bootdisk"] = config.BootDisk
}

if config.Scsihw != "" {
params["scsihw"] = config.Scsihw
}

if config.Protection != nil {
params["protection"] = *config.Protection
}

err = config.CreateQemuMachineParam(params)
if err != nil {
log.Printf("[ERROR] %q", err)
}

// Create disks config.
config.CreateQemuDisksParams(params, false)

// Create EFI disk
config.CreateQemuEfiParams(params)

// Create VirtIO RNG
config.CreateQemuRngParams(params)

// Create vga config.
vgaParam := QemuDeviceParam{}
vgaParam = vgaParam.createDeviceParam(config.QemuVga, nil)
if len(vgaParam) > 0 {
params["vga"] = strings.Join(vgaParam, ",")
}

// Create networks config.
config.CreateQemuNetworksParams(params)

// Create ipconfig.
err = config.CreateIpconfigParams(params)
if err != nil {
log.Printf("[ERROR] %q", err)
}

// Create serial interfaces
config.CreateQemuSerialsParams(params)

config.CreateQemuPCIsParams(params)

// Create usb interfaces
config.CreateQemuUsbsParams(params)

exitStatus, err := client.CreateQemuVm(vmr.node, params)
if err != nil {
return fmt.Errorf("error creating VM: %v, error status: %s (params: %v)", err, exitStatus, params)
}

_, err = client.UpdateVMHA(vmr, config.HaState, config.HaGroup)
if err != nil {
return fmt.Errorf("[ERROR] %q", err)
}

return
}

func (config *ConfigQemu) defaults() {
if config == nil {
return
Expand Down Expand Up @@ -1061,198 +937,6 @@ func (config ConfigQemu) CloneVm(sourceVmr *VmRef, vmr *VmRef, client *Client) (
return err
}

// DEPRECATED use ConfigQemu.Update instead
func (config ConfigQemu) UpdateConfig(vmr *VmRef, client *Client) (err error) {
configParams := map[string]interface{}{}

//Array to list deleted parameters
//deleteParams := []string{}

if config.Agent != 0 {
configParams["agent"] = config.Agent
}
if config.QemuOs != "" {
configParams["ostype"] = config.QemuOs
}
if config.QemuCores != 0 {
configParams["cores"] = config.QemuCores
}
if config.Memory != 0 {
configParams["memory"] = config.Memory
}

if config.QemuSockets != 0 {
configParams["sockets"] = config.QemuSockets
}

if config.QemuKVM != nil {
configParams["kvm"] = *config.QemuKVM
}

if config.QemuNuma != nil {
configParams["numa"] = *config.QemuNuma
}

if config.Onboot != nil {
configParams["onboot"] = *config.Onboot
}

if config.Protection != nil {
configParams["protection"] = *config.Protection
}

if config.Tablet != nil {
configParams["tablet"] = *config.Tablet
}

if config.Args != "" {
configParams["args"] = config.Args
}

if config.Tags != "" {
configParams["tags"] = config.Tags
}

if config.Startup != "" {
configParams["startup"] = config.Startup
}

if config.QemuIso != "" {
configParams["ide2"] = config.QemuIso + ",media=cdrom"
}

if config.Bios != "" {
configParams["bios"] = config.Bios
}

if config.Hotplug != "" {
configParams["hotplug"] = config.Hotplug
}

if config.Name != "" {
configParams["name"] = config.Name
}

if config.Description != "" {
configParams["description"] = config.Description
}

if config.Balloon >= 1 {
configParams["balloon"] = config.Balloon
}

if config.QemuVcpus >= 1 {
configParams["vcpus"] = config.QemuVcpus
}

if config.Boot != "" {
configParams["boot"] = config.Boot
}

if config.BootDisk != "" {
configParams["bootdisk"] = config.BootDisk
}

if config.Hookscript != "" {
configParams["hookscript"] = config.Hookscript
}

if config.QemuCpu != "" {
configParams["cpu"] = config.QemuCpu
}

if config.Scsihw != "" {
configParams["scsihw"] = config.Scsihw
}

err = config.CreateQemuMachineParam(configParams)
if err != nil {
log.Printf("[ERROR] %q", err)
}

// Create disks config.
configParamsDisk := map[string]interface{}{
"vmid": vmr.vmId,
}
config.CreateQemuDisksParams(configParamsDisk, false)
// TODO keep going if error=
_, err = client.createVMDisks(vmr.node, configParamsDisk)
if err != nil {
log.Printf("[ERROR] %q", err)
}
//Copy the disks to the global configParams
for key, value := range configParamsDisk {
//vmid is only required in createVMDisks
if key != "vmid" {
configParams[key] = value
}
}

// Create networks config.
config.VmID = vmr.vmId
config.CreateQemuNetworksParams(configParams)

// Create vga config.
vgaParam := QemuDeviceParam{}
vgaParam = vgaParam.createDeviceParam(config.QemuVga, nil)
if len(vgaParam) > 0 {
configParams["vga"] = strings.Join(vgaParam, ",")
}
// Create serial interfaces
config.CreateQemuSerialsParams(configParams)

// Create usb interfaces
config.CreateQemuUsbsParams(configParams)

config.CreateQemuPCIsParams(configParams)

// cloud-init options
if config.CIuser != "" {
configParams["ciuser"] = config.CIuser
}
if config.CIpassword != "" {
configParams["cipassword"] = config.CIpassword
}
if config.CIcustom != "" {
configParams["cicustom"] = config.CIcustom
}
if config.Searchdomain != "" {
configParams["searchdomain"] = config.Searchdomain
}
if config.Nameserver != "" {
configParams["nameserver"] = config.Nameserver
}
if config.Smbios1 != "" {
configParams["smbios1"] = config.Smbios1
}
if config.Sshkeys != "" {
configParams["sshkeys"] = sshKeyUrlEncode(config.Sshkeys)
}
err = config.CreateIpconfigParams(configParams)
if err != nil {
log.Printf("[ERROR] %q", err)
}

// if len(deleteParams) > 0 {
// configParams["delete"] = strings.Join(deleteParams, ", ")
// }

_, err = client.SetVmConfig(vmr, configParams)
if err != nil {
log.Print(err)
return err
}

_, err = client.UpdateVMHA(vmr, config.HaState, config.HaGroup)
if err != nil {
log.Printf("[ERROR] %q", err)
}

_, err = client.UpdateVMPool(vmr, config.Pool)

return err
}

func NewConfigQemuFromJson(input []byte) (config *ConfigQemu, err error) {
config = &ConfigQemu{}
err = json.Unmarshal([]byte(input), config)
Expand Down
4 changes: 2 additions & 2 deletions test/api/CloudInit/cloudinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ func Test_Cloud_Init_VM(t *testing.T) {
config.QemuDisks[0] = disk
config.Name = "Base-Image"

err = config.CreateVm(vmref, Test.GetClient())
err = config.Create(vmref, Test.GetClient())
require.NoError(t, err)

config.Ipconfig = pxapi.IpconfigMap{}
config.Boot = "order=virtio0;ide2;net0"

config.Ipconfig[0] = "gw=10.0.0.1,ip=10.0.0.2/24"

err = config.UpdateConfig(vmref, Test.GetClient())
_, err = config.Update(true, vmref, Test.GetClient())
require.NoError(t, err)

testConfig, _ := pxapi.NewConfigQemuFromApi(vmref, Test.GetClient())
Expand Down
2 changes: 1 addition & 1 deletion test/api/Qemu/qemu_clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Test_Clone_Qemu_VM(t *testing.T) {
_ = Test.CreateTest()
config := _create_vm_spec(false)

config.CreateVm(_create_vmref(), Test.GetClient())
config.Create(_create_vmref(), Test.GetClient())

cloneConfig := _create_vm_spec(false)

Expand Down
Loading
Loading