From 35f1405accdb7d4930fcab9345f93d2d50989454 Mon Sep 17 00:00:00 2001 From: Varad Meru Date: Sat, 22 Sep 2018 15:13:52 -0700 Subject: [PATCH] Adding a shell layer to find python version on the box --- VMAccess/HandlerManifest.json | 10 +-- VMAccess/extension_shim.sh | 114 ++++++++++++++++++++++++++++++++++ VMAccess/manifest.xml | 2 +- VMAccess/vmaccess.py | 14 ----- 4 files changed, 120 insertions(+), 20 deletions(-) create mode 100755 VMAccess/extension_shim.sh diff --git a/VMAccess/HandlerManifest.json b/VMAccess/HandlerManifest.json index 0828b0803..a6b59e87e 100644 --- a/VMAccess/HandlerManifest.json +++ b/VMAccess/HandlerManifest.json @@ -2,11 +2,11 @@ { "version": 1.0, "handlerManifest": { - "disableCommand": "./vmaccess.py -disable", - "enableCommand": "./vmaccess.py -enable", - "installCommand": "./vmaccess.py -install", - "uninstallCommand": "./vmaccess.py -uninstall", - "updateCommand": "./vmaccess.py -update", + "disableCommand": "extension_shim.sh -c ./vmaccess.py -d", + "enableCommand": "extension_shim.sh -c ./vmaccess.py -e", + "installCommand": "extension_shim.sh -c ./vmaccess.py -i", + "uninstallCommand": "extension_shim.sh -c ./vmaccess.py -u", + "updateCommand": "extension_shim.sh -c ./vmaccess.py -p", "rebootAfterInstall": false, "reportHeartbeat": false } diff --git a/VMAccess/extension_shim.sh b/VMAccess/extension_shim.sh new file mode 100755 index 000000000..2ee4d3d80 --- /dev/null +++ b/VMAccess/extension_shim.sh @@ -0,0 +1,114 @@ +#!/usr/bin/env bash + +# Keeping the default command +COMMAND="" +PYTHON="" + +USAGE="$(basename "$0") [-h] [-i|--install] [-u|--uninstall] [-d|--disable] [-e|--enable] [-p|--update] + +Program to find the installed python on the box and invoke a Python extension script. + +where: + -h|--help show this help text + -i|--install install the extension + -u|--uninstall uninstall the extension + -d|--disable disable the extension + -e|--enable enable the extension + -p|--update update the extension + -c|--command command to run + +example: +# Install usage +$ bash extension_shim.sh -i +python ./vmaccess.py -install + +# Custom executable python file +$ bash extension_shim.sh -c ""hello.py"" -i +python hello.py -install + +# Custom executable python file with arguments +$ bash extension_shim.sh -c ""hello.py --install"" +python hello.py --install +" + +function find_python(){ + local python_exec_command=$1 + + # Check if there is python defined. + if command -v python >/dev/null 2>&1 ; then + eval ${python_exec_command}="python" + else + # Python was not found. Searching for Python3 now. + if command -v python >/dev/null 2>&1 ; then + eval ${python_exec_command}="python3" + fi + fi +} + +# Transform long options to short ones for getopts support (getopts doesn't support long args) +for arg in "$@"; do + shift + case "$arg" in + "--help") set -- "$@" "-h" ;; + "--install") set -- "$@" "-i" ;; + "--update") set -- "$@" "-p" ;; + "--enable") set -- "$@" "-e" ;; + "--disable") set -- "$@" "-d" ;; + "--uninstall") set -- "$@" "-u" ;; + *) set -- "$@" "$arg" + esac +done + +if [ -z "$arg" ] +then + echo "$USAGE" >&2 + exit 1 +fi + +# Get the arguments +while getopts "iudephc:?" o; do + case "${o}" in + h|\?) + echo "$USAGE" + exit 0 + ;; + i) + operation="-install" + ;; + u) + operation="-uninstall" + ;; + d) + operation="-disable" + ;; + e) + operation="-enable" + ;; + p) + operation="-update" + ;; + c) + COMMAND="$OPTARG" + ;; + *) + echo "$USAGE" >&2 + exit 1 + ;; + esac +done + +shift $((OPTIND-1)) + +# If find_python is not able to find a python installed, $PYTHON will be null. +find_python PYTHON + + +if [ -z "$PYTHON" ]; then + echo "No Python interpreter found on the box" >&2 + exit 51 # Not Supported +else + `${PYTHON} --version` +fi + +${PYTHON} ${COMMAND} ${operation} +# DONE \ No newline at end of file diff --git a/VMAccess/manifest.xml b/VMAccess/manifest.xml index 910c5c08d..4f45a0d47 100644 --- a/VMAccess/manifest.xml +++ b/VMAccess/manifest.xml @@ -2,7 +2,7 @@ Microsoft.OSTCExtensions VMAccessForLinux - 1.5.0 + 1.5.1 VmRole diff --git a/VMAccess/vmaccess.py b/VMAccess/vmaccess.py index 68fc8fc0a..d001a3c37 100644 --- a/VMAccess/vmaccess.py +++ b/VMAccess/vmaccess.py @@ -100,13 +100,6 @@ def enable(): hutil.log("Succeeded in reset sshd_config.") if remove_user: - if re.match("^[a-z][-a-z0-9_]*$", remove_user) is None: - waagent.AddExtensionEvent(name=hutil.get_name(), - op=waagent.WALAEventOperation.Enable, - isSuccess=False, - message="(03002)Argument error, invalid remove_user") - raise Exception("'remove_user' does not match the regular expression '^[a-z][-a-z0-9_]*$'") - waagent.AddExtensionEvent(name=hutil.get_name(), op="scenario", isSuccess=True, message="remove-user") _remove_user_account(remove_user, hutil) @@ -190,13 +183,6 @@ def _set_user_account_pub_key(protect_settings, hutil): return user_name = protect_settings['username'] - if re.match("^[a-z][-a-z0-9_]*$", user_name) is None: - waagent.AddExtensionEvent(name=hutil.get_name(), - op=waagent.WALAEventOperation.Enable, - isSuccess=False, - message="(03002)Argument error, invalid username") - raise Exception("'username' does not match the regular expression '^[a-z][-a-z0-9_]*$'") - user_pass = protect_settings.get('password') cert_txt = protect_settings.get('ssh_key') expiration = protect_settings.get('expiration')