[SR-Users] Kamailio in cloud environments

Alex Balashov abalashov at evaristesys.com
Mon Nov 11 06:43:54 CET 2019


I prefer more flexibility, and so use a slightly more ornate approach
involving the Jinja2 template engine (inspired by Jango)[1] and Python
3. I'll spread it in the network in the hope that it helps someone.

--- kamailio-settings.yaml ---
listeners:
  - proto: udp
    addr: 127.0.0.1
    port: 5060

  - proto: tcp
    addr: 172.30.105.10
    port: 5060

db_conn:
  driver: mysql
  user: kamailio_ro
  password: abc123
  host: 172.30.106.11
  name: kamailio
------------------------------

And the Kamailio config:

--- kamailio.cfg.tpl ----
#!define DB_URL "{{ db_conn['driver'] }}://{{ db_conn['user'] }}:{{ db_conn['password'] }}@{{ db_c
onn['host'] }}/{{ db_conn['name'] }}"

...

{% for l in listeners -%}
listen={{ l['proto'] }}:{{ l['addr'] }}:{{ l['port'] }}
{% endfor %}
-------------------------

And:

--- apply.py ---
#!/usr/bin/python3

import sys, os, re, yaml
from jinja2 import Template, Environment

env = Environment()
cfg_buf = None
settings = None


with open('/usr/local/etc/kamailio/kamailio.cfg.tpl', 'r') as f:
  cfg_buf = f.read()

tpl_instance = env.from_string(cfg_buf)

with open('kamailio-settings.yaml', 'r') as f:
  settings = yaml.load(f.read(), Loader=yaml.FullLoader)

finished = tpl_instance.render(settings)

with open('/usr/local/etc/kamailio/kamailio.cfg', 'w') as f:
  f.write(finished)
----------------

Then, this goes in the systemd unit:

   ExecStartPre=/usr/bin/python3 /opt/evariste/scripts/apply.py

Result:

---
#!define DB_URL "mysql://kamailio_ro:abc123@172.30.106.11/kamailio"

...

listen=udp:127.0.0.1:5060
listen=tcp:172.30.105.10:5060
---

-- Alex

[1] https://jinja.palletsprojects.com/en/2.10.x/

On Mon, Nov 11, 2019 at 08:00:46AM +0300, Sergey Safarov wrote:

> You can also update kamailio.cfg to include listeners.cfg
> Example. Need add to kamailio.cfg
> 
> ####### Listeners ##########
> include_file "/var/run/kamailio/listeners-eth0.cfg"
> 
> Before kamailio started you will generate listeners.cfg file.
> This may by done by drop-in file.
> Need create "/etc/systemd/system/kamailio.service.d/10-listners-eth0.conf"
> with content
> [Service]
> ExecStartPre=/bin/curl -s -S --output /var/run/kamailio/listeners-eth0.cfg
> http://169.254.169.254/latest/meta-data/public-ipv4
> ExecStartPre=/bin/sed --in-place --regexp-extended -e
> 's/(.*)/listen=udp:\1:5060\nlisten=tcp:\1:5060\n/'
> /var/run/kamailio/listeners-eth0.cfg
> 
> 
> 
> On Mon, Nov 11, 2019 at 2:50 AM David Villasmil <
> david.villasmil.work at gmail.com> wrote:
> 
> > That's the one, thanks @Karsten!
> >
> >
> > More info, i changed my systemd unit to execute a script when starting up,
> > like so:
> >
> > # cat /etc/systemd/system/multi-user.target.wants/kamailio.service
> > [Unit]
> > Description=Kamailio (OpenSER) - the Open Source SIP Server
> > After=network.target
> >
> > [Service]
> > Type=forking
> > Environment='CFGFILE=/etc/kamailio/kamailio.cfg'
> > Environment='SHM_MEMORY=64'
> > Environment='PKG_MEMORY=8'
> > Environment='USER=kamailio'
> > Environment='GROUP=kamailio'
> > EnvironmentFile=-/etc/default/kamailio
> > EnvironmentFile=-/etc/default/kamailio.d/*
> > # PIDFile requires a full absolute path
> > PIDFile=/var/run/kamailio/kamailio.pid
> > ExecStart=/etc/kamailio/startkam.sh
> > Restart=on-abort
> >
> > [Install]
> > WantedBy=multi-user.target
> >
> > And /etc/kamailio/startkam.sh is like:
> >
> > #!/bin/bash
> >
> > MYSQL=$(/etc/kamailio/aws-getpass.sh dev us-east-1 dburl)
> > SHM_MEMORY=64
> > PKG_MEMORY=8
> > USER=kamailio
> > GROUP=kamailio
> > CFGFILE=/etc/kamailio/kamailio.cfg
> > /usr/sbin/kamailio -P /var/run/kamailio/kamailio.pid -f $CFGFILE -m
> > $SHM_MEMORY -M $PKG_MEMORY -u $USER -g $GROUP -A "DBURL=\"$MYSQL\"" -A
> > "CBDBURL=\"cb=>$MYSQL\"" -A "ASGDBURL=\"asg=>$MYSQL\""
> >
> > which in turn calls /etc/kamailio/aws-getpass.sh, which is the actual
> > script talking to AWS via BOTO3 to get the data i need.
> >
> > You can just change the params and use them in your kam script normally
> > like:
> >
> > modparam("acc", "db_url", DBURL )
> >
> > Hope that helps.
> >
> > Regards,
> >
> > David Villasmil
> > email: david.villasmil.work at gmail.com
> > phone: +34669448337
> >
> >
> > On Sun, Nov 10, 2019 at 9:22 PM Alex Balashov <abalashov at evaristesys.com>
> > wrote:
> >
> >> -A ... oh wow. That's savvy! Cheers.
> >>
> >> On Sun, Nov 10, 2019 at 08:51:59PM +0100, Karsten Horsmann wrote:
> >>
> >> > Hi David,
> >> >
> >> > You mean this thread from may 2019.
> >> > https://lists.kamailio.org/pipermail/sr-users/2019-May/105599.html
> >> >
> >> > Pro Tipp, search on lists.kamailio.org/pipermail/ to get your link.
> >> Great
> >> > help the mailing list archive btw.
> >> >
> >> > Cheers
> >> > Karsten
> >> >
> >> > David Villasmil <david.villasmil.work at gmail.com> schrieb am So., 10.
> >> Nov.
> >> > 2019, 19:03:
> >> >
> >> > > I needed to discover the user/pass for the dB when kamailio starts so
> >> as
> >> > > not to store it locally. I get the data via an AWS script, then start
> >> > > kamailio passing the parameters, then use of those in the script. You
> >> can
> >> > > do exactly this to get and set the current ip address.
> >> > >
> >> > > I don’t know how to share a thread from this mailing list, but search
> >> for
> >> > > the subject “ define a DBURL with SLQOPS fails” and you will find all
> >> info
> >> > > there.
> >> > >
> >> > > On Sun, 10 Nov 2019 at 17:48, Alex Balashov <
> >> abalashov at evaristesys.com>
> >> > > wrote:
> >> > >
> >> > >> On Sun, Nov 10, 2019 at 06:16:44PM +0100, Alejandro Recarey wrote:
> >> > >>
> >> > >> > Hi, I am trying to get kamailio working cloud provider, autoscaling
> >> > >> > them behind a TCP load balancer.
> >> > >> >
> >> > >> > Is there a way for kamailio to discover its Public IP at startup?
> >> Or
> >> > >> > do people generally program their own startup scripts that modify
> >> the
> >> > >> > kamailio config file before starting it?
> >> > >>
> >> > >> They generally do the latter, especially since public IP discovery
> >> > >> mechanisms tend to be specific to the cloud provider.
> >> > >>
> >> > >> But various templating mechanisms make this quite easy to do.
> >> > >>
> >> > >> --
> >> > >> Alex Balashov | Principal | Evariste Systems LLC
> >> > >>
> >> > >> Tel: +1-706-510-6800 / +1-800-250-5920 (toll-free)
> >> > >> Web: http://www.evaristesys.com/, http://www.csrpswitch.com/
> >> > >>
> >> > >> _______________________________________________
> >> > >> Kamailio (SER) - Users Mailing List
> >> > >> sr-users at lists.kamailio.org
> >> > >> https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users
> >> > >>
> >> > > --
> >> > > Regards,
> >> > >
> >> > > David Villasmil
> >> > > email: david.villasmil.work at gmail.com
> >> > > phone: +34669448337
> >> > > _______________________________________________
> >> > > Kamailio (SER) - Users Mailing List
> >> > > sr-users at lists.kamailio.org
> >> > > https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users
> >> > >
> >>
> >> > _______________________________________________
> >> > Kamailio (SER) - Users Mailing List
> >> > sr-users at lists.kamailio.org
> >> > https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users
> >>
> >>
> >> --
> >> Alex Balashov | Principal | Evariste Systems LLC
> >>
> >> Tel: +1-706-510-6800 / +1-800-250-5920 (toll-free)
> >> Web: http://www.evaristesys.com/, http://www.csrpswitch.com/
> >>
> >> _______________________________________________
> >> Kamailio (SER) - Users Mailing List
> >> sr-users at lists.kamailio.org
> >> https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users
> >>
> > _______________________________________________
> > Kamailio (SER) - Users Mailing List
> > sr-users at lists.kamailio.org
> > https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users
> >

> _______________________________________________
> Kamailio (SER) - Users Mailing List
> sr-users at lists.kamailio.org
> https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users


-- 
Alex Balashov | Principal | Evariste Systems LLC

Tel: +1-706-510-6800 / +1-800-250-5920 (toll-free) 
Web: http://www.evaristesys.com/, http://www.csrpswitch.com/



More information about the sr-users mailing list