Exemplo n.º 1
0
        public void LocateConfigFileEmpty_ShouldPass(string ConfigPath)
        {
            var expected = Path.Combine("etc", ".envars");
            var result   = ConfigurationLocator.LocateConfigFile(ConfigPath);

            Assert.Equal(expected, result);
        }
Exemplo n.º 2
0
        public void Compile()
        {
            IFunctionAppConfiguration configuration = ConfigurationLocator.FindConfiguration(_configurationSourceAssembly);

            if (configuration == null)
            {
                throw new ConfigurationException($"The assembly {_configurationSourceAssembly.GetName().Name} does not contain a public class implementing the IFunctionAppConfiguration interface");
            }

            string newAssemblyNamespace = $"{_configurationSourceAssembly.GetName().Name}.Functions";
            FunctionHostBuilder builder = new FunctionHostBuilder(_serviceCollection, _commandRegistry, false);

            configuration.Build(builder);
            new PostBuildPatcher().Patch(builder, newAssemblyNamespace);

            VerifyCommandAndResponseTypes(builder);

            IReadOnlyCollection <string> externalAssemblies = GetExternalAssemblyLocations(builder.FunctionDefinitions);
            OpenApiOutputModel           openApi            = _openApiCompiler.Compile(builder.OpenApiConfiguration, builder.FunctionDefinitions, _outputBinaryFolder);

            _jsonCompiler.Compile(builder.FunctionDefinitions, openApi, _outputBinaryFolder, newAssemblyNamespace);
            if (_outputProxiesJson && builder.AreProxiesEnabled)
            {
                _proxiesJsonCompiler.Compile(builder.FunctionDefinitions, builder.OpenApiConfiguration, openApi, _outputBinaryFolder);
            }

            _assemblyCompiler.Compile(builder.FunctionDefinitions,
                                      configuration.GetType(),
                                      newAssemblyNamespace,
                                      externalAssemblies,
                                      _outputBinaryFolder,
                                      $"{newAssemblyNamespace}.dll",
                                      openApi,
                                      _target, builder.OutputAuthoredSourceFolder);
        }
Exemplo n.º 3
0
        private void mergeLocalConfig(string path)
        {
            var localConfigPath = _configuration.WatchToken;

            if (_localConfigurationLocation != null)
            {
                localConfigPath = _localConfigurationLocation;
            }
            var file = new ConfigurationLocator().GetConfiguration(localConfigPath);

            if (File.Exists(file))
            {
                _bus.Publish(new InformationMessage("Loading local config file " + file));
            }
            _configuration.Reload(file);
            if (_tamperWithLogging)
            {
                if (_configuration.DebuggingEnabled)
                {
                    _configuration.EnableLogging();
                }
                else
                {
                    _configuration.DisableLogging();
                }
            }
        }
Exemplo n.º 4
0
        public static IRunner CreateForWorkerRole(Role role, RoleIsolationMode isolation)
        {
            var config = role.Config;

            var assemblyFilePath = config.Assembly;
            var assemblyPath     = Path.GetDirectoryName(assemblyFilePath);

            if (HasBeenReBuilt(assemblyPath) && isolation == RoleIsolationMode.Thread)
            {
                isolation          = RoleIsolationMode.AppDomain;
                role.IsolationMode = isolation;
            }

            switch (isolation)
            {
            case RoleIsolationMode.Thread:
                return(new ThreadRunner(role, assemblyFilePath, ConfigurationLocator.LocateConfigurationFile(config.ConfigurationPath), role.RoleName));

            case RoleIsolationMode.AppDomain:
                var setup = new AppDomainSetup
                {
                    ApplicationBase   = assemblyPath,
                    ConfigurationFile = config.Assembly + ".config",
                };
                return(new AppDomainRunner(role, setup, assemblyFilePath, ConfigurationLocator.LocateConfigurationFile(config.ConfigurationPath), role.RoleName));

            default:
                throw new NotSupportedException();
            }
        }
Exemplo n.º 5
0
 private string getLocalConfig()
 {
     try
     {
         if (_watchToken == null)
         {
             return("");
         }
         if (!File.Exists(_watchToken) && !Directory.Exists(_watchToken))
         {
             return("");
         }
         var config = new ConfigurationLocator().GetConfiguration(_watchToken);
         if (config == null)
         {
             return("");
         }
         return(config);
     }
     catch (Exception ex)
     {
         Logger.Write(ex);
         return("");
     }
 }
Exemplo n.º 6
0
        public void RefreshConfiguration()
        {
            if (licenseIsInvalid())
            {
                return;
            }

            var    config = BootStrapper.Services.Locate <IConfiguration>();
            string file   = new ConfigurationLocator().GetConfiguration(_watchPath);

            Logger.WriteDetails("Reloading configuration with local config " + file);
            config.Reload(file);
            ValidateConfiguration();
        }
Exemplo n.º 7
0
        public bool Compile()
        {
            string newAssemblyNamespace = $"{_configurationSourceAssembly.GetName().Name.Replace("-", "_")}.Functions";
            IFunctionCompilerMetadata functionCompilerMetadata = null;
            IFunctionAppConfiguration configuration            = ConfigurationLocator.FindConfiguration(_configurationSourceAssembly);

            if (configuration == null)
            {
                functionCompilerMetadata = ConfigurationLocator.FindCompilerMetadata(_configurationSourceAssembly);
                if (functionCompilerMetadata == null)
                {
                    _compilerLog.Error($"The assembly {_configurationSourceAssembly.GetName().Name} does not contain a public class implementing the IFunctionAppConfiguration interface");
                    return(false);
                }
            }
            else
            {
                FunctionHostBuilder builder = new FunctionHostBuilder(_serviceCollection, _commandRegistry, false);
                configuration.Build(builder);
                new PostBuildPatcher().Patch(builder, newAssemblyNamespace);
                if (!VerifyCommandAndResponseTypes(builder))
                {
                    return(false);
                }

                functionCompilerMetadata = new FunctionCompilerMetadata
                {
                    FunctionDefinitions        = builder.FunctionDefinitions,
                    OpenApiConfiguration       = builder.OpenApiConfiguration,
                    OutputAuthoredSourceFolder = builder.OutputAuthoredSourceFolder
                };
            }

            IReadOnlyCollection <string> externalAssemblies = GetExternalAssemblyLocations(functionCompilerMetadata.FunctionDefinitions);
            OpenApiOutputModel           openApi            = _openApiCompiler.Compile(functionCompilerMetadata.OpenApiConfiguration, functionCompilerMetadata.FunctionDefinitions, _outputBinaryFolder);

            _jsonCompiler.Compile(functionCompilerMetadata.FunctionDefinitions, openApi, _outputBinaryFolder, newAssemblyNamespace);

            return(_assemblyCompiler.Compile(functionCompilerMetadata.FunctionDefinitions,
                                             configuration?.GetType() ?? functionCompilerMetadata.BacklinkReferenceType,
                                             configuration != null ? null : functionCompilerMetadata.BacklinkPropertyInfo,
                                             newAssemblyNamespace,
                                             externalAssemblies,
                                             _outputBinaryFolder,
                                             $"{newAssemblyNamespace}.dll",
                                             openApi,
                                             _compileTarget, functionCompilerMetadata.OutputAuthoredSourceFolder));
        }
Exemplo n.º 8
0
        public static WebHostArgs Create(RoleConfiguration config)
        {
            var assembly = config.Assembly;
            var args     = new WebHostArgs
            {
                Assembly          = assembly,
                Port              = int.Parse(config.Port),
                RoleName          = config.RoleName,
                Title             = config.Title,
                ConfigurationPath = ConfigurationLocator.LocateConfigurationFile(config.ConfigurationPath),
                UseSsl            = bool.Parse(config.UseSsl),
                Hostname          = config.Hostname,
                UseHostedStorage  = false,
                Use64Bit          = false,
            };

            return(args);
        }
Exemplo n.º 9
0
        public static HostArgs ParseArgs(IEnumerable <string> args)
        {
            string assembly          = null;
            string roleName          = null;
            string title             = null;
            string configurationPath = null;
            var    useHostedStorage  = false;
            var    allowSilentFail   = false;
            var    displayHelp       = false;

            var options = new OptionSet
            {
                {
                    "a|assembly=",
                    "The path to the primary {ASSEMBLY} for the worker role.",
                    v => assembly = v
                },
                {
                    "n|roleName=",
                    "The {NAME} of the role as defined in the configuration file.",
                    v => roleName = v
                },
                {
                    "t|serviceTitle=",
                    "Optional {TITLE} for the role window. Defaults to role name if not specified.",
                    v => title = v
                },
                {
                    "c|configurationPath=",
                    "The {PATH} to the configuration file. Either the directory containing ServiceConfiguration.Local.cscfg or the path to a specific alternate .cscfg file.",
                    v => configurationPath = v
                },
                {
                    "useHostedStorage",
                    "Use hosted storage (Emulator/Actual Azure) inside the LightBlue host.",
                    v => useHostedStorage = true
                },
                {
                    "allowSilentFail",
                    "Allow the host to fail silently instead of throwing an exception when the hosted process exits.",
                    v => allowSilentFail = true
                },
                {
                    "help",
                    "Show this message and exit.",
                    v => displayHelp = true
                }
            };

            try
            {
                options.Parse(args);
            }
            catch (OptionException e)
            {
                DisplayErrorMessage(e.Message);
                return(null);
            }

            if (displayHelp)
            {
                DisplayHelp(options);
                return(null);
            }

            if (string.IsNullOrWhiteSpace(assembly))
            {
                DisplayErrorMessage("Host requires an assembly to run.");
                return(null);
            }
            if (string.IsNullOrWhiteSpace(roleName))
            {
                DisplayErrorMessage("Role Name must be specified.");
                return(null);
            }
            if (string.IsNullOrWhiteSpace(configurationPath))
            {
                DisplayErrorMessage("Configuration path must be specified.");
                return(null);
            }

            var roleAssemblyAbsolutePath = Path.IsPathRooted(assembly)
                ? assembly
                : Path.Combine(Environment.CurrentDirectory, assembly);

            if (!File.Exists(roleAssemblyAbsolutePath))
            {
                DisplayErrorMessage("The specified assembly cannot be found. The assembly must be in the host directory or be specified as an absolute path.");
                return(null);
            }

            var roleConfigurationFile = assembly + ".config";

            if (!File.Exists(roleConfigurationFile))
            {
                DisplayErrorMessage("The configuration file for the role cannot be located.");
                return(null);
            }

            return(new HostArgs
            {
                Assembly = assembly,
                RoleName = roleName,
                Title = string.IsNullOrWhiteSpace(title)
                    ? roleName
                    : title,
                ConfigurationPath = ConfigurationLocator.LocateConfigurationFile(configurationPath),
                ServiceDefinitionPath = ConfigurationLocator.LocateServiceDefinition(configurationPath),
                UseHostedStorage = useHostedStorage,
                AllowSilentFail = allowSilentFail,
                RoleConfigurationFile = roleConfigurationFile
            });
        }
Exemplo n.º 10
0
        public bool Compile()
        {
            string newAssemblyNamespace = $"{_configurationSourceAssembly.GetName().Name.Replace("-", "_")}.Functions";
            IFunctionCompilerMetadata functionCompilerMetadata = null;

            IFunctionAppConfiguration configuration  = null;
            FunctionAppHostBuilder    appHostBuilder = null;
            IFunctionAppHost          appHost        = ConfigurationLocator.FindFunctionAppHost(_configurationSourceAssembly);

            if (appHost != null)
            {
                appHostBuilder = new FunctionAppHostBuilder();
                appHost.Build(appHostBuilder);
                if (appHostBuilder.FunctionAppConfiguration != null)
                {
                    configuration = (IFunctionAppConfiguration)Activator.CreateInstance(appHostBuilder.FunctionAppConfiguration);
                }
            }

            if (configuration == null)
            {
                configuration = ConfigurationLocator.FindConfiguration(_configurationSourceAssembly);
            }

            if (configuration == null)
            {
                functionCompilerMetadata = ConfigurationLocator.FindCompilerMetadata(_configurationSourceAssembly);
                if (functionCompilerMetadata == null)
                {
                    _compilerLog.Error($"The assembly {_configurationSourceAssembly.GetName().Name} does not contain a public class implementing the IFunctionAppConfiguration interface");
                    return(false);
                }
            }
            else
            {
                FunctionHostBuilder builder = new FunctionHostBuilder(_serviceCollection, _commandRegistry, false);
                if (appHostBuilder != null)
                {
                    builder.Options = appHostBuilder.Options;
                }
                configuration.Build(builder);
                DefaultMediatorSettings.SetDefaultsIfRequired(builder);
                if (!ValidateCommandTypes(builder))
                {
                    return(false);
                }
                IMediatorResultTypeExtractor extractor = CreateMediatorResultTypeExtractor(builder.Options.MediatorResultTypeExtractor);
                if (extractor == null)
                {
                    return(false);
                }
                new PostBuildPatcher(extractor).Patch(builder, newAssemblyNamespace);
                if (!VerifyCommandAndResponseTypes(builder))
                {
                    return(false);
                }

                if (!VerifyOutputBindings(builder))
                {
                    return(false);
                }

                functionCompilerMetadata = new FunctionCompilerMetadata
                {
                    FunctionDefinitions        = builder.FunctionDefinitions,
                    OpenApiConfiguration       = builder.OpenApiConfiguration,
                    OutputAuthoredSourceFolder = builder.Options.OutputSourceTo,
                    CompilerOptions            = builder.Options
                };
            }

            PostBuildPatcher.EnsureFunctionsHaveUniqueNames(functionCompilerMetadata.FunctionDefinitions);

            IReadOnlyCollection <string> externalAssemblies =
                GetExternalAssemblyLocations(functionCompilerMetadata.FunctionDefinitions);

            ITargetCompiler targetCompiler = functionCompilerMetadata.CompilerOptions.HttpTarget == CompileTargetEnum.AzureFunctions
                ? (ITargetCompiler) new AzureFunctionsCompiler(_compilerLog)
                : new AspNetCoreCompiler(_compilerLog);

            return(targetCompiler.CompileAssets(functionCompilerMetadata,
                                                newAssemblyNamespace,
                                                configuration,
                                                externalAssemblies,
                                                _outputBinaryFolder));
        }
Exemplo n.º 11
0
        public static WebHostArgs ParseArgs(IEnumerable <string> args)
        {
            string assembly           = null;
            int?   port               = null;
            string roleName           = null;
            string title              = null;
            string configurationPath  = null;
            bool?  useSsl             = null;
            var    hostname           = "localhost";
            var    useHostedStorage   = false;
            var    allowSilentFail    = false;
            string iisExpressTemplate = null;
            var    displayHelp        = false;
            bool   use64Bit           = false;

            var options = new OptionSet
            {
                {
                    "a|assembly=",
                    "The path to the primary {ASSEMBLY} for the web role.",
                    v => assembly = v
                },
                {
                    "p|port=",
                    "The {PORT} on which the website should be available. Must be specified if useSsl is specified.",
                    (int v) => port = v
                },
                {
                    "n|roleName=",
                    "The {NAME} of the role as defined in the configuration file.",
                    v => roleName = v
                },
                {
                    "t|serviceTitle=",
                    "Optional {TITLE} for the role window. Defaults to role name if not specified.",
                    v => title = v
                },
                {
                    "c|configurationPath=",
                    "The {PATH} to the configuration file. Either the directory containing ServiceConfiguration.Local.cscfg or the path to a specific alternate .cscfg file.",
                    v => configurationPath = v
                },
                {
                    "s|useSsl=",
                    "Indicates whether the site should be started with SSL. Defaults to false. Must be specified in port is specified.",
                    (bool v) => useSsl = v
                },
                {
                    "h|hostname=",
                    "The {HOSTNAME} the site should be started with. Defaults to localhost",
                    v => hostname = v
                },
                {
                    "useHostedStorage",
                    "Use hosted storage (Emulator/Actual Azure) inside the LightBlue host.",
                    v => useHostedStorage = true
                },
                {
                    "allowSilentFail",
                    "Allow the host to fail silently instead of throwing an exception when the hosted process exits. Applies only to the background process not the website.",
                    v => allowSilentFail = true
                },
                {
                    "iet|iisExpressTemplate=",
                    "Path to an alternate {TEMPLATE} for IIS Express. If not specified the inbuilt default is used.",
                    v => iisExpressTemplate = v
                },
                {
                    "help",
                    "Show this message and exit.",
                    v => displayHelp = true
                },
                {
                    "u|use64bit=",
                    "Indicates whether to use IISExpress 64 bit. Defaults to false.",
                    (bool v) => use64Bit = v
                }
            };

            try
            {
                options.Parse(args);
            }
            catch (OptionException e)
            {
                DisplayErrorMessage(e.Message);
                return(null);
            }

            if (displayHelp)
            {
                DisplayHelp(options);
                return(null);
            }

            if (string.IsNullOrWhiteSpace(assembly))
            {
                DisplayErrorMessage("Host requires an assembly to run.");
                return(null);
            }
            if (string.IsNullOrWhiteSpace(roleName))
            {
                DisplayErrorMessage("Role Name must be specified.");
                return(null);
            }
            if (string.IsNullOrWhiteSpace(configurationPath))
            {
                DisplayErrorMessage("Configuration path must be specified.");
                return(null);
            }
            if (string.IsNullOrWhiteSpace(hostname))
            {
                DisplayErrorMessage(
                    "The hostname cannot be blank. Do not specify this option if you wish to use the default (localhost).");
                return(null);
            }

            var roleAssemblyAbsolutePath = Path.IsPathRooted(assembly)
                ? assembly
                : Path.Combine(Environment.CurrentDirectory, assembly);

            if (!File.Exists(roleAssemblyAbsolutePath))
            {
                DisplayErrorMessage("The specified site assembly cannot be found.");
                return(null);
            }

            if (port.HasValue != useSsl.HasValue)
            {
                DisplayErrorMessage("If either port or useSsl is specified both must be specified.");
                return(null);
            }

            var serviceDefinitionPath = ConfigurationLocator.LocateServiceDefinition(configurationPath);

            if (!port.HasValue)
            {
                var endpoint = GetEndpoint(serviceDefinitionPath, roleName);
                if (endpoint == null)
                {
                    DisplayErrorMessage(
                        "Cannot locate endpoint in ServiceDefinition.csdef. Verify that an endpoint is defined.");
                    return(null);
                }

                port   = endpoint.Item1;
                useSsl = endpoint.Item2;
            }

            if (!string.IsNullOrWhiteSpace(iisExpressTemplate) && !File.Exists(iisExpressTemplate))
            {
                DisplayErrorMessage("The specified IIS Express template cannot be located.");
                return(null);
            }

            if (use64Bit && (!Environment.Is64BitOperatingSystem && string.IsNullOrEmpty(Environment.GetEnvironmentVariable("ProgramW6432"))))
            {
                DisplayErrorMessage("Use64Bit flag cannot be true on a 32bit platform");
                return(null);
            }

            return(new WebHostArgs
            {
                Assembly = assembly,
                Port = port.Value,
                RoleName = roleName,
                Title = string.IsNullOrWhiteSpace(title)
                    ? roleName
                    : title,
                ConfigurationPath = ConfigurationLocator.LocateConfigurationFile(configurationPath),
                ServiceDefinitionPath = serviceDefinitionPath,
                UseSsl = useSsl.Value,
                Hostname = hostname,
                UseHostedStorage = useHostedStorage,
                AllowSilentFail = allowSilentFail,
                IisExpressTemplate = iisExpressTemplate,
                Use64Bit = use64Bit
            });
        }