How to Include START_SCRIPT (Contextualization) in EC2 instance

Good afternoon

We are deploying different EC2 instances through templates and prepared images, we have already included the contextualization packages (https://github.com/OpenNebula/addon-context-linux/releases specifically one-context-ec2_4.14.4.deb) in such images, therefore I know that there is a way to include a variable called START_SCRIPT as a parameter in the templates to configure the EC2 machines.

We have the experience to make this but using KVM as driver and doing local deployments, it means:

  1. Create the initial script (Any script *.sh)
  2. Add this initial script as an image in OpenNebula (oneimage create --type CONTEXT --path “path” --name “name” -d files)
  3. Finally add this image in the context of the VM template in the parameter (FILES_DS = xxxxx)

And all works perfectly, so the question is ¿How can I get this script in the EC2 instances, What is the workflow to use what you call in the templates START_SCRIPT ?

Hi Jorge,

Using the packages you mentioned, you can add a CONTEXT section to the template. These are the attributes supported in EC2:
http://docs.opennebula.org/4.14/user/virtual_machine_setup/bcont.html#ec2-contextualization

Therefore you can provide the script as text in the START_SCRIPT or START_SCRIPT_BASE64 attributes and the context scripts will execute it.

You can also provide extra attributes and retrieve them using the Amazon EC2 metadata server:

$ curl http://169.254.169.254/latest/user-data
ONEGATE_ENDPOINT="https://onegate...
SSH_PUBLIC_KEY="ssh-rsa ABAABeqzaC1y...

If a value for the USERDATA attribute is provided in the EC2 section of the template, the CONTEXT section will be ignored and the value provided as USERDATA will be available instead of the CONTEXT information.

Hi Daniel

I have the next template http://pastebin.com/t124UJUm in BASE64, it was tested launching it manually and works, I tried to instantiate the template, all goes fine but the START_SCRIPT is never executed automatically in the new EC2 once created machine.

The images already have the contextualization package, in fact I can retrieve info through onegate

onegate vm show
VM 865                                                                          
NAME                : ec2-Script-865      
STATE               : RUNNING

And the command to retrieve the CONTEXT params has all that were defined in the template:

ubuntu@ip-172-31-37-131:~$ curl http://169.254.169.254/latest/user-data
DISK_ID="0"
NODE="ec2-Scrip"
ONEGATE_ENDPOINT="http://84.88.36.26:5030"
START_SCRIPT_BASE64="IyEvYmluL2Jhc2gNCg0KIyAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSAjDQojIENvcHlyaWdodCAyMDE2LCBBdG9zIFNwYWluIFNBLiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICMNCiMgQXV0aG9yOiBKb3JnZSBFZGdhciBWYWxkZXJyYW1hIFJvbWVybyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIw0KIyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAjDQojIExpY2Vuc2VkIHVuZGVyIHRoZSBBcGFjaGUgTGljZW5zZSwgVmVyc2lvbiAyLjAgKHRoZSAiTGljZW5zZSIpOyB5b3UgbWF5ICAgICMNCiMgbm90IHVzZSB0aGlzIGZpbGUgZXhjZXB0IGluIGNvbXBsaWFuY2Ugd2l0aCB0aGUgTGljZW5zZS4gWW91IG1heSBvYnRhaW4gICAgIw0KIyBhIGNvcHkgb2YgdGhlIExpY2Vuc2UgYXQgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAjDQojICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICMNCiMgaHR0cDovL3d3dy5hcGFjaGUub3JnL2xpY2Vuc2VzL0xJQ0VOU0UtMi4wICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIw0KIyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAjDQojIFVubGVzcyByZXF1aXJlZCBieSBhcHBsaWNhYmxlIGxhdyBvciBhZ3JlZWQgdG8gaW4gd3JpdGluZywgc29mdHdhcmUgICAgICAgICMNCiMgZGlzdHJpYnV0ZWQgdW5kZXIgdGhlIExpY2Vuc2UgaXMgZG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ldGMvaG9zdHMNCiAgICAgc3VkbyBzZWQgLWkgInMvJE9MRF9IT1NUTkFNRS8kSE9TVE5BTUUvZyIgL2V0Yy9ob3N0bmFtZQ0KZmkNCg0KIyoqKioqKioqKioqKioqKioqKioqKioqKioqKioNCiNDaGFuZ2UgSVAgb2YgUmVzb3VyY2UgTWFuYWdlcg0KIyoqKioqKioqKioqKioqKioqKioqKioqKioqKioNCg0KSE9TVF9JUD0kKC9zYmluL2lmY29uZmlnIGV0aDAgfCBncmVwICJpbmV0IGFkZHIiIHwgYXdrIC1GOiAne3ByaW50ICQyfScgfCBhd2sgJ3twcmludCAkMX0nKQ0KDQppZiBbIC1uICIkSE9TVF9JUCIgXTsgdGhlbg0KICAgICBzdWRvIHNlZCAtaSAicy8xMjcuMC4wLjEgJEhPU1ROQU1FLyRIT1NUX0lQICRIT1NUTkFNRS9nIiAvZXRjL2hvc3RzDQogICAgIHN1ZG8gc2VydmljZSBob3N0bmFtZSByZXN0YXJ0OyBzdWRvIHNlcnZpY2UgbmV0d29ya2luZyByZXN0YXJ0DQpmaQ=="
TARGET="hda"
TOKEN="YES"
VMID="865"

Any help?

Could you check if this file exists in side the VM?
/tmp/one-context-tmp/one-start-script

If it does, try to execute it and check if there is any error.

I did a couple of tests and the file /tmp/one-context-tmp/one-start-script exists, but its content is empty, but at the moment to execute the command

curl http://169.254.169.254/latest/user-data

The result appears,

DISK_ID="0"
NODE="ec2-Scrip"
ONEGATE_ENDPOINT="http://84.88.36.26:5030"
START_SCRIPT_BASE64="IyEvYmluL2Jhc2gKCiMgLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0gIwojIENvcHlyaWdodCAyMDE2LCBBdG9z
IFNwYWluIFNBLiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICMK
IyBBdXRob3I6IEpvcmdlIEVkZ2FyIFZhbGRlcnJhbWEgUm9tZXJvICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAjCiMgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIwojIExpY2Vuc2VkIHVu
ZGVyIHRoZSBBcGFjaGUgTGljZW5zZSwgVmVyc2lvbiAyLjAgKHRoZSAiTGljZW5zZSIpOyB5b3Ug
bWF5ICAgICMKIyBub3QgdXNlIHRoaXMgZmlsZSBleGNlcHQgaW4gY29tcGxpYW5jZSB3aXRoIHRo
ZSBMaWNlbnNlLiBZb3UgbWF5IG9idGFpbiAgICAjCiMgYSBjb3B5IG9mIHRoZSBMaWNlbnNlIGF0
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIwojICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICMKIyBodHRwOi8vd3d3LmFwYWNoZS5vcmcvbGljZW5zZXMvTElDRU5T
RS0yLjAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAjCiMgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgIwojIFVubGVzcyByZXF1aXJlZCBieSBhcHBsaWNhYmxlIGxhdyBvciBhZ3JlZWQgdG8gaW4g
d3JpdGluZywgc29mdHdhcmUgICAgICAgICMKIyBkaXN0cmlidXRlZCB1bmRlciB0aGUgTGljZW5z
ZSBpcyBkaXN0cmlidXRlZCBvbiBhbiAiQVMgSVMiIEJBU0lTLCAgICAgICAgICAjCiMgV0lUSE9V
VCBXQVJSQU5USUVTIE9SIENPTkRJVElPTlMgT0YgQU5ZIEtJTkQsIGVpdGhlciBleHByZXNzIG9y
IGltcGxpZWQuICAgIwojIFNlZSB0aGUgTGljZW5zZS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KioqKioqKioKCkhPU1Rf
SVA9JCgvc2Jpbi9pZmNvbmZpZyBldGgwIHwgZ3JlcCAiaW5ldCBhZGRyIiB8IGF3ayAtRjogJ3tw
cmludCAkMn0nIHwgYXdrICd7cHJpbnQgJDF9JykKCmlmIFsgLW4gIiRIT1NUX0lQIiBdOyB0aGVu
CiAgICAgc3VkbyBzZWQgLWkgInMvMTI3LjAuMC4xICRIT1NUTkFNRS8kSE9TVF9JUCAkSE9TVE5B
TUUvZyIgL2V0Yy9ob3N0cwogICAgIHN1ZG8gc2VydmljZSBob3N0bmFtZSByZXN0YXJ0OyBzdWRv
IHNlcnZpY2UgbmV0d29ya2luZyByZXN0YXJ0CmZpCg=="
TARGET="hda"
TOKEN="YES"
VMID="877"
ONEGATE_TOKEN="ec4ZrkYjUbk1MaO2q2dUiQ=="hadoop

I did the decode using the base64 tool that comes in LInux, and performing the decode process of the variable START_SCRIPT_BASE64 I get the correct script and can be executed

base64 -d [file] > script.sh

am I missing something?

Could you try running this script in the VM and check if there is any error?