Skip to content

Managing Scripts for Remote Execution

To execute a PowerShell or Bash script or Expect script on a remote host, you must provide the script as a string to run_powershell or run_bash or run_expect. While you can keep these strings as literals in your Python code, best practice is to keep them as resource files in your source directory and access them with pkgutil.

pkgutil is part of the standard Python library. The method that is applicable to resources is pkgutil.get_data.

Basic Usage

Given the following plugin structure:

├── plugin_config.yml
├── schema.json
└── src
    └── resources

Assume SnapshotDefinition is:

"snapshotDefinition": {
    "type" : "object",
    "additionalProperties" : false,
    "properties" : {
        "name": {"type": "string"},
        "date": {"type": "string"}

and src/resources/ contains:

#!/usr/bin/env bash

If is needed in post_snapshot, it can be retrieved and executed:

import pkgutil

from dlpx.virtualization import libs
from dlpx.virtualization.platform import Plugin
from dlpx.virtualization.platform.exceptions import UserError

from generated.definitions import SnapshotDefinition

plugin = Plugin()

def post_snapshot(direct_source, repository, source_config):
    # Retrieve script contents
    script_content = pkgutil.get_data('resources', '')

    # Execute script on remote host
    response = libs.run_bash(direct_source.connection, script_content)

    # Fail operation if the timestamp couldn't be retrieved
    if response.exit_code != 0:
        raise UserError(
        'Failed to get date',
        'Make sure the user has the required permissions',
        '{}\n{}'.format(response.stdout, rsponse.stderr))

    return SnapshotDefinition(name='Snapshot', date=response.stdout)

Python's Working Directory

This assumes that src/ is Python's current working directory. This is the behavior of the Virtualization Platform.

Resources need to be in a Python module

pkgutil.get_data cannot retrieve the contents of a resource that is not in a Python package. This means that a resource that is in the first level of your source directory will not be retrievable with pkgutil. Resources must be in a subdirectory of your source directory, and that subdirectory must contain an file.

Multi-level Packages

Given the following plugin structure:

├── plugin_config.yml
├── schema.json
└── src
    └── resources
        ├── database
        │   ├──
        │   └──
        └── platform

The contents of src/resources/platform/ can be retrieved with:

script_content = pkgutil.get_data('resources.platform', '')