Automation Blog

from Stefan Schnell

WebAssembly (Wasm) is a binary instruction format and it is designed as a portable compilation for different targets. This means different architectures, different operating systems and different execution environments like browser, desktop or server - one for all. This makes Wasm as a very interesting approach.

Wasmtime is a standalone fast and secure runtime for WebAssembly, WASI and the Component Model by the Bytecode Alliance. It allows to execute Wasm modules without further dependencies. It is possible to use Wasmtime with different programming languages.

This post describes an approach how to embed and use Wasmtime with the Python runtime environment of VCF Automation.

Embed and Use Wasmtime with Python Runtime Environment

First it is necessary to download the Python Wasm Runtime powered by Wasmtime and its dependencies. These must be saved in the PyPI repository linked in VCF Automation.

vcf automation environment with python package wasmtime
The specified execution environment can now be used when building an action. This allows the seamless integration of Wasmtime in VCF Automation Python programming.
The following example is an implementation for VCF automation of the standard example.

"""
@author Stefan Schnell <mail@stefan-schnell.de>
@version 0.2.0
Checked with VMware Aria Automation 8.18.3 with Python 3.10.
"""

import json
import os
import tempfile
from wasmtime import Store, Module, Instance, Func, FuncType

def handler(context: dict, inputs: dict) -> dict:

    tempDirectory: str = tempfile.gettempdir()

    with open(os.path.join(tempDirectory, "hello.wat"), "w") as watFile:
        watFile.write(
"""(module
  (func $say_hello (import "" "hello"))
  (func (export "run") (call $say_hello))
)"""
)

    store = Store()
    module = Module.from_file(
        store.engine, os.path.join(tempDirectory, "hello.wat")
    )

    def say_hello():
        print("Hello from WebAssembly via Python.")
    hello = Func(store, FuncType([], []), say_hello)

    instance = Instance(store, module, [hello])
    run = instance.exports(store)["run"]
    run(store)

    outputs = {
      "status": "done"
    }

    return outputs

vcf automation python action with wasmtime

Conclusion

The Python WebAssembly runtime powered by Wasmtime can be used seamlessly and easily in the Python execution environment of VCF Automation. This opens up another way to use Wasm modules in VCF Automation.

References