<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Nov 6, 2014 at 10:21 AM, Juan Hernandez <span dir="ltr">&lt;<a href="mailto:jhernand@redhat.com" target="_blank">jhernand@redhat.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 11/06/2014 11:06 AM, Branimir Pejakovic wrote:<br>
&gt;<br>
&gt; On Mon, Nov 3, 2014 at 8:52 PM, Branimir Pejakovic &lt;<a href="mailto:branimirp@gmail.com">branimirp@gmail.com</a><br>
</span><span class="">&gt; &lt;mailto:<a href="mailto:branimirp@gmail.com">branimirp@gmail.com</a>&gt;&gt; wrote:<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;     On Fri, Oct 31, 2014 at 11:00 PM, Juan Hernandez<br>
</span><span class="">&gt;     &lt;<a href="mailto:jhernand@redhat.com">jhernand@redhat.com</a> &lt;mailto:<a href="mailto:jhernand@redhat.com">jhernand@redhat.com</a>&gt;&gt; wrote:<br>
&gt;<br>
&gt;         On 10/31/2014 06:47 PM, Branimir Pejakovic wrote:<br>
&gt;         &gt;<br>
&gt;         &gt;<br>
&gt;         &gt;<br>
&gt;         &gt; On Thu, Oct 30, 2014 at 12:52 PM, Branimir Pejakovic<br>
&gt;         &gt; &lt;<a href="mailto:branimirp@gmail.com">branimirp@gmail.com</a> &lt;mailto:<a href="mailto:branimirp@gmail.com">branimirp@gmail.com</a>&gt;<br>
</span><span class="">&gt;         &lt;mailto:<a href="mailto:branimirp@gmail.com">branimirp@gmail.com</a> &lt;mailto:<a href="mailto:branimirp@gmail.com">branimirp@gmail.com</a>&gt;&gt;&gt; wrote:<br>
&gt;         &gt;<br>
&gt;         &gt;<br>
&gt;         &gt;<br>
&gt;         &gt;     On Thu, Oct 30, 2014 at 12:39 PM, Shahar Havivi &lt;<a href="mailto:shaharh@redhat.com">shaharh@redhat.com</a> &lt;mailto:<a href="mailto:shaharh@redhat.com">shaharh@redhat.com</a>&gt;<br>
</span><span class="">&gt;         &gt;     &lt;mailto:<a href="mailto:shaharh@redhat.com">shaharh@redhat.com</a> &lt;mailto:<a href="mailto:shaharh@redhat.com">shaharh@redhat.com</a>&gt;&gt;&gt; wrote:<br>
&gt;         &gt;<br>
&gt;         &gt;         On 30.10.14 12:30, Branimir Pejakovic wrote:<br>
&gt;         &gt;         &gt; On Thu, Oct 30, 2014 at 12:02 PM, Shahar Havivi<br>
&gt;         &gt;         &lt;<a href="mailto:shaharh@redhat.com">shaharh@redhat.com</a> &lt;mailto:<a href="mailto:shaharh@redhat.com">shaharh@redhat.com</a>&gt;<br>
</span><span class="">&gt;         &lt;mailto:<a href="mailto:shaharh@redhat.com">shaharh@redhat.com</a> &lt;mailto:<a href="mailto:shaharh@redhat.com">shaharh@redhat.com</a>&gt;&gt;&gt; wrote:<br>
&gt;         &gt;         &gt;<br>
&gt;         &gt;         &gt; &gt; On 30.10.14 10:06, Branimir Pejakovic wrote:<br>
&gt;         &gt;         &gt; &gt; &gt; On Thu, Oct 30, 2014 at 7:22 AM, Shahar Havivi<br>
&gt;         &gt;         &lt;<a href="mailto:shaharh@redhat.com">shaharh@redhat.com</a> &lt;mailto:<a href="mailto:shaharh@redhat.com">shaharh@redhat.com</a>&gt;<br>
</span>&gt;         &lt;mailto:<a href="mailto:shaharh@redhat.com">shaharh@redhat.com</a> &lt;mailto:<a href="mailto:shaharh@redhat.com">shaharh@redhat.com</a>&gt;&gt;&gt;<br>
<div><div class="h5">&gt;         &gt;         &gt; &gt; wrote:<br>
&gt;         &gt;         &gt; &gt; &gt;<br>
&gt;         &gt;         &gt; &gt; &gt; &gt; On 29.10.14 17:35, Branimir Pejakovic wrote:<br>
&gt;         &gt;         &gt; &gt; &gt; &gt; &gt; Hi list,<br>
&gt;         &gt;         &gt; &gt; &gt; &gt; &gt;<br>
&gt;         &gt;         &gt; &gt; &gt; &gt; &gt; I was wondering is there a way to add<br>
&gt;         multiple files<br>
&gt;         &gt;         to a vm via<br>
&gt;         &gt;         &gt; &gt; &gt; &gt; cloud-init<br>
&gt;         &gt;         &gt; &gt; &gt; &gt; &gt; using python SDK. I was able to add<br>
&gt;         /etc/resolv.conf and<br>
&gt;         &gt;         &gt; &gt; additionally I<br>
&gt;         &gt;         &gt; &gt; &gt; &gt; &gt; would like to add config files for NICs<br>
&gt;         (ifcfg-ethX)<br>
&gt;         &gt;         with appropriate<br>
&gt;         &gt;         &gt; &gt; &gt; &gt; &gt; values (ip,gw,netmask). I used an example<br>
&gt;         given here:<br>
&gt;         &gt;         &gt; &gt; &gt; &gt; &gt;<br>
&gt;         <a href="http://www.ovirt.org/Features/Cloud-Init_Integration" target="_blank">http://www.ovirt.org/Features/Cloud-Init_Integration</a> . But<br>
&gt;         &gt;         &gt; &gt; &gt; &gt; unfortunately I<br>
&gt;         &gt;         &gt; &gt; &gt; &gt; &gt; cannot do the same thing If I want to write<br>
&gt;         multiple<br>
&gt;         &gt;         files to a vm in<br>
&gt;         &gt;         &gt; &gt; &gt; &gt; &gt; params.Action class.<br>
&gt;         &gt;         &gt; &gt; &gt; &gt; &gt;<br>
&gt;         &gt;         &gt; &gt; &gt; &gt; &gt; Thank you in advance!<br>
&gt;         &gt;         &gt; &gt; &gt; &gt; you can use the custom script text box in oVirts<br>
&gt;         &gt;         cloud-init section,<br>
&gt;         &gt;         &gt; &gt; &gt; &gt; there you can add any cloud-init section -<br>
&gt;         adding files<br>
&gt;         &gt;         examples can be<br>
&gt;         &gt;         &gt; &gt; &gt; &gt; found<br>
&gt;         &gt;         &gt; &gt; &gt; &gt; here:<br>
&gt;         &gt;         &gt; &gt; &gt; &gt;<br>
&gt;         &gt;         &gt; &gt; &gt; &gt;<br>
&gt;         &gt;         &gt; &gt;<br>
&gt;         &gt;<br>
&gt;          <a href="http://cloudinit.readthedocs.org/en/latest/topics/examples.html#writing-out-arbitrary-files" target="_blank">http://cloudinit.readthedocs.org/en/latest/topics/examples.html#writing-out-arbitrary-files</a><br>
&gt;         &gt;         &gt; &gt; &gt; &gt;<br>
&gt;         &gt;         &gt; &gt; &gt; &gt;   Shahar Havivi.<br>
&gt;         &gt;         &gt; &gt; &gt; &gt;<br>
&gt;         &gt;         &gt; &gt; &gt; &gt;<br>
&gt;         &gt;         &gt; &gt; &gt; Hi Shahar,<br>
&gt;         &gt;         &gt; &gt; &gt;<br>
&gt;         &gt;         &gt; &gt; &gt; Thank you for the reply.<br>
&gt;         &gt;         &gt; &gt; &gt;<br>
&gt;         &gt;         &gt; &gt; &gt; To be honest I knew for those scripts but I want<br>
&gt;         to do it<br>
&gt;         &gt;         in automate<br>
&gt;         &gt;         &gt; &gt; &gt; fashion just like in that example that I put in my<br>
&gt;         &gt;         original message. I<br>
&gt;         &gt;         &gt; &gt; have<br>
&gt;         &gt;         &gt; &gt; &gt; tried to specify in params.Files class multiple<br>
&gt;         files:<br>
&gt;         &gt;         &gt; &gt; &gt;<br>
&gt;         &gt;         &gt; &gt; &gt; vm_file1 = params.Files(file=[params.File(name =<br>
&gt;         &#39;file1&#39;,<br>
&gt;         &gt;         content =<br>
&gt;         &gt;         &gt; &gt; &gt; file1_content, type_= &#39;PLAINTEXT&#39;)])<br>
&gt;         &gt;         &gt; &gt; &gt; vm_file2 = params.Files(file=[params.File(name =<br>
&gt;         &#39;file2&#39;,<br>
&gt;         &gt;         content =<br>
&gt;         &gt;         &gt; &gt; &gt; file2_content, type_= &#39;PLAINTEXT&#39;)])<br>
&gt;         &gt;         &gt; &gt; &gt;<br>
&gt;         &gt;         &gt; &gt; &gt; and then give them as arguments to<br>
&gt;         params.CloudInit _init_<br>
&gt;         &gt;         constructor<br>
&gt;         &gt;         &gt; &gt; &gt; (files=...)  just like in that example above but<br>
&gt;         it seems<br>
&gt;         &gt;         that I am doing<br>
&gt;         &gt;         &gt; &gt; &gt; something wrong because it does not write<br>
&gt;         multiple files -<br>
&gt;         &gt;         it writes only<br>
&gt;         &gt;         &gt; &gt; &gt; when you specify one file all right. The<br>
&gt;         exception is<br>
&gt;         &gt;         &quot;Error: &#39;list&#39;<br>
&gt;         &gt;         &gt; &gt; object<br>
&gt;         &gt;         &gt; &gt; &gt; has no attribute &#39;export&#39;&quot; (I tried to specify<br>
&gt;         those files<br>
&gt;         &gt;         as a list -<br>
&gt;         &gt;         &gt; &gt; &gt; files=[vm_file1,vm_file2]).<br>
&gt;         &gt;         &gt; &gt; &gt;<br>
&gt;         &gt;         &gt; &gt; &gt; Thanks for any help.<br>
&gt;         &gt;         &gt; &gt; The custom script is currently the only option to<br>
&gt;         add files<br>
&gt;         &gt;         via Cloud-Init<br>
&gt;         &gt;         &gt; &gt;<br>
&gt;         &gt;         &gt; &gt; Thanks,<br>
&gt;         &gt;         &gt; &gt;   Shahar Havivi.<br>
&gt;         &gt;         &gt; &gt;<br>
&gt;         &gt;         &gt; &gt;<br>
&gt;         &gt;         &gt; Hi Shahar,<br>
&gt;         &gt;         &gt;<br>
&gt;         &gt;         &gt; Granted. But the example in python<br>
&gt;         &gt;         &gt; <a href="http://www.ovirt.org/Features/Cloud-Init_Integration" target="_blank">http://www.ovirt.org/Features/Cloud-Init_Integration</a><br>
&gt;         uses<br>
&gt;         &gt;         custom script :)<br>
&gt;         &gt;         &gt; I just want to know how to do the same thing in case<br>
&gt;         when I<br>
&gt;         &gt;         want to write<br>
&gt;         &gt;         &gt; more than one file to a VM - and yes  - all those<br>
&gt;         multiple<br>
&gt;         &gt;         files that I<br>
&gt;         &gt;         &gt; want to write are specified via custom scripts.<br>
&gt;         &gt;         &gt;<br>
&gt;         &gt;         &gt; From the link - (The &quot;Python SDK&quot; paragraph:<br>
&gt;         scontent object<br>
&gt;         &gt;         holds the<br>
&gt;         &gt;         &gt; custom script):<br>
&gt;         &gt;         &gt;<br>
&gt;         &gt;         &gt;  scontent = &quot;write_files:\n-   content: |\n<br>
&gt;         search<br>
&gt;         &gt;         &gt; <a href="http://example.com" target="_blank">example.com</a> &lt;<a href="http://example.com" target="_blank">http://example.com</a>&gt;<br>
&gt;         &lt;<a href="http://example.com" target="_blank">http://example.com</a>&gt;\n        nameserver<br>
&gt;         &gt;         10.10.10.1\n        nameserver<br>
&gt;         &gt;         &gt; 10.10.10.2\n    path: /etc/resolv.conf&quot;<br>
&gt;         &gt;         &gt;<br>
&gt;         &gt;         &gt; For example (and just for example), I want to write to<br>
&gt;         &gt;         /etc/issue and<br>
&gt;         &gt;         &gt; /etc/hosts not only /etc/resolv.conf. In essence,<br>
&gt;         when vm goes<br>
&gt;         &gt;         to run-once<br>
&gt;         &gt;         &gt; mode I want to write more than one file to a vm via<br>
&gt;         Python (using<br>
&gt;         &gt;         &gt; CloudInit) in automated fashion (and not touching<br>
&gt;         oVirt GUI at<br>
&gt;         &gt;         all).<br>
&gt;         &gt;         You don&#39;t need to use the GUI,<br>
&gt;         &gt;         the sdks initialization have custom_script as string<br>
&gt;         which is<br>
&gt;         &gt;         equivalent to<br>
&gt;         &gt;         the text box that you see in the GUI, and you can put<br>
&gt;         the same<br>
&gt;         &gt;         content there<br>
&gt;         &gt;         (ie multiple files).<br>
&gt;         &gt;         Is that sufficient enough?<br>
&gt;         &gt;<br>
&gt;         &gt;<br>
&gt;         &gt;     Hi Shahar,<br>
&gt;         &gt;<br>
&gt;         &gt;     Thanks! I will try this one. I was not aware of this feature.<br>
&gt;         &gt;     Although I can see that it was mentioned on the list (my bad).<br>
&gt;         &gt;<br>
&gt;         &gt;     Thank you for your effort! I really do appreciate it!<br>
&gt;         &gt;<br>
&gt;         &gt;<br>
&gt;         &gt;<br>
&gt;         &gt; Hi,<br>
&gt;         &gt;<br>
&gt;         &gt; Please if somebody can help me because I am pretty stuck. As I<br>
&gt;         said in<br>
&gt;         &gt; previous posts I used this Python example<br>
&gt;         &gt; <a href="http://www.ovirt.org/Features/Cloud-Init_Integration" target="_blank">http://www.ovirt.org/Features/Cloud-Init_Integration</a> to set root<br>
&gt;         &gt; password, hostname and resolv.conf. It worked without any<br>
&gt;         problems for<br>
&gt;         &gt; 3.4 oVirt version and equivalent Python SDK<br>
&gt;         &gt; (ovirt-engine-sdk-python-3.4.x.noarch).<br>
&gt;         &gt;<br>
&gt;         &gt; Then, I have tried to write through yuml scripts more conf<br>
&gt;         files than<br>
&gt;         &gt; just resolv.conf to configure guest. I also saw this post on<br>
&gt;         the list:<br>
&gt;         &gt;<br>
&gt;         <a href="http://lists.ovirt.org/pipermail/users/2014-October/028539.html" target="_blank">http://lists.ovirt.org/pipermail/users/2014-October/028539.html</a>. So<br>
&gt;         &gt; based on that, I decided to upgraded oVirt and SDK<br>
&gt;         &gt; (ovirt-engine-sdk-python-3.5.0.7-1.el6.noarch) to 3.5 version. The<br>
&gt;         &gt; upgrade itself passed without any problem.<br>
&gt;         &gt;<br>
&gt;         &gt; From that point forward I tried to use custom_script parameter (as<br>
&gt;         &gt; suggested by Shahar) inside Initialization class and CloudInit<br>
&gt;         class<br>
&gt;         &gt; without any luck.<br>
&gt;         &gt;<br>
&gt;         &gt; Can somebody please provide a simple Python code to submit<br>
&gt;         yaml script<br>
&gt;         &gt; to cloud-init?<br>
&gt;         &gt;<br>
&gt;         &gt; Also after the upgrade the example script I mentioned above<br>
&gt;         can only<br>
&gt;         &gt; change hostname. It does not write resolv.conf anymore nor it<br>
&gt;         sets root<br>
&gt;         &gt; password. I don&#39;t know if something is changed inside methods? For<br>
&gt;         &gt; example, customization of virtual machine (memory,cpu,high<br>
&gt;         availability,<br>
&gt;         &gt; pinning to a certain host etc works without any problems).<br>
&gt;         &gt;<br>
&gt;         &gt; Thank you!<br>
&gt;         &gt;<br>
&gt;         &gt; Best regards,<br>
&gt;         &gt;<br>
&gt;         &gt; Branimir<br>
&gt;         &gt;<br>
&gt;<br>
&gt;         The &quot;custom_script&quot; element doesn&#39;t work in combination with<br>
&gt;         cloud-init<br>
&gt;         and run once. To make it work you have to use cloud-init and a file<br>
&gt;         element containing your custom script. Here you have an example:<br>
&gt;<br>
&gt;         #!/usr/bin/python<br>
&gt;<br>
&gt;         import base64<br>
&gt;         import re<br>
&gt;<br>
&gt;         from ovirtsdk.api import API<br>
&gt;         from ovirtsdk.xml import params<br>
&gt;<br>
&gt;         # A simple function to encode using base64 and now new lines:<br>
&gt;         def encode(s):<br>
&gt;            return re.sub(&quot;\s+&quot;, &quot;&quot;, base64.encodestring(s))<br>
&gt;<br>
&gt;         # Connect to the server:<br>
&gt;         api = API(<br>
&gt;           url=&quot;<a href="https://engine35.example.com/ovirt-engine/api" target="_blank">https://engine35.example.com/ovirt-engine/api</a>&quot;,<br>
&gt;           username=&quot;admin@internal&quot;,<br>
&gt;           password=&quot;redhat123&quot;,<br>
&gt;           insecure=True,<br>
&gt;           debug=True<br>
&gt;         )<br>
&gt;<br>
&gt;         # Find the virtual machine:<br>
&gt;         myvm = api.vms.get(name=&quot;myvm&quot;)<br>
&gt;<br>
&gt;         # Prepare the cloud-init custom script to write files:<br>
&gt;         myscript = &quot;&quot;&quot;\<br>
&gt;         write_files:<br>
&gt;         &quot;&quot;&quot;<br>
&gt;<br>
&gt;         # Append one file:<br>
&gt;         myscript += &quot;&quot;&quot;\<br>
&gt;         - encoding: b64<br>
&gt;           content: %s<br>
&gt;           owner: root:root<br>
&gt;           path: /etc/firstfile.txt<br>
&gt;           permissions: &#39;0644&#39;<br>
&gt;         &quot;&quot;&quot; % encode(&quot;The content of the first file&quot;)<br>
&gt;<br>
&gt;         # Append another file:<br>
&gt;         myscript += &quot;&quot;&quot;\<br>
&gt;         - encoding: b64<br>
&gt;           content: %s<br>
&gt;           owner: root:root<br>
&gt;           path: /etc/secondfile.txt<br>
&gt;           permissions: &#39;0644&#39;<br>
&gt;         &quot;&quot;&quot; % encode(&quot;The content of the second file&quot;)<br>
&gt;<br>
&gt;         # Prepare the action to trigger initialization using cloud-init:<br>
&gt;         action = params.Action(<br>
&gt;           vm=params.VM(<br>
&gt;             initialization=params.Initialization(<br>
&gt;               cloud_init=params.CloudInit(<br>
&gt;                 files=params.Files(<br>
&gt;                   file=[<br>
&gt;                     params.File(<br>
&gt;                       name=&quot;myscript&quot;,<br>
&gt;                       type_=&quot;plaintext&quot;,<br>
&gt;                       content=myscript<br>
&gt;                     )<br>
&gt;                   ]<br>
&gt;                 )<br>
&gt;               )<br>
&gt;             )<br>
&gt;           )<br>
&gt;         )<br>
&gt;<br>
&gt;         # Start the virtual machine:<br>
&gt;         myvm.start(action)<br>
&gt;<br>
&gt;         # Disconnect from the server:<br>
&gt;         api.disconnect()<br>
&gt;<br>
&gt;         Regarding the problem with the root password we have a bug in<br>
&gt;         3.5 that<br>
&gt;         makes this fail. See here:<br>
&gt;<br>
&gt;           <a href="https://bugzilla.redhat.com/1156155" target="_blank">https://bugzilla.redhat.com/1156155</a><br>
&gt;<br>
&gt;         The problem is that we are not passing the user name to<br>
&gt;         cloud-init, and<br>
&gt;         as a result it is changing the default cloud-init user password,<br>
&gt;         not the<br>
&gt;         root pssword. To workaround the issue you can edit the<br>
&gt;         &quot;/etc/cloud/cloud.cfg&quot; file of the VM and change the default<br>
&gt;         user name:<br>
&gt;<br>
&gt;           system_info:<br>
&gt;           distro: ...<br>
&gt;           default_user:<br>
&gt;             name: root  &lt;-- Change this from &quot;fedora&quot; or &quot;cloud-init&quot; to<br>
&gt;         root<br>
&gt;<br>
&gt;         --<br>
&gt;         Dirección Comercial: C/Jose Bardasano Baos, 9, Edif. Gorbea 3,<br>
&gt;         planta<br>
&gt;         3ºD, 28016 Madrid, Spain<br>
&gt;         Inscrita en el Reg. Mercantil de Madrid – C.I.F. B82657941 - Red<br>
&gt;         Hat S.L.<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;     Hi Juan,<br>
&gt;<br>
&gt;     Sorry for the delay. Thank you very much for this. It works perfectly.<br>
&gt;<br>
&gt;     Best regards,<br>
&gt;<br>
&gt;     Branimir<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; Hi,<br>
&gt;<br>
&gt; I just want to ask one more thing - is it possible to rename virtual<br>
&gt; disk from template_disk1 to vm1_disk1. I am asking this because I create<br>
&gt; vms based on the template and every machine I create has the same disk<br>
&gt; template name.<br>
&gt;<br>
&gt; Of course, I have searched through list archives and found this:<br>
&gt;<br>
&gt; <a href="http://lists.ovirt.org/pipermail/users/2014-June/024993.html" target="_blank">http://lists.ovirt.org/pipermail/users/2014-June/024993.html</a><br>
&gt; <a href="https://bugzilla.redhat.com/show_bug.cgi?id=1105887" target="_blank">https://bugzilla.redhat.com/show_bug.cgi?id=1105887</a><br>
&gt;<br>
&gt; According to bugzilla this feature is targeted for 3.6 release. Is there<br>
&gt; any workaround maybe for this in 3.5 in Python SDK?<br>
&gt;<br>
&gt; Thank you!<br>
&gt;<br>
&gt; Best regards,<br>
&gt;<br>
&gt; Branimir<br>
&gt;<br>
<br>
</div></div>You can rename the disks after the virtual machine is created:<br>
<br>
vm = api.vms.get(name=&quot;myvm&quot;)<br>
index = 0<br>
for disk in vm.disks.list():<br>
    disk.set_alias(&quot;myvm_disk_%d&quot; % index)<br>
    disk.update()<br>
    index += 1<div dir="ltr"><div><br></div></div></blockquote><div><br><br><br></div><div>Hi Juan,<br><br><br></div><div>Thanks a lot! Works perfectly!<br><br></div><div>Branimir<br></div><div> <br></div></div>
</div></div>