onecast Guide 3.0

:!: Development version of OpenNebula 3.0. The material on this page needs to be reviewed for completeness and accuracy.

:!: To use this functionality you need to install the onecast Addon

onecast is a tool that can rewrite parts of a VM description substituting the variables it contains by some values that the user provides. It also gets values from the environment if the values are not explicitly provided by the user and can also have a default value so they will be translated to something meaningful if not found by other methods. An example of a template can be this one:

NAME    = "${NAME}"
CPU     = ${CPU|1.0}
VCPU    = ${CPU|1}
MEMORY  = ${MEMORY|512}
DISK    = [
    SOURCE  = "${HOME}/images/vm.img",
    TARGET  = sda
]

In this example we can see that the placeholder for the variable values is specified with ${VARIABLE}. The name of the variable is case sensitive and this will be translated to its value (user provided or from the environment) or left blank if not found. We have to be careful with variables not set or they will render the template unusable in some cases. To overcome this problem, and also to give some values that the user may not want to modify, default values can be provided alongside the variable name. Variable names with default values are specified with ${VARIABLE|default value}. Doing so if the variable is not set by the user in the command line or found in the environment it will be substituted by the default value.

It is also very useful to use environment variables to get some information specific to the user, in this example we suppose every user has a file called images/vm.img in their home directory so we use $HOME environment variable to point to it

To generate the final template we use the this command:

<xterm> $ onecast -d NAME=“test vm” test.one NAME = “test vm” CPU = 1.0 VCPU = 1 MEMORY = 512 DISK = [

  SOURCE  = "/home/oneuser/images/vm.img",
  TARGET  = sda

] </xterm>

We have to specify the template file to the onecast script and we can also provide variables using -d option. This option will have an argument in this form NAME=value that will be used to substitute any variable in the template with that name. More than one of those variables can be added in the command line. As another example to illustrate this we can also set the memory to 1Gb issuing this command:

<xterm> $ onecast -d NAME=“test vm” -d MEMORY=1024 test.one NAME = “test vm” CPU = 1.0 VCPU = 1 MEMORY = 1024 DISK = [

  SOURCE  = "/home/oneuser/images/vm.img",
  TARGET  = sda

] </xterm>

The output of the command can then be redirected to a file and use it to create a new VM or use the parameter -c so the VM is automatically sent to OpenNebula:

<xterm> $ onecast -d NAME=“test vm” -d MEMORY=1024 test.one -c ID: 9 $ onevm list

 ID     USER     NAME STAT CPU     MEM        HOSTNAME        TIME
  9 oneadmin  test vm pend   0      0K                 00 00:00:06

$ onevm show 9 VIRTUAL MACHINE 9 INFORMATION ID : 9 NAME : test vm STATE : PENDING LCM_STATE : LCM_INIT START TIME : 03/14 02:19:58 END TIME : - DEPLOY ID: : -

VIRTUAL MACHINE MONITORING NET_TX : 0 NET_RX : 0 USED MEMORY : 0 USED CPU : 0

VIRTUAL MACHINE TEMPLATE CPU=1.0 DISK=[

DISK_ID=0,
SOURCE=/home/oneuser/images/vm.img,
TARGET=sda ]

MEMORY=1024 NAME=test vm VCPU=1 VMID=9 </xterm>