Example #1
0
func DeleteVmHandler(w http.ResponseWriter, req *http.Request) {
	var myjail *jail.Jail
	myjail = nil

	vars := mux.Vars(req)

	if _, ok := vars["uuid"]; ok {
		myjail = jail.GetJail(db, map[string]interface{}{"uuid": vars["uuid"]})
	}

	if myjail == nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	w.Header().Add("Content-Type", "application/json")

	status := ActionStatus{}
	if err := myjail.Delete(db); err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		status.Result = "Error"
		status.ErrorMessage = err.Error()
	} else {
		w.WriteHeader(http.StatusOK)
		status.Result = "Okay"
	}

	if bytes, err := json.Marshal(status); err == nil {
		w.Write(bytes)
	}
}
Example #2
0
func StartHandler(w http.ResponseWriter, req *http.Request) {
	var myjail *jail.Jail
	myjail = nil

	vars := mux.Vars(req)

	if _, ok := vars["uuid"]; ok {
		myjail = jail.GetJail(db, map[string]interface{}{"uuid": vars["uuid"]})
	}

	if myjail == nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	w.Header().Add("Content-Type", "application/json")

	status := ActionStatus{}
	if err := myjail.PrepareHostNetworking(); err == nil {
		if err = myjail.Start(); err == nil {
			if err = myjail.PrepareGuestNetworking(); err != nil {
				w.WriteHeader(http.StatusInternalServerError)
				status.Result = "Error"
				status.ErrorMessage = err.Error()
			} else {
				if err = myjail.PostStart(); err != nil {
					w.WriteHeader(http.StatusInternalServerError)
					status.Result = "Error"
					status.ErrorMessage = err.Error()
				}
			}
		} else {
			w.WriteHeader(http.StatusInternalServerError)
			status.Result = "Error"
			status.ErrorMessage = err.Error()
		}
	} else {
		w.WriteHeader(http.StatusInternalServerError)
		status.Result = "Error"
		status.ErrorMessage = err.Error()
	}

	if len(status.Result) == 0 {
		status.Result = "Okay"
		w.WriteHeader(http.StatusOK)
	}

	if bytes, err := json.Marshal(&status); err == nil {
		w.Write(bytes)
	} else {
		fmt.Printf("Could not marshal status object: %s\n", err.Error())
	}
}
Example #3
0
func UpdateVmHandler(w http.ResponseWriter, req *http.Request) {
	var myjail *jail.Jail
	vars := mux.Vars(req)

	if _, ok := vars["uuid"]; ok {
		myjail = jail.GetJail(db, map[string]interface{}{"uuid": vars["uuid"]})
	} else {
		return
	}

	jailrest := unmarshal_jail(w, req)
	if jailrest == nil {
		return
	}

	/* Diff each of the properties of the jail */
	if len(jailrest.ZFSDataset) > 0 && myjail.ZFSDataset != jailrest.ZFSDataset {
		myjail.ZFSDataset = jailrest.ZFSDataset
	}

	if len(jailrest.Name) > 0 && myjail.Name != jailrest.Name {
		myjail.Name = jailrest.Name
	}

	if len(myjail.NetworkDevices) > 0 && len(jailrest.NetworkDevices) == 0 {
		for _, device := range myjail.NetworkDevices {
			device.Delete(db)
		}

		myjail.NetworkDevices = make([]*network.NetworkDevice, 0)
	} else {
		/* Check for new/updated network devices */
		for _, restdevice := range jailrest.NetworkDevices {
			if mydevice := network.FindDevice(myjail.NetworkDevices, restdevice); mydevice == nil {
				restdevice.UUID = ""
				for _, option := range restdevice.Options {
					option.DeviceOptionID = 0
					option.DeviceUUID = ""
				}

				for _, address := range restdevice.Addresses {
					address.DeviceAddressID = 0
					address.DeviceUUID = ""
				}

				myjail.NetworkDevices = append(myjail.NetworkDevices, restdevice)
			}
		}

		/* Check for deleted network devices */
		for i := 0; i < len(myjail.NetworkDevices); i++ {
			mydevice := myjail.NetworkDevices[i]

			if restdevice := network.FindDevice(jailrest.NetworkDevices, mydevice); restdevice == nil {
				mydevice.Delete(db)
				copy(myjail.NetworkDevices[i:], myjail.NetworkDevices[i+1:])
				myjail.NetworkDevices[len(myjail.NetworkDevices)-1] = nil
				myjail.NetworkDevices = myjail.NetworkDevices[:len(myjail.NetworkDevices)-1]
				i--
			}
		}

		for _, restdevice := range jailrest.NetworkDevices {
			jaildevice := network.FindDevice(myjail.NetworkDevices, restdevice)

			/* Check addresses */
			for _, address := range restdevice.Addresses {
				if a := network.FindAddress(jaildevice.Addresses, address); a == nil {
					address.DeviceAddressID = 0
					jaildevice.Addresses = append(jaildevice.Addresses, address)
				}
			}

			for i := 0; i < len(jaildevice.Addresses); i++ {
				address := jaildevice.Addresses[i]

				if a := network.FindAddress(restdevice.Addresses, address); a == nil {
					db.Delete(address)
					copy(jaildevice.Addresses[i:], jaildevice.Addresses[i+1:])
					jaildevice.Addresses[len(jaildevice.Addresses)-1] = nil
					jaildevice.Addresses = jaildevice.Addresses[:len(jaildevice.Addresses)-1]
					i--
				}
			}

			/* Check options */
			for _, option := range restdevice.Options {
				if o := network.FindOption(jaildevice.Options, option); o == nil {
					option.DeviceOptionID = 0
					jaildevice.Options = append(jaildevice.Options, option)
				}
			}

			for i := 0; i < len(jaildevice.Options); i++ {
				option := jaildevice.Options[i]

				if o := network.FindOption(restdevice.Options, option); o == nil {
					db.Delete(option)
					copy(jaildevice.Options[i:], jaildevice.Options[i+1:])
					jaildevice.Options[len(jaildevice.Options)-1] = nil
					jaildevice.Options = jaildevice.Options[:len(jaildevice.Options)-1]
					i--
				}
			}
		}
	}
}