PowerCLI wrapper with Skaffolder part2 – The Launch

In the last article, I introduced how Skaffolder, giving an architecture overview with models and methods, is generating the foundation code for User authentication and authorization and all CRUD actions for the objects which are composing the application. Effectively the coding effort is reduced only on custom methods which are making the real purpose of the application.

Before going ahead, it is necessary to customize the template files under .skaffolder directory. Let’s see in-depth…

Dockerfile, docker-compose, and properties

Under .skaffolder/template there are two files which are responsible to generate docker-compose and Docker file. In a development environment you should make the following modifications:

File: Dockerfile.hbs

Remember to uncomment dependency definition and “npm install” in production and also substitute CMD with “npm start”.

Note: You could use app.log to output debug.

File: docker-compose.yaml.hbs

In my opinion, the best way to execute PowerCLI scripts in a non-windows environment is using Photon. Inside Docker-hub there’s an image called vmware/powerclicore based on Photon2 that I’ll use in this integration as development and productive environment.

Through TDNF package is possible to install NodeJS, AngularJS, VMware vSphere libraries and whatever is needed to run Swagger API, call vCenter by SOAP or simply present a UI framework based on VMware Clarity.

Under development, I suggest to link the “code” directory and manually handle NodeJSstart and stop. When the code is ready for production you must add the code and execute NodeJS on container start.

My customization ends with properties.js.hbs which guide the generation of some environment data:

In this file, you can make some modifications like the working port, the secret token, the API directory path, etc…

Implement the Launch method: a brief intro on NodeJS async call

A good web application needs asynchronous process every time a long-term process is called, in order to don’t freeze “the user” waiting for task competition. In this case, a PowerCLI execution could an indefinite amount of time to execute. Executing the async process could be a painful developing process if the underline system doesn’t implement a native way to dispatch process, handle execution process, errors and gather all or part of results.

Node.js comes with ChildProcess a library which create and handle synchronous and asynchronous process scheduler without “losing your mind” with other software elements or particular system calls.

The sequence

The script launch process is composed of the following steps:

  1. Gather the information about path and script name
  2. Check if the same script is not already running
  3. Prepare for execution
  4. Launch the shell script (update DB attribute with state = Running)
  5. Wait on data (or errors) and store every incoming stream inside a global variable
  6. Wait on the end of the script and store the variable content in DB

A simple PowerCLI script

To test the real interaction with vCenter I placed the following script called test.ps1 under /app/pcli directory:

Note: Is better use authentication credential file instead of a username and password which are readable directly into the file. (I’ll show this improvement in next posts)

Implementing Launch API Method

Opening the swagger.yaml file, you must check the content of the method

The “parameters” instruction indicates that param id comes from the URL path and is required. The “real” content of the method is located in resource/PowerCLILaunch_db/custom/. Finally, the following code is the real API method which implements asynchronous handling of the shell execution.

During the execution, the incoming data stream could happen more than one time, depending on the output data. For this reason is important to gather all strings before proceeding with further operations: use a global variable.

Test, Tips and security improvements

Before starting the application, I suggest to change the URL address in the swagger definition, because probably you’ll unable to call API at 127.0.0.1… change here:

Now using docker-compose build and docker-compose up is possible run node server in the application generated container:

Then install modules and run the web server with npm install and npm start:

And browsing to http://container_host_ip_fqdn:3000/api/docs you’ll see the API documentation and tool page.

To successfully interact with the API you must log in (following the parameters provided in the example: admin/pass), copy the response token and place in the authorize barer parameter

Then insert a new ScriptExecution entry using POST /scriptexecution/ CRUD action create with the following parameters:

Annotate the ID.

Obviously, to make a “real” test executing PowerrCLI script you must ensure that the docker-host can reach and resolve correctly the vCenter FQDN (and also providing correct credentials). Launch the GET method /scriptexecutions/launch/{id} providing the ID as parameter previously annotated. Here the results:

In the next post, I’ll show the method improvement and how to collect data giving a right pattern.

Un pensiero riguardo “PowerCLI wrapper with Skaffolder part2 – The Launch

I commenti sono chiusi