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