Skip to content

Commit

Permalink
Add .env file support for comm params
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusstefan committed Dec 17, 2024
1 parent afe28ee commit 5246a01
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 24 deletions.
51 changes: 39 additions & 12 deletions opensips/event/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,40 @@
import time
import signal
import argparse
import os
from opensips.mi import OpenSIPSMI
from opensips.event import OpenSIPSEventHandler, OpenSIPSEventException


def load_env_file(env_file_path):
if not os.path.isfile(env_file_path):
return
with open(env_file_path) as f:
for line in f:
if line.startswith('#') or not line.strip():
continue
key, value = line.strip().split('=', 1)
os.environ[key] = value

parser = argparse.ArgumentParser()

parser.add_argument('--env-file',
type=str,
default='.env',
help='Load environment variables from file')

communication = parser.add_argument_group('communication')

communication.add_argument('-t', '--type',
type=str,
default='fifo',
choices=['fifo', 'http', 'datagram'],
help='OpenSIPS MI Communication Type')
communication.add_argument('-i', '--ip',
type=str,
help='OpenSIPS MI IP Address',
default='127.0.0.1')
help='OpenSIPS MI IP Address')
communication.add_argument('-p', '--port',
type=int,
help='OpenSIPS MI Port',
default=8888)
help='OpenSIPS MI Port')
communication.add_argument('-f', '--fifo-file',
metavar='FIFO_FILE',
type=str,
Expand Down Expand Up @@ -94,14 +108,27 @@ def main():

args = parser.parse_args()

load_env_file(args.env_file)

if not args.type:
args.type = os.getenv('OPENSIPS_MI_TYPE', 'datagram')
if not args.ip:
args.ip = os.getenv('OPENSIPS_MI_IP', '127.0.0.1')
if not args.port:
args.port = os.getenv('OPENSIPS_MI_PORT', 8080)
if not args.fifo_file:
args.fifo_file = os.getenv('OPENSIPS_MI_FIFO_FILE', '/tmp/opensips_fifo')
if not args.fifo_fallback:
args.fifo_fallback = os.getenv('OPENSIPS_MI_FIFO_FALLBACK', '/tmp/opensips_fifo_fallback')
if not args.fifo_reply_dir:
args.fifo_reply_dir = os.getenv('OPENSIPS_MI_FIFO_REPLY_DIR', '/tmp/opensips_fifo_reply')

if args.type == 'fifo':
fifo_args = {}
if args.fifo_file:
fifo_args['fifo_file'] = args.fifo_file
if args.fifo_fallback:
fifo_args['fifo_file_fallback'] = args.fifo_fallback
if args.fifo_reply_dir:
fifo_args['fifo_reply_dir'] = args.fifo_reply_dir
fifo_args = {
'fifo_file': args.fifo_file,
'fifo_file_fallback': args.fifo_fallback,
'fifo_reply_dir': args.fifo_reply_dir,
}
mi = OpenSIPSMI('fifo', **fifo_args)
elif args.type == 'http':
mi = OpenSIPSMI('http', url=f'http://{args.ip}:{args.port}/mi')
Expand Down
54 changes: 42 additions & 12 deletions opensips/mi/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,50 @@

import sys
import json
import os
import argparse
from opensips.mi import OpenSIPSMI, OpenSIPSMIException


def load_env_file(env_file_path):
if not os.path.isfile(env_file_path):
return
with open(env_file_path) as f:
for line in f:
if line.startswith('#') or not line.strip():
continue
key, value = line.strip().split('=', 1)
os.environ[key] = value

parser = argparse.ArgumentParser()

parser.add_argument('--env-file',
type=str,
default='.env',
help='Load environment variables from file')

communication = parser.add_argument_group('communication')

communication.add_argument('-t', '--type',
type=str,
default='fifo',
choices=['fifo', 'http', 'datagram'],
help='OpenSIPS MI Communication Type')
communication.add_argument('-i', '--ip',
type=str,
help='OpenSIPS MI IP Address',
default='127.0.0.1')
help='OpenSIPS MI IP Address')
communication.add_argument('-p', '--port',
type=int,
help='OpenSIPS MI Port',
default=8888)
help='OpenSIPS MI Port')
communication.add_argument('-f', '--fifo-file',
metavar='FIFO_FILE',
type=str,
help='OpenSIPS MI FIFO File')
communication.add_argument('-fb', '--fifo-fallback',
metavar='FIFO_FALLBACK_FILE',
type=str,
help='OpenSIPS MI Fallback FIFO File')
communication.add_argument('-fd', '--fifo-reply-dir',
metavar='FIFO_DIR',
type=str,
help='OpenSIPS MI FIFO Reply Directory')

Expand Down Expand Up @@ -86,14 +103,27 @@ def main():
""" Main function of the opensips-mi script """
args = parser.parse_args()

load_env_file(args.env_file)

if not args.type:
args.type = os.getenv('OPENSIPS_MI_TYPE', 'datagram')
if not args.ip:
args.ip = os.getenv('OPENSIPS_MI_IP', '127.0.0.1')
if not args.port:
args.port = os.getenv('OPENSIPS_MI_PORT', 8080)
if not args.fifo_file:
args.fifo_file = os.getenv('OPENSIPS_MI_FIFO_FILE', '/tmp/opensips_fifo')
if not args.fifo_fallback:
args.fifo_fallback = os.getenv('OPENSIPS_MI_FIFO_FALLBACK', '/tmp/opensips_fifo_fallback')
if not args.fifo_reply_dir:
args.fifo_reply_dir = os.getenv('OPENSIPS_MI_FIFO_REPLY_DIR', '/tmp/opensips_fifo_reply')

if args.type == 'fifo':
fifo_args = {}
if args.fifo_file:
fifo_args['fifo_file'] = args.fifo_file
if args.fifo_fallback:
fifo_args['fifo_file_fallback'] = args.fifo_fallback
if args.fifo_reply_dir:
fifo_args['fifo_reply_dir'] = args.fifo_reply_dir
fifo_args = {
'fifo_file': args.fifo_file,
'fifo_file_fallback': args.fifo_fallback,
'fifo_reply_dir': args.fifo_reply_dir,
}
mi = OpenSIPSMI('fifo', **fifo_args)
elif args.type == 'http':
mi = OpenSIPSMI('http', url=f'http://{args.ip}:{args.port}/mi')
Expand Down

0 comments on commit 5246a01

Please sign in to comment.