public ConfigurationLocationsParser(IFileSystem fileSystemInstance, IEnvironmentVariableProvider environmentVariableProvider)
        {
            AppName = CurrentApplicationInfo.Name;
            environmentVariableProvider.SetEnvironmentVariableForProcess("AppName", CurrentApplicationInfo.Name);

            ConfigRoot = environmentVariableProvider.GetEnvironmentVariable(HS_CONFIG_ROOT);

            if (string.IsNullOrEmpty(ConfigRoot))
            {
                ConfigRoot = environmentVariableProvider.PlatformSpecificPathPrefix + HS_CONFIG_ROOT_DEFAULT;
            }

            LoadPathsFilePath = environmentVariableProvider.GetEnvironmentVariable(HS_CONFIG_PATHS_FILE);

            if (string.IsNullOrEmpty(LoadPathsFilePath))
            {
                LoadPathsFilePath = Path.Combine(ConfigRoot, LOADPATHS_JSON);
            }

            //Normalize slashes
            LoadPathsFilePath = LoadPathsFilePath.Replace("\\", "/");

            Trace.WriteLine("Started parsing configurations from location " + LoadPathsFilePath + "\n");

            var configPathDeclarations = ParseAndValidateConfigLines(LoadPathsFilePath, fileSystemInstance);

            ConfigFileDeclarations = ExpandConfigPathDeclarations(environmentVariableProvider, configPathDeclarations, environmentVariableProvider.PlatformSpecificPathPrefix).ToArray();
        }
Exemplo n.º 2
0
        public EnvironmentInstance(IEnvironmentVariableProvider environmentVariableProvider, Func <DataCentersConfig> getDataCentersConfig)
        {
            _environmentVariableProvider = environmentVariableProvider;
            GetDataCentersConfig         = getDataCentersConfig;
            Zone    = environmentVariableProvider.GetEnvironmentVariable("ZONE") ?? environmentVariableProvider.GetEnvironmentVariable("DC");
            _region = environmentVariableProvider.GetEnvironmentVariable("REGION");
            DeploymentEnvironment = environmentVariableProvider.GetEnvironmentVariable("ENV");
            ConsulAddress         = environmentVariableProvider.GetEnvironmentVariable("CONSUL");

            if (string.IsNullOrEmpty(Zone) || string.IsNullOrEmpty(DeploymentEnvironment))
            {
                throw new EnvironmentException("One or more of the following environment variables, which are required, have not been set: %ZONE%, %ENV%");
            }
        }
        private List <ConfigFileDeclaration> ExpandConfigPathDeclarations(IEnvironmentVariableProvider environmentVariableProvider, ConfigFileDeclaration[] configs, string prefix)
        {
            var configPathsSet = new SortedSet <ConfigFileDeclaration>(configs);

            var toSave = new List <ConfigFileDeclaration>();

            var listOfErrors = new List <ErrorAggregator>();

            foreach (var configPath in configPathsSet)
            {
                configPath.Pattern = configPath.Pattern.Replace("$(prefix)", prefix);

                var list = Regex.Matches(configPath.Pattern, "%([^%]+)%")
                           .Cast <Match>()
                           .Select(match => new
                {
                    Placehodler = match.Groups[0].Value,
                    Value       = environmentVariableProvider.GetEnvironmentVariable(match.Groups[1].Value)
                }).ToList();

                var missingEnvVariables = list.Where(a => string.IsNullOrEmpty(a.Value)).Select(a => a.Placehodler.Trim('%')).ToList();

                if (missingEnvVariables.Any())
                {
                    listOfErrors.Add(new ErrorAggregator
                    {
                        Line         = configPath.Pattern,
                        EnvVariables = missingEnvVariables
                    });
                }
                else
                {
                    foreach (var valToReplace in list)
                    {
                        configPath.Pattern = configPath.Pattern.Replace(valToReplace.Placehodler, valToReplace.Value);
                    }

                    configPath.Pattern = configPath.Pattern.Replace("\\", "/");
                    //Assumes $(appName) present only once.
                    if (Regex.Match(configPath.Pattern, @"/?[^$/]*\$\(appName\)").Success)
                    {
                        configPath.Pattern = configPath.Pattern.Replace("$(appName)", AppName);
                    }

                    toSave.Add(configPath);
                    Trace.WriteLine(configPath.Pattern + " priority=" + configPath.Priority + " " + SearchOption.TopDirectoryOnly);
                }
            }

            if (listOfErrors.Any())
            {
                var errorMessage = PrepareErrorMessage(listOfErrors);

                throw new ConfigurationException(errorMessage);
            }
            return(toSave);
        }
        /// <summary>
        /// Parses the content of environment variables file content.
        /// </summary>
        public EnvironmentVariablesFileReader(IFileSystem fileSystem, IEnvironmentVariableProvider environmentVariableProvider)
        {
            locEnvFilePath = environmentVariableProvider.GetEnvironmentVariable(HS_ENV_VARS_FILE);

            if (string.IsNullOrEmpty(locEnvFilePath))
            {
                locEnvFilePath = string.Format(ENV_FILEPATH, environmentVariableProvider.PlatformSpecificPathPrefix);
            }

            EnvironmentVariableProvider = environmentVariableProvider;
            FileSystem = fileSystem;
        }
Exemplo n.º 5
0
        public async Task <ConfigItemsCollection> GetConfiguration()
        {
            var conf = new Dictionary <string, ConfigItem>();

            foreach (var configFile in _configurationLocations.ConfigFileDeclarations.SelectMany(FindConfigFiles))
            {
                await ReadConfiguration(configFile, conf).ConfigureAwait(false);
            }

            var collection = new ConfigItemsCollection(conf.Values);

            var notFoundEnvVariables = new List <string>();

            foreach (var configItem in collection.Items)
            {
                var list = paramMatcher.Matches(configItem.RawValue)
                           .Cast <Match>()
                           .Select(match => new
                {
                    Placehodler = "%" + match.Groups[1].Value + "%",
                    Value       = _environmentVariableProvider.GetEnvironmentVariable(match.Groups[1].Value)
                }).ToList();

                if (list.Any())
                {
                    var notFound = list.Where(a => string.IsNullOrEmpty(a.Value)).Select(a => a.Placehodler.Trim('%')).ToArray();

                    if (!notFound.Any())
                    {
                        foreach (var valToReplace in list)
                        {
                            configItem.Value = configItem.Value.Replace(valToReplace.Placehodler, valToReplace.Value);
                        }
                    }
                    else
                    {
                        notFoundEnvVariables.AddRange(notFound);
                    }
                }
            }

            if (notFoundEnvVariables.Any())
            {
                throw new EnvironmentException("Configuration is dependent on following enviroment variables:" + string.Join("\n", notFoundEnvVariables) + "\n but they are not set.");
            }

            // return merged configuration
            return(collection);
        }
Exemplo n.º 6
0
        public void BaseTest(Dictionary <string, string> envDictionary, ConfigFileDeclaration[] expected)
        {
            environmentVariableProvider.GetEnvironmentVariable(Arg.Any <string>()).Returns(key => {
                envDictionary.TryGetValue(key.Arg <string>(), out string val);
                return(val);
            });

            var configs = new ConfigurationLocationsParser(_fileSystem, environmentVariableProvider, new CurrentApplicationInfo(""));

            configs.ConfigFileDeclarations.Count.ShouldBe(expected.Length);

            foreach (var pair in configs.ConfigFileDeclarations.Zip(expected, (first, second) => new { first, second }))
            {
                pair.first.Pattern.ShouldBe(pair.second.Pattern);
                pair.first.Priority.ShouldBe(pair.second.Priority);
            }
        }
Exemplo n.º 7
0
        public EnvironmentInstance(IEnvironmentVariableProvider environmentVariableProvider, Func <DataCentersConfig> getDataCentersConfig, CurrentApplicationInfo applicationInfo)
        {
            _environmentVariableProvider = environmentVariableProvider;
            GetDataCentersConfig         = getDataCentersConfig;
            Zone    = environmentVariableProvider.GetEnvironmentVariable("ZONE") ?? environmentVariableProvider.GetEnvironmentVariable("DC");
            _region = environmentVariableProvider.GetEnvironmentVariable("REGION");
            DeploymentEnvironment = environmentVariableProvider.GetEnvironmentVariable("ENV");
            ConsulAddress         = environmentVariableProvider.GetEnvironmentVariable("CONSUL");
            InstanceName          = applicationInfo.InstanceName ?? environmentVariableProvider.GetEnvironmentVariable("GIGYA_SERVICE_INSTANCE_NAME") ?? DEFAULT_INSTANCE_NAME;

            if (string.IsNullOrEmpty(Zone) || string.IsNullOrEmpty(DeploymentEnvironment))
            {
                throw new EnvironmentException("One or more of the following environment variables, which are required, have not been set: %ZONE%, %ENV%");
            }
        }
Exemplo n.º 8
0
 public string GetEnvironmentVariable(string name) => _environmentVariableProvider.GetEnvironmentVariable(name);