Skip to content

Commit

Permalink
[cli] Improve UX of graceful shutdown message (eclipse-che#4064)
Browse files Browse the repository at this point in the history
* Fix eclipse-che#1682 : Improve UX of graceful shutdown message
- base scripts : catch error code of action lifecycle and return it
- cha-lib : return error code based on http error code
- display better message when auth is required
- skip graceful mode when repo is mounted

Change-Id: I1ad021426444b5b4d880df518512e53ef4828336
Signed-off-by: Florent BENOIT <fbenoit@codenvy.com>
  • Loading branch information
benoitf committed Feb 13, 2017
1 parent 28ea60c commit c5dc519
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 6 deletions.
13 changes: 11 additions & 2 deletions dockerfiles/base/scripts/base/commands/cmd_stop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,22 @@ cmd_stop() {
FORCE_STOP=false
if [[ "$@" == *"--skip:graceful"* ]]; then
FORCE_STOP=true
elif local_repo || local_assembly; then
FORCE_STOP=true
fi

if server_is_booted $(get_server_container_id $CHE_CONTAINER_NAME); then
if [[ ${FORCE_STOP} = "false" ]]; then
info "stop" "Stopping workspaces..."
if ! $(cmd_lifecycle action "graceful-stop" "$@" >> "${LOGS}" 2>&1 || false); then
error "We encountered an error -- see cli.log"
local GRACEFUL_STATUS_RESULT=0
cmd_lifecycle action "graceful-stop" "$@" >> "${LOGS}" 2>&1 || GRACEFUL_STATUS_RESULT=$?
# error on authentication (401 modulo 256 = 145)
if [[ ${GRACEFUL_STATUS_RESULT} -eq 145 ]]; then
error "Authentication failed (hint: --user/--password for auth, --skip:graceful bypasses workspace stop)"
return 2;
elif [[ ${GRACEFUL_STATUS_RESULT} -ne 0 ]]; then
error "Error during graceful stop - see $CHE_HOST_CONFIG/cli.log. (hint: --skip:graceful bypasses workspace stop)"
return 2;
fi
fi
# stop containers booted by docker compose
Expand Down
13 changes: 13 additions & 0 deletions dockerfiles/base/scripts/base/startup_05_pre_exec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,28 @@ cmd_lifecycle() {
fi
fi

local PRE_COMMAND_STATUS=0
ANSWER=$(declare -f $PRE_COMMAND ) # > /dev/null)
if [ $? = "0" ]; then
eval $PRE_COMMAND "$@"
PRE_COMMAND_STATUS=$?
fi

eval $COMMAND "$@"
local COMMAND_STATUS=$?



local POST_COMMAND_STATUS=0
ANSWER=$(declare -f $POST_COMMAND > /dev/null)
if [ $? = "0" ]; then
eval $POST_COMMAND "$@"
POST_COMMAND_STATUS=$?
fi

if [[ POST_COMMAND_STATUS -ne 0 ]]; then
return ${POST_COMMAND_STATUS};
else
return ${COMMAND_STATUS};
fi
}
9 changes: 8 additions & 1 deletion dockerfiles/lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {Log} from "./spi/log/log";
import {CheDir} from "./internal/dir/che-dir";
import {CheTest} from "./internal/test/che-test";
import {CheAction} from "./internal/action/che-action";
import {ErrorMessage} from "./spi/error/error-message";
/**
* Entry point of this library providing commands.
* @author Florent Benoit
Expand Down Expand Up @@ -79,6 +80,12 @@ export class EntryPoint {

// handle error of the promise
promise.catch((error) => {
let exitCode : number = 1;
if (error instanceof ErrorMessage) {
exitCode = error.getExitCode();
error = error.getError();
}

try {
let errorMessage = JSON.parse(error);
if (errorMessage.message) {
Expand All @@ -92,7 +99,7 @@ export class EntryPoint {
console.log(error.stack);
}
}
process.exit(1);
process.exit(exitCode);
});

} catch (e) {
Expand Down
38 changes: 38 additions & 0 deletions dockerfiles/lib/src/spi/error/error-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2016-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*/
/**
* Error message used to provide custom exit code
* @author Florent Benoit
*/
export class ErrorMessage {

private exitCode? : number = 1;

private error : any;

constructor(error: any, exitCode : number) {
this.error = error;
if (exitCode) {
this.exitCode = exitCode;
}
console.log("build error message with exit code", exitCode);
}



getExitCode() : number {
return this.exitCode;
}

getError() : any {
return this.error;
}
}
7 changes: 4 additions & 3 deletions dockerfiles/lib/src/spi/http/default-http-json-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import {AuthData} from "../../api/wsmaster/auth/auth-data";
import {Log} from "../log/log";
import {org} from "../../api/dto/che-dto";
import {ErrorMessage} from "../error/error-message";
/**
* Implementation of a Request on the remote server
* @author Florent Benoit
Expand Down Expand Up @@ -90,12 +91,12 @@ export class DefaultHttpJsonRequest implements HttpJsonRequest {
try {
var parsed = JSON.parse(data);
if (parsed.message) {
reject('Call on rest url ' + this.options.path + ' returned invalid response code (' + res.statusCode + ') with error:' + parsed.message);
reject(new ErrorMessage('Call on rest url ' + this.options.path + ' returned invalid response code (' + res.statusCode + ') with error:' + parsed.message, res.statusCode));
} else {
reject('Call on rest url ' + this.options.path + ' returned invalid response code (' + res.statusCode + ') with error:' + data);
reject(new ErrorMessage('Call on rest url ' + this.options.path + ' returned invalid response code (' + res.statusCode + ') with error:' + data, res.statusCode));
}
} catch (error) {
reject('Call on rest url ' + this.options.path + ' returned invalid response code (' + res.statusCode + ') with error:' + data.toString());
reject(new ErrorMessage('Call on rest url ' + this.options.path + ' returned invalid response code (' + res.statusCode + ') with error:' + data.toString(), res.statusCode));
}

}
Expand Down

0 comments on commit c5dc519

Please sign in to comment.