Run Cloud Assistant commands with a script interpreter

Updated at:

Specify the CommandContent and Launcher parameters to run scripts with interpreters such as python or kubectl.

Prerequisites

  • The instance is in the Running state.

  • The Cloud Assistant Agent installed on the instance is later than the following versions:

    • Linux: 2.2.3.668

    • Windows: 2.1.3.668

Limitations

  • Only script interpreters installed on the instance are supported.

  • Script interpreters requiring interactive input are not supported.

Usage

Call the RunCommand or InvokeCommand API operation and specify the CommandContent and Launcher parameters.

CommandContent

The CommandContent parameter specifies the script content. For example, to run a Python script, set CommandContent to the Python script content.

Launcher

The Launcher parameter passes arguments to the script interpreter. Use the {{ACS::ScriptFileName}} identifier to control where the script file path appears in the command.

Specify a script interpreter

How you specify the interpreter depends on whether its absolute path is in the PATH environment variable:

  • If the path is not configured: Specify the absolute path of the interpreter. For example:

    /usr/bin/python
  • If the path is configured: Specify the interpreter name. For example:

    python

Use an identifier to specify the script file position

  • Unspecified identifier

    Cloud Assistant appends the script file path to the command. For example, setting Launcher to python runs python <file>, where <file> is the CommandContent script file path.

  • Specify the script file position with an identifier

    Some interpreters require the script file path at a specific position. Use the {{ACS::ScriptFileName}} identifier in the Launcher parameter to set the position. For example:

    python {{ACS::ScriptFileName}} arg1 arg2

    Cloud Assistant runs python <file> arg1 arg2, where <file> is the CommandContent script file path.

  • Specify the script file extension with an identifier

    Some interpreters require a specific file extension. Use the {{ACS::ScriptFileName|Ext(.ext)}} identifier to set the extension. For example:

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

    Cloud Assistant replaces {{ACS::ScriptFileName|Ext(.py)}} with the script file path using the .py extension, then runs python3 <file>.py, where <file> is the CommandContent script file path.

Example scenarios

Run a Python script

CommandContent

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

Launcher

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

Apply a Kubernetes manifest with kubectl

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)}}

Import a registry file with regedit

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 Cloud Assistant Agent versions

If the Cloud Assistant Agent version is earlier than those listed in Prerequisites, use the here-document (heredoc) mechanism to specify the interpreter. This mechanism is supported only on Linux and allows multi-line text input in the command line.

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