use of io.fabric8.agent.model.Config in project fabric8 by fabric8io.
the class WebHooks method createGogsWebhook.
/**
* Creates a webook in the given gogs repo for the user and password if the webhook does not already exist
*/
public static boolean createGogsWebhook(GitRepoClient repoClient, Logger log, String gogsUser, String repoName, String webhookUrl, String webhookSecret) throws JsonProcessingException {
if (repoClient == null) {
log.info("Cannot create Gogs webhooks as no Gogs service could be found or created");
return false;
}
String gogsAddress = repoClient.getAddress();
log.info("Querying webhooks in gogs at address: " + gogsAddress + " for user " + gogsUser + " repoName: " + repoName);
RepositoryDTO repository = repoClient.getRepository(gogsUser, repoName);
if (repository == null) {
log.info("No repository found for user: " + gogsUser + " repo: " + repoName + " so cannot create any web hooks");
// return false;
}
List<WebHookDTO> webhooks = repoClient.getWebhooks(gogsUser, repoName);
for (WebHookDTO webhook : webhooks) {
String url = null;
WebhookConfig config = webhook.getConfig();
if (config != null) {
url = config.getUrl();
if (Objects.equal(webhookUrl, url)) {
log.info("Already has webhook for: " + url + " so not creating again");
return false;
}
log.info("Ignoring webhook " + url + " from: " + toJson(config));
}
}
CreateWebhookDTO createWebhook = new CreateWebhookDTO();
createWebhook.setType("gogs");
WebhookConfig config = createWebhook.getConfig();
config.setUrl(webhookUrl);
config.setSecret(webhookSecret);
WebHookDTO webhook = repoClient.createWebhook(gogsUser, repoName, createWebhook);
if (log.isDebugEnabled()) {
log.debug("Got created web hook: " + toJson(webhook));
}
log.info("Created webhook for " + webhookUrl + " for user: " + gogsUser + " repoName: " + repoName + " on gogs URL: " + gogsAddress);
return true;
}
use of io.fabric8.agent.model.Config in project fabric8 by fabric8io.
the class KubernetesConfigAdminBridge method updateConfig.
// **********************
// ConfigAdmin
// **********************
private void updateConfig(ConfigMap map) {
Long ver = Long.parseLong(map.getMetadata().getResourceVersion());
String pid = map.getMetadata().getLabels().get(pidLabel);
String[] p = parsePid(pid);
try {
final Configuration config = getConfiguration(configAdmin.get(), pid, p[0], p[1]);
final Map<String, String> configMapData = map.getData();
if (configMapData == null) {
LOGGER.debug("Ignoring configuration pid={}, (empty)", config.getPid());
return;
}
final Dictionary<String, Object> props = config.getProperties();
final Hashtable<String, Object> configAdmCfg = props != null ? new Hashtable<String, Object>() : null;
Hashtable<String, Object> configMapCfg = new Hashtable<>();
/*
* If there is a key named as pid + ".cfg" (as the pid file on karaf)
* it will be used as source of configuration instead of the content
* of the data field. The name of the key can be changed by setting
* the key fabric8.config.pid.cfg
*
* i.e.
* apiVersion: v1
* data:
* org.ops4j.pax.logging.cfg: |+
* log4j.rootLogger=DEBUG, out
*/
String pidCfg = configMapData.get(FABRIC8_CONFIG_PID_CFG);
if (pidCfg == null) {
pidCfg = pid + ".cfg";
}
String cfgString = configMapData.get(pidCfg);
if (Utils.isNotNullOrEmpty(cfgString)) {
java.util.Properties cfg = new java.util.Properties();
cfg.load(new StringReader(cfgString));
for (Map.Entry<Object, Object> entry : cfg.entrySet()) {
configMapCfg.put((String) entry.getKey(), entry.getValue());
}
} else {
for (Map.Entry<String, String> entry : map.getData().entrySet()) {
configMapCfg.put(entry.getKey(), entry.getValue());
}
}
/*
* Configure if mete-data should be added to the Config Admin or not
*/
boolean meta = configMapData.containsKey(FABRIC8_CONFIG_META) ? Boolean.valueOf(configMapData.get(FABRIC8_CONFIG_META)) : configMeta;
/*
* Configure if ConfigMap data should be merge with ConfigAdmin or it
* should override it.
*/
boolean merge = configMapData.containsKey(FABRIC8_CONFIG_MERGE) ? Boolean.valueOf(configMapData.get(FABRIC8_CONFIG_MERGE)) : configMerge;
if (configAdmCfg != null) {
Long oldVer = (Long) props.get(FABRIC8_K8S_META_RESOURCE_VERSION);
if (oldVer != null && (oldVer >= ver)) {
LOGGER.debug("Ignoring configuration pid={}, oldVersion={} newVersion={} (no changes)", config.getPid(), oldVer, ver);
return;
}
for (Enumeration<String> e = props.keys(); e.hasMoreElements(); ) {
String key = e.nextElement();
Object val = props.get(key);
configAdmCfg.put(key, val);
}
}
if (shouldUpdate(configAdmCfg, configMapCfg)) {
LOGGER.debug("Updating configuration pid={}", config.getPid());
if (meta) {
configMapCfg.put(FABRIC8_PID, pid);
configMapCfg.put(FABRIC8_K8S_META_RESOURCE_VERSION, ver);
configMapCfg.put(FABRIC8_K8S_META_NAME, map.getMetadata().getName());
configMapCfg.put(FABRIC8_K8S_META_NAMESPACE, map.getMetadata().getNamespace());
}
if (merge && configAdmCfg != null) {
for (Map.Entry<String, Object> entry : configMapCfg.entrySet()) {
// Do not override ConfigAdmin meta data
if (!CM_META_KEYS.contains(entry.getKey())) {
configAdmCfg.put(entry.getKey(), entry.getValue());
}
}
configMapCfg = configAdmCfg;
}
config.update(configMapCfg);
} else {
LOGGER.debug("Ignoring configuration pid={} (no changes)", config.getPid());
}
} catch (Exception e) {
LOGGER.warn("", e);
}
}
use of io.fabric8.agent.model.Config in project fabric8 by fabric8io.
the class KubernetesConfigAdminBridge method deleteConfig.
private void deleteConfig(ConfigMap map) {
String pid = map.getMetadata().getLabels().get(pidLabel);
String[] p = parsePid(pid);
try {
Map<String, String> configMapData = map.getData();
Configuration config = getConfiguration(configAdmin.get(), pid, p[0], p[1]);
if (configMapData != null) {
boolean merge = configMapData.containsKey(FABRIC8_CONFIG_MERGE) ? Boolean.valueOf(configMapData.get(FABRIC8_CONFIG_MERGE)) : configMerge;
if (!merge) {
LOGGER.debug("Delete configuration {}", config.getPid());
config.delete();
}
}
} catch (Exception e) {
LOGGER.warn("", e);
}
}
use of io.fabric8.agent.model.Config in project fabric8 by fabric8io.
the class YamlTest method testParseYaml.
@Test
public void testParseYaml() throws Exception {
String basedir = System.getProperty("basedir", ".");
File file = new File(basedir, "src/test/resources/fabric8.yml");
assertThat(file).exists();
ProjectConfig config = ProjectConfigs.parseProjectConfig(file);
System.out.println("Parsed: " + config);
assertThat(config.getChatRoom()).isEqualTo("myroom");
assertThat(config.getIssueProjectName()).isEqualTo("foo");
assertThat(config.getPipeline()).isEqualTo("maven/CanaryReleaseThenStage.groovy");
LinkedHashMap<String, String> environments = config.getEnvironments();
// lets assert that things are in the correct order...
List<Pair<String, String>> expectedEnvironents = Arrays.asList(new Pair<String, String>("Testing", "gogsadmin-james4-testing"), new Pair<String, String>("Staging", "gogsadmin-james4-staging"), new Pair<String, String>("Production", "gogsadmin-james4-prod"));
int idx = 0;
Set<Map.Entry<String, String>> entries = environments.entrySet();
for (Map.Entry<String, String> entry : entries) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println("Found environment " + key + " = " + value);
Pair<String, String> actual = new Pair<>(key, value);
assertTrue("Too many entries - found unexpected value: " + actual, expectedEnvironents.size() > idx);
Pair<String, String> expected = expectedEnvironents.get(idx++);
assertEquals("environment " + idx, expected, actual);
}
}
use of io.fabric8.agent.model.Config in project strimzi by strimzi.
the class TopicSerializationTest method testJsonSerializationRoundTrip.
@Test
public void testJsonSerializationRoundTrip() throws UnsupportedEncodingException {
Topic.Builder builder = new Topic.Builder();
builder.withTopicName("tom");
builder.withMapName("bob");
builder.withNumReplicas((short) 1);
builder.withNumPartitions(2);
builder.withConfigEntry("foo", "bar");
Topic wroteTopic = builder.build();
byte[] bytes = TopicSerialization.toJson(wroteTopic);
String json = new String(bytes, "UTF-8");
assertEquals("{\"map-name\":\"bob\"," + "\"topic-name\":\"tom\"," + "\"partitions\":2," + "\"replicas\":1," + "\"config\":{\"foo\":\"bar\"}" + "}", json);
Topic readTopic = TopicSerialization.fromJson(bytes);
assertEquals(wroteTopic, readTopic);
}
Aggregations