Onetemplate edit via script

I am using OpenNebula 4.8 and have a lot of templates that are the same
except for the image that they launch and the IP address on which they run.
Obviously I can do a onetemplate clone and then use onetemplate update with vi as
my editor to make the changes by hand. But I am trying to find a way to script it.

In OpenNebula 3.2 you could "onetemplate show > /tmp/template"
and then use sed to make the changes. That doesn’t quite work in open nebula 4.8
because you have to add some stuff from the headers back into the template
(the NAME) to make it a legal template again.

Is there a way from the command line to set the EDITOR variable to some script
with arguments and then use onetemplate update to pipe to that script?

Hi.

My Little hack to do that is change $EDITOR var and set to some command.

for example.

my template 404

onetemplate show 404

CONTEXT=[
  NETWORK="YES",
  SSH_PUBLIC_KEY="$USER[SSH_PUBLIC_KEY]" ]
CPU="1"
DISK=[
  IMAGE="install raw",
  IMAGE_UNAME="oneadmin" ]
DISK=[
  IMAGE="UBUNTU-SERVER-CD",
  IMAGE_UNAME="oneadmin" ]
GRAPHICS=[
  KEYMAP="es",
  LISTEN="0.0.0.0",
  TYPE="VNC" ]
HYPERVISOR="kvm"
MEMORY="1024"
NIC=[
  NETWORK="Default-Private",
  NETWORK_UNAME="sistemas" ]
OS=[
  ARCH="x86_64",
  BOOT="cdrom" ]
SUNSTONE_CAPACITY_SELECT="YES"
SUNSTONE_NETWORK_SELECT="YES"

Now i want to change the OS ARCH (in this case use the random text “CHANGETEXT”)

To do that i define the EDITOR and set the SED comand. Then i Execute the update.

The comand is that.

[user@opennebula ~]$ export EDITOR=“sed -i ‘s/x86_64/CHANGETEXT/’”; onetemplate update 404

Now, my template 404 will be:

CONTEXT=[
  NETWORK="YES",
  SSH_PUBLIC_KEY="$USER[SSH_PUBLIC_KEY]" ]
CPU="1"
DISK=[
  IMAGE="install raw",
  IMAGE_UNAME="oneadmin" ]
DISK=[
  IMAGE="UBUNTU-SERVER-CD",
  IMAGE_UNAME="oneadmin" ]
GRAPHICS=[
  KEYMAP="es",
  LISTEN="0.0.0.0",
  TYPE="VNC" ]
HYPERVISOR="kvm"
MEMORY="1024"
NIC=[
  NETWORK="Default-Private",
  NETWORK_UNAME="sistemas" ]
OS=[
  ARCH="CHANGETEXT",
  BOOT="cdrom" ]
SUNSTONE_CAPACITY_SELECT="YES"
SUNSTONE_NETWORK_SELECT="YES"
VCPU="1"

Be careful, try before in random template… sed command is not your friend the most of cases hehe.

I am using ‘onetemplate update --append filename’. It is for appending attributes, but if attribute exists it will replace it.
Here is example:

$ onetemplate show 1
TEMPLATE 1 INFORMATION
ID : 1
NAME : CentOS 7 - KVM system-ssh
USER : oneadmin
GROUP : oneadmin
REGISTER TIME : 04/14 11:03:24

PERMISSIONS
OWNER : um-
GROUP : —
OTHER : —

TEMPLATE CONTENTS
CONTEXT=[
NETWORK=“YES”,
SSH_PUBLIC_KEY="$USER[SSH_PUBLIC_KEY]" ]
CPU="1"
DISK=[
IMAGE=“CentOS-7”,
IMAGE_UNAME=“oneadmin” ]
FROM_APP="53e7bf928fb81d6a69000002"
FROM_APP_NAME="CentOS 7 - KVM"
GRAPHICS=[
LISTEN=“0.0.0.0”,
TYPE=“vnc” ]
LOGO="images/logos/centos.png"
MEMORY="768"
NIC=[
NETWORK=“vmnet1”,
NETWORK_UNAME=“oneadmin” ]
OS=[
ARCH=“x86_64” ]
SUNSTONE_CAPACITY_SELECT="YES"
SUNSTONE_NETWORK_SELECT=“YES”

To change OS/ARCH attribute to “i386” Create update.txt file and update the template:

$ echo ‘OS=[ARCH=“i386”]’ > update.txt
$ onetemplate update --append 1 update.txt

Now you have:

$ onetemplate show 1
TEMPLATE 1 INFORMATION
ID : 1
NAME : CentOS 7 - KVM system-ssh
USER : oneadmin
GROUP : oneadmin
REGISTER TIME : 04/14 11:03:24

PERMISSIONS
OWNER : um-
GROUP : —
OTHER : —

TEMPLATE CONTENTS
CONTEXT=[
NETWORK=“YES”,
SSH_PUBLIC_KEY="$USER[SSH_PUBLIC_KEY]" ]
CPU="1"
DISK=[
IMAGE=“CentOS-7”,
IMAGE_UNAME=“oneadmin” ]
FROM_APP="53e7bf928fb81d6a69000002"
FROM_APP_NAME="CentOS 7 - KVM"
GRAPHICS=[
LISTEN=“0.0.0.0”,
TYPE=“vnc” ]
LOGO="images/logos/centos.png"
MEMORY="768"
NIC=[
NETWORK=“vmnet1”,
NETWORK_UNAME=“oneadmin” ]
OS=[
ARCH=“i386” ]
SUNSTONE_CAPACITY_SELECT="YES"
SUNSTONE_NETWORK_SELECT=“YES”

BR,
Anton

1 Like

WoW @atodorov_storpool fantastic :smiley: better like mine as far.

Thanks for the ideas. I am aware of the --append option, I have tried it and it works but I have not used it because I then don’t have any persistent record of how I started each virtual machine… it is easier to have a fully formed template for each VM that other staff can then use to help relaunch it later if necessary.

It is good to know the sed trick works… I was hoping for something like that.
Thank you for all your responses.

Steve Timm