Example #1
0
        private static void SetAppPool(ApplicationPool appPool, dynamic model)
        {
            Debug.Assert(appPool != null);
            Debug.Assert((bool)(model != null));

            DynamicHelper.If((object)model.name, v => SetName(appPool, v));

            appPool.ManagedPipelineMode   = DynamicHelper.To <ManagedPipelineMode>(model.pipeline_mode) ?? appPool.ManagedPipelineMode;
            appPool.ManagedRuntimeVersion = DynamicHelper.Value(model.managed_runtime_version) ?? appPool.ManagedRuntimeVersion;
            appPool.Enable32BitAppOnWin64 = DynamicHelper.To <bool>(model.enable_32bit_win64) ?? appPool.Enable32BitAppOnWin64;
            appPool.QueueLength           = DynamicHelper.To(model.queue_length, 10, 65535) ?? appPool.QueueLength;
            appPool.AutoStart             = DynamicHelper.To <bool>(model.auto_start) ?? appPool.AutoStart;

            // CPU
            if (model.cpu != null)
            {
                dynamic cpu = model.cpu;

                appPool.Cpu.Limit                     = DynamicHelper.To(cpu.limit, 0, 100000) ?? appPool.Cpu.Limit;
                appPool.Cpu.SmpAffinitized            = DynamicHelper.To <bool>(cpu.processor_affinity_enabled) ?? appPool.Cpu.SmpAffinitized;
                appPool.Cpu.SmpProcessorAffinityMask  = DynamicHelper.ToLong(cpu.processor_affinity_mask32, 16, 0, 4294967295) ?? appPool.Cpu.SmpProcessorAffinityMask;
                appPool.Cpu.SmpProcessorAffinityMask2 = DynamicHelper.ToLong(cpu.processor_affinity_mask64, 16, 0, 4294967295) ?? appPool.Cpu.SmpProcessorAffinityMask2;

                try {
                    appPool.Cpu.Action = DynamicHelper.To <ProcessorAction>(cpu.action) ?? appPool.Cpu.Action;
                }
                catch (COMException e) {
                    throw new ApiArgumentException("cpu.action", e);
                }

                long?resetInterval = DynamicHelper.To(cpu.limit_interval, 0, 1440);
                appPool.Cpu.ResetInterval = (resetInterval != null) ? TimeSpan.FromMinutes(resetInterval.Value) : appPool.Cpu.ResetInterval;
            }

            // Process Model
            if (model.process_model != null)
            {
                dynamic processModel = model.process_model;

                if (appPool.ProcessModel.Schema.HasAttribute(IdleTimeoutActionAttribute))
                {
                    appPool.ProcessModel.IdleTimeoutAction = DynamicHelper.To <IdleTimeoutAction>(processModel.idle_timeout_action) ?? appPool.ProcessModel.IdleTimeoutAction;
                }
                appPool.ProcessModel.MaxProcesses   = DynamicHelper.To(processModel.max_processes, 0, 2147483647) ?? appPool.ProcessModel.MaxProcesses;
                appPool.ProcessModel.PingingEnabled = DynamicHelper.To <bool>(processModel.pinging_enabled) ?? appPool.ProcessModel.PingingEnabled;

                long?idleTimeout = DynamicHelper.To(processModel.idle_timeout, 0, 43200);
                appPool.ProcessModel.IdleTimeout = (idleTimeout != null) ? TimeSpan.FromMinutes(idleTimeout.Value) : appPool.ProcessModel.IdleTimeout;

                long?pingInterval = DynamicHelper.To(processModel.ping_interval, 1, 4294967);
                appPool.ProcessModel.PingInterval = (pingInterval != null) ? TimeSpan.FromSeconds(pingInterval.Value) : appPool.ProcessModel.PingInterval;

                long?pingResponseTime = DynamicHelper.To(processModel.ping_response_time, 1, 4294967);
                appPool.ProcessModel.PingResponseTime = (pingResponseTime != null) ? TimeSpan.FromSeconds(pingResponseTime.Value) : appPool.ProcessModel.PingResponseTime;

                long?shutDownTimeLimit = DynamicHelper.To(processModel.shutdown_time_limit, 1, 4294967);
                appPool.ProcessModel.ShutdownTimeLimit = (shutDownTimeLimit != null) ? TimeSpan.FromSeconds(shutDownTimeLimit.Value) : appPool.ProcessModel.ShutdownTimeLimit;

                long?startupTimeLimit = DynamicHelper.To(processModel.startup_time_limit, 1, 4294967);
                appPool.ProcessModel.StartupTimeLimit = (startupTimeLimit != null) ? TimeSpan.FromSeconds(startupTimeLimit.Value): appPool.ProcessModel.StartupTimeLimit;
            }

            // Identity
            if (model.identity != null)
            {
                dynamic identity = model.identity;

                appPool.ProcessModel.IdentityType    = DynamicHelper.To <ProcessModelIdentityType>(identity.identity_type) ?? appPool.ProcessModel.IdentityType;
                appPool.ProcessModel.LoadUserProfile = DynamicHelper.To <bool>(identity.load_user_profile) ?? appPool.ProcessModel.LoadUserProfile;
                appPool.ProcessModel.UserName        = DynamicHelper.Value(identity.username) ?? appPool.ProcessModel.UserName;
                DynamicHelper.If((object)identity.password, v => appPool.ProcessModel.Password = v);
            }

            // Recycling
            if (model.recycling != null)
            {
                dynamic recycling = model.recycling;

                appPool.Recycling.DisallowOverlappingRotation    = DynamicHelper.To <bool>(recycling.disable_overlapped_recycle) ?? appPool.Recycling.DisallowOverlappingRotation;
                appPool.Recycling.DisallowRotationOnConfigChange = DynamicHelper.To <bool>(recycling.disable_recycle_on_config_change) ?? appPool.Recycling.DisallowRotationOnConfigChange;

                // Check if log event collection provided
                if (recycling.log_events != null)
                {
                    try {
                        // Convert the log_events dynamic into a string and then deserialize it into a Dictionary<string,bool>, from there we turn it into a flags enum
                        Dictionary <string, bool> logEvents = JsonConvert.DeserializeObject <Dictionary <string, bool> >(recycling.log_events.ToString());

                        var flags = appPool.Recycling.LogEventOnRecycle;

                        if (logEvents == null)
                        {
                            throw new ApiArgumentException("recycling.log_events");
                        }

                        Dictionary <string, RecyclingLogEventOnRecycle> flagPairs = new Dictionary <string, RecyclingLogEventOnRecycle>
                        {
                            { "time", RecyclingLogEventOnRecycle.Time },
                            { "requests", RecyclingLogEventOnRecycle.Requests },
                            { "schedule", RecyclingLogEventOnRecycle.Schedule },
                            { "memory", RecyclingLogEventOnRecycle.Memory },
                            { "isapi_unhealthy", RecyclingLogEventOnRecycle.IsapiUnhealthy },
                            { "on_demand", RecyclingLogEventOnRecycle.OnDemand },
                            { "config_change", RecyclingLogEventOnRecycle.ConfigChange },
                            { "private_memory", RecyclingLogEventOnRecycle.PrivateMemory }
                        };

                        foreach (var key in flagPairs.Keys)
                        {
                            if (logEvents.ContainsKey(key))
                            {
                                if (logEvents[key])
                                {
                                    flags |= flagPairs[key];
                                }
                                else
                                {
                                    flags &= ~flagPairs[key];
                                }
                            }
                        }

                        appPool.Recycling.LogEventOnRecycle = flags;
                    }
                    catch (JsonSerializationException e) {
                        throw new ApiArgumentException("recycling.log_events", e);
                    }
                }

                // Periodic Restart
                if (recycling.periodic_restart != null)
                {
                    dynamic periodicRestart = recycling.periodic_restart;

                    appPool.Recycling.PeriodicRestart.PrivateMemory = DynamicHelper.To(periodicRestart.private_memory, 0, 4294967295) ?? appPool.Recycling.PeriodicRestart.PrivateMemory;
                    appPool.Recycling.PeriodicRestart.Requests      = DynamicHelper.To(periodicRestart.request_limit, 0, 4294967295) ?? appPool.Recycling.PeriodicRestart.Requests;
                    appPool.Recycling.PeriodicRestart.Memory        = DynamicHelper.To(periodicRestart.virtual_memory, 0, 4294967295) ?? appPool.Recycling.PeriodicRestart.Memory;

                    long?timeInterval = DynamicHelper.To(periodicRestart.time_interval, 0, 432000);
                    appPool.Recycling.PeriodicRestart.Time = timeInterval != null?TimeSpan.FromMinutes(timeInterval.Value) : appPool.Recycling.PeriodicRestart.Time;


                    // Check if schedule provided
                    if (periodicRestart.schedule != null)
                    {
                        if (!(periodicRestart.schedule is JArray))
                        {
                            throw new ApiArgumentException("recyclying.periodic_restart.schedule", ApiArgumentException.EXPECTED_ARRAY);
                        }

                        // Clear the old time spans in the schedule
                        appPool.Recycling.PeriodicRestart.Schedule.Clear();
                        IEnumerable <dynamic> schedule = periodicRestart.schedule;

                        // Add the time spans
                        foreach (var d in schedule)
                        {
                            var value = DynamicHelper.Value(d);

                            DateTime dt = default(DateTime);
                            if (value == null || !DateTime.TryParseExact(value, "HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
                            {
                                throw new ApiArgumentException("recyclying.periodic_restart.schedule.item", "Expected hh:mm");
                            }

                            appPool.Recycling.PeriodicRestart.Schedule.Add(dt.TimeOfDay);
                        }
                    }
                }
            }

            // Rapid Fail Protection
            if (model.rapid_fail_protection != null)
            {
                var protection = model.rapid_fail_protection;

                appPool.Failure.RapidFailProtection           = DynamicHelper.To <bool>(protection.enabled) ?? appPool.Failure.RapidFailProtection;
                appPool.Failure.LoadBalancerCapabilities      = DynamicHelper.To <LoadBalancerCapabilities>(protection.load_balancer_capabilities) ?? appPool.Failure.LoadBalancerCapabilities;
                appPool.Failure.RapidFailProtectionMaxCrashes = DynamicHelper.To(protection.max_crashes, 1, 2147483647) ?? appPool.Failure.RapidFailProtectionMaxCrashes;
                appPool.Failure.AutoShutdownExe    = DynamicHelper.Value(protection.auto_shutdown_exe) ?? appPool.Failure.AutoShutdownExe;
                appPool.Failure.AutoShutdownParams = DynamicHelper.Value(protection.auto_shutdown_params) ?? appPool.Failure.AutoShutdownParams;

                long?protectionInterval = DynamicHelper.To(protection.interval, 1, 144400);
                appPool.Failure.RapidFailProtectionInterval = (protectionInterval != null) ? TimeSpan.FromMinutes(protectionInterval.Value) : appPool.Failure.RapidFailProtectionInterval;
            }

            // Process Orphaning
            if (model.process_orphaning != null)
            {
                var orphaning = model.process_orphaning;

                appPool.Failure.OrphanWorkerProcess = DynamicHelper.To <bool>(orphaning.enabled) ?? appPool.Failure.OrphanWorkerProcess;
                appPool.Failure.OrphanActionExe     = DynamicHelper.Value(orphaning.orphan_action_exe) ?? appPool.Failure.OrphanActionExe;
                appPool.Failure.OrphanActionParams  = DynamicHelper.Value(orphaning.orphan_action_params) ?? appPool.Failure.OrphanActionParams;
            }
        }