Virtual Server Tuning via Pre/Post Installation Scripts

Overview

Pre- and post-installation scripts can be used for additional custom configuration of a virtual server. For example, they can be used for network configuration, installation of supplementary software in a virtual server and so on.

The scripts should be located and executed on IM node.

The scripts are defined per OS template and can be triggered on the following event types:

  • precreate – script is executed before the virtual server is created.
  • postcreate – script is executed after the virtual server is created.

    Important: Be aware that this event implies that a virtual server is created but not started. Therefore, if you need to perform an operation like installing supplementary software into a virtual server, you should add a command for starting a virtual server before installing this software (prlctl start #{veId}). If your script contains operations requiring network, which configuration is usually time-consuming, you should also add a command to suspend a program execution for a specified time (sleep <timeout>). Refer to the Example 1 below.

  • predelete – script is executed before the virtual server is deleted.
  • postdelete – script is executed after the virtual server is deleted.
  • prestop – script is executed before the virtual server is stopped.
  • poststart – script is executed after the virtual server is started.
  • prerecreate – script is executed before the virtual server is recreated.
  • postrecreate – script is executed after the virtual server is recreated.
  • premigrate – script is executed before the virtual server is migrated.
  • postmigrate – script is executed after the virtual server is migrated. Post-migrate script is executed on a node where the virtual server is migrated to.

There might be cases when you want to execute the same script on the virtual server recreation and migration as the one you specified for the creation events, for example, mounting disk. To do it, you can link the same script to be executed on recreation/migration operation as well as on creation. The example will be listed below.

Important: You can define only one script of a certain type per OS template. For example, you can’t define two post-start scripts for one OS template. The result of script execution doesn't influence on the operation itself. For example, if execution of the script assigned to an event of a virtual server creation/deletion/start/stop/recreation/migration fails, the operation itself won't fail.

You can perform the following operations with scripts:

  • adding a script to the IM DB
  • renaming a script in the IM DB
  • updating a script's body in the IM DB
  • listing all the scripts in the IM DB
  • getting a script by its name
  • listing the scripts by OS template
  • linking a script to OS template
  • unlinking a script from OS template
  • deleting a script from the IM DB

Assigning Custom Script to Particular OS Template

We will consider an example, where the scripts are located on IM node.

To define pre/post installation scripts for a particular OS template to be executed on a particular event, do the following:

  1. Prepare bash script.

    Note: The length of the script can be no more than 1048576 characters.

  2. Locate your script file on IM node.
  3. To specify a script for a particular OS template, you need to find out the OS template name.

    You can find the OS template name in PCP > Services > Cloud Infrastructure > OS Templates > Id column.

    OR

    You can list all OS templates with their names by executing the curl command on MN:

    curl -D - -k -E /usr/local/pem/APS/certificates/poa.pem 'https://127.0.0.1:6308/aps/2/resources?implementing(http://www.parallels.com/infrastructure/virtual-environment/os-template/1.0)'

    where /usr/local/pem/APS/certificates/poa.pem – path to CloudBlue Commerce certificate, allowing you to execute commands on MN.

  4. Use curl commands on IM to request the appropriate API method.

    Be aware that:

    • for the curl -d option you can define either the absolute path to the script "@<path_to_script_file>" or the body of the script "<script_body>"
    • if the script should be executed on virtualization node, no special commands needed. If the script should be executed inside the virtual server, use prlctl exec #{veId} command before bash code.
    • #{veId} – UUID of the virtual server, that is stored on virtualization node. You can get it using the prlctl list -a command. It will be replaced automatically on the script execution.
  5. To add a custom script to the IM DB, perform the following curl command:

    curl -u "<login>:<password>" http://127.0.0.1:4465/paci/v1.0/template/<OS_template_name>/custom_scripts/<script_name>/<event> -X POST -d "<script_body>" -H "Content-Type:application/xml"

    OR

    curl -u "<login>:<password>" http://127.0.0.1:4465/paci/v1.0/template/<OS_template_name>/custom_scripts/<script_name>/<event> -X POST -d "@<path_to_script_file>" -H "Content-Type:application/xml"

    The following placeholders are used in the example:

    • <script_name> – a name of the custom script to be saved in the IM DB.
    • <event> – the type of event when the script will be triggered. You can use the following events: precreate, postcreate, predelete, postdelete, prestop, postcreate, prerecreate, postrecreate, premigrate, and postmigrate.
    • <OS_template_name> – name of the OS template to which the script will be applied.
    • <login> – administrator's login.
    • <password> – API access key. You can generate it in the PCP. Go to Services > Cloud Infrastructure > Cloud Infrastructure API and click Generate new API Access Key.

How to Manage Scripts

The following operations are available:

  • to add a custom script to the IM DB:

    curl -u "<login>:<password>" http://127.0.0.1:4465/paci/v1.0/template/<OS_template_name>/custom_scripts/<script_name>/<event> -X POST -d "<script_body>" -H "Content-Type:application/xml"

    OR

    curl -u "<login>:<password>" http://127.0.0.1:4465/paci/v1.0/template/<OS_template_name>/custom_scripts/<script_name>/<event> -X POST -d "@<path_to_script_file>" -H "Content-Type:application/xml"
  • to update a custom script's body in the IM DB:

    curl -u "<login>:<password>" http://127.0.0.1:4465/paci/v1.0/template/custom_scripts/<script_name> -X PUT -d "<script_body>" -H "Content-Type:application/xml"

    where <script_new_name> – a new name of the custom script to be rewritten in the IM DB.

  • to rename a custom script in the IM DB:

    curl -u "<login>:<password>" http://127.0.0.1:4465/paci/v1.0/template/custom_scripts/<script_name>?new-name=<script_new_name> -X PUT -H "Content-Type:application/xml"
  • to list all the custom scripts:

    curl -u "<login>:<password>" http://127.0.0.1:4465/paci/v1.0/template/custom_scripts -X GET -H "Content-Type:application/xml"
  • to get a single custom script:

    curl -u "<login>:<password>" http://127.0.0.1:4465/paci/v1.0/template/custom_scripts/<script_name> -X GET -H "Content-Type:application/xml"
  • to list custom scripts by OS template:

    curl -u "<login>:<password>" http://127.0.0.1:4465/paci/v1.0/template/<OS_template_name>/custom_scripts -X GET -H "Content-Type:application/xml"
  • to link a custom script to OS template to be executed on a specified event:

    curl -u "<login>:<password>" http://127.0.0.1:4465/paci/v1.0/template/<OS_template_name>/custom_scripts/<script_name>/<event> -X PUT -H "Content-Type:application/xml"
  • to unlink a custom script, being executed on a specified event, from OS template:

    curl -u "<login>:<password>" http://127.0.0.1:4465/paci/v1.0/template/<OS_template_name>/custom_scripts/<script_name>/<event> -X DELETE -H "Content-Type:application/xml"
  • to delete a custom script from the IM DB:

    curl -u "<login>:<password>" http://127.0.0.1:4465/paci/v1.0/template/custom_scripts/<script_name> -X DELETE -H "Content-Type:application/xml"

Example 1. This script installs php in the virtual server after it is started.

Name: "Installing_php"

Type: "postcreate"

Body: "prlctl start #{veId}; sleep 10; prlctl exec #{veId} \"yum install php -y\" "

Curl command to add the script:

curl -u "admin:<password>" http://127.0.0.1:4465/paci/v1.0/template/centos-7-x86_64-pcs6/custom_scripts/Installing_php/postcreate -X POST -d "prlctl start #{veId}; sleep 10; prlctl exec #{veId} \"yum install php -y\" " -H "Content-Type:application/xml"

Example 2. This script configures firewall rules for the virtual server by opening port 22 and closing the rest of the ports.

Name: "Configuring _firewall"

Type: "postcreate"

Body: "prlctl exec #{veId} "iptables -P INPUT DROP ; iptables -A INPUT -p tcp --dport 22 -j ACCEPT;" "

Curl command to add the script:

curl -u "admin:<password>" http://127.0.0.1:4465/paci/v1.0/template/centos-7-x86_64-pcs6/custom_scripts/Configuring_firewall/postcreate -X POST -d "prlctl exec #{veId} \"iptables -P INPUT DROP ; iptables -A INPUT -p tcp --dport 22 -j ACCEPT;\" " -H "Content-Type:application/xml"

To make the same firewall configuration on virtual server recreation, link the same script to the OS template:

curl -u "admin:<password>" http://127.0.0.1:4465/paci/v1.0/template/centos-7-x86_64-pcs6/custom_scripts/Configuring _firewall/postcreate -X PUT -H "Content-Type:application/xml"