use of liquibase.configuration.ConfiguredValue in project liquibase by liquibase.
the class HubUpdater method register.
/**
* Automatically register the current user with Hub
*
* @param changeLogFile ChangeLog path for this operation
* @throws LiquibaseException Thrown if registration fails
* @throws CommandExecutionException Thrown if registerChangeLog fails
*/
public HubRegisterResponse register(String changeLogFile) throws LiquibaseException {
HubRegisterResponse registerResponse = null;
if (!hubService.isOnline()) {
return null;
}
//
if (!Scope.getCurrentScope().getUI().getAllowPrompt()) {
return null;
}
// 2. We have a changelog and a changeLogId in it already
if (!StringUtil.isEmpty(HubConfiguration.LIQUIBASE_HUB_API_KEY.getCurrentValue()) || (changeLog != null && changeLog.getChangeLogId() != null)) {
return null;
}
if (skipAutoRegistration != null && skipAutoRegistration) {
return null;
}
//
try {
LockService lockService = LockServiceFactory.getInstance().getLockService(database);
lockService.releaseLock();
} catch (LockException e) {
Scope.getCurrentScope().getLog(HubUpdater.class).warning(Liquibase.MSG_COULD_NOT_RELEASE_LOCK);
}
String promptString = "Do you want to see this operation's report in Liquibase Hub, which improves team collaboration? \n" + "If so, enter your email. If not, enter [N] to no longer be prompted, or [S] to skip for now, but ask again next time";
String input = Scope.getCurrentScope().getUI().prompt(promptString, "S", (input1, returnType) -> {
input1 = input1.trim().toLowerCase();
if (!(input1.equals("s") || input1.equals("n") || input1.contains("@"))) {
throw new IllegalArgumentException(String.format("Invalid value: '%s'", input1));
}
return input1;
}, String.class);
//
// Re-lock before proceeding
//
LockService lockService = LockServiceFactory.getInstance().getLockService(database);
lockService.waitForLock();
String defaultsFilePath = Scope.getCurrentScope().get("defaultsFile", String.class);
File defaultsFile = null;
if (defaultsFilePath != null) {
defaultsFile = new File(defaultsFilePath);
}
input = input.toLowerCase();
if (input.equals("n")) {
//
try {
String message = "No operations will be reported. Simply add a liquibase.hub.apiKey setting to generate free deployment reports. Learn more at https://hub.liquibase.com";
Scope.getCurrentScope().getUI().sendMessage(message);
Scope.getCurrentScope().getLog(getClass()).info(message);
writeToPropertiesFile(defaultsFile, "\nliquibase.hub.mode=off\n");
message = "* Updated properties file " + defaultsFile + " to set liquibase.hub.mode=off";
Scope.getCurrentScope().getUI().sendMessage(message);
Scope.getCurrentScope().getLog(getClass()).info(message);
DeprecatedConfigurationValueProvider.setData(HubConfiguration.LIQUIBASE_HUB_MODE, HubConfiguration.HubMode.OFF);
} catch (IOException ioe) {
String message = "Unable to write hubMode to liquibase.properties: " + ioe.getMessage();
Scope.getCurrentScope().getUI().sendMessage(message);
Scope.getCurrentScope().getLog(getClass()).warning(message);
}
} else if (input.equals("s")) {
String message = "Skipping auto-registration";
Scope.getCurrentScope().getUI().sendMessage(message);
Scope.getCurrentScope().getLog(getClass()).warning(message);
skipAutoRegistration = true;
} else {
//
try {
registerResponse = hubService.register(input);
} catch (LiquibaseException lhe) {
String message = "Account creation failed for email address '" + input + "': " + lhe.getMessage() + ".\n" + "No operation report will be generated.";
Scope.getCurrentScope().getUI().sendMessage(message);
Scope.getCurrentScope().getLog(HubUpdater.class).warning(message);
return registerResponse;
}
if (registerResponse == null) {
String message = "Account creation failed for email address '" + input + "'.\n" + "No operation report will be generated.";
Scope.getCurrentScope().getUI().sendMessage(message);
Scope.getCurrentScope().getLog(HubUpdater.class).warning(message);
return registerResponse;
}
String message;
try {
//
// Update the properties file
//
writeToPropertiesFile(defaultsFile, "\nliquibase.hub.apiKey=" + registerResponse.getApiKey() + "\n");
//
// If there is no liquibase.hub.mode setting then add one with value 'all'
// Do not update liquibase.hub.mode if it is already set
//
ConfiguredValue<HubConfiguration.HubMode> hubModeProperty = HubConfiguration.LIQUIBASE_HUB_MODE.getCurrentConfiguredValue();
if (hubModeProperty.wasDefaultValueUsed()) {
writeToPropertiesFile(defaultsFile, "\nliquibase.hub.mode=all\n");
message = "* Updated properties file " + defaultsFile + " to set liquibase.hub properties";
Scope.getCurrentScope().getUI().sendMessage(message);
Scope.getCurrentScope().getLog(getClass()).info(message);
} else {
message = "* Updated the liquibase.hub.apiKey property.";
String message2 = "The liquibase.hub.mode is already set to " + hubModeProperty.getValue() + ". It will not be updated.";
Scope.getCurrentScope().getUI().sendMessage(message);
Scope.getCurrentScope().getUI().sendMessage(message2);
Scope.getCurrentScope().getLog(getClass()).warning(message);
Scope.getCurrentScope().getLog(getClass()).warning(message2);
}
// register the changelog if it exist
DeprecatedConfigurationValueProvider.setData(HubConfiguration.LIQUIBASE_HUB_API_KEY, registerResponse.getApiKey());
if (changeLog != null) {
message = "* Registering changelog file " + changeLogFile + " with Hub";
Scope.getCurrentScope().getUI().sendMessage(message);
Scope.getCurrentScope().getLog(getClass()).info(message);
// Update the API key in HubConfiguration
registerChangeLog(registerResponse.getProjectId(), changeLog, changeLogFile);
}
message = "Great! Your free operation and deployment reports will be available to you after your local Liquibase commands complete.";
Scope.getCurrentScope().getUI().sendMessage(message);
Scope.getCurrentScope().getLog(getClass()).info(message);
} catch (IOException ioe) {
message = "Unable to write information to liquibase.properties: " + ioe.getMessage() + "\n" + "Please check your permissions. No operations will be reported.";
Scope.getCurrentScope().getUI().sendMessage(message);
Scope.getCurrentScope().getLog(getClass()).warning(message);
} catch (CommandExecutionException cee) {
message = "Unable to register changelog: " + cee.getMessage() + "\n" + "No operations will be reported.";
Scope.getCurrentScope().getUI().sendMessage(message);
Scope.getCurrentScope().getLog(getClass()).warning(message);
// System.setProperty(HubConfiguration.LIQUIBASE_HUB_API_KEY.getKey(), null);
DeprecatedConfigurationValueProvider.setData(HubConfiguration.LIQUIBASE_HUB_API_KEY, null);
}
}
return registerResponse;
}
use of liquibase.configuration.ConfiguredValue in project liquibase by liquibase.
the class TestSystem method getConfiguredValue.
/**
* Returns the configured value for the given propertyName. It will check (in priority order):
* <ol>
* <li>properties set directly on this object</li>
* <li>liquibase.sdk.testSystem.[name].[profile(s)].propertyName in the order the profiles are set on this object</li>
* <li>liquibase.sdk.testSystem.[name].propertyName</li>
* <li>liquibase.sdk.testSystem.default.propertyName</li>
* </ol>
* <br>
* If a value is not found, it will return null or throw an {@link UnexpectedLiquibaseException} if 'required' is true.
*/
public <T> T getConfiguredValue(String propertyName, ConfigurationValueConverter<T> converter, boolean required) {
ConfigurationValueConverter<T> finalConverter = value -> {
if (value instanceof String && ((String) value).contains("${")) {
final Matcher matcher = Pattern.compile("(\\$\\{.+?})").matcher((String) value);
while (matcher.find()) {
final String config = matcher.group(1).replace("${", "").replace("}", "").trim();
value = ((String) value).replace(matcher.group(1), getConfiguredValue(config, String.class));
}
}
if (converter == null) {
return (T) value;
} else {
return converter.convert(value);
}
};
final SortedMap<String, Object> properties = definition.getProperties();
if (properties.containsKey(propertyName)) {
return finalConverter.convert(properties.get(propertyName));
}
final LiquibaseConfiguration config = Scope.getCurrentScope().getSingleton(LiquibaseConfiguration.class);
ConfiguredValue<T> configuredValue;
// first check profiles
for (String profile : definition.getProfiles()) {
configuredValue = config.getCurrentConfiguredValue(finalConverter, null, "liquibase.sdk.testSystem." + getDefinition().getName() + ".profiles." + profile + "." + propertyName);
if (configuredValue.found()) {
return configuredValue.getValue();
}
}
configuredValue = config.getCurrentConfiguredValue(finalConverter, null, "liquibase.sdk.testSystem." + getDefinition().getName() + "." + propertyName);
if (configuredValue.found()) {
return configuredValue.getValue();
}
// fall back to "default" setup
configuredValue = config.getCurrentConfiguredValue(finalConverter, null, "liquibase.sdk.testSystem.default." + propertyName);
if (configuredValue.found()) {
return configuredValue.getValue();
}
if (required) {
throw new UnexpectedLiquibaseException("No required liquibase.sdk.testSystem configuration for " + getDefinition().getName() + " of " + propertyName + " set");
}
return null;
}
Aggregations