Undefined method error while using xpath.rb

Hello,
I’m trying to use, this script in the documentation to easily parse the VM template :
http://docs.opennebula.org/5.0/integration/infrastructure_integration/hooks.html#developing-your-hooks

But when I launch the script with a base64 template in argument, I got this errors :
/var/lib/one/remotes/datastore/xpath.rb:66:in block in <main>': undefined methodelements’ for nil:NilClass (NoMethodError)
from /var/lib/one/remotes/datastore/xpath.rb:60:in each' from /var/lib/one/remotes/datastore/xpath.rb:60:in
/var/lib/one/remotes/datastore/xpath.rb:66:in block in <main>': undefined methodelements’ for nil:NilClass (NoMethodError)
from /var/lib/one/remotes/datastore/xpath.rb:60:in each' from /var/lib/one/remotes/datastore/xpath.rb:60:in

Can somebody help me :blush: ?

Mura,

Can you share with us the whole script?

Yes, I’m using the same as the link :

#!/bin/bash
# Argument hook for virtual network add to oned.conf
# VNET_HOOK = [
#   name="bash_arguments",
#   on="CREATE",
#   command=<path_to_this_file>,
#   arguments="$TEMPLATE" ]

XPATH=/var/lib/one/remotes/datastore/xpath.rb
T64=$1

USER_NAME=`$XPATH -b $T64 UNAME`
OWNER_USE_PERMISSION=`$XPATH -b $T64 PERMISSIONS/OWNER_U`

#UNAME and PERMISSIONS/OWNER_U are the XPATH for the attributes without the root element

and my template file is :
template_code64 (4.2 KB)

So, I run this :
./test_xpath.sh /tmp/template

And got the errors above.

Mura,

Hi Mura,

The base64 template should be in the variable, not in a file. Something like

T64=`cat $1`

:wink:

You can follow similar scripts with the following:

#!/bin/bash
XPATH=/var/lib/one/remotes/datastore/xpath.rb
T64="$1"
unset i XPATH_ELEMENTS
while IFS= read -r -d '' element; do
    XPATH_ELEMENTS[i++]="$element"
done < <($XPATH -b $T64 UNAME PERMISSIONS/OWNER_U)
unset i
USER_NAME="${XPATH_ELEMENTS[i++]}"
OWNER_USE_PERMISSIONS="$XPATH_ELEMENTS[i++]"

and call it with

cat /tmp/template | ./test_xpath.sh

Kind Regards,
Anton Todorov

I juste try your script and got the same error :frowning:

Mura,

Well I am obviously sleeping when typed the example. Please try

./test_xpath.sh `cat /tmp/template`

And fixed typo in the snippet:

#!/bin/bash
XPATH=/var/lib/one/remotes/datastore/xpath.rb
T64="$1"
unset i XPATH_ELEMENTS
while IFS= read -r -d '' element; do
    XPATH_ELEMENTS[i++]="$element"
done < <($XPATH -b $T64 UNAME PERMISSIONS/OWNER_U)
unset i
USER_NAME="${XPATH_ELEMENTS[i++]}"
OWNER_USE_PERMISSIONS="${XPATH_ELEMENTS[i++]}"
echo "USER_NAME=$USER_NAME"
echo "OWNER_USE_PERMISSIONS=$OWNER_USE_PERMISSIONS"

Anton