Use a script interpreter to run Cloud Assistant commands

更新时间:
复制 MD 格式

You can call the RunCommand or InvokeCommand API operation of Cloud Assistant to run Shell, PowerShell, or Bat scripts. To run scripts that require other interpreters, such as python or kubectl, you must specify the CommandContent and Launcher parameters.

Prerequisites

  • The instance must be in the Running state.

  • The Cloud Assistant Agent must be installed on the instance, and the Cloud Assistant Agent version must be newer than the versions listed below.

    • Linux: 2.2.3.668

    • Windows: 2.1.3.668

Limits

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

  • Script interpreters that require interactive input are not supported.

Usage

To run a Cloud Assistant command, call the RunCommand or InvokeCommand API operation and specify the CommandContent and Launcher parameters.

The CommandContent parameter

The CommandContent parameter specifies the content of the script to run. For example, if you want to run a Python script using Cloud Assistant, you must set the CommandContent parameter to the content of the Python script.

The Launcher parameter

The Launcher parameter passes arguments to the script interpreter. It also uses the {{ACS::ScriptFileName}} identifier to control the position of the script file path in the command.

Specify a script interpreter

You can specify the script interpreter in the Launcher parameter in one of the following ways. The method that you choose depends on whether the absolute path of the interpreter is configured in the PATH environment variable:

  • If the path is not configured: Specify the absolute path of the script interpreter in the Launcher parameter. For example, if the path of the Python interpreter is not in the PATH environment variable, set the Launcher parameter as follows:

    /usr/bin/python
  • If the path is configured: Specify the name of the interpreter in the Launcher parameter. For example, if the path of the Python interpreter is in the PATH environment variable, set the Launcher parameter to python:

    python

Use an identifier to specify the script file position

  • Unspecified identifier

    Cloud Assistant adds the script file path to the end of the command. For example, if you set Launcher to python, Cloud Assistant runs python <file> on the instance. In this command, <file> is the path to the file that contains the content of the CommandContent parameter.

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

    Some script interpreters require the script file path to be in a specific position in the command. For example, the python interpreter requires the script file path as its first argument. For these interpreters, you can use the {{ACS::ScriptFileName}} identifier in the Launcher parameter to specify the position of the script file. For example:

    python {{ACS::ScriptFileName}} arg1 arg2

    Cloud Assistant then runs python <file> arg1 arg2 on the instance. In this command, <file> is the path to the file that contains the content of the CommandContent parameter.

  • Use an identifier to specify the script file extension

    Some script interpreters require a specific file extension. For these interpreters, you can use the {{ACS::ScriptFileName|Ext(.ext)}} identifier to specify the file extension. For example:

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

    Cloud Assistant replaces {{ACS::ScriptFileName|Ext(.py)}} in the Launcher parameter with the path to the script file that has the .py extension. Cloud Assistant then runs python3 <file>.py on the instance. In this command, <file> is the path to the file that contains the content of the CommandContent parameter.

Example Scenarios

Invoke a Python program on the system to run a script

CommandContent

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

Launcher

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

Invoke a kubectl program on 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 a registry script file

CommandContent

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Personalization]
"NoLockScreen"=dword:00000001

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

Launcher

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

Use script interpreters with earlier versions of Cloud Assistant

If the version of the Cloud Assistant Agent is earlier than the versions listed in the prerequisites, you can use the here-document (heredoc) mechanism to specify the script interpreter. This mechanism is supported only on Linux and lets you enter multi-line text in the command line.

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