Exemple #1
0
        private void BindFormControls()
        {
            var virtualMachine = new VirtualMachine
            {
                DynamicMemory = new DynamicMemory
                {
                    Buffer   = 20,
                    Priority = 50
                }
            };

            // bind password policy
            password.SetPackagePolicy(PanelSecurity.PackageId, UserSettings.VPS_POLICY, "AdministratorPasswordPolicy");

            // OS templates
            listOperatingSystems.DataSource = ES.Services.VPS2012.GetOperatingSystemTemplates(PanelSecurity.PackageId);
            listOperatingSystems.DataBind();
            listOperatingSystems.Items.Insert(0, new ListItem(GetLocalizedString("SelectOsTemplate.Text"), ""));

            // summary letter e-mail
            PackageInfo package = ES.Services.Packages.GetPackage(PanelSecurity.PackageId);

            if (package != null)
            {
                UserInfo user = ES.Services.Users.GetUserById(package.UserId);
                if (user != null)
                {
                    chkSendSummary.Checked = IsMailConfigured(user);
                    txtSummaryEmail.Text   = user.Email;
                }
            }

            // load package context
            PackageContext cntx = PackagesHelper.GetCachedPackageContext(PanelSecurity.PackageId);

            // bind CPU cores
            int maxCores = ES.Services.VPS2012.GetMaximumCpuCoresNumber(PanelSecurity.PackageId);

            QuotaValueInfo cpuQuota2 = cntx.Quotas[Quotas.VPS2012_CPU_NUMBER];

            if (cpuQuota2.QuotaAllocatedValue == -1)
            {
                for (int i = 1; i < maxCores + 1; i++)
                {
                    ddlCpu.Items.Add(i.ToString());
                }

                ddlCpu.SelectedIndex = ddlCpu.Items.Count - 1; // select last (maximum) item
            }
            else if (cpuQuota2.QuotaAllocatedValue >= cpuQuota2.QuotaUsedValue)
            {
                if ((cpuQuota2.QuotaAllocatedValue + 1 - cpuQuota2.QuotaUsedValue) > maxCores)
                {
                    for (int i = 1; i < maxCores + 1; i++)
                    {
                        ddlCpu.Items.Add(i.ToString());
                    }

                    ddlCpu.SelectedIndex = ddlCpu.Items.Count - 1; // select last (maximum) item
                }
                else
                {
                    for (int i = 1; i < (cpuQuota2.QuotaAllocatedValue - cpuQuota2.QuotaUsedValue) + 1; i++)
                    {
                        ddlCpu.Items.Add(i.ToString());
                    }

                    ddlCpu.SelectedIndex = ddlCpu.Items.Count - 1; // select last (maximum) item
                }
            }
            else
            {
                ddlCpu.Items.Add("0");
            }

            // external network details
            if (PackagesHelper.IsQuotaEnabled(PanelSecurity.PackageId, Quotas.VPS2012_EXTERNAL_NETWORK_ENABLED))
            {
                List <int> dupevlans = new List <int>();
                List <int> vlans     = new List <int>();

                bool isUnassignedPackageIPs = false;
                // bind vlan list
                PackageIPAddress[] ips = ES.Services.Servers.GetPackageUnassignedIPAddresses(PanelSecurity.PackageId, 0, IPAddressPool.VpsExternalNetwork);
                if (ips.Length > 0)
                {
                    foreach (PackageIPAddress ip in ips)
                    {
                        dupevlans.Add(ip.VLAN);
                    }
                }
                else
                {
                    if (PanelSecurity.EffectiveUser.Role != UserRole.User)
                    {
                        IPAddressInfo[] uips = ES.Services.Servers.GetUnallottedIPAddresses(PanelSecurity.PackageId, ResourceGroups.VPS2012, IPAddressPool.VpsExternalNetwork);
                        isUnassignedPackageIPs = true;
                        foreach (IPAddressInfo ip in uips)
                        {
                            dupevlans.Add(ip.VLAN);
                        }
                    }
                }

                // return vlan list without dupes
                vlans = dupevlans.Distinct().ToList();

                //List<int> vlans = ES.Services.VPS2012.GetAvailableVLANs(PanelSecurity.PackageId).vlans;
                listVlanLists.Items.Clear();
                //listVlanLists.Items.Add(new ListItem("External network disabled", "-1"));
                listVlanLists.Items.Add(new ListItem("Select a VLAN", "-1"));
                foreach (int vlan in vlans)
                {
                    listVlanLists.Items.Add(new ListItem(String.Format("VLAN {0}", vlan.ToString()), vlan.ToString()));
                }

                // bind external network ips 4 selected vlan
                if (isUnassignedPackageIPs)
                {
                    BindExternalUnallottedIps();
                }
                else
                {
                    BindExternalIps();
                }

                //GenerateMac
                txtExternalMACAddress.Text = ES.Services.VPS2012.GenerateMacAddress();
            }

            // private network
            if (PackagesHelper.IsQuotaEnabled(PanelSecurity.PackageId, Quotas.VPS2012_PRIVATE_NETWORK_ENABLED))
            {
                NetworkAdapterDetails nic = ES.Services.VPS2012.GetPrivateNetworkDetails(PanelSecurity.PackageId);
                litPrivateNetworkFormat.Text = nic.NetworkFormat;
                litPrivateSubnetMask.Text    = nic.SubnetMask;
                txtGateway.Text = nic.DefaultGateway;
                txtDNS1.Text    = nic.PreferredNameServer;
                txtDNS2.Text    = nic.AlternateNameServer;
                txtMask.Text    = nic.SubnetMask;

                //firewall find
                VirtualMachines2012Helper vmh      = new VirtualMachines2012Helper();
                VirtualMachineMetaItem[]  machines = vmh.GetVirtualMachines(PanelSecurity.PackageId, "", "", "SI.ItemID", 20, 0);
                foreach (var item in machines)
                {
                    if (!String.IsNullOrEmpty(item.ExternalIP) && !String.IsNullOrEmpty(item.IPAddress))
                    {
                        txtGateway.Text = item.IPAddress;
                        txtDNS1.Text    = item.IPAddress;
                        VirtualMachine vm = ES.Services.VPS2012.GetVirtualMachineItem(item.ItemID);
                        if (vm != null && !String.IsNullOrEmpty(vm.CustomPrivateMask))
                        {
                            txtMask.Text = vm.CustomPrivateMask;
                        }
                        break;
                    }
                }

                // set max number
                QuotaValueInfo privQuota  = cntx.Quotas[Quotas.VPS2012_PRIVATE_IP_ADDRESSES_NUMBER];
                int            maxPrivate = privQuota.QuotaAllocatedValue;
                if (maxPrivate == -1)
                {
                    maxPrivate = 10;
                }

                // handle DHCP mode
                if (nic.IsDHCP)
                {
                    maxPrivate        = 0;
                    ViewState["DHCP"] = true;
                }

                txtPrivateAddressesNumber.Text = "1";
                litMaxPrivateAddresses.Text    = String.Format(GetLocalizedString("litMaxPrivateAddresses.Text"), maxPrivate);

                listPrivateNetworkVLAN.Items.Clear();
                PackageVLANsPaged vlans = ES.Services.Servers.GetPackagePrivateNetworkVLANs(PanelSecurity.PackageId, "", 0, Int32.MaxValue);
                if (vlans != null && vlans.Count > 0)
                {
                    foreach (PackageVLAN vlan in vlans.Items)
                    {
                        listPrivateNetworkVLAN.Items.Add(new ListItem(String.Format("VLAN {0}", vlan.Vlan.ToString()), vlan.Vlan.ToString()));
                    }
                }
            }



            // RAM size
            if (cntx.Quotas.ContainsKey(Quotas.VPS2012_RAM))
            {
                QuotaValueInfo ramQuota = cntx.Quotas[Quotas.VPS2012_RAM];
                if (ramQuota.QuotaAllocatedValue == -1)
                {
                    // unlimited RAM
                    txtRam.Text = "";
                }
                else
                {
                    int availSize = ramQuota.QuotaAllocatedValue - ramQuota.QuotaUsedValue;
                    txtRam.Text = availSize < 0 ? "" : availSize.ToString();

                    if (availSize > 0)
                    {
                        virtualMachine.DynamicMemory.Minimum = availSize / 2;
                        virtualMachine.DynamicMemory.Maximum = availSize;
                    }
                }
            }

            // HDD size
            if (cntx.Quotas.ContainsKey(Quotas.VPS2012_HDD))
            {
                QuotaValueInfo hddQuota = cntx.Quotas[Quotas.VPS2012_HDD];
                if (hddQuota.QuotaAllocatedValue == -1)
                {
                    // unlimited HDD
                    txtHdd.Text = "";
                }
                else
                {
                    int availSize = hddQuota.QuotaAllocatedValue - hddQuota.QuotaUsedValue;
                    txtHdd.Text = availSize < 0 ? "" : availSize.ToString();
                }
            }

            // IOPS number
            // TODO: checke
            txtHddMinIOPS.Text = "0";
            txtHddMaxIOPS.Text = "0";

            // snapshots number
            if (cntx.Quotas.ContainsKey(Quotas.VPS2012_SNAPSHOTS_NUMBER))
            {
                int snapsNumber = cntx.Quotas[Quotas.VPS2012_SNAPSHOTS_NUMBER].QuotaAllocatedValue;
                txtSnapshots.Text    = (snapsNumber != -1) ? snapsNumber.ToString() : "";
                txtSnapshots.Enabled = (snapsNumber != 0);
            }

            // toggle controls
            BindCheckboxOption(chkDvdInstalled, Quotas.VPS2012_DVD_ENABLED);
            chkBootFromCd.Enabled = PackagesHelper.IsQuotaEnabled(PanelSecurity.PackageId, Quotas.VPS2012_BOOT_CD_ALLOWED);

            BindCheckboxOption(chkStartShutdown, Quotas.VPS2012_START_SHUTDOWN_ALLOWED);
            BindCheckboxOption(chkPauseResume, Quotas.VPS2012_PAUSE_RESUME_ALLOWED);
            BindCheckboxOption(chkReset, Quotas.VPS2012_RESET_ALOWED);
            BindCheckboxOption(chkReboot, Quotas.VPS2012_REBOOT_ALLOWED);
            BindCheckboxOption(chkReinstall, Quotas.VPS2012_REINSTALL_ALLOWED);

            // the settings user controls
            this.BindSettingsControls(virtualMachine);
        }
Exemple #2
0
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
            {
                return;
            }

            try
            {
                // check rights
                bool manageAllowed = VirtualMachines2012Helper.IsVirtualMachineManagementAllowed(PanelSecurity.PackageId);
                if (!manageAllowed)
                {
                    return;
                }

                // check snapshots
                VirtualMachineSnapshot[] snapshots = ES.Services.VPS2012.GetVirtualMachineSnapshots(PanelRequest.ItemID);
                if (snapshots.Length > 0)
                {
                    return;
                }

                VirtualMachine virtualMachine = new VirtualMachine();
                VirtualMachine vm             = ES.Services.VPS2012.GetVirtualMachineItem(PanelRequest.ItemID);

                if (!chkIgnoreHddWarning.Checked || PanelSecurity.EffectiveUser.Role == UserRole.User)
                {
                    if (Utils.ParseInt(hiddenTxtValHdd.Value) > Utils.ParseInt(txtHdd.Text.Trim()))
                    {
                        messageBox.ShowWarningMessage("VPS_CHANGE_HDD_SIZE");
                        return;
                    }
                    List <AdditionalHdd> hdds = GetAdditionalHdd();
                    foreach (AdditionalHdd hdd in hdds)
                    {
                        for (int i = 0; i < vm.VirtualHardDrivePath.Length; i++)
                        {
                            if (String.IsNullOrEmpty(vm.VirtualHardDrivePath[i]))
                            {
                                continue;
                            }
                            if (Path.GetFileName(vm.VirtualHardDrivePath[i]).ToLower().Equals(Path.GetFileName(hdd.DiskPath).ToLower()) && Utils.ParseInt(hdd.DiskSize.Trim()) < vm.HddSize[i])
                            {
                                messageBox.ShowWarningMessage("VPS_CHANGE_HDD_SIZE");
                                return;
                            }
                        }
                    }
                }

                // the custom provider control
                this.SaveSettingsControls(ref virtualMachine);
                virtualMachine.CpuCores = Utils.ParseInt(ddlCpu.SelectedValue);
                virtualMachine.RamSize  = Utils.ParseInt(txtRam.Text.Trim());
                List <int>    hddSize = new List <int>();
                List <String> hddPath = new List <String>();
                hddSize.Add(Utils.ParseInt(txtHdd.Text.Trim()));
                hddPath.Add(vm.VirtualHardDrivePath[0]);
                List <AdditionalHdd> additionalHdd = GetAdditionalHdd();
                foreach (AdditionalHdd hdd in additionalHdd)
                {
                    int size = Utils.ParseInt(hdd.DiskSize.Trim());
                    if (size > 0)
                    {
                        hddSize.Add(size);
                        hddPath.Add(hdd.DiskPath);
                    }
                }
                virtualMachine.HddSize = hddSize.ToArray();
                virtualMachine.VirtualHardDrivePath   = hddPath.ToArray();
                virtualMachine.SnapshotsNumber        = Utils.ParseInt(txtSnapshots.Text.Trim());
                virtualMachine.HddMinimumIOPS         = Utils.ParseInt(txtHddMinIOPS.Text.Trim());
                virtualMachine.HddMaximumIOPS         = Utils.ParseInt(txtHddMaxIOPS.Text.Trim());
                virtualMachine.DvdDriveInstalled      = chkDvdInstalled.Checked;
                virtualMachine.BootFromCD             = chkBootFromCd.Checked;
                virtualMachine.NumLockEnabled         = chkNumLock.Checked;
                virtualMachine.EnableSecureBoot       = chkSecureBoot.Checked;
                virtualMachine.StartTurnOffAllowed    = chkStartShutdown.Checked;
                virtualMachine.PauseResumeAllowed     = chkPauseResume.Checked;
                virtualMachine.RebootAllowed          = chkReboot.Checked;
                virtualMachine.ResetAllowed           = chkReset.Checked;
                virtualMachine.ReinstallAllowed       = chkReinstall.Checked;
                virtualMachine.ExternalNetworkEnabled = chkExternalNetworkEnabled.Checked;
                virtualMachine.PrivateNetworkEnabled  = chkPrivateNetworkEnabled.Checked;
                virtualMachine.NeedReboot             = chkForceReboot.Checked;
                virtualMachine.defaultaccessvlan      = vm.defaultaccessvlan;
                virtualMachine.PrivateNetworkVlan     = vm.PrivateNetworkVlan;

                bool  setupExternalNetwork = !vm.ExternalNetworkEnabled && chkExternalNetworkEnabled.Checked;
                bool  setupPrivateNetwork  = !vm.PrivateNetworkEnabled && chkPrivateNetworkEnabled.Checked;
                int[] ipId         = new int[1];
                int   privAdrCount = 0;

                if (setupExternalNetwork)
                {
                    PackageIPAddress[] ips = ES.Services.Servers.GetPackageUnassignedIPAddresses(PanelSecurity.PackageId, 0, IPAddressPool.VpsExternalNetwork);
                    if (ips.Length > 0)
                    {
                        virtualMachine.defaultaccessvlan = ips[0].VLAN;
                        ipId[0] = ips[0].PackageAddressID;
                    }
                }

                if (setupPrivateNetwork)
                {
                    PackageVLANsPaged vlans = ES.Services.Servers.GetPackagePrivateNetworkVLANs(PanelSecurity.PackageId, "", 0, Int32.MaxValue);
                    if (vlans.Count > 0)
                    {
                        virtualMachine.PrivateNetworkVlan = vlans.Items[0].Vlan;
                    }

                    PackageContext cntx = PackagesHelper.GetCachedPackageContext(PanelSecurity.PackageId);

                    if (cntx.Quotas.ContainsKey(Quotas.VPS2012_PRIVATE_IP_ADDRESSES_NUMBER))
                    {
                        QuotaValueInfo privQuota = cntx.Quotas[Quotas.VPS2012_PRIVATE_IP_ADDRESSES_NUMBER];
                        if (privQuota.QuotaAllocatedValue > 0 || privQuota.QuotaAllocatedValue == -1)
                        {
                            privAdrCount = 1;
                        }
                    }
                }

                ResultObject res = ES.Services.VPS2012.UpdateVirtualMachineResource(PanelRequest.ItemID, virtualMachine);
                //ResultObject res = ES.Services.VPS2012.UpdateVirtualMachineConfiguration(PanelRequest.ItemID,
                //    Utils.ParseInt(ddlCpu.SelectedValue),
                //    Utils.ParseInt(txtRam.Text.Trim()),
                //    Utils.ParseInt(txtHdd.Text.Trim()),
                //    Utils.ParseInt(txtSnapshots.Text.Trim()),
                //    chkDvdInstalled.Checked,
                //    chkBootFromCd.Checked,
                //    chkNumLock.Checked,
                //    chkStartShutdown.Checked,
                //    chkPauseResume.Checked,
                //    chkReboot.Checked,
                //    chkReset.Checked,
                //    chkReinstall.Checked,
                //    chkExternalNetworkEnabled.Checked,
                //    chkPrivateNetworkEnabled.Checked,
                //    virtualMachine);

                if (res.IsSuccess)
                {
                    if (setupExternalNetwork && ipId[0] != 0)
                    {
                        ES.Services.VPS2012.AddVirtualMachineExternalIPAddresses(PanelRequest.ItemID, false, 1, ipId);
                    }

                    if (setupPrivateNetwork && privAdrCount > 0)
                    {
                        ES.Services.VPS2012.AddVirtualMachinePrivateIPAddresses(PanelRequest.ItemID, true, privAdrCount, new string[0], false, null, null, null, null);
                    }

                    // redirect back
                    RedirectBack("changed");
                }
                else
                {
                    // show error
                    messageBox.ShowMessage(res, "VPS_CHANGE_VM_CONFIGURATION", "VPS");
                }
            }
            catch (Exception ex)
            {
                messageBox.ShowErrorMessage("VPS_CHANGE_VM_CONFIGURATION", ex);
            }
        }
Exemple #3
0
 public PackageVLAN[] GetPackageVLANs(int packageId, string sortColumn, int maximumRows, int startRowIndex)
 {
     packageVLANs = ES.Services.Servers.GetPackagePrivateNetworkVLANs(packageId, sortColumn, startRowIndex, maximumRows);
     return(packageVLANs.Items);
 }