Exemplo n.º 1
0
        internal VirtualMachineConfigInfo GetVmConfig(P2PBackup.Common.Node vm)
        {
            ObjectContent[]        oCary = GetObjectProperties("VirtualMachine", new string[] { "config" }, null /*GetVmMoRef(vm)*/);
            ObjectContent          oc    = null;
            ManagedObjectReference mor   = null;

            //ManagedObjectReference snapMor = null;
            DynamicProperty[] pcary = null;
            //DynamicProperty pc = null;
            for (int oci = 0; oci < oCary.Length; oci++)
            {
                oc    = oCary[oci];
                mor   = oc.obj;
                pcary = oc.propSet;

                if (mor.type != "VirtualMachine" || pcary == null)
                {
                    continue;
                }
                //for (int i = 0; i < pcary.Length; i++){
                //   pc = pcary[i];
                if (pcary[pcary.Length - 1].val is VirtualMachineConfigInfo)
                {
                    return((VirtualMachineConfigInfo)pcary[pcary.Length - 1].val);
                }
            }
            return(null);
        }
Exemplo n.º 2
0
        public P2PBackup.Common.Node Update(P2PBackup.Common.Node newN)
        {
            // prevent from updating available space
            // and prevent updating storage space if new value < available
            P2PBackup.Common.Node dbNode   = Get(newN.Id);
            P2PBackup.Common.Node liveNode = Hub.NodesList.GetById(newN.Id);

            // If storage space size was chaged, ensure it was not set to less than currently used space
            if (liveNode != null)
            {
                lock (liveNode){
                    if (newN.StorageSize < liveNode.StorageUsed)
                    {
                        throw new ArgumentException("Cannot set node storage size to a value being less than currently used storage value.");
                    }
                    else
                    {
                        liveNode.StorageSize = newN.StorageSize;
                    }
                }
            }
            else
            {
                if (newN.StorageSize < dbNode.StorageUsed)
                {
                    throw new ArgumentException("Cannot set node storage size to a value being less than currently used storage value.");
                }
            }
            newN.StorageUsed = dbNode.StorageUsed;
            using (dbc = DAL.Instance.GetDb())
                dbc.Update(newN);
            return(newN);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Changes a node's group or parent
        /// </summary>
        /// <param name="n">The node with its updated group information</param>

        /*public Node UpdateParent(Node n){
         *      using(dbc = DAL.Instance.GetDb()){
         *              dbc.Update<P2PBackup.Common.Node>(new {Group = n.Group}, p => p.Id == n.Id);
         *      }
         *      return n;
         * }*/

        public void UpdateStorageSpace(P2PBackup.Common.Node n)
        {
            using (dbc = DAL.Instance.GetDb())
                dbc.Update <P2PBackup.Common.Node>(
                    new  { Id = n.Id, Available = n.StorageUsed }, p => p.Id == n.Id
                    );
        }
Exemplo n.º 4
0
 internal void WakeUp(P2PBackup.Common.Node n)
 {
     if (n == null)
     {
         return;
     }
     Logger.Append("TASK", Severity.DEBUG, "Sending wakeup signal to node #" + n.Id);
     SendUdpMessage("WAKEUP", n);
 }
Exemplo n.º 5
0
        public string DeleteSnapshot(P2PBackup.Common.Node vm, string snapMoref)
        {
            string deleteTask = null;

            ObjectContent[]        oCary   = GetObjectProperties("VirtualMachine", new string[] { "snapshot" }, null /*GetVmMoRef(vm)*/);
            ObjectContent          oc      = null;
            ManagedObjectReference mor     = null;
            ManagedObjectReference snapMor = null;

            DynamicProperty[] pcary = null;
            DynamicProperty   pc    = null;

            for (int oci = 0; oci < oCary.Length; oci++)
            {
                oc    = oCary[oci];
                mor   = oc.obj;
                pcary = oc.propSet;

                if (mor.type != "VirtualMachine" || pcary == null)
                {
                    continue;
                }
                for (int i = 0; i < pcary.Length; i++)
                {
                    pc = pcary[i];
                    if (pc.val is VirtualMachineSnapshotInfo)
                    {
                        VirtualMachineSnapshotInfo snapInfo   = (VirtualMachineSnapshotInfo)pc.val;
                        ManagedObjectReference     theSnapMor = snapInfo.currentSnapshot;
                        if (theSnapMor.Value == snapMoref)
                        {
                            snapMor = theSnapMor;
                            break;
                        }
                    }
                }
                if (snapMor != null)
                {
                    break;
                }
            }
            if (snapMor != null)
            {
                if (LogEvent != null)
                {
                    LogEvent(this, new LogEventArgs(0, Severity.INFO, "Deleting snapshot '" + snapMor.Value + "' for virtual node " + vm.ToString()));
                }

                try{
                    deleteTask = service.RemoveSnapshot_Task(snapMor, true).Value;
                }
                catch {}                // timed-out getting vsphere response, ignore.
            }
            return(deleteTask);
        }
Exemplo n.º 6
0
        public string Snapshot(P2PBackup.Common.Node vm, string snapshotName)
        {
            ManagedObjectReference searchIndex = this.serviceContent.searchIndex;
            ManagedObjectReference vmMor       = service.FindByUuid(searchIndex, null, vm.InternalId, true);

            LogEvent(this, new LogEventArgs(0, Severity.DEBUG, "VM search returned vmMor '" + vmMor.Value + "'"));
            LogEvent(this, new LogEventArgs(0, Severity.DEBUG, "About to create a snapshot with name '" + snapshotName + "'"));
            ManagedObjectReference taskMor = service.CreateSnapshot_Task(vmMor, snapshotName, "", false, true);

            //TaskInfo ti = new TaskInfo();
            if (LogEvent != null)
            {
                LogEvent(this, new LogEventArgs(0, Severity.TRIVIA, "Snapshot task has mor '" + taskMor.Value + "'"));
            }
            TaskInfo snapTaskInfo = new TaskInfo();

            while (true)
            {
                try{
                    ObjectContent[] oCary = GetObjectProperties("Task", new string[] { "info" }, taskMor);
                    snapTaskInfo = snapTaskInfo = (TaskInfo)oCary[0].propSet[0].val;
                    if (LogEvent != null)
                    {
                        LogEvent(this, new LogEventArgs(700, Severity.DEBUG, "Snapshot task completion " + snapTaskInfo.progress + "%, state=" + snapTaskInfo.state.ToString()));
                    }
                    if (snapTaskInfo.state == TaskInfoState.running || snapTaskInfo.state == TaskInfoState.queued)
                    {
                        System.Threading.Thread.Sleep(10000);
                    }
                    else
                    {
                        break;
                    }
                }
                catch {}
            }
            if (snapTaskInfo.state != TaskInfoState.success)
            {
                throw new Exception("Couldn't get VM snapshot, task ended with status " + snapTaskInfo.state + ", reason=" + snapTaskInfo.reason.ToString());
            }
            ManagedObjectReference snapMor = (ManagedObjectReference)snapTaskInfo.result;

            //	Console.WriteLine ("VMWareHandler.Snapshot') : task mor="+snapName);
            if (LogEvent != null)
            {
                LogEvent(this, new LogEventArgs(0, Severity.INFO, "Created VM snapshot with mor '" + snapMor.Value + "'"));
            }
            return(snapMor.Value);
        }
Exemplo n.º 7
0
        public List <P2PBackup.Common.Node> GetVMs()
        {
            int nbDomains = Libvirt.Connect.NumOfDomains(conn);

            int[] runningDomains = new int[nbDomains];
            Libvirt.Connect.ListDomains(conn, runningDomains, nbDomains);
            List <P2PBackup.Common.Node> vms = new List <P2PBackup.Common.Node>();

            Logger.Append("HUBRN", Severity.DEBUG, "Hypervisor " + this.Name + " has " + nbDomains + " domains(s) registered");
            //ArrayList vmNames = new ArrayList();
            // HOW to correctly retrieve the list of existing doamin IDs?? for now, hardcode 500
            for (int i = 0; i < runningDomains.Length; i++)
            {
                int domainID = runningDomains[i];
                try{
                    IntPtr domainPtr = Libvirt.Domain.LookupByID(conn, domainID);
                    if (domainPtr != IntPtr.Zero)
                    {
                        string domainName        = Libvirt.Domain.GetName(domainPtr);
                        P2PBackup.Common.Node vm = new P2PBackup.Common.Node();
                        vm.Name = domainName;
                        vm.OS   = Libvirt.Domain.GetOSType(domainPtr);
                        vm.Kind = KindEnum.Virtual;

                        /*IntPtr buf = IntPtr.Zero;
                         * //Libvirt.Domain.GetUUIDString(domainPtr, buf);
                         * //Libvirt.Domain.G
                         * char[] uuid = new char[128];
                         * Libvirt.Domain.GetUUID(domainPtr, uuid);
                         * Console.WriteLine("uuid=");
                         * foreach(char c in uuid){
                         *      Console.Write(c);
                         * }
                         * Console.WriteLine("");
                         * vm.InternalId = Marshal.PtrToStringAnsi(buf);*/
                        /*Console.WriteLine(Domain.GetXMLDesc(domainPtr, 0));
                         * foreach (XmlNode deviceNode in  GetDomainXmlNodes(Domain.GetXMLDesc(domainPtr, 0))){
                         *      Console.WriteLine(deviceNode.Name+" : "+deviceNode.Value);
                         * }*/
                        vm.InternalId = GetDomainUuid(Domain.GetXMLDesc(domainPtr, 0)).InnerText;
                        Domain.Free(domainPtr);
                        vms.Add(vm);
                    }
                }
                catch {}                // domain ID doesn't exist
            }

            return(vms);
        }
Exemplo n.º 8
0
 /// <summary>
 /// When a node connects, update some information : IP, LastConnection, OS and Version.
 /// </summary>
 public void UpdatePartial(P2PBackup.Common.Node n)
 {
     using (dbc = DAL.Instance.GetDb()){
         dbc.Update <P2PBackup.Common.Node>(new
                                            { OS             = n.OS,
                                              Version        = n.Version,
                                              LastConnection = DateTime.Now,
                                              IP             = n.IP,
                                              HostName       = n.HostName,
                                              //Plugins = n.Plugins
                                            }, p => p.Id == n.Id);
     }
     // update plugins. TODO : maybe implement a Repository pattern instead of cross-DAO calls
     new PluginDAO().AddOrUpdateForNode(n.Id, n.Plugins);
 }
Exemplo n.º 9
0
        private ManagedObjectReference GetVmMoRef(P2PBackup.Common.Node vm)
        {
            //VimApi.ServiceContent localServiceContent = service.RetrieveServiceContent(serviceRef);
            ManagedObjectReference searchIndex = serviceContent.searchIndex;

            LogEvent(this, new LogEventArgs(0, Severity.TRIVIA, "Searching VM with uuid '" + vm.InternalId + "'"));
            ManagedObjectReference vmMor = service.FindByUuid(searchIndex, null, vm.InternalId, true);

            if (vmMor != null)
            {
                LogEvent(this, new LogEventArgs(0, Severity.DEBUG, "VM search returned vmMor '" + vmMor.Value + "'"));
            }
            else
            {
                throw new Exception("Didn't found any matching VM");
            }
            return(vmMor);
        }
Exemplo n.º 10
0
        private string BuildNode(P2PBackup.Common.Node u)
        {
            StringBuilder jsb = new StringBuilder();

            jsb.AppendLine("{userName:'******',\n");
            jsb.Append("id:'" + u.Uid + "',\n");
            jsb.Append("ip:'" + u.IP + "',\n");
            jsb.Append("share:" + u.StorageSize + ",\n");
            jsb.Append("available:" + u.Available + ",\n");
            jsb.Append("group:" + u.Group + ",\n");
            jsb.Append("storagegroup:" + u.StorageGroup + ",\n");
            //jsb.Append("percent:'"+Math.Round((double)100 - u.Available/u.ShareSize*100,1)+"%', \n");
            jsb.Append("version:'" + u.Version + "',\n");
            jsb.Append("os:'" + u.OS + "',\n");
            jsb.Append("delegations:'" + String.Empty + "',\n");
            jsb.Append("backupsets:'" + u.NbBackupSets + "',\n");
            jsb.Append("lastconnection:'" + u.LastConnection + "',\n");
            if (u.Quota == -1)
            {
                jsb.Append("quota:'None',\n");
            }
            else
            {
                jsb.Append("quota:" + u.Quota + ",\n");
            }
            if (u.Quota == -1)
            {
                jsb.Append("usedquota:'" + "<img class=\"ssi\" src=\"/images/sq_di.png\"/>', \n");
            }
            else
            {
                jsb.Append("usedquota:'" + u.UsedQuota + "',\n");
            }
            //jsb.Append("usedquota:'"+FormatPercent(u.Quota, u.UsedQuota)+"%',\n");
            // specific to display

            jsb.Append("certificateStatus:'sec-OK',\n");
            jsb.Append("uiProvider:'col',\n}");
            //Console.WriteLine ("task sn : "+jsb.ToString());
            return(jsb.ToString());
        }
Exemplo n.º 11
0
        private void HandleAuthMessage(NodeMessage message)
        {
            char[]   separator = { ' ' };
            string[] decoded   = message.Data.Split(separator);
            switch (message.Action)
            {
            case "CLIENTINFO":                     // send version and OS

                P2PBackup.Common.Node remoteInfo = message.Data.FromJson <P2PBackup.Common.Node>();

                this.Version        = remoteInfo.Version;
                this.OS             = remoteInfo.OS;
                this.HostName       = remoteInfo.HostName;
                this.Plugins        = remoteInfo.Plugins;
                this.LastConnection = DateTime.Now;
                new DAL.NodeDAO().UpdatePartial(this);

                break;

            default:
                throw new P2PBackup.Common.ProtocolViolationException(message);
            }
        }
Exemplo n.º 12
0
        public List <Disk> GetDisks(P2PBackup.Common.Node vm)
        {
            //Logger.Append("HUBRN", Severity.TRIVIA, "Searching for vm with uuid '"+vm.InternalId+"' disks...");
            List <Disk> vmRawDisks = new List <Disk>();

            //try{


            string[]        props     = new string[] { "config" };
            ObjectContent[] vmObjects = GetObjectProperties("VirtualMachine", props, GetVmMoRef(vm));

            ObjectContent          oc  = null;
            ManagedObjectReference mor = null;

            DynamicProperty[] pcary = null;
            DynamicProperty   pc    = null;

            for (int oci = 0; oci < vmObjects.Length; oci++)
            {
                oc    = vmObjects[oci];
                mor   = oc.obj;
                pcary = oc.propSet;

                if (mor.type != "VirtualMachine" || pcary == null)
                {
                    continue;
                }
                //Console.WriteLine("Id : " + mor.Value); //   VM MoRef

                for (int i = 0; i < pcary.Length; i++)
                {
                    pc = pcary[i];
                    if (pc.val is VirtualMachineConfigInfo)
                    {
                        VirtualMachineConfigInfo vmci = (VirtualMachineConfigInfo)pc.val;
                        //VmConfig = vmci;

                        /*Console.WriteLine("\t Version : " + vmci.version);
                         * Console.WriteLine("\t files.vmPathName : " + vmci.files.vmPathName);
                         * // changeTrackingEnabled
                         * Console.WriteLine("\t guestId : " + vmci.guestId);
                         * Console.WriteLine("\t disk devices : ");*/
                        foreach (VirtualDevice vDev in vmci.hardware.device)
                        {
                            //Console.WriteLine("\t\t"+vDev.deviceInfo.label+" "+vDev.deviceInfo.dynamicProperty.ToString());
                            //if (vDev.backing is
                            if (vDev.backing is VirtualDiskFlatVer2BackingInfo ||
                                vDev.backing is VirtualDiskFlatVer1BackingInfo ||
                                vDev.backing is VirtualDiskSparseVer2BackingInfo ||
                                vDev.backing is VirtualDiskRawDiskMappingVer1BackingInfo ||
                                vDev.backing is VirtualDiskRawDiskVer2BackingInfo
                                )
                            {
                                Disk rawDrive = new Disk();
                                VirtualDiskFlatVer2BackingInfo vDisk = (VirtualDiskFlatVer2BackingInfo)vDev.backing;

                                //rawDrive.BlockDevice = vDisk.fileName;
                                //rawDrive.DriveType = System.IO.DriveType.Fixed;
                                //rawDrive.DriveFormat = "VMFS";
                                rawDrive.SnapshotType = SnapshotType.VADP;
                                rawDrive.Path         = vDisk.fileName;                                 //.Substring(vDisk.fileName.IndexOf("]"+1));
                                if (LogEvent != null)
                                {
                                    LogEvent(this, new LogEventArgs(0, Severity.TRIVIA, "Found disk '" + vDev.deviceInfo.label + "' with path '" + rawDrive.Path + "'"));
                                }

                                // independant_persistent disks are not part of snapshots, thus unbackupable
                                if (vDisk.diskMode == "independent_persistent" ||
                                    vDisk.dynamicType != null

                                    )
                                {
                                    if (LogEvent != null)
                                    {
                                        LogEvent(this, new LogEventArgs(0, Severity.INFO, "The disk '" + vDisk.fileName + "' won't be accessible : mode=" + vDisk.diskMode + ", dynamic=" + vDisk.dynamicType));
                                    }
                                    rawDrive.Enabled = false;
                                }
                                else if (vDev.backing is VirtualDiskRawDiskMappingVer1BackingInfo)
                                {
                                    if (((VirtualDiskRawDiskMappingVer1BackingInfo)vDev.backing).compatibilityMode == "physical")
                                    {
                                        if (LogEvent != null)
                                        {
                                            LogEvent(this, new LogEventArgs(0, Severity.INFO, "The disk '" + vDisk.fileName + "' won't be accessible since it's a physical RDM"));
                                        }
                                        rawDrive.Enabled = false;
                                    }
                                }

                                else
                                {
                                    rawDrive.Enabled = true;
                                }

                                /*Console.WriteLine("\t\t  diskMode: " + vDisk.diskMode);
                                 * Console.WriteLine("\t\t  fileName: " + vDisk.fileName);
                                 * String dataStoreName = vDisk.fileName.Substring(vDisk.fileName.IndexOf("[")+1, vDisk.fileName.LastIndexOf("]") - 1);
                                 * //Console.WriteLine("\t\t  fileName: " + vDisk.dynamicProperty);
                                 * Console.WriteLine("\t\t  dynamic: " + vDisk.dynamicType);*/
                                /*if (vDisk.dynamicProperty == null) continue;
                                 * foreach (DynamicProperty dp in vDisk.dynamicProperty){
                                 *      Console.WriteLine("\t\t* " + dp.name + " : " + dp.val);
                                 * }*/
                                vmRawDisks.Add(rawDrive);
                            }
                        }
                    }
                }                         // end for
            }

            //}
            //catch(Exception ex){
            //Logger.Append("HUBRN", Severity.ERROR, "Could not get vm disks : "+ex.ToString());
            //}

            return(vmRawDisks);
        }
Exemplo n.º 13
0
 public string GetVmMMorefId(P2PBackup.Common.Node vm)
 {
     return(GetVmMoRef(vm).Value);
 }
Exemplo n.º 14
0
 public P2PBackup.Common.Node Put(P2PBackup.Common.Node req)
 {
     return(RemotingManager.GetRemoteObject().UpdateNode((P2PBackup.Common.Node)req));
 }
Exemplo n.º 15
0
 public P2PBackup.Common.Node Get(P2PBackup.Common.Node req)
 {
     return(RemotingManager.GetRemoteObject().GetNode(req.Id));
 }
Exemplo n.º 16
0
 public P2PBackup.Common.Node UpdateNode(P2PBackup.Common.Node node)
 {
     return(new DAL.NodeDAO(sessionUser).Update(node));
 }
Exemplo n.º 17
0
        public List <P2PBackup.Common.Node> GetVMs()
        {
            List <P2PBackup.Common.Node> vms = new List <P2PBackup.Common.Node>();

            string[] props = new string[] { "name", "config", "guest.ipAddress", "guest.guestFamily" };
            //Console.WriteLine("GetVMs : calling GetObjectProperties ");
            ObjectContent[] vmObjects = GetObjectProperties("VirtualMachine", props, null);
            if (LogEvent != null)
            {
                LogEvent(this, new LogEventArgs(0, Severity.DEBUG, "GetVMs : got " + vmObjects.Length + " Objects"));
            }
            if (vmObjects == null)
            {
                return(vms);
            }
            ObjectContent          oc  = null;
            ManagedObjectReference mor = null;

            DynamicProperty[] pcary = null;
            DynamicProperty   pc    = null;

            for (int oci = 0; oci < vmObjects.Length; oci++)
            {
                oc    = vmObjects[oci];
                mor   = oc.obj;
                pcary = oc.propSet;

                if (mor.type != "VirtualMachine" || pcary == null)
                {
                    continue;
                }

                P2PBackup.Common.Node vm = new P2PBackup.Common.Node();
                for (int i = 0; i < pcary.Length; i++)
                {
                    pc = pcary[i];
                    if (pc.name == "guest.ipAddress")
                    {
                        vm.IP = pc.val.ToString();
                    }
                    else if (pc.name == "guest.guestFamily")
                    {
                        vm.OS = pc.val.ToString();
                    }
                    else if (pc.name == "name")
                    {
                        vm.Name = pc.val.ToString();
                    }
                    if (pc == null)
                    {
                        continue;
                    }
                    if (pc.val is VirtualMachineConfigInfo)
                    {
                        VirtualMachineConfigInfo vmci = (VirtualMachineConfigInfo)pc.val;
                        vm.InternalId = vmci.uuid;
                        //vm.NodeName = vmci.name;
                        vm.Kind    = KindEnum.Virtual;
                        vm.Version = vmci.version;
                    }
                }                 // end for
                vms.Add(vm);
                if (LogEvent != null)
                {
                    LogEvent(this, new LogEventArgs(0, Severity.TRIVIA, "Found VM name '" + vm.Name + "', ip=" + vm.IP + ", uuid=" + vm.InternalId));
                }
                //GetDisks(vm);
            }
            if (LogEvent != null)
            {
                LogEvent(this, new LogEventArgs(0, Severity.TRIVIA, "Discovery end."));
            }
            return(vms);
        }
Exemplo n.º 18
0
        public List <Disk> GetDisks(P2PBackup.Common.Node vm)
        {
            IntPtr domainPtr = Libvirt.Domain.LookupByUUID(conn, vm.InternalId.ToCharArray());

            return(new List <Disk>());
        }