Skip to content

Commit 5246a01

Browse files
committed
Add .env file support for comm params
1 parent afe28ee commit 5246a01

File tree

2 files changed

+81
-24
lines changed

2 files changed

+81
-24
lines changed

opensips/event/__main__.py

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,40 @@
2424
import time
2525
import signal
2626
import argparse
27+
import os
2728
from opensips.mi import OpenSIPSMI
2829
from opensips.event import OpenSIPSEventHandler, OpenSIPSEventException
2930

31+
32+
def load_env_file(env_file_path):
33+
if not os.path.isfile(env_file_path):
34+
return
35+
with open(env_file_path) as f:
36+
for line in f:
37+
if line.startswith('#') or not line.strip():
38+
continue
39+
key, value = line.strip().split('=', 1)
40+
os.environ[key] = value
41+
3042
parser = argparse.ArgumentParser()
3143

44+
parser.add_argument('--env-file',
45+
type=str,
46+
default='.env',
47+
help='Load environment variables from file')
48+
3249
communication = parser.add_argument_group('communication')
3350

3451
communication.add_argument('-t', '--type',
3552
type=str,
36-
default='fifo',
3753
choices=['fifo', 'http', 'datagram'],
3854
help='OpenSIPS MI Communication Type')
3955
communication.add_argument('-i', '--ip',
4056
type=str,
41-
help='OpenSIPS MI IP Address',
42-
default='127.0.0.1')
57+
help='OpenSIPS MI IP Address')
4358
communication.add_argument('-p', '--port',
4459
type=int,
45-
help='OpenSIPS MI Port',
46-
default=8888)
60+
help='OpenSIPS MI Port')
4761
communication.add_argument('-f', '--fifo-file',
4862
metavar='FIFO_FILE',
4963
type=str,
@@ -94,14 +108,27 @@ def main():
94108

95109
args = parser.parse_args()
96110

111+
load_env_file(args.env_file)
112+
113+
if not args.type:
114+
args.type = os.getenv('OPENSIPS_MI_TYPE', 'datagram')
115+
if not args.ip:
116+
args.ip = os.getenv('OPENSIPS_MI_IP', '127.0.0.1')
117+
if not args.port:
118+
args.port = os.getenv('OPENSIPS_MI_PORT', 8080)
119+
if not args.fifo_file:
120+
args.fifo_file = os.getenv('OPENSIPS_MI_FIFO_FILE', '/tmp/opensips_fifo')
121+
if not args.fifo_fallback:
122+
args.fifo_fallback = os.getenv('OPENSIPS_MI_FIFO_FALLBACK', '/tmp/opensips_fifo_fallback')
123+
if not args.fifo_reply_dir:
124+
args.fifo_reply_dir = os.getenv('OPENSIPS_MI_FIFO_REPLY_DIR', '/tmp/opensips_fifo_reply')
125+
97126
if args.type == 'fifo':
98-
fifo_args = {}
99-
if args.fifo_file:
100-
fifo_args['fifo_file'] = args.fifo_file
101-
if args.fifo_fallback:
102-
fifo_args['fifo_file_fallback'] = args.fifo_fallback
103-
if args.fifo_reply_dir:
104-
fifo_args['fifo_reply_dir'] = args.fifo_reply_dir
127+
fifo_args = {
128+
'fifo_file': args.fifo_file,
129+
'fifo_file_fallback': args.fifo_fallback,
130+
'fifo_reply_dir': args.fifo_reply_dir,
131+
}
105132
mi = OpenSIPSMI('fifo', **fifo_args)
106133
elif args.type == 'http':
107134
mi = OpenSIPSMI('http', url=f'http://{args.ip}:{args.port}/mi')

opensips/mi/__main__.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,50 @@
2121

2222
import sys
2323
import json
24+
import os
2425
import argparse
2526
from opensips.mi import OpenSIPSMI, OpenSIPSMIException
2627

28+
29+
def load_env_file(env_file_path):
30+
if not os.path.isfile(env_file_path):
31+
return
32+
with open(env_file_path) as f:
33+
for line in f:
34+
if line.startswith('#') or not line.strip():
35+
continue
36+
key, value = line.strip().split('=', 1)
37+
os.environ[key] = value
38+
2739
parser = argparse.ArgumentParser()
2840

41+
parser.add_argument('--env-file',
42+
type=str,
43+
default='.env',
44+
help='Load environment variables from file')
45+
2946
communication = parser.add_argument_group('communication')
3047

3148
communication.add_argument('-t', '--type',
3249
type=str,
33-
default='fifo',
3450
choices=['fifo', 'http', 'datagram'],
3551
help='OpenSIPS MI Communication Type')
3652
communication.add_argument('-i', '--ip',
3753
type=str,
38-
help='OpenSIPS MI IP Address',
39-
default='127.0.0.1')
54+
help='OpenSIPS MI IP Address')
4055
communication.add_argument('-p', '--port',
4156
type=int,
42-
help='OpenSIPS MI Port',
43-
default=8888)
57+
help='OpenSIPS MI Port')
4458
communication.add_argument('-f', '--fifo-file',
59+
metavar='FIFO_FILE',
4560
type=str,
4661
help='OpenSIPS MI FIFO File')
4762
communication.add_argument('-fb', '--fifo-fallback',
63+
metavar='FIFO_FALLBACK_FILE',
4864
type=str,
4965
help='OpenSIPS MI Fallback FIFO File')
5066
communication.add_argument('-fd', '--fifo-reply-dir',
67+
metavar='FIFO_DIR',
5168
type=str,
5269
help='OpenSIPS MI FIFO Reply Directory')
5370

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

106+
load_env_file(args.env_file)
107+
108+
if not args.type:
109+
args.type = os.getenv('OPENSIPS_MI_TYPE', 'datagram')
110+
if not args.ip:
111+
args.ip = os.getenv('OPENSIPS_MI_IP', '127.0.0.1')
112+
if not args.port:
113+
args.port = os.getenv('OPENSIPS_MI_PORT', 8080)
114+
if not args.fifo_file:
115+
args.fifo_file = os.getenv('OPENSIPS_MI_FIFO_FILE', '/tmp/opensips_fifo')
116+
if not args.fifo_fallback:
117+
args.fifo_fallback = os.getenv('OPENSIPS_MI_FIFO_FALLBACK', '/tmp/opensips_fifo_fallback')
118+
if not args.fifo_reply_dir:
119+
args.fifo_reply_dir = os.getenv('OPENSIPS_MI_FIFO_REPLY_DIR', '/tmp/opensips_fifo_reply')
120+
89121
if args.type == 'fifo':
90-
fifo_args = {}
91-
if args.fifo_file:
92-
fifo_args['fifo_file'] = args.fifo_file
93-
if args.fifo_fallback:
94-
fifo_args['fifo_file_fallback'] = args.fifo_fallback
95-
if args.fifo_reply_dir:
96-
fifo_args['fifo_reply_dir'] = args.fifo_reply_dir
122+
fifo_args = {
123+
'fifo_file': args.fifo_file,
124+
'fifo_file_fallback': args.fifo_fallback,
125+
'fifo_reply_dir': args.fifo_reply_dir,
126+
}
97127
mi = OpenSIPSMI('fifo', **fifo_args)
98128
elif args.type == 'http':
99129
mi = OpenSIPSMI('http', url=f'http://{args.ip}:{args.port}/mi')

0 commit comments

Comments
 (0)