Example #1
0
        public Uri CurrentSourceUri()
        {
            HostUriBuilder helperHostUriBuilder = this._Context.CredentialHelper.ReverseHelperHostUriBuilder;
            HostUriBuilder hostUriBuilder       = new HostUriBuilder(helperHostUriBuilder.Uri);

            CUtils.CombinUsernameAndDomain(helperHostUriBuilder.Credentials.UserName, helperHostUriBuilder.Credentials.Domain);
            string password = helperHostUriBuilder.Credentials.Password;

            if (this._Context.JobInfoWrapper.IsSourceHostCluster)
            {
                this._Logger.Information("Finding Owning node for clustered Vm.");
                ClusteredVirtualMachinesInfo[] clusteredVirtualMachines = this._Context.ServiceResolver.Resolve <IClusterProvider>().GetCluster(helperHostUriBuilder.Uri).GetClusteredVirtualMachines();
                ClusteredVirtualMachinesInfo   virtualMachinesInfo      = ((IEnumerable <ClusteredVirtualMachinesInfo>)clusteredVirtualMachines).Where <ClusteredVirtualMachinesInfo>((Func <ClusteredVirtualMachinesInfo, bool>)(n => ((IEnumerable <VirtualMachine>)n.VirtualMachine).Any <VirtualMachine>((Func <VirtualMachine, bool>)(v => string.Compare(v.Id, this._Context.JobInfoWrapper.SourceVmInfo.Id.ToString("D"), true) == 0)))).FirstOrDefault <ClusteredVirtualMachinesInfo>();
                if (virtualMachinesInfo == null)
                {
                    this._Logger.FormatError("Couldn't find the owning node of {0}", (object)this._Context.JobInfoWrapper.SourceVmInfo.DisplayName);
                    this._Logger.Verbose("The considered nodes were :");
                    foreach (ClusteredVirtualMachinesInfo data in clusteredVirtualMachines)
                    {
                        this._Logger.Verbose(DataContractUtility.ToXmlString <ClusteredVirtualMachinesInfo>(data));
                    }
                    throw new OculiServiceException(0, "Couldn't find the owning node for the Vm resource " + this._Context.JobInfoWrapper.SourceVmInfo.DisplayName);
                }
                hostUriBuilder.NetworkId = virtualMachinesInfo.OwningNode;
            }
            return(hostUriBuilder.Uri);
        }
        /// <summary>
        /// Adds a settings object from a section in configuration
        /// </summary>
        /// <typeparam name="T">The type of settings object to create</typeparam>
        /// <param name="encryptionKey">The encryption key used for encrypting and serializing the settings object</param>
        /// <param name="sectionName">The name of the section in config</param>
        public void AddSettingsFromConfig <TSettings, TSection>(string encryptionKey, string sectionName)
            where TSettings : IConfigurableSettings <TSection>, new()
            where TSection : ConfigurationSection
        {
            // get the section from config
            TSection section = (TSection)ConfigurationManager.GetSection(sectionName);

            // if the section exists...
            if (section != null)
            {
                // create a new settings object
                TSettings settings = new TSettings();

                // populate the object from the section
                settings.PopulateFromConfigurationSection(section);

                // create new settings data object, with the new settings object serialized
                SectionSettings.Add(new ConfigurableSettingsData()
                {
                    Name = sectionName,
                    TypeAssemblyQualifiedName = typeof(TSettings).AssemblyQualifiedName,
                    TypeFullName   = typeof(TSettings).FullName,
                    SerializedData = DataContractUtility.Serialize(typeof(TSettings), settings, encryptionKey)
                });
            }
        }
Example #3
0
        /// <summary>
        /// Handles startup of the application by setting the configuration encryption key for retrieving config, then
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void OnStartup(object sender, StartupEventArgs e)
        {
            // set encryption key
            ConfigurationManager.EncryptionKey = CONFIG_ENCRYPTION_KEY;

            // load up all available data contract types
            DataContractUtility.LoadDataContractTypes();

            // start the app once loaded
            ConfigurationLoaded +=
                (app, args) =>
            {
                Deployment.Current.Dispatcher.BeginInvoke(
                    () =>
                {
                    // once config is loaded, kick off the bootstrapper
                    Bootstrapper bootstrapper = new Bootstrapper();
                    bootstrapper.Run();

                    // set the container
                    Container = bootstrapper.Container;
                });
            };

            // call base to load configuration
            base.OnStartup(sender, e);
        }
Example #4
0
 public virtual void Save()
 {
     if (this._BatchLevel != 0)
     {
         return;
     }
     this._persistenceStream.OnNext(DataContractUtility.Clone <JobInfo>(this._jobInfo));
 }
Example #5
0
        /// <summary>
        /// Reads a section of the given type from the stored sections
        /// </summary>
        /// <typeparam name="T">The type of section to deserialize</typeparam>
        /// <param name="encryptionKey">The key to use to decrypt when deserializing</param>
        /// <returns>An configuration section object of type T</returns>
        public object ReadSection(Type sectionType, string encryptionKey)
        {
            // check that there are any sections
            if (SectionSettings != null)
            {
                // get the data for the given section type and deserialize the section
                ConfigurableSettingsData configurationSectionData = SectionSettings.FirstOrDefault(s => s.TypeFullName == sectionType.FullName);
                if (configurationSectionData != null)
                {
                    return(DataContractUtility.Deserialize(sectionType, configurationSectionData.SerializedData, encryptionKey));
                }
            }

            return(null);
        }
Example #6
0
        /// <summary>
        /// Writes a section object to xml and stores it with its type in a ConfigurationSectionData object
        /// </summary>
        /// <typeparam name="T">The type of the configuration section</typeparam>
        /// <param name="encryptionKey">The key to use to encrypt when serializing</param>
        /// <param name="obj">The object to serialize and store</param>
        public void WriteSection(Type sectionType, string encryptionKey, string sectionName, object obj)
        {
            // instantiate section list, if necessary
            if (SectionSettings == null)
            {
                SectionSettings = new List <ConfigurableSettingsData>();
            }

            // add the serialized section with its type
            SectionSettings.Add(new ConfigurableSettingsData()
            {
                Name           = sectionName,
                TypeFullName   = sectionType.AssemblyQualifiedName,
                SerializedData = DataContractUtility.Serialize(sectionType, obj, encryptionKey)
            });
        }
Example #7
0
        /// <summary>
        /// Gets configuration from the server and invokes a callback when completed
        /// </summary>
        /// <param name="callback"></param>
        public void LoadConfiguration(Action callback)
        {
            // invoke service call
            base.Channel.BeginGetConfiguration(
                (asyncResult) =>
            {
                // get server configuration by ending async call
                Shared.ServerConfiguration serverConfiguration = base.Channel.EndGetConfiguration(asyncResult);
                if (serverConfiguration != null)
                {
                    // load app settings
                    if (serverConfiguration.AppSettings != null)
                    {
                        serverConfiguration.AppSettings.ForEach(appSetting => AppSettings.Add(appSetting.Key, appSetting.Value));
                    }

                    // load configuration sections
                    if (serverConfiguration.SectionSettings != null)
                    {
                        serverConfiguration.SectionSettings.ForEach(sectionData =>
                        {
                            // check that the type is recognized
                            Type sectionType = DataContractUtility.GetType(sectionData.TypeAssemblyQualifiedName, sectionData.TypeFullName, false);
                            if (sectionType != null)
                            {
                                // deserialize to object and store in configuration section
                                object section = serverConfiguration.ReadSection(sectionType, EncryptionKey);
                                if (section != null)
                                {
                                    ConfigurationSections.Add(sectionData.Name, section);
                                }
                            }
                        });
                    }
                }

                callback();
            },
                null);
        }
Example #8
0
        public Uri CurrentTargetUri()
        {
            HostUriBuilder hostUriBuilder = new HostUriBuilder(this._Context.CredentialHelper.TargetHelperUri);

            if (this._Context.JobInfoWrapper.TargetIsCluster)
            {
                this._Logger.Information("Finding Owning node for clustered Vm.");
                ClusteredVirtualMachinesInfo[] clusteredVirtualMachines = this._Context.ServiceResolver.Resolve <IClusterProvider>().GetCluster().GetClusteredVirtualMachines();
                ClusteredVirtualMachinesInfo   virtualMachinesInfo      = ((IEnumerable <ClusteredVirtualMachinesInfo>)clusteredVirtualMachines).Where <ClusteredVirtualMachinesInfo>((Func <ClusteredVirtualMachinesInfo, bool>)(n => ((IEnumerable <VirtualMachine>)n.VirtualMachine).Any <VirtualMachine>((Func <VirtualMachine, bool>)(v => string.Compare(v.Id, this._Context.JobInfoWrapper.VmUuid.ToString("D"), true) == 0)))).FirstOrDefault <ClusteredVirtualMachinesInfo>();
                if (virtualMachinesInfo == null)
                {
                    this._Logger.FormatError("Couldn't find the owning node of vm with uuid {0}", (object)this._Context.JobInfoWrapper.VmUuid);
                    this._Logger.Verbose("The considered nodes were :");
                    foreach (ClusteredVirtualMachinesInfo data in clusteredVirtualMachines)
                    {
                        this._Logger.Verbose(DataContractUtility.ToXmlString <ClusteredVirtualMachinesInfo>(data));
                    }
                    throw new OculiServiceException(0, "Couldn't find the owning node for the Vm with uuid " + (object)this._Context.JobInfoWrapper.VmUuid);
                }
                hostUriBuilder.NetworkId = virtualMachinesInfo.OwningNode;
            }
            return(hostUriBuilder.Uri);
        }
Example #9
0
        /// <summary>
        /// Creates a list of serialized parameters for invocation of a service operation
        /// </summary>
        /// <param name="serializer">The utility used to serialize the parameter objects</param>
        /// <param name="parameters">The list of parameter types and objects</param>
        /// <returns>A list of invoke parameters</returns>
        public static List <InvokeParameter> CreateInvokeParameters(List <Tuple <Type, object> > parameters, string encryptionKey)
        {
            if (parameters != null && parameters.Count > 0)
            {
                // create list of parameters
                List <InvokeParameter> serializedParameters = new List <InvokeParameter>();
                foreach (Tuple <Type, object> parameter in parameters)
                {
                    // get the object type
                    Type objectType = parameter.Item1;

                    // check if the parameter is an enumerable
                    bool isEnumerable = false;
                    if (typeof(IEnumerable).IsAssignableFrom(objectType) && objectType.IsGenericType)
                    {
                        // get the underlying object type
                        objectType   = objectType.GetGenericArguments().First();
                        isEnumerable = true;
                    }

                    // create the parameter with the type information and the serialize object
                    serializedParameters.Add(
                        new InvokeParameter()
                    {
                        TypeName = objectType.FullName,
                        AssemblyQualifiedTypeName = objectType.AssemblyQualifiedName,
                        IsEnumerable        = isEnumerable,
                        SerializedParameter = DataContractUtility.Serialize(parameter.Item1, parameter.Item2, encryptionKey)
                    });
                }

                return(serializedParameters);
            }

            return(null);
        }
Example #10
0
 /// <summary>
 /// Deserializes the operation result to the given type, using the given encryption key
 /// </summary>
 /// <typeparam name="T">The type to which to deserialize the result</typeparam>
 /// <param name="encryptionKey">The key to use to decrypt the serialized object before deserializing</param>
 /// <returns>The deserialized operation result</returns>
 public T GetValue <T>(string encryptionKey)
 {
     // deserialize to the given type
     return(DataContractUtility.Deserialize <T>(OperationResult, encryptionKey));
 }
Example #11
0
        public InvokeResponse Invoke(InvokeRequest request)
        {
            InvokeResponse response = new InvokeResponse();

            if (!string.IsNullOrEmpty(request.ComponentType))
            {
                IServiceComponentManager componentManager = Container.Resolve <IServiceComponentManager>();
                IServiceComponent        component        = componentManager.ServiceComponents.FirstOrDefault(c => c != null && c.GetType().GetInterface(request.ComponentType) != null);

                if (component != null)
                {
                    MethodInfo operation =
                        component.GetType().GetMethod(request.OperationName,
                                                      request.OperationParameters.Select(op => DataContractUtility.GetType(op.AssemblyQualifiedTypeName, op.TypeName, op.IsEnumerable)).ToArray());
                    if (operation != null)
                    {
                        try
                        {
                            List <object> parameters = new List <object>();
                            foreach (InvokeParameter parameter in request.OperationParameters)
                            {
                                parameters.Add(DataContractUtility.Deserialize(DataContractUtility.GetType(parameter.AssemblyQualifiedTypeName, parameter.TypeName, parameter.IsEnumerable),
                                                                               parameter.SerializedParameter));
                            }

                            object result;

                            try
                            {
                                result = operation.Invoke(component, parameters.ToArray());
                                if (result != null)
                                {
                                    response.OperationResult = DataContractUtility.Serialize(result.GetType(), result);
                                }
                            }
                            catch (Exception ex)
                            {
                                response.Error = new Exception("An exception occurred trying to invoke operation.", ex);
                            }
                        }
                        catch (Exception ex)
                        {
                            response.Error = new InvalidRequestException("An exception occurred trying to deserialize operation parameters.", ex);
                        }
                    }
                    else
                    {
                        response.Error = new InvalidRequestException("Operation not found.");
                    }
                }
                else
                {
                    response.Error = new InvalidRequestException("Component not found.");
                }
            }
            else
            {
                response.Error = new InvalidRequestException("Component type not provided.");
            }

            return(response);
        }