[ovirt-users] Passing custom script to cloud init using api

Juan Hernandez jhernand at redhat.com
Fri Sep 5 08:27:35 UTC 2014


On 09/04/2014 11:59 AM, Shanil S wrote:
> Hi Juan,
> 
> I tried with the above touch command but it seems the 'iwashere' file
> isn't created after executing it the above command.
>

Trying to make an example for this I discovered that the "custom_script"
element is currently ignored if the "cloud_init" element is present.
Instead we are taking the content of the first "file" element from the
"cloud_init" element and appending it to the cloud-init configuration
file. I believe that this is a bug, as it breaks backwards compatibility:

  https://bugzilla.redhat.com/1138564

However, you can exploit this bug to do what you want. This is an example:

#!/bin/sh -ex

url="https://ovirt.example.com/ovirt-engine/api"
user="admin at internal"
password="******"

curl \
--insecure \
--request POST \
--header "Accept: application/xml" \
--header "Content-Type: application/xml" \
--user "${user}:${password}" \
--data '
<action>
  <vm>
    <initialization>
      <cloud_init>
        <host>
          <address>myhost.mydomain.com</address>
          </host>
        <users>
          <user>
            <user_name>root</user_name>
            <password>mypassword</password>
          </user>
        </users>
        <network_configuration>
          <nics>
            <nic>
              <name>eth0</name>
              <boot_protocol>static</boot_protocol>
              <network>
                <ip address="192.168.122.31" netmask="255.255.255"
gateway="192.168.122.1"/>
              </network>
              <on_boot>true</on_boot>
            </nic>
          </nics>
          <dns>
            <servers>
              <host>
                <address>192.168.122.1</address>
              </host>
            </servers>
            <search_domains>
              <host>
                <address>mydomain.com</address>
              </host>
            </search_domains>
          </dns>
        </network_configuration>
        <files>
          <file>
            <name>ignored</name>
            <content><![CDATA[runcmd:
 - echo "I was here!" > /iwashere.txt
]]></content>
            <type>plaintext</type>
          </file>
        </files>
      </cloud_init>
      <custom_script><![CDATA[runcmd:
 - echo "I was here!" > /iwashere.txt
]]></custom_script>
    </initialization>
  </vm>
</action>
' \
"${url}/vms/480225cf-0cbd-4166-b9ca-3857b124618a/start"

Note that in this example I am including the custom script both in the
first "file" element inside "cloud_init" and in the "custom_script"
element, this way it will work with the current version of the engine
and also if/when we eventually fix the bug.

Please remember to update the wiki with your feedback:

http://www.ovirt.org/REST-Api#How_can_I_run_a_custom_script_using_cloud-init.3F

> 
> On Thu, Sep 4, 2014 at 3:02 PM, Juan Hernandez <jhernand at redhat.com
> <mailto:jhernand at redhat.com>> wrote:
> 
>     On 09/04/2014 10:58 AM, Shanil S wrote:
>     > Hi Juan,
>     >
>     > Okay.. Thanks for your update. Also i found some sample cloud init
>     > scripts from here
>     > https://github.com/number5/cloud-init/tree/master/doc/examples and i
>     > tried the following scripts
>     >
>     > - type: foo/wark
>     > filename: bar
>     > content: |
>     > This is my payload
>     > hello
>     > - this is also payload
>     > - |
>     > multi line payload
>     > here
>     > -
>     > type: text/upstart-job
>     > filename: my-upstart.conf
>     > content: |
>     > Test file contents
>     >
>     > And run the start vm using the above script using cloud init and api,
>     > Could you please tell me where i can find the result of the above
>     script ?
>     > I think the above script will create a file like my-upstart.conf
>     >
> 
>     As I said I am not a cloud-init expert, so I don't really know what is
>     the meaning or effect of this script. But I think that you need to use
>     the "runcmd" option. For example:
> 
>     <action>
>       ...
>       <initialization>
>         <cloud-init>...</cloud-init>
>         <custom-script><![CDATA[runcmd:
>      - touch /iwashere
>     ]]></custom-script>
>       </initialization>
>     </action>
> 
>     If you use this it should run the "touch /iwashere" command, and you can
>     check that it did verifying that the "/iwashere" file exists.
> 
>     As I said I didn't test this, so I may not work. I'd appreciate if you
>     can test it, and once it works update the wiki with the correct
>     instructions:
> 
>     http://www.ovirt.org/REST-Api#How_can_I_run_a_custom_script_using_cloud-init.3F
> 
>     >
>     > On Thu, Sep 4, 2014 at 1:56 PM, Juan Hernandez
>     <jhernand at redhat.com <mailto:jhernand at redhat.com>
>     > <mailto:jhernand at redhat.com <mailto:jhernand at redhat.com>>> wrote:
>     >
>     >     On 09/04/2014 06:29 AM, Shanil S wrote:
>     >     > Hi Juan,
>     >     >
>     >     > Is there anyways to specify the custom script from a file in
>     the xml ?
>     >     > ie, i have a script in a file called script.sh and i would
>     like to
>     >     > include this script in the xml with the api call. Is there
>     any methods
>     >     > for this ?
>     >     >
>     >
>     >     You can deploy files using cloud-init, and the run them using
>     the custom
>     >     script. Something like this:
>     >
>     >       <action>
>     >         ...
>     >         <initialization>
>     >           <cloud-init>
>     >             <files>
>     >               <file name="/root/myscript.sh"/>
>     >                 <content><![CDATA[#!/bin/sh the content of your
>     >     script]]></content>
>     >               </file>
>     >             </files>
>     >           </cloud-init>
>     >           <custom_script><![CDATA[runcmd:
>     >      - sh /root/myscript.sh
>     >     ]]></custom_script>
>     >         </initialization>
>     >       </action>
>     >
>     >     But I'm not a cloud-init expert, and I didn't test this, so I
>     don't
>     >     really know if the custom_script will run before or after the
>     files are
>     >     deployed. It makes more sense to run the scripts after
>     deploying the
>     >     files, but you will have to test it yourself.
>     >
>     >     The content of the files and the scripts has to be embedded in
>     the XML
>     >     document that you send to the RESTAPI server, there is no way
>     to send an
>     >     XML document and a separate file. Building this kind of XML
>     document
>     >     shouldn't be complicated if you use the CDATA syntax like in
>     the example
>     >     above.
>     >
>     >     Another thing that you may consider, if you have to run a
>     complicated
>     >     script or set of scripts, is to have them pre-installed in the
>     template
>     >     that you use to create the VM, then you just need to run them.
>     >
>     >     > --
>     >     > Regards
>     >     > Shanil
>     >     >
>     >     >
>     >     > On Tue, Sep 2, 2014 at 3:02 PM, Shanil S
>     <xielesshanil at gmail.com <mailto:xielesshanil at gmail.com>
>     >     <mailto:xielesshanil at gmail.com <mailto:xielesshanil at gmail.com>>
>     >     > <mailto:xielesshanil at gmail.com
>     <mailto:xielesshanil at gmail.com> <mailto:xielesshanil at gmail.com
>     <mailto:xielesshanil at gmail.com>>>>
>     >     wrote:
>     >     >
>     >     >     Hi Juan,
>     >     >
>     >     >     Thanks for the solution.
>     >     >
>     >     >     --
>     >     >     Regards
>     >     >     Shanil
>     >     >
>     >     >
>     >     >     On Tue, Sep 2, 2014 at 2:59 PM, Juan Hernandez
>     >     <jhernand at redhat.com <mailto:jhernand at redhat.com>
>     <mailto:jhernand at redhat.com <mailto:jhernand at redhat.com>>
>     >     >     <mailto:jhernand at redhat.com <mailto:jhernand at redhat.com>
>     <mailto:jhernand at redhat.com <mailto:jhernand at redhat.com>>>> wrote:
>     >     >
>     >     >         On 09/02/2014 05:46 AM, Shanil S wrote:
>     >     >         > Hi All,
>     >     >         >
>     >     >         > From the ovirt admin panel, there is an option to
>     give the
>     >     >         custom script
>     >     >         > in the cloud init ( Run Once-> cloud init- > Custom
>     >     script ).
>     >     >         Is there
>     >     >         > any ways to pass the custom script to cloud init
>     using the
>     >     >         rest api ?
>     >     >         >
>     >     >
>     >     >         The custom script should go inside the
>     "custom_script" element
>     >     >         inside
>     >     >         the "initialization" element:
>     >     >
>     >     >           <action>
>     >     >             ...
>     >     >             <initialization>
>     >     >               <cloud_init>...</cloud_init>
>     >     >               <custom_script>your custom script</custom_script>
>     >     >             </initialization>
>     >     >           </action>
>     >     >
> 

-- 
Dirección Comercial: C/Jose Bardasano Baos, 9, Edif. Gorbea 3, planta
3ºD, 28016 Madrid, Spain
Inscrita en el Reg. Mercantil de Madrid – C.I.F. B82657941 - Red Hat S.L.



More information about the Users mailing list