public async override Task<bool> InvokeAsync(string paramList) { var currentConfig = _configBusiness.OpenDatabaseConfig(); var config = new DatabaseConfig(currentConfig.Url, string.Empty, string.Empty, string.Empty); var index = 0; var logonInfo = await GetUsernameAsync(paramList, index++, config, "config_database"); if (logonInfo == null) return false; var result = await ServiceCommands.GetServiceStatusAsync(); if (result != null) { await ServiceCommands.RestartServiceAsync(); } return true; }
public async override Task<bool> InvokeAsync(string paramList) { var index = 0; var response = await GetServerUrlAsync(paramList, index++, null); if (string.IsNullOrEmpty(response)) return false; var config = new DatabaseConfig(response, null, null, null); var logonInfo = await GetUsernameAsync(paramList, index++, config, "config_change"); if (logonInfo == null) return false; var result = await ServiceCommands.GetServiceStatusAsync(); if (result != null) { await ServiceCommands.RestartServiceAsync(); } return true; }
protected async Task<IDatabaseConfig> GetUsernameAsync(string paramList, int index, IDatabaseConfig config, string action) { var points = new[] { new Point { Name = Constants.ServiceName, Tags = new Dictionary<string, string> { { "hostname", Environment.MachineName }, { "version", Assembly.GetExecutingAssembly().GetName().Version.ToString() }, { "action", action }, }, Fields = new Dictionary<string, object> { { "value", 1 } }, Precision = TimeUnit.Microseconds, Timestamp = DateTime.UtcNow }, }; var dataChanged = false; var url = config.Url; IInfluxDbAgent client = null; InfluxDbApiResponse response = null; try { if (!string.IsNullOrEmpty(config.Name) && !string.IsNullOrEmpty(config.Username) && !string.IsNullOrEmpty(config.Password)) { client = _influxDbAgentLoader.GetAgent(config); response = await client.WriteAsync(points); } } catch (Exception exception) { OutputError(exception.Message); } if (response == null || !response.Success) OutputInformation("Enter the database, username and password for the InfluxDB."); while (response == null || !response.Success) { var database = string.Empty; try { database = QueryParam<string>("DatabaseName", GetParam(paramList, index++)); var user = QueryParam<string>("Username", GetParam(paramList, index++)); var password = QueryParam<string>("Password", GetParam(paramList, index++)); config = new DatabaseConfig(url, user, password, database); client = _influxDbAgentLoader.GetAgent(config); response = await client.WriteAsync(points); dataChanged = true; } catch (CommandEscapeException) { return null; } catch (InfluxDbApiException exception) { if (exception.StatusCode == HttpStatusCode.NotFound) { var create = QueryParam("Database does not exist, create?", GetParam(paramList, index++), new Dictionary<bool, string>() { { true, "Yes" }, { false, "No" } }); if (create) { client.CreateDatabaseAsync(database); response = client.WriteAsync(points).Result; dataChanged = true; } } } catch (Exception exception) { OutputError("{0}", exception.Message); } } OutputInformation("Access to database {0} confirmed.", config.Name); if (dataChanged) _configBusiness.SaveDatabaseConfig(config.Name, config.Username, config.Password); return config; }
private static DatabaseConfig GetDatabaseConfig(XmlDocument document) { var databases = document.GetElementsByTagName("Database"); if (databases.Count == 0) return null; string url = null; string username = null; string password = null; string name = null; foreach (XmlElement item in databases[0].ChildNodes) { switch (item.Name) { case "Url": url = item.InnerText; break; case "Username": username = item.InnerText; break; case "Password": password = Decrypt(item.InnerText); break; case "Name": name = item.InnerText; break; case "": break; } } var database = new DatabaseConfig(url, username, password, name); return database; }
private void SaveDatabaseConfigEx(DatabaseConfig newDbConfig) { var path = GetAppDataFolder(); var databaseConfigFilePath = path + "\\database.xml"; var xml = new XmlDocument(); var xme = xml.CreateElement(Constants.ServiceName); xml.AppendChild(xme); var dme = xml.CreateElement("Database"); xme.AppendChild(dme); var xmeUrl = xml.CreateElement("Url"); xmeUrl.InnerText = newDbConfig.Url; dme.AppendChild(xmeUrl); var xmeUsername = xml.CreateElement("Username"); xmeUsername.InnerText = newDbConfig.Username; dme.AppendChild(xmeUsername); var xmePassword = xml.CreateElement("Password"); xmePassword.InnerText = Encrypt(newDbConfig.Password); dme.AppendChild(xmePassword); var xmeName = xml.CreateElement("Name"); xmeName.InnerText = newDbConfig.Name; dme.AppendChild(xmeName); var xmlData = xml.ToFormattedString(); _fileLoaderAgent.WriteAllText(databaseConfigFilePath, xmlData); }
public void SaveDatabaseConfig(string databaseName, string username, string password) { var config = OpenDatabaseConfig(); var newDbConfig = new DatabaseConfig(config.Url, username, password, databaseName); SaveDatabaseConfigEx(newDbConfig); }
public void SaveDatabaseUrl(string url) { var config = OpenDatabaseConfig(); var newDbConfig = new DatabaseConfig(url, config.Username, config.Password, config.Name); SaveDatabaseConfigEx(newDbConfig); }