From 1948d20af88e8192117d1800210cb9506aaa8f5d Mon Sep 17 00:00:00 2001 From: David Albertson Date: Tue, 11 Sep 2018 11:56:20 -0700 Subject: [PATCH 1/3] Update customscript.py --- CustomScript/customscript.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CustomScript/customscript.py b/CustomScript/customscript.py index 697731830..540d98a5f 100644 --- a/CustomScript/customscript.py +++ b/CustomScript/customscript.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # CustomScript extension # From edabf04cb2460a85798824a2f04aaf5b14729ecb Mon Sep 17 00:00:00 2001 From: David Albertson Date: Mon, 24 Sep 2018 10:39:54 -0700 Subject: [PATCH 2/3] Adding shim script for CSE --- CustomScript/HandlerManifest.json | 10 +++--- CustomScript/customscript.py | 8 ++--- CustomScript/manifest.xml | 2 +- CustomScript/shim.sh | 52 +++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 CustomScript/shim.sh diff --git a/CustomScript/HandlerManifest.json b/CustomScript/HandlerManifest.json index 2594429a2..66bf704c2 100644 --- a/CustomScript/HandlerManifest.json +++ b/CustomScript/HandlerManifest.json @@ -2,11 +2,11 @@ { "version": 1.0, "handlerManifest": { - "disableCommand": "customscript.py -disable", - "enableCommand": "customscript.py -enable", - "installCommand": "customscript.py -install", - "uninstallCommand": "customscript.py -uninstall", - "updateCommand": "customscript.py -update", + "disableCommand": "./shim.sh -disable", + "enableCommand": "./shim.sh -enable", + "installCommand": "./shim.sh -install", + "uninstallCommand": "./shim.sh -uninstall", + "updateCommand": "./shim.sh -update", "rebootAfterInstall": false, "reportHeartbeat": false } diff --git a/CustomScript/customscript.py b/CustomScript/customscript.py index 540d98a5f..50524cbd0 100644 --- a/CustomScript/customscript.py +++ b/CustomScript/customscript.py @@ -236,14 +236,14 @@ def download_files(hutil): def start_daemon(hutil): cmd = get_command_to_execute(hutil) if cmd: - args = [os.path.join(os.getcwd(), __file__), "-daemon"] + args = [os.path.join(os.getcwd(), "shim.sh"), "-daemon"] # This process will start a new background process by calling - # customscript.py -daemon - # to run the script and will exit itself immediatelly. + # shim.sh -daemon + # to run the script and will exit itself immediately. # Redirect stdout and stderr to /dev/null. Otherwise daemon process - # will throw Broke pipe exeception when parent process exit. + # will throw Broke pipe exception when parent process exit. devnull = open(os.devnull, 'w') subprocess.Popen(args, stdout=devnull, stderr=devnull) hutil.do_exit(0, 'Enable', 'transitioning', '0', diff --git a/CustomScript/manifest.xml b/CustomScript/manifest.xml index 1a306c80e..a7130a867 100644 --- a/CustomScript/manifest.xml +++ b/CustomScript/manifest.xml @@ -2,7 +2,7 @@ Microsoft.OSTCExtensions CustomScriptForLinux - 1.5.3 + 1.5.4 VmRole diff --git a/CustomScript/shim.sh b/CustomScript/shim.sh new file mode 100644 index 000000000..67ca1b6b3 --- /dev/null +++ b/CustomScript/shim.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env bash + +# The shim scripts provide a single entry point for CSE and will invoke the customscript.py entry point using the +# appropriate python interpreter version. +# Arguments passed to the shim layer are redirected to the invoked script without any validation. + +COMMAND="./customscript.py" +PYTHON="" +ARG="$@" + +function find_python(){ + local python_version=$1 + + # Check if there is python defined. + which python > /dev/null 2>&1 + return_code=`echo $?` + + if [[ ${return_code} == 0 ]] + then + eval ${python_version}="python" + else + # Python was not found. Searching for Python3 now. + which python3 > /dev/null 2>&1 + return_code=`echo $?` + + if [[ ${return_code} == 0 ]] + then + eval ${python_version}="python3" + fi + fi + + if [ -z ${python_version} ] + then + echo "No Python interpreter found on the box" >&2 + exit 51 # Not Supported + fi +} + +find_python PYTHON + +if [ -z "$PYTHON" ] +then + echo "No Python interpreter found on the box" >&2 + exit 51 # Not Supported +else + echo "Found: `${PYTHON} --version`" +fi + +${PYTHON} ${COMMAND} ${ARG} +exit $? + +# DONE \ No newline at end of file From 375e33f33c0c3fdb0b06269014d13ab177d911aa Mon Sep 17 00:00:00 2001 From: David Albertson Date: Mon, 24 Sep 2018 11:09:39 -0700 Subject: [PATCH 3/3] Adressing comments --- CustomScript/HandlerManifest.json | 10 +++++----- CustomScript/customscript.py | 2 +- CustomScript/shim.sh | 24 +++++------------------- 3 files changed, 11 insertions(+), 25 deletions(-) diff --git a/CustomScript/HandlerManifest.json b/CustomScript/HandlerManifest.json index 66bf704c2..4726a7c59 100644 --- a/CustomScript/HandlerManifest.json +++ b/CustomScript/HandlerManifest.json @@ -2,11 +2,11 @@ { "version": 1.0, "handlerManifest": { - "disableCommand": "./shim.sh -disable", - "enableCommand": "./shim.sh -enable", - "installCommand": "./shim.sh -install", - "uninstallCommand": "./shim.sh -uninstall", - "updateCommand": "./shim.sh -update", + "disableCommand": "shim.sh -disable", + "enableCommand": "shim.sh -enable", + "installCommand": "shim.sh -install", + "uninstallCommand": "shim.sh -uninstall", + "updateCommand": "shim.sh -update", "rebootAfterInstall": false, "reportHeartbeat": false } diff --git a/CustomScript/customscript.py b/CustomScript/customscript.py index 50524cbd0..7e2904a77 100644 --- a/CustomScript/customscript.py +++ b/CustomScript/customscript.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python # # CustomScript extension # diff --git a/CustomScript/shim.sh b/CustomScript/shim.sh index 67ca1b6b3..0f6affa82 100644 --- a/CustomScript/shim.sh +++ b/CustomScript/shim.sh @@ -9,31 +9,17 @@ PYTHON="" ARG="$@" function find_python(){ - local python_version=$1 + local python_exec_command=$1 # Check if there is python defined. - which python > /dev/null 2>&1 - return_code=`echo $?` - - if [[ ${return_code} == 0 ]] - then - eval ${python_version}="python" + if command -v python >/dev/null 2>&1 ; then + eval ${python_exec_command}="python" else # Python was not found. Searching for Python3 now. - which python3 > /dev/null 2>&1 - return_code=`echo $?` - - if [[ ${return_code} == 0 ]] - then - eval ${python_version}="python3" + if command -v python >/dev/null 2>&1 ; then + eval ${python_exec_command}="python3" fi fi - - if [ -z ${python_version} ] - then - echo "No Python interpreter found on the box" >&2 - exit 51 # Not Supported - fi } find_python PYTHON