Commit 7912597ff3719a797fa212d9d6102bf8834fa4ba
1 parent
2b10800f95
Exists in
master
Log
Showing 1 changed file with 3 additions and 0 deletions Inline Diff
lib/apiserver/routers/smstools-config.js
1 | const childProcess = require('child_process'); | 1 | const childProcess = require('child_process'); |
2 | 2 | ||
3 | const express = require('express'); | 3 | const express = require('express'); |
4 | const bodyParser = require('body-parser'); | 4 | const bodyParser = require('body-parser'); |
5 | 5 | ||
6 | const config = require('komodo-sdk/config'); | 6 | const config = require('komodo-sdk/config'); |
7 | const logger = require('komodo-sdk/logger'); | ||
8 | |||
7 | const smstoolsConfig = require('../../smstools-config'); | 9 | const smstoolsConfig = require('../../smstools-config'); |
8 | const smstoolsConfigCreator = require('../../smstools-config/creator'); | 10 | const smstoolsConfigCreator = require('../../smstools-config/creator'); |
9 | const smstoolsConfigSetter = require('../../smstools-config/setter'); | 11 | const smstoolsConfigSetter = require('../../smstools-config/setter'); |
10 | const smstoolsConfigData = require('../../smstools-config/config-file'); | 12 | const smstoolsConfigData = require('../../smstools-config/config-file'); |
11 | 13 | ||
12 | const router = express.Router(); | 14 | const router = express.Router(); |
13 | module.exports = router; | 15 | module.exports = router; |
14 | 16 | ||
15 | function pageIndex(req, res) { | 17 | function pageIndex(req, res) { |
16 | const configString = smstoolsConfigCreator(); | 18 | const configString = smstoolsConfigCreator(); |
17 | res.json({ | 19 | res.json({ |
18 | dirty: smstoolsConfig.config.dirty, | 20 | dirty: smstoolsConfig.config.dirty, |
19 | config: smstoolsConfig.config, | 21 | config: smstoolsConfig.config, |
20 | configStringLength: configString.length, | 22 | configStringLength: configString.length, |
21 | configStringNrLines: (configString.match(/\n/g) || []).length, | 23 | configStringNrLines: (configString.match(/\n/g) || []).length, |
22 | configString, | 24 | configString, |
23 | }); | 25 | }); |
24 | } | 26 | } |
25 | 27 | ||
26 | function pageGenerate(req, res) { | 28 | function pageGenerate(req, res) { |
27 | res.end(smstoolsConfigCreator()); | 29 | res.end(smstoolsConfigCreator()); |
28 | } | 30 | } |
29 | 31 | ||
30 | async function pageModemList(req, res) { | 32 | async function pageModemList(req, res) { |
31 | const modems = []; | 33 | const modems = []; |
32 | // eslint-disable-next-line no-restricted-syntax | 34 | // eslint-disable-next-line no-restricted-syntax |
33 | for (const [key, value] of Object.entries(smstoolsConfigData.modems || {})) { | 35 | for (const [key, value] of Object.entries(smstoolsConfigData.modems || {})) { |
34 | modems.push({ | 36 | modems.push({ |
35 | label: `${key}: ${value.device}, INCOMING${value.outgoing ? ' and OUTGOING' : ''}`, | 37 | label: `${key}: ${value.device}, INCOMING${value.outgoing ? ' and OUTGOING' : ''}`, |
36 | value: key, | 38 | value: key, |
37 | data: value, | 39 | data: value, |
38 | }); | 40 | }); |
39 | } | 41 | } |
40 | 42 | ||
41 | res.json(modems); | 43 | res.json(modems); |
42 | } | 44 | } |
43 | 45 | ||
44 | async function pageSet(req, res) { | 46 | async function pageSet(req, res) { |
45 | const keyword = (req.params.keyword || req.query.keyword || '').trim(); | 47 | const keyword = (req.params.keyword || req.query.keyword || '').trim(); |
46 | const value = (req.query.value || '').trim(); | 48 | const value = (req.query.value || '').trim(); |
47 | 49 | ||
48 | if (!keyword) { | 50 | if (!keyword) { |
49 | res.end('Invalid keyword'); | 51 | res.end('Invalid keyword'); |
50 | return; | 52 | return; |
51 | } | 53 | } |
52 | 54 | ||
53 | const result = await smstoolsConfigSetter.set(keyword, value); | 55 | const result = await smstoolsConfigSetter.set(keyword, value); |
54 | res.json({ | 56 | res.json({ |
55 | dirty: !!smstoolsConfigData.dirty, | 57 | dirty: !!smstoolsConfigData.dirty, |
56 | result, | 58 | result, |
57 | }); | 59 | }); |
58 | } | 60 | } |
59 | 61 | ||
60 | async function pageModemSet(req, res) { | 62 | async function pageModemSet(req, res) { |
61 | if (!req.body) { | 63 | if (!req.body) { |
62 | res.json({ | 64 | res.json({ |
63 | status: 'NOT OK', | 65 | status: 'NOT OK', |
64 | message: 'Empty body', | 66 | message: 'Empty body', |
65 | }); | 67 | }); |
66 | return; | 68 | return; |
67 | } | 69 | } |
68 | 70 | ||
69 | const modemName = (req.params.modemName || '').trim(); | 71 | const modemName = (req.params.modemName || '').trim(); |
70 | if (!modemName) { | 72 | if (!modemName) { |
71 | res.json({ | 73 | res.json({ |
72 | status: 'NOT OK', | 74 | status: 'NOT OK', |
73 | message: 'Invalid modem name', | 75 | message: 'Invalid modem name', |
74 | }); | 76 | }); |
75 | return; | 77 | return; |
76 | } | 78 | } |
77 | 79 | ||
78 | const result = await smstoolsConfigSetter.setModem(modemName, req.body); | 80 | const result = await smstoolsConfigSetter.setModem(modemName, req.body); |
79 | res.json({ | 81 | res.json({ |
80 | dirty: !!smstoolsConfigData.dirty, | 82 | dirty: !!smstoolsConfigData.dirty, |
81 | result, | 83 | result, |
82 | }); | 84 | }); |
83 | } | 85 | } |
84 | 86 | ||
85 | async function pageModemSetSingleValue(req, res) { | 87 | async function pageModemSetSingleValue(req, res) { |
86 | if (!req.body) { | 88 | if (!req.body) { |
87 | res.json({ | 89 | res.json({ |
88 | status: 'NOT OK', | 90 | status: 'NOT OK', |
89 | message: 'Empty body', | 91 | message: 'Empty body', |
90 | }); | 92 | }); |
91 | return; | 93 | return; |
92 | } | 94 | } |
93 | 95 | ||
94 | const modemName = (req.params.modemName || '').trim(); | 96 | const modemName = (req.params.modemName || '').trim(); |
95 | if (!modemName) { | 97 | if (!modemName) { |
96 | res.json({ | 98 | res.json({ |
97 | status: 'NOT OK', | 99 | status: 'NOT OK', |
98 | message: 'Invalid modem name', | 100 | message: 'Invalid modem name', |
99 | }); | 101 | }); |
100 | return; | 102 | return; |
101 | } | 103 | } |
102 | 104 | ||
103 | const keyword = (req.body.keyword || '').trim(); | 105 | const keyword = (req.body.keyword || '').trim(); |
104 | if (!keyword) { | 106 | if (!keyword) { |
105 | res.json({ | 107 | res.json({ |
106 | status: 'NOT OK', | 108 | status: 'NOT OK', |
107 | message: 'Invalid keyword', | 109 | message: 'Invalid keyword', |
108 | }); | 110 | }); |
109 | return; | 111 | return; |
110 | } | 112 | } |
111 | 113 | ||
112 | const { value } = req.body; | 114 | const { value } = req.body; |
113 | 115 | ||
114 | const result = await smstoolsConfigSetter.setModemSingleValue(modemName, keyword, value); | 116 | const result = await smstoolsConfigSetter.setModemSingleValue(modemName, keyword, value); |
115 | res.json({ | 117 | res.json({ |
116 | dirty: !!smstoolsConfigData.dirty, | 118 | dirty: !!smstoolsConfigData.dirty, |
117 | result, | 119 | result, |
118 | }); | 120 | }); |
119 | } | 121 | } |
120 | 122 | ||
121 | async function pageModemDelete(req, res) { | 123 | async function pageModemDelete(req, res) { |
122 | const modemName = (req.params.modemName || '').trim(); | 124 | const modemName = (req.params.modemName || '').trim(); |
123 | if (!modemName) { | 125 | if (!modemName) { |
124 | res.json({ | 126 | res.json({ |
125 | status: 'NOT OK', | 127 | status: 'NOT OK', |
126 | message: 'Invalid modem name', | 128 | message: 'Invalid modem name', |
127 | }); | 129 | }); |
128 | return; | 130 | return; |
129 | } | 131 | } |
130 | 132 | ||
131 | const result = await smstoolsConfigSetter.delModem(modemName); | 133 | const result = await smstoolsConfigSetter.delModem(modemName); |
132 | res.json({ | 134 | res.json({ |
133 | dirty: !!smstoolsConfigData.dirty, | 135 | dirty: !!smstoolsConfigData.dirty, |
134 | result, | 136 | result, |
135 | }); | 137 | }); |
136 | } | 138 | } |
137 | 139 | ||
138 | function pageInstallConfig(req, res) { | 140 | function pageInstallConfig(req, res) { |
141 | logger.info('ROUTER-SMSTOOLS-CONFIG: Going to write configuration and restart smsd service'); | ||
139 | const fileToExec = `${process.cwd()}/bin/smstools-config-install`; | 142 | const fileToExec = `${process.cwd()}/bin/smstools-config-install`; |
140 | childProcess.execFile(fileToExec, [config.smstools_config_file || '/etc/smsd.conf'], (err, stdout, stderr) => { | 143 | childProcess.execFile(fileToExec, [config.smstools_config_file || '/etc/smsd.conf'], (err, stdout, stderr) => { |
141 | res.json({ | 144 | res.json({ |
142 | err, | 145 | err, |
143 | stdout, | 146 | stdout, |
144 | stderr, | 147 | stderr, |
145 | }); | 148 | }); |
146 | }); | 149 | }); |
147 | } | 150 | } |
148 | 151 | ||
149 | router.get('/', pageIndex); | 152 | router.get('/', pageIndex); |
150 | router.get('/generate', pageGenerate); | 153 | router.get('/generate', pageGenerate); |
151 | router.get('/modems', pageModemList); | 154 | router.get('/modems', pageModemList); |
152 | 155 | ||
153 | router.get('/set/:keyword', pageSet); | 156 | router.get('/set/:keyword', pageSet); |
154 | router.post('/modem/set/:modemName', bodyParser.json({ type: '*/json' }), pageModemSet); | 157 | router.post('/modem/set/:modemName', bodyParser.json({ type: '*/json' }), pageModemSet); |
155 | router.post('/modem/set-single-value/:modemName', bodyParser.json({ type: '*/json' }), pageModemSetSingleValue); | 158 | router.post('/modem/set-single-value/:modemName', bodyParser.json({ type: '*/json' }), pageModemSetSingleValue); |
156 | router.get('/modem/delete/:modemName', pageModemDelete); | 159 | router.get('/modem/delete/:modemName', pageModemDelete); |
157 | router.get('/install-config', pageInstallConfig); | 160 | router.get('/install-config', pageInstallConfig); |
158 | 161 |