//--------------------------------------------------------------------- // Static members /// <summary> /// Parses the Couchbase settings from the common module arguments. /// </summary> /// <param name="context">The module context.</param> /// <returns>The <see cref="CouchbaseArgs"/> or <c>null</c> if there's an error.</returns> public static CouchbaseArgs Parse(ModuleContext context) { var hive = HiveHelper.Hive; var nodeGroups = hive.Definition.GetHostGroups(excludeAllGroup: true); var couchbaseArgs = new CouchbaseArgs(); var servers = context.ParseStringArray("servers"); if (servers.Count == 0) { throw new ArgumentException($"[servers] must specify at least one Couchbase server."); } var ssl = context.ParseBool("ssl"); if (!ssl.HasValue) { ssl = false; } var scheme = ssl.Value ? "https" : "http"; var port = context.ParseInt("port", v => 0 < v && v <= ushort.MaxValue); foreach (var server in servers) { // The server can be an IP address, FQDN (with at least one dot), a hive // node name or a hive node group name. if (IPAddress.TryParse(server, out var address)) { couchbaseArgs.Settings.Servers.Add(new Uri($"{scheme}://{address}:{port}")); } else if (server.Contains(".") || server.Equals("localhost", StringComparison.InvariantCultureIgnoreCase)) { // Must be a FQDN. couchbaseArgs.Settings.Servers.Add(new Uri($"{scheme}://{server}:{port}")); } else if (nodeGroups.TryGetValue(server, out var group)) { // It's a node group so add a URL with the IP address for each // node in the group. foreach (var node in group) { couchbaseArgs.Settings.Servers.Add(new Uri($"{scheme}://{node.PrivateAddress}:{port}")); } } else { // Must be a node name. if (hive.Definition.NodeDefinitions.TryGetValue(server, out var node)) { couchbaseArgs.Settings.Servers.Add(new Uri($"{scheme}://{node.PrivateAddress}:{port}")); } else { context.WriteErrorLine($"[{server}] is not a valid IP address, FQDN, or known hive node or node group name."); return(null); } } } couchbaseArgs.Settings.Bucket = context.ParseString("bucket", v => !string.IsNullOrWhiteSpace(v)); couchbaseArgs.Credentials.Username = context.ParseString("username"); couchbaseArgs.Credentials.Password = context.ParseString("password"); if (!couchbaseArgs.Settings.IsValid) { context.WriteErrorLine("Invalid Couchbase connection settings."); return(null); } return(couchbaseArgs); }
/// <summary> /// Executes a built-in neonHIVE Ansible module. /// </summary> /// <param name="login">The hive login.</param> /// <param name="commandLine">The module command line: MODULE ARGS...</param> private void ExecuteModule(HiveLogin login, CommandLine commandLine) { var module = commandLine.Arguments.ElementAtOrDefault(0); if (commandLine.HasHelpOption || module == null) { Console.WriteLine(moduleHelp); Program.Exit(0); } var context = new ModuleContext() { Module = module }; try { // Verify that we're running in the context of another Ansible // command (probably [exec] or [play]). if (Environment.GetEnvironmentVariable("IN_NEON_ANSIBLE_COMMAND") == null) { throw new NotSupportedException("Built-in neonHIVE Ansible modules can run only within [neon ansible exec] or [play]."); } // Read the Ansible module arguments. var argsPath = commandLine.Arguments.ElementAtOrDefault(1); if (string.IsNullOrEmpty(argsPath)) { throw new ArgumentException("Expected a path to the module arguments file."); } context.Login = login; context.SetArguments(argsPath); // Connect to the hive so the [HiverHelper] methods will work. HiveHelper.OpenHive(login); // Run the module. switch (module.ToLowerInvariant()) { case "neon_certificate": new CertificateModule().Run(context); break; case "neon_couchbase_import": new CouchbaseImportModule().Run(context); break; case "neon_couchbase_index": new CouchbaseIndexModule().Run(context); break; case "neon_couchbase_query": new CouchbaseQueryModule().Run(context); break; case "neon_dashboard": new DashboardModule().Run(context); break; case "neon_docker_config": new DockerConfigModule().Run(context); break; case "neon_docker_login": new DockerLoginModule().Run(context); break; case "neon_docker_registry": new DockerRegistryModule().Run(context); break; case "neon_docker_secret": new DockerSecretModule().Run(context); break; case "neon_docker_service": new DockerServiceModule().Run(context); break; case "neon_docker_stack": new DockerStackModule().Run(context); break; case "neon_globals": new GlobalsModule().Run(context); break; case "neon_hive_dns": new HiveDnsModule().Run(context); break; case "neon_hivemq": new HiveMQModule().Run(context); break; case "neon_traffic_manager": new TrafficManagerModule().Run(context); break; default: throw new ArgumentException($"[{module}] is not a recognized neonHIVE Ansible module."); } } catch (Exception e) { context.Failed = true; context.Message = NeonHelper.ExceptionError(e); context.WriteErrorLine(context.Message); context.WriteErrorLine(e.StackTrace.ToString()); } // Handle non-exception based errors. if (context.HasErrors && !context.Failed) { context.Failed = true; context.Message = context.GetFirstError(); } Console.WriteLine(context.ToString()); // Exit right now to be sure that nothing else is written to STDOUT. Program.Exit(0); }