iPXE discussion forum

Full Version: python & ipxe - syntax errors
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am setting up a new infrastructure. Previous one used mod_python with the following script to handle some python work and then pass it to ipxe. On the new setup I am using mod_wsgi and I can not seem to get it to work.

import requests, json, cgi

req.headers_out['Content-Type'] = 'text/plain'
req.content_type = req.headers_out['Content-Type']

ip = ""
default = ""
build = False

mac = cgi.parse_qs(req.args)["mac"][0]

r = requests.get('http://puppetnext.lab.beer.town/api/v2/hosts?search=has_mac=' + mac, auth=('user', 'password'), verify=False)
resultjson = r.json()["results"]
if len(resultjson) > 0:
        hostid = resultjson[0]["id"]

        hostr = requests.get('http://puppetnext.lab.beer.town/api/v2/hosts/' + str(hostid), auth=('user', 'password'), verify=False)
        ip = hostr.json()["ip"]
        build = hostr.json()["build"]

if ip:
        if build:
                default = "build"
                default = "local"
        default = "discovery"


menu Boot options for <% req.write(mac)%>
item --key l local              Continue with local boot
item --key d discovery          Boot foreman discovery image
item --key b build              Build local host <% req.write(ip)%>
choose --default <% req.write(default)%> --timeout 5000 target && goto ${target}


# WORKING!!!!!!!!!!!
kernel boot/fdi-image/vmlinuz0
initrd boot/fdi-image/initrd0.img
imgargs vmlinuz0 rootflags=loop root=live:/fdi.iso rootfstype=auto ro rd.live.image acpi=force rd.luks=0 rd.md=0 rd.dm=0 rd.lvm=0 rd.bootif=0 rd.neednet=0 rd.debug=1 nomodeset BOOTIF=01-${net0/mac:hexhyp} proxy.url=http://<% req.write(req.hostname)%> proxy.type=foreman
#echo Information
#read void
echo Now Booting..
boot || goto failed
#iappend 2 for pxelinux is BOOTIF=01-${net0/mac:hexhyp} for iPXE

#kernel http://<% req.write(req.hostname)%>:81/boot/fdi-image/vmlinuz0 rootflags=loop root=live:/fdi.iso rootfstype=auto ro rd.live.image acpi=force rd.luks=0 rd.md=0 rd.dm=0 rd.lvm=0 rd.bootif=0 rd.neednet=0 nomodeset proxy.url=https://<% req.write(req.hostname)%> proxy.type=foreman
#initrd http://<% req.write(req.hostname)%>:81/boot/fdi-image/initrd0.img

chain http://<% req.write(req.hostname)%>/unattended/iPXE?mac=<% req.write(mac) %>

Before, this would execute the python script and then set the correct settings for the following ipxe script. Now I just seem to get python syntax errors.

Any idea whats going on here?
If I access it via boot.py:
[Thu Jan 04 08:40:31.583620 2018] [:error] [pid 11518] [client] mod_wsgi (pid=11518): Exception occurred processing WSGI script '/usr/foremanipxe/boot.py'., referer: http://puppetnext.lab.beer.town:81/
[Thu Jan 04 08:40:31.583661 2018] [:error] [pid 11518] [client]   File "/usr/foremanipxe/boot.py", line 1, referer: http://puppetnext.lab.beer.town:81/
[Thu Jan 04 08:40:31.583671 2018] [:error] [pid 11518] [client]     <%, referer: http://puppetnext.lab.beer.town:81/
[Thu Jan 04 08:40:31.583674 2018] [:error] [pid 11518] [client]     ^, referer: http://puppetnext.lab.beer.town:81/
[Thu Jan 04 08:40:31.583677 2018] [:error] [pid 11518] [client] SyntaxError: invalid syntax, referer: http://puppetnext.lab.beer.town:81/

If I access it via just boot:
[Thu Jan 04 08:46:24.773479 2018] [authz_core:debug] [pid 11522] mod_authz_core.c(835): [client] AH01628: authorization result: granted (no directives)
[Thu Jan 04 08:46:24.780514 2018] [:info] [pid 11522] [client] mod_wsgi (pid=11522, process='', application='puppet.lab.beer.town:81|/boot'): Loading WSGI script '/usr/foremanipxe/boot.py'.
[Thu Jan 04 08:46:24.780771 2018] [:error] [pid 11522] [client] mod_wsgi (pid=11522, process='', application='puppet.lab.beer.town:81|/boot'): Failed to parse WSGI script file '/usr/foremanipxe/boot.py'.
[Thu Jan 04 08:46:24.780789 2018] [:error] [pid 11522] [client] mod_wsgi (pid=11522): Exception occurred processing WSGI script '/usr/foremanipxe/boot.py'.
[Thu Jan 04 08:46:24.780830 2018] [:error] [pid 11522] [client]   File "/usr/foremanipxe/boot.py", line 1
[Thu Jan 04 08:46:24.780840 2018] [:error] [pid 11522] [client]     <%
[Thu Jan 04 08:46:24.780843 2018] [:error] [pid 11522] [client]     ^
[Thu Jan 04 08:46:24.780846 2018] [:error] [pid 11522] [client] SyntaxError: invalid syntax

WSGIScriptAlias /boot /usr/foremanipxe/boot.py
<VirtualHost *:81>
  ServerAdmin non@none.com
  ServerName  puppet.lab.beer.town
  ServerAlias puppet
  DocumentRoot /usr/foremanipxe
  LogLevel Debug
  ErrorLog /var/log/httpd/ipxe_error.log
  CustomLog /var/log/httpd/ipxe_access.log  Combined
  <Directory /usr/foremanipxe >
     Options Indexes FollowSymLinks MultiViews ExecCGI
     AllowOverride None
     Order allow,deny
     allow from all
I think you would get much better help with this in a forum for wsgi, or maybe python then here, since it's not iPXE specific but rather generic to wsgi, something that I don't know anything about (more then what I have read the last couple of minutes)

It seems that for wsgi you need to rewrite your script to have functions that generate the data, which probably will be quite a bit of work, what I have found so far is this: https://www.python.org/dev/peps/pep-3333...ework-side
As a rule of thumb I always suggest trying out the URL you're trying to get working in iPXE in a web browser. That usually helps you to figure out exactly what is going on and why things are not working. In this particular situation it seems like your Apache configuration is not treating the python code as code, but something else. As NiKiZe mentions, this is a Apache/Python issue that you're probably getting better help for in a Python/Apache forum. The iPXE script code that you want to emit from this python code can be considered just plain-text from the perspective of Apache/Python, which should simplify everything when talking to the Python community.

UPDATE: When I look more closely at your Apache configuration, it seems like you're actually using ExecCGI to run the code (a traditional CGI script), meaning you use mod_cgid, most likely, to execute it. If that is the case, the file should be executable, it should start with #!/usr/bin/env python as the first line of the script and not the <% line at start and the %> at the end of the file.
Thanks both of you. I have since gone back to mod_python. I gave up trying to get wsgi to execute/compile.

Figured I would try the ipxe forum, as every other apache/python forum I went to had no clue on how python + ipxe worked. Figured I would try it from an angle of someone that might have got ipxe and python scripts working. Day 6 straight of this issue, had to try somewhere and this forum has been very helpful in the past.

As Robin mentioned, treat the iPXE script as just text, because that is what it is, and that way "it's not related to iPXE"

Just say that you want the browser to display the #!ipxe as the first line and that it's just text and I'm sure you will be able to get more help from any python forum.
Reference URL's