Search in sources :

Example 1 with ConfigPropertyValue

use of com.peterphi.std.guice.config.rest.types.ConfigPropertyValue in project stdlib by petergeneric.

the class NetworkConfigReloadDaemon method reload.

/**
 * Called to initiate an intelligent reload of a particular config
 *
 * @param config
 */
void reload(NetworkConfig config) {
    try {
        log.trace("Load config data from " + config.path + " into " + config);
        final ConfigPropertyData read = configService.read(config.path, configInstanceId, config.getLastRevision());
        // Abort if the server returns no config - we have the latest revision
        if (read == null || read.properties == null || read.properties.isEmpty())
            return;
        for (ConfigPropertyValue property : read.properties) {
            config.properties.set(property.getName(), property.getValue());
        }
        config.setLastRevision(read.revision);
    } catch (Throwable t) {
        log.warn("Error loading config from path " + config.path, t);
    }
}
Also used : ConfigPropertyData(com.peterphi.std.guice.config.rest.types.ConfigPropertyData) ConfigPropertyValue(com.peterphi.std.guice.config.rest.types.ConfigPropertyValue)

Example 2 with ConfigPropertyValue

use of com.peterphi.std.guice.config.rest.types.ConfigPropertyValue in project stdlib by petergeneric.

the class RepoHelper method write.

public static void write(final Repository repo, final String name, final String email, final Map<String, Map<String, ConfigPropertyValue>> data, final ConfigChangeMode changeMode, final String message) {
    final File workTree = repo.getWorkTree();
    if (changeMode == ConfigChangeMode.WIPE_ALL) {
        // Remove all existing config
        try {
            for (File file : workTree.listFiles()) {
                if (!StringUtils.startsWithIgnoreCase(file.getName(), ".git"))
                    FileUtils.forceDelete(file);
            }
        } catch (IOException e) {
            throw new RuntimeException("Error clearing current work tree!", e);
        }
    }
    // Now write all config
    try {
        for (Map.Entry<String, Map<String, ConfigPropertyValue>> entry : data.entrySet()) {
            final String path = entry.getKey();
            final Map<String, ConfigPropertyValue> props = entry.getValue();
            final File folder = new File(workTree, path.replace('/', File.separatorChar));
            // Make sure the path exists
            FileUtils.forceMkdir(folder);
            if (changeMode == ConfigChangeMode.WIPE_REFERENCED_PATHS) {
                File[] files = folder.listFiles((FileFilter) FileFilterUtils.fileFileFilter());
                if (files != null)
                    for (File file : files) FileUtils.forceDelete(file);
            }
            for (Map.Entry<String, ConfigPropertyValue> propEntry : props.entrySet()) {
                final File file = new File(folder, propEntry.getKey());
                try {
                    FileHelper.write(file, propEntry.getValue().value);
                } catch (IOException e) {
                    throw new IOException("Error writing to " + file, e);
                }
            }
        }
    } catch (IOException e) {
        throw new RuntimeException("Error writing properties to work tree!", e);
    }
    // Commit the changes to the repository
    {
        try {
            Git git = new Git(repo);
            // Add all changes
            git.add().addFilepattern(".").call();
            // Now commit
            git.commit().setAll(true).setAuthor(name, email).setMessage(message).call();
        } catch (GitAPIException e) {
            throw new RuntimeException("Error committing changes!", e);
        }
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) IOException(java.io.IOException) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) ConfigPropertyValue(com.peterphi.std.guice.config.rest.types.ConfigPropertyValue)

Example 3 with ConfigPropertyValue

use of com.peterphi.std.guice.config.rest.types.ConfigPropertyValue in project stdlib by petergeneric.

the class GuiceFactory method applyNetworkConfiguration.

/**
 * Add to the configuration any properties defined by the network configuration endpoint (if network config is enabled in a
 * previous property)
 *
 * @param config
 */
private static void applyNetworkConfiguration(final GuiceConfig config) {
    final String configEndpoint = config.get(GuiceProperties.CONFIG_ENDPOINT, null);
    final Boolean configSkip = config.getBoolean(GuiceProperties.CONFIG_SKIP, false);
    if (configEndpoint != null && !configSkip) {
        final boolean useMoxy = config.getBoolean(GuiceProperties.MOXY_ENABLED, true);
        final JAXBContextResolver jaxb = new JAXBContextResolver(new JAXBSerialiserFactory(useMoxy));
        final ResteasyClientFactoryImpl clientFactory = new ResteasyClientFactoryImpl(null, null, null, jaxb);
        try {
            final ResteasyProxyClientFactoryImpl proxyFactory = new ResteasyProxyClientFactoryImpl(clientFactory, config);
            final ConfigRestService client = proxyFactory.getClient(ConfigRestService.class);
            // We set it in the config because otherwise the NetworkConfigReloadDaemon won't be able to load the config
            if (config.get(GuiceProperties.CONFIG_PATH) == null) {
                config.set(GuiceProperties.CONFIG_PATH, "services/" + config.get(GuiceProperties.SERVLET_CONTEXT_NAME, "unknown-service"));
            }
            // Get the config path to read
            final String path = config.get(GuiceProperties.CONFIG_PATH);
            final ConfigPropertyData data = client.read(path, config.get(GuiceProperties.INSTANCE_ID), null);
            for (ConfigPropertyValue property : data.properties) {
                config.set(property.name, property.value);
            }
            // Let others know that the configuration data is coming from a network source
            config.set(GuiceProperties.CONFIG_SOURCE, GuiceConstants.CONFIG_SOURCE_NETWORK);
            if (data.revision != null)
                config.set(GuiceProperties.CONFIG_REVISION, data.revision);
        } finally {
            clientFactory.shutdown();
        }
    } else {
        // Config is not coming from the network
        config.set(GuiceProperties.CONFIG_SOURCE, GuiceConstants.CONFIG_SOURCE_LOCAL);
    }
}
Also used : ConfigRestService(com.peterphi.std.guice.config.rest.iface.ConfigRestService) ResteasyClientFactoryImpl(com.peterphi.std.guice.restclient.resteasy.impl.ResteasyClientFactoryImpl) JAXBSerialiserFactory(com.peterphi.std.util.jaxb.JAXBSerialiserFactory) ConfigPropertyData(com.peterphi.std.guice.config.rest.types.ConfigPropertyData) JAXBContextResolver(com.peterphi.std.guice.restclient.resteasy.impl.JAXBContextResolver) ConfigPropertyValue(com.peterphi.std.guice.config.rest.types.ConfigPropertyValue) ResteasyProxyClientFactoryImpl(com.peterphi.std.guice.restclient.resteasy.impl.ResteasyProxyClientFactoryImpl)

Example 4 with ConfigPropertyValue

use of com.peterphi.std.guice.config.rest.types.ConfigPropertyValue in project stdlib by petergeneric.

the class ConfigUIServiceImpl method getConfigPage.

@Override
public String getConfigPage(final String path) {
    final ConfigPropertyData config = repo.get(REF, path);
    final List<String> paths = repo.getPaths(REF);
    final List<ConfigPropertyValue> inheritedProperties;
    final List<ConfigPropertyValue> definedProperties;
    {
        // Sort alphabetically for the UI
        config.properties.sort(Comparator.comparing(ConfigPropertyValue::getName));
        inheritedProperties = computeInheritedProperties(config);
        definedProperties = computeDefinedProperties(config);
    }
    final TemplateCall call = templater.template("config-edit");
    call.set("nonce", nonceStore.getValue(NONCE_USE));
    call.set("repo", repo);
    call.set("config", config);
    call.set("inheritedProperties", inheritedProperties);
    call.set("definedProperties", definedProperties);
    call.set("path", config.path);
    call.set("paths", paths);
    call.set("children", getChildren(config.path, paths));
    call.set("parent", getParent(config.path));
    return call.process();
}
Also used : ConfigPropertyData(com.peterphi.std.guice.config.rest.types.ConfigPropertyData) ConfigPropertyValue(com.peterphi.std.guice.config.rest.types.ConfigPropertyValue) TemplateCall(com.peterphi.std.guice.web.rest.templating.TemplateCall)

Example 5 with ConfigPropertyValue

use of com.peterphi.std.guice.config.rest.types.ConfigPropertyValue in project stdlib by petergeneric.

the class RepoHelper method _parseCommit.

private static Map<String, Map<String, ConfigPropertyValue>> _parseCommit(final Repository repo, final RevCommit commit) throws IOException {
    TreeWalk walk = new TreeWalk(repo);
    walk.addTree(commit.getTree());
    walk.setRecursive(false);
    // Map of Path to properties defined at this path
    Map<String, Map<String, ConfigPropertyValue>> all = new HashMap<>();
    all.put("", new HashMap<>());
    while (walk.next()) {
        if (walk.isSubtree()) {
            final String path = walk.getPathString();
            all.put(path, new HashMap<>());
            walk.enterSubtree();
        } else {
            final String path;
            final String propertyName;
            {
                final String pathString = walk.getPathString();
                final int lastSlash = pathString.lastIndexOf('/');
                if (lastSlash == -1) {
                    path = "";
                    propertyName = pathString;
                } else {
                    path = pathString.substring(0, lastSlash);
                    propertyName = pathString.substring(lastSlash + 1);
                }
            }
            final byte[] bytes = repo.open(walk.getObjectId(0)).getCachedBytes();
            // Parse the data as a UTF-8 String
            final String str = new String(bytes, StandardCharsets.UTF_8);
            ConfigPropertyValue val = new ConfigPropertyValue(path, propertyName, str);
            all.get(path).put(propertyName, val);
        }
    }
    return all;
}
Also used : HashMap(java.util.HashMap) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) HashMap(java.util.HashMap) Map(java.util.Map) ConfigPropertyValue(com.peterphi.std.guice.config.rest.types.ConfigPropertyValue)

Aggregations

ConfigPropertyValue (com.peterphi.std.guice.config.rest.types.ConfigPropertyValue)5 ConfigPropertyData (com.peterphi.std.guice.config.rest.types.ConfigPropertyData)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConfigRestService (com.peterphi.std.guice.config.rest.iface.ConfigRestService)1 JAXBContextResolver (com.peterphi.std.guice.restclient.resteasy.impl.JAXBContextResolver)1 ResteasyClientFactoryImpl (com.peterphi.std.guice.restclient.resteasy.impl.ResteasyClientFactoryImpl)1 ResteasyProxyClientFactoryImpl (com.peterphi.std.guice.restclient.resteasy.impl.ResteasyProxyClientFactoryImpl)1 TemplateCall (com.peterphi.std.guice.web.rest.templating.TemplateCall)1 JAXBSerialiserFactory (com.peterphi.std.util.jaxb.JAXBSerialiserFactory)1 File (java.io.File)1 IOException (java.io.IOException)1 Git (org.eclipse.jgit.api.Git)1 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)1 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)1