Re: [PATCH] MAJOR: contrib: porting spoa_server to support python3

2020-05-11 Thread Gilchrist DADAGLO
Hi,
Thank you for the fast merge Willy and Thierry.
I will keep this process in mind for any future contribution.

Cheers,
Gilchrist

On Mon, May 11, 2020, 11:01 Willy Tarreau  wrote:

> On Mon, May 11, 2020 at 10:32:27AM +0200, Thierry Fournier wrote:
> > Hi, thanks Willy,
> >
> > This a good idea to support python3.
> >
> > In facts, python2 is no longer supported, but it is not ready to
> disappear
> > because a lot of scripts in many enterprises are written in python2
> > (exemple: Centos7/RHEL7 the package manager yum is written in python2).
> >
> > In other way the spoe-server is newer and I guess it is not so used, so
> > I should be a good idea to move to python3.
> >
> > So, its ok for me.
>
> Thanks for the quick response. So that's now merged :-)
>
> Gilchrist, thank you for this really clean work and the extensive
> tests you've run on it.
>
> If we notice there's more contribution to this code, maybe we'll have to
> push one step further like we did to support many openssl versions, which
> consists in always sticking to the most recent API and emulate it for the
> old ones. In your case that would for example mean that instead of having
> this :
>
>   -  value = PyString_FromStringAndSize(args[i].name.str,
> args[i].name.len);
>   +  value = PY_STRING_FROM_STRING_AND_SIZE(args[i].name.str,
> args[i].name.len);
>
> You would switch it to "PyUnicode_FromStringAndSize" (which is the new one
> from python3) and add this in your compatibility layer to keep suppor for
> 2.7:
>
>   #define PyUnicode_FromStringAndSize  PyString_FromStringAndSize
>
> That's something that can almost never be done in a single step, as it's
> the best way to mix everything and cause breakage, but with your work
> this seems to become within reach. But given that I don't know if we
> should expect many contributions there, I don't know if it's worth
> investing more time on it!
>
> Cheers,
> Willy
>


Re: [PATCH] MAJOR: contrib: porting spoa_server to support python3

2020-05-11 Thread Willy Tarreau
On Mon, May 11, 2020 at 10:32:27AM +0200, Thierry Fournier wrote:
> Hi, thanks Willy,
> 
> This a good idea to support python3.
> 
> In facts, python2 is no longer supported, but it is not ready to disappear
> because a lot of scripts in many enterprises are written in python2
> (exemple: Centos7/RHEL7 the package manager yum is written in python2).
> 
> In other way the spoe-server is newer and I guess it is not so used, so
> I should be a good idea to move to python3.
> 
> So, its ok for me.

Thanks for the quick response. So that's now merged :-)

Gilchrist, thank you for this really clean work and the extensive
tests you've run on it.

If we notice there's more contribution to this code, maybe we'll have to
push one step further like we did to support many openssl versions, which
consists in always sticking to the most recent API and emulate it for the
old ones. In your case that would for example mean that instead of having
this :

  -  value = PyString_FromStringAndSize(args[i].name.str, args[i].name.len);
  +  value = PY_STRING_FROM_STRING_AND_SIZE(args[i].name.str, 
args[i].name.len);

You would switch it to "PyUnicode_FromStringAndSize" (which is the new one
from python3) and add this in your compatibility layer to keep suppor for
2.7:

  #define PyUnicode_FromStringAndSize  PyString_FromStringAndSize

That's something that can almost never be done in a single step, as it's
the best way to mix everything and cause breakage, but with your work
this seems to become within reach. But given that I don't know if we
should expect many contributions there, I don't know if it's worth
investing more time on it!

Cheers,
Willy



Re: [PATCH] MAJOR: contrib: porting spoa_server to support python3

2020-05-11 Thread Thierry Fournier
Hi, thanks Willy,

This a good idea to support python3.

In facts, python2 is no longer supported, but it is not ready to disappear
because a lot of scripts in many enterprises are written in python2
(exemple: Centos7/RHEL7 the package manager yum is written in python2).

In other way the spoe-server is newer and I guess it is not so used, so
I should be a good idea to move to python3.

So, its ok for me.

Thierry


> On 11 May 2020, at 10:19, Willy Tarreau  wrote:
> 
> CCing Thierry as I suspect he didn't notice it :-)
> 
> Thierry, I'm just seeking a simple instruction such as "let's merge it",
> "please give me more time" or "don't do it, it will break X or Y". I'd
> rather not miss it before 2.2 without a good reason.
> 
> Thanks,
> Willy
> 
> On Wed, May 06, 2020 at 12:25:31PM +, Gilchrist Dadaglo wrote:
>> 
>>Background:
>>Python 2 is no longer supported since January, 1st 2020 as per 
>> https://antiphishing.ozon.io/4/0@MA85Se5hVy0@Ftkk8IJL5AfiE-5CIh6PYjO-yQQskH-5ghbuD3ZQFFEUV7nusU8YNJqjq5OWm_raT4Eb1h8La3tdyS-n5cB6l_rrp3Ffo4Ol_v5uzolbbzLQtISQhFt93yhkg6noOlI1@87af154d3ed6896fbeb877900d8310458c756a70
>>The purpose of this change is to make the spoa_server contrib library
>>compatible with Python 3 to allow transition to Python 3.
>> 
>>Test Settings:
>>ps_python.py:
>>...
>>spoa.set_var_null("null", spoa.scope_txn)
>>spoa.set_var_boolean("boolean", spoa.scope_txn, True)
>>spoa.set_var_int32("int32", spoa.scope_txn, 1234)
>>spoa.set_var_uint32("uint32", spoa.scope_txn, 1234)
>>spoa.set_var_int64("int64", spoa.scope_txn, 1234)
>>spoa.set_var_uint64("uint64", spoa.scope_txn, 1234)
>>spoa.set_var_ipv4("ipv4", spoa.scope_txn, 
>> ipaddress.IPv4Address(u"127.0.0.1"))
>>spoa.set_var_ipv6("ipv6", spoa.scope_txn, 
>> ipaddress.IPv6Address(u"1::f"))
>>spoa.set_var_str("str", spoa.scope_txn, "1::f")
>>spoa.set_var_bin("bin", spoa.scope_txn, "1:\x01:\x42f\x63\x63")
>>spoa.set_var_str("python_version", spoa.scope_sess, 
>> str(sys.version_info))
>>...
>>haproxy.cfg:
>>...
>>http-request capture var(txn.verb.null),debug len 1
>>http-request capture var(txn.verb.boolean),debug len 1
>>http-request capture var(txn.verb.int32),debug len 4
>>http-request capture var(txn.verb.uint32),debug len 4
>>http-request capture var(txn.verb.int64),debug len 4
>>http-request capture var(txn.verb.uint64),debug len 4
>>http-request capture var(txn.verb.ipv4),debug len 16
>>http-request capture var(txn.verb.ipv6),debug len 45
>>http-request capture var(txn.verb.str),debug len 32
>>http-request capture var(txn.verb.bin),debug len 32
>>http-request capture var(sess.verb.python_version),debug len 100
>>...
>> 
>>Test result:
>>Python 3.8:
>>ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR-- 
>> 1/1/0/0/0 0/0 
>> {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=3,
>>  minor=8, micro=1, releaselevel='final', serial=0)} "POST / HTTP/1.1"
>>Python 3.7:
>>ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR-- 
>> 1/1/0/0/0 0/0 
>> {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=3,
>>  minor=7, micro=6, releaselevel='final', serial=0)} "POST / HTTP/1.1"
>>Python 3.6:
>>ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR-- 
>> 1/1/0/0/0 0/0 
>> {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=3,
>>  minor=6, micro, releaselevel='final', serial=0)} "POST / HTTP/1.1"
>>Python 2.7:
>>ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR-- 
>> 1/1/0/0/0 0/0 
>> {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=2,
>>  minor=7, micro, releaselevel='final', serial=0)} "POST / HTTP/1.1"
>> 
>>Not tested:
>>Python <2.7
>> ---
>> haproxy/contrib/spoa_server/Makefile|  37 +
>> haproxy/contrib/spoa_server/README  |  10 +-
>> haproxy/contrib/spoa_server/ps_python.c | 179 +++-
>> haproxy/contrib/spoa_server/ps_python.h |  52 +++
>> 4 files changed, 241 insertions(+), 37 deletions(-)
>> create mode 100644 haproxy/contrib/spoa_server/ps_python.h
>> 
> 
>> diff --git a/haproxy/contrib/spoa_server/Makefile 
>> b/haproxy/contrib/spoa_server/Makefile
>> index f075282..e7b20db 100644
>> --- a/haproxy/contrib/spoa_server/Makefile
>> +++ b/haproxy/contrib/spoa_server/Makefile
>> @@ -23,10 +23,47 @@ endif
>> 
>> ifneq ($(USE_PYTHON),)
>> OBJS + ps_python.o
>> +
>> +# "--embed" flag is supported (and required) only from python 3.8+
>> +check_python_config : $(shell if python3-config --embed; then echo 
>> "python3.8+"; \
>> +elif hash python3-config; then echo "python3"; \
>> +elif hash python-config; then echo "python2"; fi)
>> +
>> +ifeq ($(check_python_config), python3.8+)
>> 

Re: [PATCH] MAJOR: contrib: porting spoa_server to support python3

2020-05-11 Thread Willy Tarreau
CCing Thierry as I suspect he didn't notice it :-)

Thierry, I'm just seeking a simple instruction such as "let's merge it",
"please give me more time" or "don't do it, it will break X or Y". I'd
rather not miss it before 2.2 without a good reason.

Thanks,
Willy

On Wed, May 06, 2020 at 12:25:31PM +, Gilchrist Dadaglo wrote:
> 
> Background:
> Python 2 is no longer supported since January, 1st 2020 as per 
> https://www.python.org/doc/sunset-python-2/
> The purpose of this change is to make the spoa_server contrib library
> compatible with Python 3 to allow transition to Python 3.
> 
> Test Settings:
> ps_python.py:
> ...
> spoa.set_var_null("null", spoa.scope_txn)
> spoa.set_var_boolean("boolean", spoa.scope_txn, True)
> spoa.set_var_int32("int32", spoa.scope_txn, 1234)
> spoa.set_var_uint32("uint32", spoa.scope_txn, 1234)
> spoa.set_var_int64("int64", spoa.scope_txn, 1234)
> spoa.set_var_uint64("uint64", spoa.scope_txn, 1234)
> spoa.set_var_ipv4("ipv4", spoa.scope_txn, 
> ipaddress.IPv4Address(u"127.0.0.1"))
> spoa.set_var_ipv6("ipv6", spoa.scope_txn, 
> ipaddress.IPv6Address(u"1::f"))
> spoa.set_var_str("str", spoa.scope_txn, "1::f")
> spoa.set_var_bin("bin", spoa.scope_txn, "1:\x01:\x42f\x63\x63")
> spoa.set_var_str("python_version", spoa.scope_sess, 
> str(sys.version_info))
> ...
> haproxy.cfg:
> ...
> http-request capture var(txn.verb.null),debug len 1
> http-request capture var(txn.verb.boolean),debug len 1
> http-request capture var(txn.verb.int32),debug len 4
> http-request capture var(txn.verb.uint32),debug len 4
> http-request capture var(txn.verb.int64),debug len 4
> http-request capture var(txn.verb.uint64),debug len 4
> http-request capture var(txn.verb.ipv4),debug len 16
> http-request capture var(txn.verb.ipv6),debug len 45
> http-request capture var(txn.verb.str),debug len 32
> http-request capture var(txn.verb.bin),debug len 32
> http-request capture var(sess.verb.python_version),debug len 100
> ...
> 
> Test result:
> Python 3.8:
> ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR-- 
> 1/1/0/0/0 0/0 
> {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=3,
>  minor=8, micro=1, releaselevel='final', serial=0)} "POST / HTTP/1.1"
> Python 3.7:
> ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR-- 
> 1/1/0/0/0 0/0 
> {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=3,
>  minor=7, micro=6, releaselevel='final', serial=0)} "POST / HTTP/1.1"
> Python 3.6:
> ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR-- 
> 1/1/0/0/0 0/0 
> {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=3,
>  minor=6, micro=10, releaselevel='final', serial=0)} "POST / HTTP/1.1"
> Python 2.7:
> ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR-- 
> 1/1/0/0/0 0/0 
> {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=2,
>  minor=7, micro=17, releaselevel='final', serial=0)} "POST / HTTP/1.1"
> 
> Not tested:
> Python <2.7
> ---
>  haproxy/contrib/spoa_server/Makefile|  37 +
>  haproxy/contrib/spoa_server/README  |  10 +-
>  haproxy/contrib/spoa_server/ps_python.c | 179 +++-
>  haproxy/contrib/spoa_server/ps_python.h |  52 +++
>  4 files changed, 241 insertions(+), 37 deletions(-)
>  create mode 100644 haproxy/contrib/spoa_server/ps_python.h
> 

> diff --git a/haproxy/contrib/spoa_server/Makefile 
> b/haproxy/contrib/spoa_server/Makefile
> index f075282..e7b20db 100644
> --- a/haproxy/contrib/spoa_server/Makefile
> +++ b/haproxy/contrib/spoa_server/Makefile
> @@ -23,10 +23,47 @@ endif
>  
>  ifneq ($(USE_PYTHON),)
>  OBJS += ps_python.o
> +
> +# "--embed" flag is supported (and required) only from python 3.8+
> +check_python_config := $(shell if python3-config --embed; then echo 
> "python3.8+"; \
> +elif hash python3-config; then echo "python3"; \
> +elif hash python-config; then echo "python2"; fi)
> +
> +ifeq ($(check_python_config), python3.8+)
> +PYTHON_DEFAULT_INC := $(shell python3-config --includes)
> +PYTHON_DEFAULT_LIB := $(shell python3-config --libs --embed)
> +else ifeq ($(check_python_config), python3)
> +PYTHON_DEFAULT_INC := $(shell python3-config --includes)
> +PYTHON_DEFAULT_LIB := $(shell python3-config --libs)
> +else ifeq ($(check_python_config), python2)
> +PYTHON_DEFAULT_INC := $(shell python-config --includes)
> +PYTHON_DEFAULT_LIB := $(shell python-config --libs)
> +endif
> +
> +
> +# Add default path
> +ifneq ($(PYTHON_DEFAULT_INC),)
> +CFLAGS += $(PYTHON_DEFAULT_INC)
> +else
>  CFLAGS += -I/usr/include/python2.7
> +endif
> +ifneq ($(PYTHON_DEFAULT_LIB),)
> +LDLIBS += $(PYTHON_DEFAULT_LIB)
> +else
>  LDLIBS += 

Re: [PATCH] MAJOR: contrib: porting spoa_server to support python3

2020-05-06 Thread Gilchrist DADAGLO
Hi,
It was a manual test.
I used the default example and adapted it with the parameters in "Test
settings".
"Test result" is just a copy from the logs to stdio.

I was just to make sure we exchange all data types supported by the
protocol.

This could be automated indeed but I didn't do anything for that purpose.

Gilchrist

On Wed, May 6, 2020, 16:13 Илья Шипицин  wrote:

> How did you get "test result"?
> Should we add automated test for that? For example, once a week
>
> On Wed, May 6, 2020, 5:28 PM Gilchrist Dadaglo  wrote:
>
>>
>> Background:
>> Python 2 is no longer supported since January, 1st 2020 as per
>> https://www.python.org/doc/sunset-python-2/
>> The purpose of this change is to make the spoa_server contrib
>> library
>> compatible with Python 3 to allow transition to Python 3.
>>
>> Test Settings:
>> ps_python.py:
>> ...
>> spoa.set_var_null("null", spoa.scope_txn)
>> spoa.set_var_boolean("boolean", spoa.scope_txn, True)
>> spoa.set_var_int32("int32", spoa.scope_txn, 1234)
>> spoa.set_var_uint32("uint32", spoa.scope_txn, 1234)
>> spoa.set_var_int64("int64", spoa.scope_txn, 1234)
>> spoa.set_var_uint64("uint64", spoa.scope_txn, 1234)
>> spoa.set_var_ipv4("ipv4", spoa.scope_txn,
>> ipaddress.IPv4Address(u"127.0.0.1"))
>> spoa.set_var_ipv6("ipv6", spoa.scope_txn,
>> ipaddress.IPv6Address(u"1::f"))
>> spoa.set_var_str("str", spoa.scope_txn, "1::f")
>> spoa.set_var_bin("bin", spoa.scope_txn, "1:\x01:\x42f\x63\x63")
>> spoa.set_var_str("python_version", spoa.scope_sess,
>> str(sys.version_info))
>> ...
>> haproxy.cfg:
>> ...
>> http-request capture var(txn.verb.null),debug len 1
>> http-request capture var(txn.verb.boolean),debug len 1
>> http-request capture var(txn.verb.int32),debug len 4
>> http-request capture var(txn.verb.uint32),debug len 4
>> http-request capture var(txn.verb.int64),debug len 4
>> http-request capture var(txn.verb.uint64),debug len 4
>> http-request capture var(txn.verb.ipv4),debug len 16
>> http-request capture var(txn.verb.ipv6),debug len 45
>> http-request capture var(txn.verb.str),debug len 32
>> http-request capture var(txn.verb.bin),debug len 32
>> http-request capture var(sess.verb.python_version),debug len 100
>> ...
>>
>> Test result:
>> Python 3.8:
>> ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR--
>> 1/1/0/0/0 0/0
>> {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=3,
>> minor=8, micro=1, releaselevel='final', serial=0)} "POST / HTTP/1.1"
>> Python 3.7:
>> ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR--
>> 1/1/0/0/0 0/0
>> {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=3,
>> minor=7, micro=6, releaselevel='final', serial=0)} "POST / HTTP/1.1"
>> Python 3.6:
>> ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR--
>> 1/1/0/0/0 0/0
>> {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=3,
>> minor=6, micro=10, releaselevel='final', serial=0)} "POST / HTTP/1.1"
>> Python 2.7:
>> ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR--
>> 1/1/0/0/0 0/0
>> {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=2,
>> minor=7, micro=17, releaselevel='final', serial=0)} "POST / HTTP/1.1"
>>
>> Not tested:
>> Python <2.7
>> ---
>>  haproxy/contrib/spoa_server/Makefile|  37 +
>>  haproxy/contrib/spoa_server/README  |  10 +-
>>  haproxy/contrib/spoa_server/ps_python.c | 179 +++-
>>  haproxy/contrib/spoa_server/ps_python.h |  52 +++
>>  4 files changed, 241 insertions(+), 37 deletions(-)
>>  create mode 100644 haproxy/contrib/spoa_server/ps_python.h
>>
>>


Re: [PATCH] MAJOR: contrib: porting spoa_server to support python3

2020-05-06 Thread Илья Шипицин
How did you get "test result"?
Should we add automated test for that? For example, once a week

On Wed, May 6, 2020, 5:28 PM Gilchrist Dadaglo  wrote:

>
> Background:
> Python 2 is no longer supported since January, 1st 2020 as per
> https://www.python.org/doc/sunset-python-2/
> The purpose of this change is to make the spoa_server contrib
> library
> compatible with Python 3 to allow transition to Python 3.
>
> Test Settings:
> ps_python.py:
> ...
> spoa.set_var_null("null", spoa.scope_txn)
> spoa.set_var_boolean("boolean", spoa.scope_txn, True)
> spoa.set_var_int32("int32", spoa.scope_txn, 1234)
> spoa.set_var_uint32("uint32", spoa.scope_txn, 1234)
> spoa.set_var_int64("int64", spoa.scope_txn, 1234)
> spoa.set_var_uint64("uint64", spoa.scope_txn, 1234)
> spoa.set_var_ipv4("ipv4", spoa.scope_txn,
> ipaddress.IPv4Address(u"127.0.0.1"))
> spoa.set_var_ipv6("ipv6", spoa.scope_txn,
> ipaddress.IPv6Address(u"1::f"))
> spoa.set_var_str("str", spoa.scope_txn, "1::f")
> spoa.set_var_bin("bin", spoa.scope_txn, "1:\x01:\x42f\x63\x63")
> spoa.set_var_str("python_version", spoa.scope_sess,
> str(sys.version_info))
> ...
> haproxy.cfg:
> ...
> http-request capture var(txn.verb.null),debug len 1
> http-request capture var(txn.verb.boolean),debug len 1
> http-request capture var(txn.verb.int32),debug len 4
> http-request capture var(txn.verb.uint32),debug len 4
> http-request capture var(txn.verb.int64),debug len 4
> http-request capture var(txn.verb.uint64),debug len 4
> http-request capture var(txn.verb.ipv4),debug len 16
> http-request capture var(txn.verb.ipv6),debug len 45
> http-request capture var(txn.verb.str),debug len 32
> http-request capture var(txn.verb.bin),debug len 32
> http-request capture var(sess.verb.python_version),debug len 100
> ...
>
> Test result:
> Python 3.8:
> ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR--
> 1/1/0/0/0 0/0
> {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=3,
> minor=8, micro=1, releaselevel='final', serial=0)} "POST / HTTP/1.1"
> Python 3.7:
> ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR--
> 1/1/0/0/0 0/0
> {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=3,
> minor=7, micro=6, releaselevel='final', serial=0)} "POST / HTTP/1.1"
> Python 3.6:
> ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR--
> 1/1/0/0/0 0/0
> {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=3,
> minor=6, micro=10, releaselevel='final', serial=0)} "POST / HTTP/1.1"
> Python 2.7:
> ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR--
> 1/1/0/0/0 0/0
> {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=2,
> minor=7, micro=17, releaselevel='final', serial=0)} "POST / HTTP/1.1"
>
> Not tested:
> Python <2.7
> ---
>  haproxy/contrib/spoa_server/Makefile|  37 +
>  haproxy/contrib/spoa_server/README  |  10 +-
>  haproxy/contrib/spoa_server/ps_python.c | 179 +++-
>  haproxy/contrib/spoa_server/ps_python.h |  52 +++
>  4 files changed, 241 insertions(+), 37 deletions(-)
>  create mode 100644 haproxy/contrib/spoa_server/ps_python.h
>
>


[PATCH] MAJOR: contrib: porting spoa_server to support python3

2020-05-06 Thread Gilchrist Dadaglo

Background:
Python 2 is no longer supported since January, 1st 2020 as per 
https://www.python.org/doc/sunset-python-2/
The purpose of this change is to make the spoa_server contrib library
compatible with Python 3 to allow transition to Python 3.

Test Settings:
ps_python.py:
...
spoa.set_var_null("null", spoa.scope_txn)
spoa.set_var_boolean("boolean", spoa.scope_txn, True)
spoa.set_var_int32("int32", spoa.scope_txn, 1234)
spoa.set_var_uint32("uint32", spoa.scope_txn, 1234)
spoa.set_var_int64("int64", spoa.scope_txn, 1234)
spoa.set_var_uint64("uint64", spoa.scope_txn, 1234)
spoa.set_var_ipv4("ipv4", spoa.scope_txn, 
ipaddress.IPv4Address(u"127.0.0.1"))
spoa.set_var_ipv6("ipv6", spoa.scope_txn, 
ipaddress.IPv6Address(u"1::f"))
spoa.set_var_str("str", spoa.scope_txn, "1::f")
spoa.set_var_bin("bin", spoa.scope_txn, "1:\x01:\x42f\x63\x63")
spoa.set_var_str("python_version", spoa.scope_sess, 
str(sys.version_info))
...
haproxy.cfg:
...
http-request capture var(txn.verb.null),debug len 1
http-request capture var(txn.verb.boolean),debug len 1
http-request capture var(txn.verb.int32),debug len 4
http-request capture var(txn.verb.uint32),debug len 4
http-request capture var(txn.verb.int64),debug len 4
http-request capture var(txn.verb.uint64),debug len 4
http-request capture var(txn.verb.ipv4),debug len 16
http-request capture var(txn.verb.ipv6),debug len 45
http-request capture var(txn.verb.str),debug len 32
http-request capture var(txn.verb.bin),debug len 32
http-request capture var(sess.verb.python_version),debug len 100
...

Test result:
Python 3.8:
ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR-- 1/1/0/0/0 
0/0 
{|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=3,
 minor=8, micro=1, releaselevel='final', serial=0)} "POST / HTTP/1.1"
Python 3.7:
ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR-- 1/1/0/0/0 
0/0 
{|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=3,
 minor=7, micro=6, releaselevel='final', serial=0)} "POST / HTTP/1.1"
Python 3.6:
ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR-- 1/1/0/0/0 
0/0 
{|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=3,
 minor=6, micro=10, releaselevel='final', serial=0)} "POST / HTTP/1.1"
Python 2.7:
ft_public ft_public/ 0/-1/-1/-1/0 403 212 - - PR-- 1/1/0/0/0 
0/0 
{|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=2,
 minor=7, micro=17, releaselevel='final', serial=0)} "POST / HTTP/1.1"

Not tested:
Python <2.7
---
 haproxy/contrib/spoa_server/Makefile|  37 +
 haproxy/contrib/spoa_server/README  |  10 +-
 haproxy/contrib/spoa_server/ps_python.c | 179 +++-
 haproxy/contrib/spoa_server/ps_python.h |  52 +++
 4 files changed, 241 insertions(+), 37 deletions(-)
 create mode 100644 haproxy/contrib/spoa_server/ps_python.h

diff --git a/haproxy/contrib/spoa_server/Makefile b/haproxy/contrib/spoa_server/Makefile
index f075282..e7b20db 100644
--- a/haproxy/contrib/spoa_server/Makefile
+++ b/haproxy/contrib/spoa_server/Makefile
@@ -23,10 +23,47 @@ endif
 
 ifneq ($(USE_PYTHON),)
 OBJS += ps_python.o
+
+# "--embed" flag is supported (and required) only from python 3.8+
+check_python_config := $(shell if python3-config --embed; then echo "python3.8+"; \
+elif hash python3-config; then echo "python3"; \
+elif hash python-config; then echo "python2"; fi)
+
+ifeq ($(check_python_config), python3.8+)
+PYTHON_DEFAULT_INC := $(shell python3-config --includes)
+PYTHON_DEFAULT_LIB := $(shell python3-config --libs --embed)
+else ifeq ($(check_python_config), python3)
+PYTHON_DEFAULT_INC := $(shell python3-config --includes)
+PYTHON_DEFAULT_LIB := $(shell python3-config --libs)
+else ifeq ($(check_python_config), python2)
+PYTHON_DEFAULT_INC := $(shell python-config --includes)
+PYTHON_DEFAULT_LIB := $(shell python-config --libs)
+endif
+
+
+# Add default path
+ifneq ($(PYTHON_DEFAULT_INC),)
+CFLAGS += $(PYTHON_DEFAULT_INC)
+else
 CFLAGS += -I/usr/include/python2.7
+endif
+ifneq ($(PYTHON_DEFAULT_LIB),)
+LDLIBS += $(PYTHON_DEFAULT_LIB)
+else
 LDLIBS += -lpython2.7
 endif
 
+# Add user additional paths if any
+ifneq ($(PYTHON_INC),)
+CFLAGS += -I$(PYTHON_INC)
+endif
+ifneq ($(PYTHON_LIB),)
+LDLIBS += -L$(PYTHON_LIB)
+endif
+
+LDLIBS +=-Wl,--export-dynamic
+endif
+
 spoa: $(OBJS)
 	$(LD) $(LDFLAGS) -o $@ $^ $(LDLIBS)
 
diff --git a/haproxy/contrib/spoa_server/README b/haproxy/contrib/spoa_server/README
index 341f5f9..f654a23 100644
--- a/haproxy/contrib/spoa_server/README
+++ b/haproxy/contrib/spoa_server/README
@@ -14,9 +14,10 @@ is done.
 You have to install the development packages, either from the