how to load 3rd party lua libs (installed via luarocks) into haproxy
Asked Answered
Q

1

4

I'm writing lua script that is going to run inside HAproxy using it's Lua API.

My script is using socket package which I want to install on my machine.

Currently I'm running inside docker and my dockerfile looks like this:

FROM haproxy:1.7

RUN apt-get update -y &&  apt-get install curl luarocks -y
RUN luarocks install luasocket

EXPOSE 80 9000

COPY 500error.json.http /etc/haproxy/errorfiles/
COPY hello_world.lua /etc/haproxy/scripts/

my script have the next line:

local http = require('socket.http')

which works okay when running lua interpreter but not when running haproxy:

[ALERT] 298/104833 (8) : parsing [/usr/local/etc/haproxy/haproxy.cfg:5] : lua runtime error: /etc/haproxy/scripts/hello_world.lua:1: module 'socket.http' not found:

How should I load this correctly to haproxy?

Quarter answered 5/11, 2017 at 16:40 Comment(1)
While this isn't the cause of the error, you may subsequently find that you are not be able to use socket.http with HAProxy, if it performs any blocking operations. It has been a while since I did any intense Lua programming with HAProxy, but I remember doing remote HTTP requests "by hand," using HAProxy's built in Lua sockets capability to send raw HTTP requests built by my code, and parsing the responses with regexes, and I had to use hard-coded IP addresses since I no asynchronous DNS capability. YMMV.Misappropriate
H
1

You can print package.path and package.cpath values just before require luasocket module.

This values is where lua store paths to load a library.

Homeless answered 5/11, 2017 at 18:48 Comment(1)
Ah... the implication being that the version of Lua compiled into HAproxy may be looking somewhere different from where the default local Lua interpreter is looking. With HAProxy you might need write these to syslog to see the output, e.g. core.Alert("package search path is " .. package.path);Misappropriate

© 2022 - 2024 — McMap. All rights reserved.