Search in sources :

Example 1 with ConfiguredValue

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;
}
Also used : LockService(liquibase.lockservice.LockService) LockException(liquibase.exception.LockException) ConfiguredValue(liquibase.configuration.ConfiguredValue) CommandExecutionException(liquibase.exception.CommandExecutionException) IOException(java.io.IOException) LiquibaseException(liquibase.exception.LiquibaseException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 2 with ConfiguredValue

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;
}
Also used : Statement(org.junit.runners.model.Statement) java.util(java.util) TestRule(org.junit.rules.TestRule) Plugin(liquibase.plugin.Plugin) ConfiguredValue(liquibase.configuration.ConfiguredValue) Description(org.junit.runner.Description) MultipleFailureException(org.junit.runners.model.MultipleFailureException) StringUtil(liquibase.util.StringUtil) Matcher(java.util.regex.Matcher) ConfigurationValueConverter(liquibase.configuration.ConfigurationValueConverter) LiquibaseConfiguration(liquibase.configuration.LiquibaseConfiguration) ObjectUtil(liquibase.util.ObjectUtil) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) Assume(org.junit.Assume) Pattern(java.util.regex.Pattern) Scope(liquibase.Scope) Matcher(java.util.regex.Matcher) LiquibaseConfiguration(liquibase.configuration.LiquibaseConfiguration) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Aggregations

ConfiguredValue (liquibase.configuration.ConfiguredValue)2 File (java.io.File)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 java.util (java.util)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Scope (liquibase.Scope)1 ConfigurationValueConverter (liquibase.configuration.ConfigurationValueConverter)1 LiquibaseConfiguration (liquibase.configuration.LiquibaseConfiguration)1 CommandExecutionException (liquibase.exception.CommandExecutionException)1 LiquibaseException (liquibase.exception.LiquibaseException)1 LockException (liquibase.exception.LockException)1 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)1 LockService (liquibase.lockservice.LockService)1 Plugin (liquibase.plugin.Plugin)1 ObjectUtil (liquibase.util.ObjectUtil)1 StringUtil (liquibase.util.StringUtil)1 Assume (org.junit.Assume)1 TestRule (org.junit.rules.TestRule)1