Search in sources :

Example 1 with ConcourseClientPreferences

use of com.cinchapi.concourse.config.ConcourseClientPreferences in project concourse by cinchapi.

the class ManagedConcourseServer method syncDefaultClientConnectionInfo.

/**
 * Copy the connection information for this managed server to a
 * {@code concourse_client.prefs} file located in the root of the working
 * directory so that source code relying on the default connection behaviour
 * will properly connect to this server.
 * <p>
 * A test case that uses an indirect connection to Concourse (i.e. the test
 * case doesn't directly use the provided {@code client} variable provided
 * by the framework, but uses classes from the application source code that
 * has its own mechanism for connecting to Concourse) SHOULD call this
 * method so that the application code will connect to the this server for
 * the purpose of the unit test.
 * </p>
 * <p>
 * Any connection information that is synchronized will be cleaned up after
 * the test. If a prefs file already existed in the root of the working
 * directory, that file is backed up and restored so that the application
 * can run normally outside of the test cases.
 * </p>
 */
public void syncDefaultClientConnectionInfo() {
    try {
        Path prefs = Paths.get("concourse_client.prefs").toAbsolutePath();
        if (Files.exists(prefs)) {
            Path backup = Paths.get("concourse_client.prefs.bak").toAbsolutePath();
            Files.move(prefs, backup);
            clientPrefsCleanupAction = ClientPrefsCleanupAction.RESTORE_BACKUP;
            log.info("Took backup for client prefs file located at {}. " + "The backup is stored in {}", prefs, backup);
        } else {
            clientPrefsCleanupAction = ClientPrefsCleanupAction.DELETE;
        }
        log.info("Synchronizing the managed server's connection " + "information to the client prefs file at {}", prefs);
        ConcourseClientPreferences ccp = ConcourseClientPreferences.from(Paths.get(FileOps.touch(prefs.toString())));
        ccp.setPort(getClientPort());
        ccp.setUsername("admin");
        ccp.setPassword("admin".toCharArray());
    } catch (IOException e) {
        throw CheckedExceptions.wrapAsRuntimeException(e);
    }
}
Also used : Path(java.nio.file.Path) ConcourseClientPreferences(com.cinchapi.concourse.config.ConcourseClientPreferences) IOException(java.io.IOException)

Example 2 with ConcourseClientPreferences

use of com.cinchapi.concourse.config.ConcourseClientPreferences in project concourse by cinchapi.

the class ConcourseShell method main.

/**
 * Run the program...
 *
 * @param args
 * @throws Exception
 */
public static void main(String... args) throws Exception {
    try {
        ConcourseShell cash = new ConcourseShell();
        Options opts = new Options();
        JCommander parser = null;
        try {
            parser = new JCommander(opts, args);
        } catch (Exception e) {
            die(e.getMessage());
        }
        parser.setProgramName("concourse-shell");
        if (opts.help) {
            parser.usage();
            System.exit(1);
        }
        if (!Strings.isNullOrEmpty(opts.prefs)) {
            opts.prefs = FileOps.expandPath(opts.prefs, System.getProperty("user.dir.real"));
            ConcourseClientPreferences prefs = ConcourseClientPreferences.from(Paths.get(opts.prefs));
            opts.username = prefs.getUsername();
            opts.password = new String(prefs.getPasswordExplicit());
            opts.host = prefs.getHost();
            opts.port = prefs.getPort();
            opts.environment = prefs.getEnvironment();
        }
        if (Strings.isNullOrEmpty(opts.password)) {
            cash.setExpandEvents(false);
            opts.password = cash.console.readLine("Password [" + opts.username + "]: ", '*');
        }
        try {
            cash.concourse = Concourse.connect(opts.host, opts.port, opts.username, opts.password, opts.environment);
            cash.whoami = opts.username;
        } catch (Exception e) {
            if (e.getCause() instanceof TTransportException) {
                die("Unable to connect to the Concourse Server at " + opts.host + ":" + opts.port);
            } else if (e.getCause() instanceof SecurityException) {
                die("Invalid username/password combination.");
            } else {
                die(e.getMessage());
            }
        }
        if (!opts.ignoreRunCommands) {
            cash.loadExternalScript(opts.ext);
        }
        if (!Strings.isNullOrEmpty(opts.run)) {
            try {
                String result = cash.evaluate(opts.run);
                System.out.println(result);
                cash.concourse.exit();
                System.exit(0);
            } catch (IrregularEvaluationResult e) {
                die(e.getMessage());
            }
        } else {
            try {
                cash.enableInteractiveSettings();
            } catch (PermissionException e) {
                die(e.getMessage());
            }
            boolean running = true;
            String input = "";
            while (running) {
                boolean extraLineBreak = true;
                boolean clearInput = true;
                boolean clearPrompt = false;
                try {
                    input = input + cash.console.readLine().trim();
                    String result = cash.evaluate(input);
                    System.out.println(result);
                } catch (UserInterruptException e) {
                    if (Strings.isNullOrEmpty(e.getPartialLine()) && Strings.isNullOrEmpty(input)) {
                        cash.console.println("Type EXIT to quit.");
                    }
                } catch (HelpRequest e) {
                    String text = getHelpText(e.topic);
                    if (!Strings.isNullOrEmpty(text)) {
                        Process p = Runtime.getRuntime().exec(new String[] { "sh", "-c", "echo \"" + text + "\" | less > /dev/tty" });
                        p.waitFor();
                    }
                    cash.console.getHistory().removeLast();
                } catch (ExitRequest e) {
                    running = false;
                    cash.console.getHistory().removeLast();
                } catch (NewLineRequest e) {
                    extraLineBreak = false;
                } catch (ProgramCrash e) {
                    die(e.getMessage());
                } catch (MultiLineRequest e) {
                    extraLineBreak = false;
                    clearInput = false;
                    clearPrompt = true;
                } catch (IrregularEvaluationResult e) {
                    System.err.println(e.getMessage());
                } finally {
                    if (extraLineBreak) {
                        cash.console.print("\n");
                    }
                    if (clearInput) {
                        input = "";
                    }
                    if (clearPrompt) {
                        cash.console.setPrompt("> ");
                    } else {
                        cash.console.setPrompt(cash.defaultPrompt);
                    }
                }
            }
            cash.concourse.exit();
            System.exit(0);
        }
    } finally {
        TerminalFactory.get().restore();
    }
}
Also used : PermissionException(com.cinchapi.concourse.PermissionException) TTransportException(org.apache.thrift.transport.TTransportException) SecurityException(com.cinchapi.concourse.thrift.SecurityException) UserInterruptException(jline.console.UserInterruptException) TTransportException(org.apache.thrift.transport.TTransportException) UserInterruptException(jline.console.UserInterruptException) SecurityException(com.cinchapi.concourse.thrift.SecurityException) MissingMethodException(groovy.lang.MissingMethodException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) ParseException(com.cinchapi.concourse.thrift.ParseException) TApplicationException(org.apache.thrift.TApplicationException) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) IOException(java.io.IOException) PermissionException(com.cinchapi.concourse.PermissionException) JCommander(com.beust.jcommander.JCommander) ConcourseClientPreferences(com.cinchapi.concourse.config.ConcourseClientPreferences)

Aggregations

ConcourseClientPreferences (com.cinchapi.concourse.config.ConcourseClientPreferences)2 IOException (java.io.IOException)2 JCommander (com.beust.jcommander.JCommander)1 PermissionException (com.cinchapi.concourse.PermissionException)1 ParseException (com.cinchapi.concourse.thrift.ParseException)1 SecurityException (com.cinchapi.concourse.thrift.SecurityException)1 MissingMethodException (groovy.lang.MissingMethodException)1 Path (java.nio.file.Path)1 UserInterruptException (jline.console.UserInterruptException)1 TApplicationException (org.apache.thrift.TApplicationException)1 TTransportException (org.apache.thrift.transport.TTransportException)1 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)1 MultipleCompilationErrorsException (org.codehaus.groovy.control.MultipleCompilationErrorsException)1