Skip to content

Commit

Permalink
Create the default cohctl configuration after Coherence starts (#623)
Browse files Browse the repository at this point in the history
  • Loading branch information
thegridman committed Oct 10, 2023
1 parent 01553e8 commit b263089
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates.
* Copyright (c) 2021, 2023, Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/

package com.oracle.coherence.k8s;

import java.io.File;
import java.io.PrintWriter;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -15,7 +17,11 @@
import com.tangosol.application.Context;
import com.tangosol.application.LifecycleListener;
import com.tangosol.coherence.component.util.daemon.queueProcessor.service.grid.partitionedService.PartitionedCache;
import com.tangosol.coherence.config.Config;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.Cluster;
import com.tangosol.net.DistributedCacheService;
import com.tangosol.net.Member;
import com.tangosol.net.PartitionedService;
import com.tangosol.net.events.Event;
import com.tangosol.net.events.EventDispatcher;
Expand Down Expand Up @@ -157,6 +163,7 @@ public void preStart(Context context) {

@Override
public void postStart(Context context) {
initCohCtl();
}

@Override
Expand Down Expand Up @@ -264,4 +271,56 @@ static Map<String, Boolean> getResumeMap(String services) {
return null;
}
}

void initCohCtl() {
try {
Cluster cluster = CacheFactory.getCluster();
Member member = cluster.getLocalMember();
String clusterName = member.getClusterName();
String port = Config.getProperty("coherence.management.http.port", "30000");
String provider = Config.getProperty("coherence.management.http.provider");
String defaultProtocol = provider == null || provider.isEmpty() ? "http" : "https";
String protocol = Config.getProperty("coherence.operator.cli.protocol", defaultProtocol);
String home = System.getProperty("user.home");
String connectionType = "http";

File cohctlHome = new File(home + File.separator + ".cohctl");
File configFile = new File(cohctlHome, "cohctl.yaml");

if (!configFile.exists()) {
LOGGER.info("CoherenceOperator: creating default cohctl config at " + configFile.getAbsolutePath());
if (!cohctlHome.exists()) {
cohctlHome.mkdirs();
}
try (PrintWriter out = new PrintWriter(configFile)) {
out.println("clusters:");
out.println(" - name: default");
out.println(" discoverytype: manual");
out.println(" connectiontype: " + connectionType);
out.println(" connectionurl: " + protocol + "://127.0.0.1:" + port + "/management/coherence/cluster");
out.println(" nameservicediscovery: \"\"");
out.println(" clusterversion: \"" + CacheFactory.VERSION + "\"");
out.println(" clustername: \"" + clusterName + "\"");
out.println(" clustertype: Standalone");
out.println(" manuallycreated: false");
out.println(" baseclasspath: \"\"");
out.println(" additionalclasspath: \"\"");
out.println(" arguments: \"\"");
out.println(" managementport: 0");
out.println(" persistencemode: \"\"");
out.println(" loggingdestination: \"\"");
out.println(" managementavailable: false");
out.println("color: \"on\"");
out.println("currentcontext: default");
out.println("debug: false");
out.println("defaultbytesformat: m");
out.println("ignoreinvalidcerts: false");
out.println("requesttimeout: 30");
}
}
}
catch (Exception e) {
LOGGER.error(e, "Coherence Operator: Failed to create default cohctl config");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@

package com.oracle.coherence.k8s;

import java.io.File;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.concurrent.CompletableFuture;

import com.tangosol.coherence.config.Config;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.Cluster;
import com.tangosol.net.Coherence;
import com.tangosol.net.DefaultCacheServer;
import com.tangosol.net.Member;

/**
* A main class that is used to run some initialisation code before
Expand Down Expand Up @@ -49,7 +42,6 @@ else if (DEFAULT_MAIN.equals(args[0])) {
}

init();
CompletableFuture.runAsync(Main::initCohCtl);

String sMainClass = args[0];
String[] asArgsReal = new String[args.length - 1];
Expand Down Expand Up @@ -83,57 +75,4 @@ private static String getMainClass() {
return DefaultCacheServer.class.getCanonicalName();
}
}

private static void initCohCtl() {
try {
Cluster cluster = CacheFactory.getCluster();
Member member = cluster.getLocalMember();
String clusterName = member.getClusterName();
String port = Config.getProperty("coherence.management.http.port", "30000");
String provider = Config.getProperty("coherence.management.http.provider");
String defaultProtocol = provider == null || provider.isEmpty() ? "http" : "https";
String protocol = Config.getProperty("coherence.operator.cli.protocol", defaultProtocol);
String home = System.getProperty("user.home");
String connectionType = "http";

File cohctlHome = new File(home + File.separator + ".cohctl");
File configFile = new File(cohctlHome, "cohctl.yaml");

if (!configFile.exists()) {
System.out.println("CoherenceOperator: creating default cohctl config at " + configFile.getAbsolutePath());
if (!cohctlHome.exists()) {
cohctlHome.mkdirs();
}
try (PrintWriter out = new PrintWriter(configFile)) {
out.println("clusters:");
out.println(" - name: default");
out.println(" discoverytype: manual");
out.println(" connectiontype: " + connectionType);
out.println(" connectionurl: " + protocol + "://127.0.0.1:" + port + "/management/coherence/cluster");
out.println(" nameservicediscovery: \"\"");
out.println(" clusterversion: \"" + CacheFactory.VERSION + "\"");
out.println(" clustername: \"" + clusterName + "\"");
out.println(" clustertype: Standalone");
out.println(" manuallycreated: false");
out.println(" baseclasspath: \"\"");
out.println(" additionalclasspath: \"\"");
out.println(" arguments: \"\"");
out.println(" managementport: 0");
out.println(" persistencemode: \"\"");
out.println(" loggingdestination: \"\"");
out.println(" managementavailable: false");
out.println("color: \"on\"");
out.println("currentcontext: default");
out.println("debug: false");
out.println("defaultbytesformat: m");
out.println("ignoreinvalidcerts: false");
out.println("requesttimeout: 30");
}
}
}
catch (Exception e) {
System.err.println("Coherence Operator: Failed to create default cohctl config");
e.printStackTrace();
}
}
}
4 changes: 2 additions & 2 deletions pkg/runner/cmd_initialise.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ func initialiseWithEnv(cmd *cobra.Command, getEnv EnvFunction) (bool, error) {

cohctl := filesDir + pathSep + "cohctl"
if _, err := os.Stat(cohctl); err == nil {
fmt.Println("Copying cohctl utility")
fmt.Printf("Copying cohctl utility to \"%s%scohctl\"\n", utilDir, pathSep)
err = utils.CopyFile(cohctl, utilDir+pathSep+"cohctl")
if err != nil {
return false, err
fmt.Printf("Failed to copy cohctl utility to \"%s%scohctl\" - %s\n", utilDir, pathSep, err.Error())
}
}

Expand Down

0 comments on commit b263089

Please sign in to comment.