Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create the default cohctl configuration after Coherence starts #623

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
/*
* 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;
import java.util.Map;

import java.util.concurrent.CompletableFuture;

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;
import com.tangosol.net.events.EventDispatcherAwareInterceptor;

import com.tangosol.net.events.partition.PartitionedServiceDispatcher;

import com.tangosol.util.Service;
import com.tangosol.util.ServiceEvent;
import com.tangosol.util.ServiceListener;
Expand Down Expand Up @@ -157,6 +171,7 @@ public void preStart(Context context) {

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

@Override
Expand Down Expand Up @@ -264,4 +279,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