Skip to content

Commit

Permalink
Change logic of login via Openshift CLI. (eclipse-che#11366)
Browse files Browse the repository at this point in the history
Signed-off-by: kkanova <kkanova@redhat.com>
  • Loading branch information
Katka92 authored and nickboldt committed Oct 3, 2018
1 parent 587d650 commit e727e09
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public String execute(String command) throws IOException {
}

private void obtainKeycloakPodName() throws IOException {
openShiftCliCommandExecutor.login();

// obtain name of keycloak pod
String getKeycloakPodNameCommand =
format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.io.FileUtils;
import org.eclipse.che.selenium.core.provider.OpenShiftWebConsoleUrlProvider;
import org.eclipse.che.selenium.core.utils.executor.CommandExecutor;
Expand All @@ -42,6 +43,8 @@ public class OpenShiftCliCommandExecutor implements CommandExecutor {
private static final boolean IS_MAC_OS = getProperty("os.name").toLowerCase().startsWith("mac");
private static final String DEFAULT_OPENSHIFT_USERNAME = "developer";
private static final String DEFAULT_OPENSHIFT_PASSWORD = "any";
private boolean isLoggedIn;
private ReentrantLock loginLock = new ReentrantLock();

private static final Path PATH_TO_OPENSHIFT_CLI_DIRECTORY =
Paths.get(getProperty("java.io.tmpdir"));
Expand All @@ -66,17 +69,34 @@ public class OpenShiftCliCommandExecutor implements CommandExecutor {

@Override
public String execute(String command) throws IOException {
return execute(command, true);
}

private String execute(String command, boolean needToLogin) throws IOException {
if (!PATH_TO_OPENSHIFT_CLI.toFile().exists()) {
downloadOpenShiftCli();
}

if (needToLogin && !isLoggedIn) {
loginLock.lock();
try {
login();
isLoggedIn = true;
} catch (IOException e) {
isLoggedIn = false;
throw e;
} finally {
loginLock.unlock();
}
}

String openShiftCliCommand = format("%s %s", PATH_TO_OPENSHIFT_CLI, command);

return processAgent.process(openShiftCliCommand);
}

/** Logs into OpensShift as a regular user */
public void login() throws IOException {
private void login() throws IOException {
String loginToOpenShiftCliCommand;
if (openShiftToken != null) {
loginToOpenShiftCliCommand =
Expand All @@ -92,7 +112,7 @@ public void login() throws IOException {
openShiftPassword != null ? openShiftPassword : DEFAULT_OPENSHIFT_PASSWORD);
}

execute(loginToOpenShiftCliCommand);
execute(loginToOpenShiftCliCommand, false);
}

private void downloadOpenShiftCli() throws IOException {
Expand Down

0 comments on commit e727e09

Please sign in to comment.