Exemplo n.º 1
0
 public bool RunCommand(
     string[] _UniqueInstanceNames,
     EBVMOSType _VMOperationSystemType,
     string[] _Commands,
     Action _OnCompleted,
     Action _OnFailure,
     Action <string> _ErrorMessageAction = null)
 {
     if (_UniqueInstanceNames != null && _UniqueInstanceNames.Length > 0)
     {
         return(PerformRunCommandActions(_UniqueInstanceNames, _VMOperationSystemType, _Commands, _OnCompleted, _OnFailure, _ErrorMessageAction) > 0);
     }
     return(false);
 }
Exemplo n.º 2
0
        public bool CreateInstance(
            string _UniqueInstanceName,
            string _Description,
            string _MachineType,
            long _DiskSizeGB,
            int _GpuCount,
            string _GpuName,
            string _OSSourceImageURL,
            EBVMDiskType _DiskType,
            EBVMOSType _OSType,
            IDictionary <string, string> _Labels,
            BVMNetworkFirewall _FirewallSettings,
            string _OptionalStartupScript,
            out int _ErrorCode,
            Action <string> _ErrorMessageAction = null)
        {
            _ErrorCode = 400;

            return(false);
        }
Exemplo n.º 3
0
 public bool RunCommand(string[] _UniqueInstanceNames, EBVMOSType _VMOperationSystemType, string[] _Commands, Action _OnCompleted, Action _OnFailure, Action <string> _ErrorMessageAction = null)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 4
0
        //EBVMInstanceStatus is the condition in here
        private int PerformRunCommandActions(
            string[] _UniqueInstanceNames,
            EBVMOSType _VMOperationSystemType,
            string[] _Commands,
            Action _OnCompleted,
            Action _OnFailure,
            Action <string> _ErrorMessageAction = null)
        {
            int ProgressStackIx = Interlocked.Increment(ref CurrentActionIndex);

            var ProgressStack = new Stack <object>();

            if (_UniqueInstanceNames != null && _UniqueInstanceNames.Length > 0)
            {
                lock (ProgressStacks_Lock)
                {
                    ProgressStacks.Add(ProgressStackIx, ProgressStack);
                }

                var Request = new ConcurrentQueue <Task>();

                foreach (var _InstanceName in _UniqueInstanceNames)
                {
                    var FoundInstance = FindInstanceByUniqueName(_InstanceName, _ErrorMessageAction);
                    if (FoundInstance != null)
                    {
                        if (GetStatusFromString(FoundInstance.PowerState.ToString()) == EBVMInstanceStatus.Running)
                        {
                            try
                            {
                                var _CommandId = "RunPowerShellScript";
                                if (_VMOperationSystemType == EBVMOSType.Linux)
                                {
                                    _CommandId = "RunShellScript";
                                }

                                var _RunCommandInput = new RunCommandInput()
                                {
                                    CommandId = _CommandId,
                                    Script    = _Commands.ToList()
                                };
                                Task RequestAction = FoundInstance.RunCommandAsync(_RunCommandInput);
                                Request.Enqueue(RequestAction);
                                ProgressStack.Push(new object());
                            }
                            catch (System.Exception ex)
                            {
                                _ErrorMessageAction?.Invoke($"BVMServiceAZ->PerformRunCommandActions->An error occurred when RunCommandInput is casting. Error: { ex.Message } - StackTrace: {ex.StackTrace}");
                                _OnFailure?.Invoke();
                            }
                        }
                        else
                        {
                            _ErrorMessageAction?.Invoke("BVMServiceAZ->PerformRunCommandActions->Virtual Machine is not running.");
                            _OnFailure?.Invoke();
                        }
                    }
                }
                if (ProgressStack.Count > 0)
                {
                    BTaskWrapper.Run(() =>
                    {
                        Thread.CurrentThread.IsBackground = true;

                        try
                        {
                            if (Request.TryDequeue(out Task CreatedTask))
                            {
                                using (CreatedTask)
                                {
                                    CreatedTask.Wait();
                                    lock (ProgressStacks_Lock)
                                    {
                                        if (ProgressStacks.TryGetValue(ProgressStackIx, out Stack <object> FoundStack) && FoundStack.Count > 0)
                                        {
                                            if (CreatedTask.Exception != null)
                                            {
                                                _ErrorMessageAction?.Invoke("BVMServiceAZ->PerformRunCommandActions->Error: " + CreatedTask.Exception.Message);
                                                FoundStack.Clear();
                                                _OnFailure?.Invoke();
                                            }
                                            else
                                            {
                                                FoundStack.Pop();
                                                if (FoundStack.Count == 0)
                                                {
                                                    ProgressStacks.Remove(ProgressStackIx);
                                                    _OnCompleted?.Invoke();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                _ErrorMessageAction?.Invoke("BVMServiceAZ->PerformRunCommandActions->TryDequeue error occured.");
                                _OnFailure?.Invoke();
                            }
                        }
                        catch (Exception e)
                        {
                            _ErrorMessageAction?.Invoke("BVMServiceAZ->PerformRunCommandActions->Exception: " + e.Message);
                            _OnFailure?.Invoke();
                        }
                    });
                }
                else
                {
                    lock (ProgressStacks_Lock)
                    {
                        ProgressStacks.Remove(ProgressStackIx);
                    }
                }
            }
            return(ProgressStack.Count);
        }
Exemplo n.º 5
0
        public bool CreateInstance(
            string _UniqueInstanceName,
            string _Description,
            string _MachineType,
            long _DiskSizeGB,
            int _GpuCount,
            string _GpuName,
            string _OSSourceImageURL,
            EBVMDiskType _DiskType,
            EBVMOSType _OSType,
            IDictionary <string, string> _Labels,
            BVMNetworkFirewall _FirewallSettings,
            string _OptionalStartupScript,
            out int _ErrorCode,
            Action <string> _ErrorMessageAction = null)
        {
            _ErrorCode = 400;

            if (!BUtility.CalculateStringMD5(BUtility.RandomString(32, true), out string RandomFirewallTag, _ErrorMessageAction))
            {
                _ErrorMessageAction?.Invoke("BVMServiceGC->CreateInstance: Firewall tag MD5 generation has failed.");
                return(false);
            }

            RandomFirewallTag = "a-" /*Has to start with a letter*/ + RandomFirewallTag;

            try
            {
                using (var Service = GetService())
                {
                    var NewInstance = new Instance()
                    {
                        Name               = _UniqueInstanceName,
                        Description        = _Description,
                        DeletionProtection = false,
                        Zone               = "projects/" + ProjectID + "/zones/" + ZoneName,
                        Labels             = _Labels,
                        MachineType        = "projects/" + ProjectID + "/zones/" + ZoneName + "/machineTypes/" + _MachineType,
                        Disks              = new List <AttachedDisk>()
                        {
                            new AttachedDisk()
                            {
                                AutoDelete       = true,
                                Boot             = true,
                                Kind             = "compute#attachedDisk",
                                DeviceName       = _UniqueInstanceName,
                                Mode             = "READ_WRITE",
                                InitializeParams = new AttachedDiskInitializeParams()
                                {
                                    SourceImage = _OSSourceImageURL,
                                    DiskType    = "projects/" + ProjectID + "/zones/" + ZoneName + "/diskTypes/" + (_DiskType == EBVMDiskType.SSD ? "pd-ssd" : "pd-standard"),
                                    DiskSizeGb  = _DiskSizeGB
                                },
                                Type = "PERSISTENT"
                            }
                        },
                        NetworkInterfaces = new List <NetworkInterface>()
                        {
                            new NetworkInterface()
                            {
                                AccessConfigs = new List <AccessConfig>()
                                {
                                    new AccessConfig()
                                    {
                                        Kind        = "compute#accessConfig",
                                        Name        = "External NAT",
                                        NetworkTier = "PREMIUM",
                                        Type        = "ONE_TO_ONE_NAT"
                                    }
                                },
                                Kind       = "compute#networkInterface",
                                Name       = "nic0",
                                Network    = "projects/" + ProjectID + "/global/networks/default",
                                Subnetwork = "projects/" + ProjectID + "/regions/" + ZoneName.Substring(0, ZoneName.LastIndexOf('-')) + "/subnetworks/default"
                            }
                        },
                        Tags = new Tags()
                        {
                            Items = new List <string>()
                            {
                                RandomFirewallTag
                            }
                        },
                        Metadata = new Metadata()
                        {
                            Kind  = "compute#metadata",
                            Items = new List <Metadata.ItemsData>()
                        },
                        ShieldedInstanceConfig = new ShieldedInstanceConfig()
                        {
                            EnableVtpm                = true,
                            EnableSecureBoot          = false,
                            EnableIntegrityMonitoring = true
                        },
                        Scheduling = new Scheduling()
                        {
                            AutomaticRestart  = true,
                            Preemptible       = false,
                            OnHostMaintenance = "TERMINATE"
                        }
                    };

                    if (_OptionalStartupScript != null)
                    {
                        NewInstance.Metadata.Items.Add(new Metadata.ItemsData()
                        {
                            Key   = _OSType == EBVMOSType.Linux ? "startup-script" : "windows-startup-script-ps1",
                            Value = _OptionalStartupScript
                        });
                    }

                    if (_GpuCount > 0)
                    {
                        if (NewInstance.GuestAccelerators == null)
                        {
                            NewInstance.GuestAccelerators = new List <AcceleratorConfig>();
                        }
                        NewInstance.GuestAccelerators.Add(
                            new AcceleratorConfig()
                        {
                            AcceleratorCount = _GpuCount,
                            AcceleratorType  = "projects/" + ProjectID + "/zones/" + ZoneName + "/acceleratorTypes/" + _GpuName
                        });
                    }

                    if (_OSType == EBVMOSType.Windows)
                    {
                        if (NewInstance.Disks[0].GuestOsFeatures == null)
                        {
                            NewInstance.Disks[0].GuestOsFeatures = new List <GuestOsFeature>();
                        }

                        if (!NewInstance.Disks[0].GuestOsFeatures.Any(Item => Item.Type == "VIRTIO_SCSI_MULTIQUEUE"))
                        {
                            NewInstance.Disks[0].GuestOsFeatures.Add(new GuestOsFeature()
                            {
                                Type = "VIRTIO_SCSI_MULTIQUEUE"
                            });
                        }

                        if (!NewInstance.Disks[0].GuestOsFeatures.Any(Item => Item.Type == "WINDOWS"))
                        {
                            NewInstance.Disks[0].GuestOsFeatures.Add(new GuestOsFeature()
                            {
                                Type = "WINDOWS"
                            });
                        }

                        if (!NewInstance.Disks[0].GuestOsFeatures.Any(Item => Item.Type == "MULTI_IP_SUBNET"))
                        {
                            NewInstance.Disks[0].GuestOsFeatures.Add(new GuestOsFeature()
                            {
                                Type = "MULTI_IP_SUBNET"
                            });
                        }

                        if (!NewInstance.Disks[0].GuestOsFeatures.Any(Item => Item.Type == "UEFI_COMPATIBLE"))
                        {
                            NewInstance.Disks[0].GuestOsFeatures.Add(new GuestOsFeature()
                            {
                                Type = "UEFI_COMPATIBLE"
                            });
                        }
                    }

                    var NewFirewall = new Firewall()
                    {
                        Kind         = "compute#firewall",
                        Name         = RandomFirewallTag,
                        Priority     = 1000,
                        Direction    = "INGRESS",
                        SelfLink     = "projects/" + ProjectID + "/global/firewalls/" + RandomFirewallTag,
                        Network      = "projects/" + ProjectID + "/global/networks/default",
                        SourceRanges = new List <string>(),
                        TargetTags   = new List <string>()
                        {
                            RandomFirewallTag
                        },
                        Allowed = new List <Firewall.AllowedData>()
                    };
                    if (_FirewallSettings.bOpenAll)
                    {
                        NewFirewall.Allowed.Add(new Firewall.AllowedData()
                        {
                            IPProtocol = "tcp"
                        });
                        NewFirewall.Allowed.Add(new Firewall.AllowedData()
                        {
                            IPProtocol = "udp"
                        });
                    }
                    else
                    {
                        foreach (var Current in _FirewallSettings.OpenPorts)
                        {
                            string[] OpenFor;
                            if (Current.OpenFor == BVMNetworkFirewall.EVMNetworkFirewallPortProtocol.TCP)
                            {
                                OpenFor = new string[] { "tcp" }
                            }
                            ;
                            else if (Current.OpenFor == BVMNetworkFirewall.EVMNetworkFirewallPortProtocol.UDP)
                            {
                                OpenFor = new string[] { "udp" }
                            }
                            ;
                            else
                            {
                                OpenFor = new string[] { "tcp", "udp" }
                            };

                            var PortList = new List <string>()
                            {
                                Current.FromPortInclusive + "-" + Current.ToPortInclusive
                            };
                            foreach (var OFor in OpenFor)
                            {
                                NewFirewall.Allowed.Add(new Firewall.AllowedData()
                                {
                                    IPProtocol = OFor,
                                    Ports      = PortList
                                });
                            }
                        }
                    }

                    var FirewallCreationResult = Service.Firewalls.Insert(NewFirewall, ProjectID).Execute();
                    if (FirewallCreationResult == null || (FirewallCreationResult.HttpErrorStatusCode.HasValue && FirewallCreationResult.HttpErrorStatusCode.Value >= 400))
                    {
                        _ErrorMessageAction?.Invoke("BVMServiceGC->CreateInstance: Firewall creation has failed: " + (FirewallCreationResult == null ? "Result is null." : FirewallCreationResult.HttpErrorMessage));
                        _ErrorCode = FirewallCreationResult.HttpErrorStatusCode.Value;
                        return(false);
                    }

                    var VMCreationResult = Service.Instances.Insert(NewInstance, ProjectID, ZoneName).Execute();
                    if (VMCreationResult == null || (VMCreationResult.HttpErrorStatusCode.HasValue && VMCreationResult.HttpErrorStatusCode.Value >= 400))
                    {
                        _ErrorMessageAction?.Invoke("BVMServiceGC->CreateInstance: VM creation has failed: " + (VMCreationResult == null ? "Result is null." : VMCreationResult.HttpErrorMessage));
                        _ErrorCode = VMCreationResult.HttpErrorStatusCode.Value;
                        return(false);
                    }
                }
            }
            catch (Exception e)
            {
                _ErrorMessageAction?.Invoke("BVMServiceGC->CreateInstance: " + e.Message + ", Trace: " + e.StackTrace);
                return(false);
            }
            return(true);
        }