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

Juan Hernandez jhernand at redhat.com
Thu Sep 4 09:32:22 UTC 2014


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>> 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>>>
>     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>>> 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