예제 #1
0
        private void LoadApplicationConfigurations()
        {
            foreach (var site in serverManager.Sites)
            {
                var bindings = site.Bindings.Select(b => string.Format("{0}://{1}", b.Protocol, b.BindingInformation)).ToArray();
                if (logger.IsDebugEnabled)
                {
                    logger.Debug("Found site: {0}, binding: {1}", site.Name, string.Join(", ", bindings));
                }

                foreach (var app in site.Applications)
                {
                    try {
                        logger.Debug("Found application: {0}, pool: {1}", app.Path, app.ApplicationPoolName);
                        // get the default virtual directory - we will treat it as the application path
                        var defaultVirtDir = app.VirtualDirectories["/"];
                        var c = new ApplicationServerConfig {
                            AppType        = ApplicationServerConfig.WebAppType,
                            AppPath        = defaultVirtDir.PhysicalPath,
                            Server         = Environment.MachineName,
                            ServerFqdnOrIp = MusketeerConfiguration.ServerFqdnOrIp,
                            AppPoolName    = app.ApplicationPoolName,
                            Bindings       = bindings
                        };
                        configs.Add(c);
                    } catch (Exception ex) {
                        logger.Error(ex, "Failed when querying information about application: '{0}'", app.Path);
                    }
                }
            }
        }
예제 #2
0
        public virtual async Task AddOrUpdateAppServerConfigAsync(ApplicationServerConfig config)
        {
            if (config == null || config.AppPath == null || config.Server == null)
            {
                throw new ArgumentException("AppPath and Server must be provided");
            }
            var c = new AppConfig {
                PathHash       = GetApplicationHash(config.AppPath),
                Path           = config.AppPath,
                Server         = config.Server,
                ServerFqdnOrIp = config.ServerFqdnOrIp,
                Binding        = string.Join("|", config.Bindings),
                AppPoolName    = config.AppPoolName,
                AppType        = config.AppType,
                ServiceName    = config.ServiceName,
                DisplayName    = config.DisplayName
            };

            using (var conn = CreateConnection()) {
                await conn.OpenAsync();

                lock (lck) {
                    // try to update the record or insert it
                    var rec = conn.Execute("update ApplicationConfigs set Path = @Path, Binding = @Binding, AppPoolName = @AppPoolName, " +
                                           "AppType = @AppType, ServiceName = @ServiceName, DisplayName = @DisplayName, ServerFqdnOrIp = @ServerFqdnOrIp " +
                                           "where PathHash = @PathHash and Server = @Server", c);
                    if (rec == 0)
                    {
                        // no application found - we need to insert it
                        conn.Execute("insert into ApplicationConfigs (PathHash, Path, Server, ServerFqdnOrIp, Binding, AppPoolName, AppType, ServiceName, DisplayName) values " +
                                     "(@PathHash, @Path, @Server, @ServerFqdnOrIp, @Binding, @AppPoolName, @AppType, @ServiceName, @DisplayName)", c);
                    }
                }
            }
        }
        public override async Task AddOrUpdateAppServerConfigAsync(ApplicationServerConfig config)
        {
            if (config == null || config.AppPath == null || config.Server == null)
            {
                throw new ArgumentException("AppPath and Server must be provided");
            }
            var c = new AppConfig {
                PathHash       = GetApplicationHash(config.AppPath),
                Path           = config.AppPath,
                Server         = config.Server,
                ServerFqdnOrIp = config.ServerFqdnOrIp,
                Binding        = string.Join("|", config.Bindings),
                AppPoolName    = config.AppPoolName,
                AppType        = config.AppType,
                ServiceName    = config.ServiceName,
                DisplayName    = config.DisplayName
            };

            using (var conn = new MySqlConnection(dbConnString)) {
                await conn.OpenAsync();

                // try to update the record or insert it
                await conn.ExecuteAsync("replace into ApplicationConfigs (PathHash, Path, Server, ServerFqdnOrIp, Binding, AppPoolName, AppType, ServiceName, DisplayName) values " +
                                        "(@PathHash, @Path, @Server, @ServerFqdnOrIp, @Binding, @AppPoolName, @AppType, @ServiceName, @DisplayName)", c);
            }
        }
        public async Task AddOrUpdateAppServerConfigAsync(ApplicationServerConfig config)
        {
            if (config == null || config.AppPath == null || config.Server == null)
            {
                throw new ArgumentException("AppPath and Server must be provided");
            }
            var econfig = new ElasticApplicationConfig();

            Map(config, econfig);
            await eclient.IndexAsync(econfig, ind => ind.Index(AppConfIndexName));
        }
        private void Map(ElasticApplicationConfig from, ApplicationServerConfig to)
        {
            to.AppPath        = from.Path;
            to.Server         = from.Server;
            to.ServerFqdnOrIp = from.ServerFqdnOrIp;
            to.ServiceName    = from.ServiceName;
            to.AppPoolName    = from.AppPoolName;
            to.AppType        = from.AppType;
            to.Bindings       = from.Binding != null?from.Binding.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries) : null;

            to.DisplayName = from.DisplayName;
        }
        private void Map(ApplicationServerConfig from, ElasticApplicationConfig to)
        {
            to.Id = BitConverter.ToString(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(from.AppPath + ":" +
                                                                                          from.Server))).Replace("-", string.Empty);
            to.Path           = from.AppPath;
            to.Server         = from.Server;
            to.ServerFqdnOrIp = from.ServerFqdnOrIp;
            to.ServiceName    = from.ServiceName;
            to.AppPoolName    = from.AppPoolName;
            to.AppType        = from.AppType;
            to.Binding        = from.Bindings != null?string.Join("|", from.Bindings) : null;

            to.DisplayName = from.DisplayName;
        }
        public async Task <IEnumerable <ApplicationServerConfig> > GetAppConfigsAsync(string[] appPaths, string server = null)
        {
            var filter = Query <ElasticApplicationConfig> .Bool(q => q.Filter(
                                                                    qf => qf.Terms(tq => tq.Field(econf => econf.Path).Terms(appPaths))));

            if (server != null)
            {
                filter = Query <ElasticApplicationConfig> .Bool(q => q.Filter(f => filter, f => f.Term(econf => econf.Server, server)));
            }
            return((await eclient.SearchAsync <ElasticApplicationConfig>(s => s.Query(q => filter).Index(
                                                                             AppConfIndexName).Take(400))).Hits.Select(h => {
                var conf = new ApplicationServerConfig();
                Map(h.Source, conf);
                return conf;
            }));
        }
예제 #8
0
        public async Task TestConfiguration()
        {
            var conf = new ElasticSearchAppConfigurationManager();

            var expectedApp = new Application {
                Path = path, IsExcluded = true, IsHidden = false
            };
            await conf.AddOrUpdateAppAsync(expectedApp);

            // give it 1s to swallow
            await Task.Delay(1000);

            var app = await conf.FindAppAsync(expectedApp.Path);

            Assert.NotNull(app);
            Assert.Equal(expectedApp.Path, app.Path);
            Assert.Equal(expectedApp.IsExcluded, app.IsExcluded);
            Assert.Equal("blablabla", app.Name); // when no name is provided we will use the one based on a path
            Assert.Equal(true, app.IsExcluded);
            Assert.Equal(false, app.IsHidden);

            expectedApp.IsExcluded = false;
            expectedApp.IsHidden   = true;

            await conf.AddOrUpdateAppAsync(expectedApp);

            // give it 1s to swallow
            await Task.Delay(1000);

            var apps = await conf.GetAppsAsync();

            Assert.Contains(apps, ap => expectedApp.Path.Equals(ap.Path, StringComparison.OrdinalIgnoreCase));

            app = await conf.FindAppAsync(expectedApp.Path);

            Assert.Equal(true, app.IsExcluded);
            Assert.Equal(true, app.IsHidden);

            expectedApp.Name       = "newappname";
            expectedApp.IsExcluded = false;

            await conf.AddOrUpdateAppAsync(expectedApp);

            // give it 1s to swallow
            await Task.Delay(1000);

            app = await conf.FindAppAsync(expectedApp.Path);

            Assert.NotNull(app);
            Assert.Equal(expectedApp.Path, app.Path);
            Assert.Equal(expectedApp.IsExcluded, app.IsExcluded);
            Assert.Equal(expectedApp.Name, app.Name);

            app.IsExcluded = true;
            await conf.UpdateAppPropertiesAsync(app, new[] { "IsExcluded" });

            // give it 1s to swallow
            await Task.Delay(1000);

            app = await conf.FindAppAsync(expectedApp.Path);

            Assert.True(app.IsExcluded);

            var appconf = new ApplicationServerConfig {
                AppPath        = app.Path,
                Server         = "TEST2",
                ServerFqdnOrIp = "test2.ad.com",
                Bindings       = new[] { "*:80:", "127.0.0.1:80:", ":80:www.test.com" },
                AppType        = ApplicationServerConfig.WinSvcType,
                ServiceName    = "Test.Service",
                DisplayName    = "Test Service Display"
            };
            await conf.AddOrUpdateAppServerConfigAsync(appconf);

            // give it 1s to swallow
            await Task.Delay(2000);

            var dbconf = (await conf.GetAppConfigsAsync(new[] { app.Path })).FirstOrDefault();

            Assert.NotNull(dbconf);
            Assert.Equal(appconf.AppPath, dbconf.AppPath);
            Assert.Equal(appconf.AppPoolName, dbconf.AppPoolName);
            Assert.Equal(appconf.Server, dbconf.Server);
            Assert.Equal(appconf.ServerFqdnOrIp, dbconf.ServerFqdnOrIp);
            Assert.Contains(appconf.Bindings[0], dbconf.Bindings);
            Assert.Contains(appconf.Bindings[1], dbconf.Bindings);
            Assert.Contains(appconf.Bindings[2], dbconf.Bindings);
            Assert.Equal(appconf.AppType, dbconf.AppType);
            Assert.Equal(appconf.ServiceName, dbconf.ServiceName);
            Assert.Equal(appconf.DisplayName, dbconf.DisplayName);

            Assert.True(app.IsExcluded);
        }
 public Task AddOrUpdateAppServerConfigAsync(ApplicationServerConfig config)
 {
     return(wrappedInstance.AddOrUpdateAppServerConfigAsync(config));
 }
예제 #10
0
        private void LoadIISAppConfigs(IList <ApplicationServerConfig> configs, IDictionary <string, AppInfo> appinfo)
        {
            Debug.Assert(appinfo != null);
            Debug.Assert(configs != null);

            ServerManager mgr = null;

            try {
                mgr = ServerManager.OpenRemote("localhost");
                var poolToApps = new Dictionary <string, List <string> >();
                // information about application logs (if any) - Item1 is logpath, Item2 is filter
                var applogs = new Dictionary <string, Tuple <string, string> >(StringComparer.Ordinal);

                foreach (var site in mgr.Sites)
                {
                    var bindings = site.Bindings.Select(b => string.Format("{0}://{1}", b.Protocol, b.BindingInformation)).ToArray();
                    if (logger.IsDebugEnabled)
                    {
                        logger.Debug("Found site: {0}, binding: {1}", site.Name, string.Join(", ", bindings));
                    }
                    // log configuration (we currently support only W3C logs
                    string logpath = null;
                    if (site.LogFile.Enabled && site.LogFile.LogFormat == LogFormat.W3c &&
                        !string.IsNullOrEmpty(site.LogFile.Directory))
                    {
                        logpath = Path.Combine(site.LogFile.Directory, "W3SVC" + site.Id);
                    }

                    foreach (var app in site.Applications)
                    {
                        try {
                            logger.Debug("Found application: {0}, pool: {1}", app.Path, app.ApplicationPoolName);
                            // get the default virtual directory - we will treat it as the application path
                            var defaultVirtDir = app.VirtualDirectories["/"];
                            var c = new ApplicationServerConfig {
                                AppType        = ApplicationServerConfig.WebAppType,
                                AppPath        = defaultVirtDir.PhysicalPath,
                                Server         = Environment.MachineName,
                                ServerFqdnOrIp = serverFqdnOrIp,
                                AppPoolName    = app.ApplicationPoolName,
                                Bindings       = bindings
                            };
                            configs.Add(c);
                            if (logpath != null && !applogs.ContainsKey(c.AppPath))
                            {
                                applogs.Add(c.AppPath, new Tuple <string, string>(logpath, app.Path));
                            }

                            List <string> poolApps;
                            if (!poolToApps.TryGetValue(c.AppPoolName, out poolApps))
                            {
                                poolApps = new List <string>();
                                poolToApps.Add(c.AppPoolName, poolApps);
                            }
                            poolApps.Add(c.AppPath);
                        } catch (Exception ex) {
                            logger.Error(ex, "Failed when querying information about application: '{0}'", app.Path);
                        }
                    }
                }

                // now it's time to fill the workers table
                foreach (var pool in mgr.ApplicationPools)
                {
                    try {
                        if (pool.WorkerProcesses != null && pool.WorkerProcesses.Count > 0)
                        {
                            List <string> appPaths;
                            if (poolToApps.TryGetValue(pool.Name, out appPaths))
                            {
                                var pids = pool.WorkerProcesses.Select(w => w.ProcessId).ToArray();
                                if (logger.IsDebugEnabled)
                                {
                                    logger.Debug("Found worker processes: {0} for the pool: '{1}'", string.Join(",", pids), pool.Name);
                                }
                                foreach (var path in appPaths)
                                {
                                    if (!appinfo.ContainsKey(path))
                                    {
                                        var ai = new AppInfo {
                                            Path       = path,
                                            ProcessIds = pids
                                        };
                                        Tuple <string, string> li;
                                        if (applogs.TryGetValue(path, out li))
                                        {
                                            ai.LogType    = ELogType.W3SVC;
                                            ai.LogEnabled = true;
                                            ai.LogsPath   = li.Item1;
                                            ai.LogFilter  = li.Item2;
                                        }
                                        appinfo.Add(path, ai);
                                    }
                                }
                            }
                        }
                    } catch (COMException ex) {
                        logger.Warn(ex, "Problem when querying information about a pool: '{0}'", pool.Name);
                    }
                }
            } catch (Exception ex) {
                logger.Error(ex, "Problem when querying information about the IIS.");
            } finally {
                if (mgr != null)
                {
                    mgr.Dispose();
                }
            }
        }
예제 #11
0
        public async Task TestConfiguration()
        {
            var conf = new SqlServerAppConfigurationManager();

            var expectedApp = new Application {
                Path = path, IsExcluded = true, IsHidden = false
            };

            await conf.AddOrUpdateAppAsync(expectedApp);

            var app = await conf.FindAppAsync(expectedApp.Path);

            Assert.NotNull(app);
            Assert.Equal(expectedApp.Path, app.Path);
            Assert.Equal(true, app.IsExcluded);
            Assert.Equal(false, app.IsHidden);
            Assert.Equal("defaultstest12312312", app.Name); // when no name is provided we will use the one based on a path

            expectedApp.IsExcluded = false;
            expectedApp.IsHidden   = true;

            await conf.AddOrUpdateAppAsync(expectedApp);

            app = await conf.FindAppAsync(expectedApp.Path);

            Assert.Equal(true, app.IsExcluded);
            Assert.Equal(true, app.IsHidden);

            expectedApp.Name       = "newappname";
            expectedApp.IsExcluded = false;

            await conf.AddOrUpdateAppAsync(expectedApp);

            app = await conf.FindAppAsync(expectedApp.Path);

            Assert.NotNull(app);
            Assert.Equal(expectedApp.Path, app.Path);
            Assert.Equal(expectedApp.IsExcluded, app.IsExcluded);
            Assert.Equal(expectedApp.Name, app.Name);

            app.IsExcluded = true;
            await conf.UpdateAppPropertiesAsync(app, new [] { "IsExcluded" });

            app = await conf.FindAppAsync(expectedApp.Path);

            Assert.True(app.IsExcluded);

            var appconf = new ApplicationServerConfig {
                AppPath        = app.Path,
                Server         = "TEST2",
                ServerFqdnOrIp = "test2.ad.com",
                Bindings       = new [] { "*:80:", "127.0.0.1:80:", ":80:www.test.com" },
                AppType        = ApplicationServerConfig.WebAppType,
                ServiceName    = "Test.Service",
                DisplayName    = "Test Service Display"
            };
            await conf.AddOrUpdateAppServerConfigAsync(appconf);

            var dbconf = (await conf.GetAppConfigsAsync(new[] { app.Path })).FirstOrDefault();

            Assert.NotNull(dbconf);
            Assert.Equal(appconf.AppPath, dbconf.AppPath);
            Assert.Equal(appconf.AppPoolName, dbconf.AppPoolName);
            Assert.Equal(appconf.Server, dbconf.Server);
            Assert.Equal(appconf.ServerFqdnOrIp, dbconf.ServerFqdnOrIp);
            Assert.Contains(appconf.Bindings[0], dbconf.Bindings);
            Assert.Contains(appconf.Bindings[1], dbconf.Bindings);
            Assert.Contains(appconf.Bindings[2], dbconf.Bindings);
            Assert.Equal(appconf.AppType, dbconf.AppType);
            Assert.Equal(appconf.ServiceName, dbconf.ServiceName);
            Assert.Equal(appconf.DisplayName, dbconf.DisplayName);

            Assert.True(app.IsExcluded);
        }