|
21 | 21 |
|
22 | 22 | import sys |
23 | 23 | import json |
| 24 | +import os |
24 | 25 | import argparse |
25 | 26 | from opensips.mi import OpenSIPSMI, OpenSIPSMIException |
26 | 27 |
|
| 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 | + |
27 | 39 | parser = argparse.ArgumentParser() |
28 | 40 |
|
| 41 | +parser.add_argument('--env-file', |
| 42 | + type=str, |
| 43 | + default='.env', |
| 44 | + help='Load environment variables from file') |
| 45 | + |
29 | 46 | communication = parser.add_argument_group('communication') |
30 | 47 |
|
31 | 48 | communication.add_argument('-t', '--type', |
32 | 49 | type=str, |
33 | | - default='fifo', |
34 | 50 | choices=['fifo', 'http', 'datagram'], |
35 | 51 | help='OpenSIPS MI Communication Type') |
36 | 52 | communication.add_argument('-i', '--ip', |
37 | 53 | type=str, |
38 | | - help='OpenSIPS MI IP Address', |
39 | | - default='127.0.0.1') |
| 54 | + help='OpenSIPS MI IP Address') |
40 | 55 | communication.add_argument('-p', '--port', |
41 | 56 | type=int, |
42 | | - help='OpenSIPS MI Port', |
43 | | - default=8888) |
| 57 | + help='OpenSIPS MI Port') |
44 | 58 | communication.add_argument('-f', '--fifo-file', |
| 59 | + metavar='FIFO_FILE', |
45 | 60 | type=str, |
46 | 61 | help='OpenSIPS MI FIFO File') |
47 | 62 | communication.add_argument('-fb', '--fifo-fallback', |
| 63 | + metavar='FIFO_FALLBACK_FILE', |
48 | 64 | type=str, |
49 | 65 | help='OpenSIPS MI Fallback FIFO File') |
50 | 66 | communication.add_argument('-fd', '--fifo-reply-dir', |
| 67 | + metavar='FIFO_DIR', |
51 | 68 | type=str, |
52 | 69 | help='OpenSIPS MI FIFO Reply Directory') |
53 | 70 |
|
@@ -86,14 +103,27 @@ def main(): |
86 | 103 | """ Main function of the opensips-mi script """ |
87 | 104 | args = parser.parse_args() |
88 | 105 |
|
| 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 | + |
89 | 121 | 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 | + } |
97 | 127 | mi = OpenSIPSMI('fifo', **fifo_args) |
98 | 128 | elif args.type == 'http': |
99 | 129 | mi = OpenSIPSMI('http', url=f'http://{args.ip}:{args.port}/mi') |
|
0 commit comments