From e0deb861b556d528640d907b934c1b49ae6dc9bc Mon Sep 17 00:00:00 2001 From: Tyler Jewell Date: Thu, 25 Aug 2016 08:28:12 -0700 Subject: [PATCH] Che script improvements (#2211) * remove *-container * moved profiles to ~/.che/profiles * Add product names to profile * added env-file to other commands * Simpler docker run syntax * Add changes for che dir, action, test Change-Id: I8524478628144afe7034541bd7b86f7ad8847027 Signed-off-by: Florent BENOIT * Rename CHE_FILE into CHE_DIR --- che.sh | 235 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 123 insertions(+), 112 deletions(-) diff --git a/che.sh b/che.sh index 27673c2b5dc..b966c0761f7 100755 --- a/che.sh +++ b/che.sh @@ -40,35 +40,32 @@ check_docker() { init_global_variables() { + # Name used in INFO statements DEFAULT_CHE_PRODUCT_NAME="ECLIPSE CHE" + # Name used in CLI statements + DEFAULT_CHE_MINI_PRODUCT_NAME="che" + DEFAULT_CHE_LAUNCHER_IMAGE_NAME="codenvy/che-launcher" DEFAULT_CHE_SERVER_IMAGE_NAME="codenvy/che-server" - DEFAULT_CHE_FILE_IMAGE_NAME="codenvy/che-file" + DEFAULT_CHE_DIR_IMAGE_NAME="codenvy/che-dir" DEFAULT_CHE_MOUNT_IMAGE_NAME="codenvy/che-mount" + DEFAULT_CHE_ACTION_IMAGE_NAME="codenvy/che-action" DEFAULT_CHE_TEST_IMAGE_NAME="codenvy/che-test" - - DEFAULT_CHE_LAUNCHER_CONTAINER_NAME="che-launcher" DEFAULT_CHE_SERVER_CONTAINER_NAME="che-server" - DEFAULT_CHE_FILE_CONTAINER_NAME="che-file" - DEFAULT_CHE_MOUNT_CONTAINER_NAME="che-mount" - DEFAULT_CHE_TEST_CONTAINER_NAME="che-test" - DEFAULT_CHE_VERSION="latest" DEFAULT_CHE_CLI_ACTION="help" CHE_PRODUCT_NAME=${CHE_PRODUCT_NAME:-${DEFAULT_CHE_PRODUCT_NAME}} + CHE_MINI_PRODUCT_NAME=${CHE_MINI_PRODUCT_NAME:-${DEFAULT_CHE_MINI_PRODUCT_NAME}} CHE_LAUNCHER_IMAGE_NAME=${CHE_LAUNCHER_IMAGE_NAME:-${DEFAULT_CHE_LAUNCHER_IMAGE_NAME}} CHE_SERVER_IMAGE_NAME=${CHE_SERVER_IMAGE_NAME:-${DEFAULT_CHE_SERVER_IMAGE_NAME}} - CHE_FILE_IMAGE_NAME=${CHE_FILE_IMAGE_NAME:-${DEFAULT_CHE_FILE_IMAGE_NAME}} + CHE_DIR_IMAGE_NAME=${CHE_DIR_IMAGE_NAME:-${DEFAULT_CHE_DIR_IMAGE_NAME}} CHE_MOUNT_IMAGE_NAME=${CHE_MOUNT_IMAGE_NAME:-${DEFAULT_CHE_MOUNT_IMAGE_NAME}} + CHE_ACTION_IMAGE_NAME=${CHE_ACTION_IMAGE_NAME:-${DEFAULT_CHE_ACTION_IMAGE_NAME}} CHE_TEST_IMAGE_NAME=${CHE_TEST_IMAGE_NAME:-${DEFAULT_CHE_TEST_IMAGE_NAME}} - CHE_LAUNCHER_CONTAINER_NAME=${CHE_LAUNCHER_CONTAINER_NAME:-${DEFAULT_CHE_LAUNCHER_CONTAINER_NAME}} CHE_SERVER_CONTAINER_NAME=${CHE_SERVER_CONTAINER_NAME:-${DEFAULT_CHE_SERVER_CONTAINER_NAME}} - CHE_FILE_CONTAINER_NAME=${CHE_FILE_CONTAINER_NAME:-${DEFAULT_CHE_FILE_CONTAINER_NAME}} - CHE_MOUNT_CONTAINER_NAME=${CHE_MOUNT_CONTAINER_NAME:-${DEFAULT_CHE_MOUNT_CONTAINER_NAME}} - CHE_TEST_CONTAINER_NAME=${CHE_TEST_CONTAINER_NAME:-${DEFAULT_CHE_TEST_CONTAINER_NAME}} CHE_VERSION=${CHE_VERSION:-${DEFAULT_CHE_VERSION}} CHE_CLI_ACTION=${CHE_CLI_ACTION:-${DEFAULT_CHE_CLI_ACTION}} @@ -79,26 +76,30 @@ init_global_variables() { GLOBAL_GET_DOCKER_HOST_IP=$(get_docker_host_ip) USAGE=" -Usage: che [COMMAND] - start Starts Che server - stop Stops Che server - restart Restart Che server +Usage: ${CHE_MINI_PRODUCT_NAME} [COMMAND] + start Starts ${CHE_MINI_PRODUCT_NAME} server + stop Stops ${CHE_MINI_PRODUCT_NAME} server + restart Restart ${CHE_MINI_PRODUCT_NAME} server update Pulls specific version, respecting CHE_VERSION profile add Add a profile to ~/.che/ - profile set Set this profile as the default for Che CLI + profile set Set this profile as the default for ${CHE_MINI_PRODUCT_NAME} CLI profile unset Removes the default profile - leaves it unset profile rm Remove this profile from ~/.che/ profile update Update profile in ~/.che/ profile info Print the profile configuration profile list List available profiles mount Synchronize workspace to a local directory - init Initialize directory with Che configuration - up Create workspace from source in current directory + dir init Initialize directory with ${CHE_MINI_PRODUCT_NAME} configuration + dir up Create workspace from source in current directory + dir down Stop workspace running in current directory + dir status Display status of ${CHE_MINI_PRODUCT_NAME} in current directory + action [--help] Start action on ${CHE_MINI_PRODUCT_NAME} instance + test [--help] Start test on ${CHE_MINI_PRODUCT_NAME} instance info [ --all Run all debugging tests - --server Run Che launcher and server debugging tests - --networking Test connectivity between Che sub-systems - --cli Print CLI (this program)debugging info - --create [] Test creating a workspace and project in Che + --server Run ${CHE_MINI_PRODUCT_NAME} launcher and server debugging tests + --networking Test connectivity between ${CHE_MINI_PRODUCT_NAME} sub-systems + --cli Print CLI (this program) debugging info + --create [] Test creating a workspace and project in ${CHE_MINI_PRODUCT_NAME} [] [] ] " @@ -125,7 +126,7 @@ parse_command_line () { CHE_CLI_ACTION="help" else case $1 in - start|stop|restart|update|info|profile|init|up|mount|test|help|-h|--help) + start|stop|restart|update|info|profile|action|dir|mount|test|help|-h|--help) CHE_CLI_ACTION=$1 ;; *) @@ -144,6 +145,14 @@ docker_exec() { fi } +docker_run() { + docker_exec run --rm -it -v /var/run/docker.sock:/var/run/docker.sock \ + --env-file=$(get_list_of_che_system_environment_variables) "$@" + + # Remove temporary file -- only due to POSIX weirdness with --env-file + rm -rf "tmp" > /dev/null 2>&1 +} + get_docker_host_ip() { case $(get_docker_install_type) in boot2docker) @@ -317,42 +326,28 @@ check_current_image_and_update_if_not_found() { CURRENT_IMAGE=$(docker images -q "$1":"${CHE_VERSION}") - if [ "${CURRENT_IMAGE}" != "" ]; then - info "${CHE_PRODUCT_NAME}: Found image $1:${CHE_VERSION}" - else + if [ "${CURRENT_IMAGE}" == "" ]; then update_che_image $1 fi } execute_che_launcher() { - check_current_image_and_update_if_not_found ${CHE_LAUNCHER_IMAGE_NAME} - - info "${CHE_PRODUCT_NAME}: Starting launcher" - - docker_exec run -t --rm --name "${CHE_LAUNCHER_CONTAINER_NAME}" \ - -v /var/run/docker.sock:/var/run/docker.sock \ - --env-file=$(get_list_of_che_system_environment_variables) \ - "${CHE_LAUNCHER_IMAGE_NAME}":"${CHE_VERSION}" "${CHE_CLI_ACTION}" || true - - # Remove temporary file - rm -rf "tmp" > /dev/null 2>&1 + docker_run "${CHE_LAUNCHER_IMAGE_NAME}":"${CHE_VERSION}" "${CHE_CLI_ACTION}" || true } -execute_che_file() { - - check_current_image_and_update_if_not_found ${CHE_FILE_IMAGE_NAME} - info "${CHE_PRODUCT_NAME}: Starting che-dir container" - +execute_che_dir() { + check_current_image_and_update_if_not_found ${CHE_DIR_IMAGE_NAME} CURRENT_DIRECTORY=$(get_mount_path "${PWD}") + docker_run -v "$CURRENT_DIRECTORY":"$CURRENT_DIRECTORY" "${CHE_DIR_IMAGE_NAME}":"${CHE_VERSION}" "${CURRENT_DIRECTORY}" "$@" +} - docker_exec run -it --rm --name "${CHE_FILE_CONTAINER_NAME}" \ - -v /var/run/docker.sock:/var/run/docker.sock \ - -v "$CURRENT_DIRECTORY":"$CURRENT_DIRECTORY" \ - "${CHE_FILE_IMAGE_NAME}":"${CHE_VERSION}" \ - "${CURRENT_DIRECTORY}" "${CHE_CLI_ACTION}" +execute_che_action() { + check_current_image_and_update_if_not_found ${CHE_ACTION_IMAGE_NAME} + docker_run "${CHE_ACTION_IMAGE_NAME}":"${CHE_VERSION}" "$@" } + update_che_image() { if [ -z "${CHE_VERSION}" ]; then CHE_VERSION=${DEFAULT_CHE_VERSION} @@ -381,11 +376,10 @@ mount_local_directory() { return fi - docker_exec run --rm -it --cap-add SYS_ADMIN \ - --device /dev/fuse \ - --name "${CHE_MOUNT_CONTAINER_NAME}" \ - -v "${MOUNT_PATH}":/mnthost \ - "${CHE_MOUNT_IMAGE_NAME}":"${CHE_VERSION}" "${GLOBAL_GET_DOCKER_HOST_IP}" $3 + docker_run --cap-add SYS_ADMIN \ + --device /dev/fuse \ + -v "${MOUNT_PATH}":/mnthost \ + "${CHE_MOUNT_IMAGE_NAME}":"${CHE_VERSION}" "${GLOBAL_GET_DOCKER_HOST_IP}" $3 } execute_che_debug() { @@ -401,7 +395,7 @@ execute_che_debug() { print_che_cli_debug execute_che_launcher run_connectivity_tests - execute_che_test "$@" + execute_che_test post-flight-check "$@" ;; --cli|-cli) print_che_cli_debug @@ -422,11 +416,10 @@ execute_che_debug() { esac } -execute_che_test() { - docker_exec run --rm -it --name "${CHE_TEST_CONTAINER_NAME}" \ - -v /var/run/docker.sock:/var/run/docker.sock \ - "${CHE_TEST_IMAGE_NAME}":"${CHE_VERSION}" "$@" +execute_che_test() { + check_current_image_and_update_if_not_found ${CHE_TEST_IMAGE_NAME} + docker_run "${CHE_TEST_IMAGE_NAME}":"${CHE_VERSION}" "$@" } print_che_cli_debug() { @@ -445,9 +438,6 @@ print_che_cli_debug() { debug "HAS_DOCKER_FOR_WINDOWS_IP = $(has_docker_for_windows_ip && echo "YES" || echo "NO")" debug "IS_MOBY_VM = $(is_moby_vm && echo "YES" || echo "NO")" debug "" - debug "---------------------------------------" - debug "---------------------------------------" - debug "---------------------------------------" } run_connectivity_tests() { @@ -521,7 +511,7 @@ execute_profile(){ if [ ! $# -ge 2 ]; then error "" - error "che profile: Wrong number of arguments." + error "${CHE_MINI_PRODUCT_NAME} profile: Wrong number of arguments." error "" return fi @@ -530,7 +520,7 @@ execute_profile(){ add|rm|set|info|update) if [ ! $# -eq 3 ]; then error "" - error "che profile: Wrong number of arguments." + error "${CHE_MINI_PRODUCT_NAME} profile: Wrong number of arguments." error "" return fi @@ -538,7 +528,7 @@ execute_profile(){ unset|list) if [ ! $# -eq 2 ]; then error "" - error "che profile: Wrong number of arguments." + error "${CHE_MINI_PRODUCT_NAME} profile: Wrong number of arguments." error "" return fi @@ -547,44 +537,41 @@ execute_profile(){ case ${2} in add) - if [ -f ~/.che/"${3}" ]; then + if [ -f ~/.che/profiles/"${3}" ]; then error "" - error "Profile ~/.che/${3} already exists. Nothing to do. Exiting." + error "Profile ~/.che/profiles/${3} already exists. Nothing to do. Exiting." error "" return fi - test -d ~/.che || mkdir -p ~/.che - touch ~/.che/"${3}" - - echo "CHE_LAUNCHER_IMAGE_NAME=$CHE_LAUNCHER_IMAGE_NAME" > ~/.che/"${3}" - echo "CHE_SERVER_IMAGE_NAME=$CHE_SERVER_IMAGE_NAME" >> ~/.che/"${3}" - echo "CHE_FILE_IMAGE_NAME=$CHE_FILE_IMAGE_NAME" >> ~/.che/"${3}" - echo "CHE_MOUNT_IMAGE_NAME=$CHE_MOUNT_IMAGE_NAME" >> ~/.che/"${3}" - echo "CHE_TEST_IMAGE_NAME=$CHE_TEST_IMAGE_NAME" >> ~/.che/"${3}" + test -d ~/.che/profiles || mkdir -p ~/.che/profiles + touch ~/.che/profiles/"${3}" - echo "CHE_LAUNCHER_CONTAINER_NAME=$CHE_LAUNCHER_CONTAINER_NAME" >> ~/.che/"${3}" - echo "CHE_SERVER_CONTAINER_NAME=$CHE_SERVER_CONTAINER_NAME" >> ~/.che/"${3}" - echo "CHE_FILE_CONTAINER_NAME=$CHE_FILE_CONTAINER_NAME" >> ~/.che/"${3}" - echo "CHE_MOUNT_CONTAINER_NAME=$CHE_MOUNT_CONTAINER_NAME" >> ~/.che/"${3}" - echo "CHE_TEST_CONTAINER_NAME=$CHE_TEST_CONTAINER_NAME" >> ~/.che/"${3}" + echo "CHE_PRODUCT_NAME=$CHE_PRODUCT_NAME" > ~/.che/profiles/"${3}" + echo "CHE_MINI_PRODUCT_NAME=$CHE_MINI_PRODUCT_NAME" > ~/.che/profiles/"${3}" + echo "CHE_LAUNCHER_IMAGE_NAME=$CHE_LAUNCHER_IMAGE_NAME" > ~/.che/profiles/"${3}" + echo "CHE_SERVER_IMAGE_NAME=$CHE_SERVER_IMAGE_NAME" >> ~/.che/profiles/"${3}" + echo "CHE_DIR_IMAGE_NAME=$CHE_DIR_IMAGE_NAME" >> ~/.che/profiles/"${3}" + echo "CHE_MOUNT_IMAGE_NAME=$CHE_MOUNT_IMAGE_NAME" >> ~/.che/profiles/"${3}" + echo "CHE_TEST_IMAGE_NAME=$CHE_TEST_IMAGE_NAME" >> ~/.che/profiles/"${3}" + echo "CHE_SERVER_CONTAINER_NAME=$CHE_SERVER_CONTAINER_NAME" >> ~/.che/profiles/"${3}" # Add all other variables to the profile - env | grep CHE_ >> ~/.che/"${3}" + env | grep CHE_ >> ~/.che/profiles/"${3}" # Remove duplicates, if any - cat ~/.che/"${3}" | sort | uniq > ~/.che/tmp - mv -f ~/.che/tmp ~/.che/"${3}" + cat ~/.che/profiles/"${3}" | sort | uniq > ~/.che/profiles/tmp + mv -f ~/.che/profiles/tmp ~/.che/profiles/"${3}" info "" - info "Added new Che CLI profile ~/.che/${3}." + info "Added new ${CHE_MINI_PRODUCT_NAME} CLI profile ~/.che/profiles/${3}." info "" ;; update) - if [ ! -f ~/.che/"${3}" ]; then + if [ ! -f ~/.che/profiles/"${3}" ]; then error "" - error "Profile ~/.che/${3} does not exist. Nothing to update. Exiting." + error "Profile ~/.che/profiles/${3} does not exist. Nothing to update. Exiting." error "" return fi @@ -593,79 +580,87 @@ execute_profile(){ execute_profile profile add "${3}" ;; rm) - if [ ! -f ~/.che/"${3}" ]; then + if [ ! -f ~/.che/profiles/"${3}" ]; then error "" - error "Profile ~/.che/${3} does not exist. Nothing to do. Exiting." + error "Profile ~/.che/profiles/${3} does not exist. Nothing to do. Exiting." error "" return fi - rm ~/.che/"${3}" > /dev/null + rm ~/.che/profiles/"${3}" > /dev/null info "" - info "Removed Che CLI profile ~/.che/${3}." + info "Removed ${CHE_MINI_PRODUCT_NAME} CLI profile ~/.che/profiles/${3}." info "" ;; info) - if [ ! -f ~/.che/"${3}" ]; then + if [ ! -f ~/.che/profiles/"${3}" ]; then error "" - error "Profile ~/.che/${3} does not exist. Nothing to do. Exiting." + error "Profile ~/.che/profiles/${3} does not exist. Nothing to do. Exiting." error "" return fi debug "---------------------------------------" - debug "--------- CHE CLI PROFILE INFO --------" + debug "--------- CLI PROFILE INFO --------" debug "---------------------------------------" debug "" - debug "Profile ~/.che/${3} contains:" + debug "Profile ~/.che/profiles/${3} contains:" while IFS= read line do # display $line or do somthing with $line debug "$line" - done <~/.che/"${3}" + done <~/.che/profiles/"${3}" ;; set) - if [ ! -f ~/.che/"${3}" ]; then + if [ ! -f ~/.che/profiles/"${3}" ]; then error "" error "Profile ~/.che/${3} does not exist. Nothing to do. Exiting." error "" return fi - echo "CHE_PROFILE=${3}" > ~/.che/.profile + echo "CHE_PROFILE=${3}" > ~/.che/profiles/.profile info "" - info "Set active Che CLI profile to ~/.che/${3}." + info "Set active ${CHE_MINI_PRODUCT_NAME} CLI profile to ~/.che/profiles/${3}." info "" ;; unset) - if [ ! -f ~/.che/.profile ]; then + if [ ! -f ~/.che/profiles/.profile ]; then error "" error "Default profile not set. Nothing to do. Exiting." error "" return fi - rm -rf ~/.che/.profile + rm -rf ~/.che/profiles/.profile info "" - info "Unset the default Che CLI profile. No profile currently set." + info "Unset the default ${CHE_MINI_PRODUCT_NAME} CLI profile. No profile currently set." info "" ;; list) if [ -d ~/.che ]; then - info "" - info "Available Che CLI profiles:" - ls ~/.che/ + info "Available ${CHE_MINI_PRODUCT_NAME} CLI profiles:" + ls ~/.che/profiles + else + info "No ${CHE_MINI_PRODUCT_NAME} CLI profiles currently set." + fi + + if has_default_profile; then + info "Default profile set to:" + get_default_profile + else + info "Default profile currently unset." fi ;; esac } has_default_profile() { - if [ -f ~/.che/.profile ]; then + if [ -f ~/.che/profiles/.profile ]; then return 0 else return 1 @@ -674,7 +669,7 @@ has_default_profile() { get_default_profile() { if [ has_default_profile ]; then - source ~/.che/.profile + source ~/.che/profiles/.profile echo "${CHE_PROFILE}" else echo "" @@ -684,16 +679,16 @@ get_default_profile() { load_profile() { if has_default_profile; then - source ~/.che/.profile + source ~/.che/profiles/.profile - if [ ! -f ~/.che/"${CHE_PROFILE}" ]; then + if [ ! -f ~/.che/profiles/"${CHE_PROFILE}" ]; then error "" - error "Che CLI profile set in ~/.che/.profile to '${CHE_PROFILE}' but ~/.che/${CHE_PROFILE} does not exist." + error "${CHE_MINI_PRODUCT_NAME} CLI profile set in ~/.che/profiles/.profile to '${CHE_PROFILE}' but ~/.che/profiles/${CHE_PROFILE} does not exist." error "" return fi - source ~/.che/"${CHE_PROFILE}" + source ~/.che//profiles/"${CHE_PROFILE}" fi } @@ -719,15 +714,31 @@ case ${CHE_CLI_ACTION} in profile) execute_profile "$@" ;; - init|up) + dir) + # remove "dir" arg by shifting it + shift + load_profile + execute_che_dir "$@" + ;; + action) + # remove "action" arg by shifting it + shift + load_profile + execute_che_action "$@" + ;; + test) + # remove "test" arg by shifting it + shift load_profile - execute_che_file + execute_che_test "$@" ;; update) load_profile update_che_image ${CHE_LAUNCHER_IMAGE_NAME} update_che_image ${CHE_MOUNT_IMAGE_NAME} - update_che_image ${CHE_FILE_IMAGE_NAME} + update_che_image ${CHE_DIR_IMAGE_NAME} + update_che_image ${CHE_ACTION_IMAGE_NAME} + update_che_image ${CHE_TEST_IMAGE_NAME} # Delegate updating che-server to the launcher execute_che_launcher