All Products
Search
Document Center

Elastic Compute Service:Use a script interpreter to run Cloud Assistant commands

Last Updated:Sep 14, 2024

You can call the RunCommand or InvokeCommand API operation related to Cloud Assistant to run Shell, PowerShell, or Bat scripts. Specify the CommandContent and Launcher parameters if you want to run scripts by using other specified interpreters, such as python or kubectl.

Prerequisites

  • The instance is in Running status.

  • Cloud Assistant Agent is installed on the instance, and the version of Cloud Assistant Agent should be later than the following versions.

    • Linux: 2.2.3.668

    • Windows: 2.1.3.668

Limits

  • Only the script interpreters already installed on the instance are supported.

  • Interpreters that require interactive input are not supported.

Usage

Specify the CommandContent and Launcher parameters when you call the RunCommand or InvokeCommand operation to run a Cloud Assistant command.

CommandContent parameter

The CommandContent parameter contains the script to be run. For example, when you want to run a Python script through Cloud Assistant, specify the Python script as the value of CommandContent.

Launcher parameter

The Launcher parameter can pass arguments to the script interpreter and controls the script file path in the running command by using the {{ACS::ScriptFileName}} identifier.

Specify the script interpreter

To specify the script interpreter in the Launcher parameter, follow one of these methods, depending on whether the absolute path of the interpreter is configured in the PATH environment variable:

  • Environment variable not configured: Enter the absolute path of the specified script interpreter in the Launcher parameter. For example, the path of the Python script interpreter is not configured in the PATH environment variable, configure the Launcher parameter as follows:

    /usr/bin/python
  • Environment variable configured: Enter the name of the interpreter in the Launcher parameter. For example, if the path of the Python script interpreter is in the PATH, specify it as python in the Launcher parameter:

    python

Use the identifier to specify the position of the script file in the command

  • Identifier not specified

    Cloud Assistant appends the actual script file path to the end of the command. For example, if the Launcher is set to python, Cloud Assistant will run python <file> within the instance. <file> indicates the actual file path of the script, corresponding to the script content specified in CommandContent.

  • Use the identifier to specify the position of the script file in the command

    For certain script interpreters that mandate a specific placement of the script file path within the command, such as the python interpreter which requires the script file path as the first parameter, the position of the script file can be specified by using the {{ACS::ScriptFileName}} identifier within the Launcher parameter. For example:

    python {{ACS::ScriptFileName}} arg1 arg2

    Cloud Assistant runs the command python <file> arg1 arg2 within the instance, where <file> is the actual path to the script, corresponding to the CommandContent.

  • Use the identifier to specify the extension of the script file

    For script interpreters that require specific file extensions, the file extension can be specified by using the {{ACS::ScriptFileName|Ext(.ext)}} identifier. For example:

    python3 {{ACS::ScriptFileName|Ext(.py)}}

    In this case, Cloud Assistant replaces {{ACS::ScriptFileName|Ext(.py)}} in the Launcher parameter with the path of the script file, appending the .py extension. Cloud Assistant runs the command python3 <file>.py within the instance, where <file> is the actual file path to the script specified in CommandContent.

Scenarios

Invoke the Python program in the system to run a script

CommandContent

import datetime
current_date = datetime.date.today()
print(current_date)

Launcher

python {{ACS::ScriptFileName|Ext(.py)}}

Invoke the kubectl program in the system to run a script

CommandContent

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  labels:
    app: my-app
spec:
  containers:
  - name: my-container
    image: nginx
    ports:
    - containerPort: 80

Launcher

kubectl apply -f {{ACS::ScriptFileName|Ext(.yaml)}}

Invoke regedit to run the registry script

CommandContent

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Micorsoft\Windows\Persionalization]
"NoLockScreen"=dword:000000001

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control]
"WaittoKillServiceTimeout"="2000"

Launcher

regedit import {{ACS::ScriptFileName|Ext(.reg)}}

Use script interpreters with lower versions of Cloud Assistant

When the version of Cloud Assistant Agent does not meet the prerequisites, you can use the Heredoc mechanism to specify the script interpreter, which is available only on Linux systems. Heredoc enables the input of multiline text into the command line.

python3 <<EOF
# This is a simple Python script
import datetime
current_date = datetime.date.today()
print(current_date)
EOF