@@ -2,27 +2,28 @@ package command
22
33import (
44 "fmt"
5-
5+ "net"
66 "github.com/go-cmd/cmd"
77 "github.com/h-software/isp-net-adminator-queue/internal/log"
8+ "github.com/h-software/isp-net-adminator-queue/internal/flag"
89)
910
1011type Command struct {
1112 cmd string
1213 args []string
1314}
1415
15- var (
16- logger * log.Logger
17- command Command
18- )
19-
2016const (
21- gateway_wifi_fqdn = "10.128.0.2"
22- gateway_3_fqdn = "10.128.0.3"
2317 ext_scripts_path = "external_scripts"
2418)
2519
20+ var (
21+ logger * log.Logger
22+ command Command
23+ err error
24+ work_items_path = fmt .Sprintf ("%s/AdminatorWorkItems/src" , ext_scripts_path )
25+ )
26+
2627func init () {
2728 logger = log .NewLogger (nil )
2829}
@@ -35,46 +36,160 @@ func RunCommand(itemId int) error {
3536 case 1 :
3637 // gateway-3 - restriction (net-n/sikana)
3738 command .cmd = "php"
38- command .args = []string {fmt .Sprintf ("%v/AdminatorWorkItems/ mk_rh_restriction.php" , ext_scripts_path ), gateway_3_fqdn }
39+ command .args = []string {fmt .Sprintf ("%v/mk_rh_restriction.php" , work_items_path ), getIpAddress ( flag . ConfigGateway3Fqdn ) }
3940
40- ExecuteCommand (command .cmd , command .args )
41+ err = ExecuteCommand (command .cmd , command .args )
4142
4243 // $mess_ok = "gateway-3-restriction ok ";
4344 // $mess_er = "gateway-3-restriction error ";
44- case 2 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 33 :
45+ case 2 :
4546 // gateway-wifi (1) - restrictions (net-n/sikana)
46-
4747 command .cmd = "php"
48- command .args = []string {fmt .Sprintf ("%v/AdminatorWorkItems/mk_rh_restriction.php" , ext_scripts_path ), gateway_wifi_fqdn }
49-
50- ExecuteCommand (command .cmd , command .args )
51-
52- // executeCommand(command.cmd, command.args)
48+ command .args = []string {fmt .Sprintf ("%v/mk_rh_restriction.php" , work_items_path ), getIpAddress (flag .ConfigGatewayWifiFqdn )}
49+
50+ err = ExecuteCommand (command .cmd , command .args )
5351
5452 // $mess_ok = "gateway-wifi-iptables-restart ok ";
5553 // $mess_er = "gateway-wifi-iptables-restart error ";
56- case 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 :
54+ case 3 :
5755 //gateway-fiber (2) - iptables (net-n/sikana)
5856
5957 // $cmd = "/root/bin/gateway-fiber.remote.exec2.sh \"/etc/init.d/iptables-adminator restart\" ";
6058
6159 // $mess_ok = "gateway-fiber.iptables ok ";
6260 // $mess_er = "gateway-fiber.iptables error ";
61+ case 4 :
62+ //gateway-fiber - radius
63+ // $cmd = "/root/bin/reinhard-fiber.remote.exec2.sh \"/root/bin/radius.restart.sh\"";
64+
65+ // $mess_ok = "reinhard-fiber.radius ok ";
66+ // $mess_er = "reinhard-fiber.radius error ";
67+ case 5 :
68+ // $cmd = "/root/bin/gateway-fiber.remote.exec2.sh \"/etc/init.d/shaper restart\" ";
69+
70+ // $mess_ok = "reinhard-fiber.shaper ok ";
71+ // $mess_er = "reinhard-fiber.shaper error ";
72+ case 6 :
73+ // $cmd = "/root/bin/trinity.local.exec2.sh \"/root/bin/mikrotik.dhcp.leases.erase.sh\" ";
74+
75+ // $mess_ok = "(trinity) mikrotik.dhcp.leases.erase ok ";
76+ // $mess_er = "(trinity) mikrotik.dhcp.leases.erase error ";
77+
78+ case 7 :
79+ // $cmd = "/root/bin/trinity.local.exec2.sh \"/root/bin/scripts_fiber/sw.h3c.vlan.set.pl update\" ";
80+
81+ // $mess_ok = "trinity.sw.h3c.vlan.set ok ";
82+ // $mess_er = "trinity.sw.h3c.vlan.set error ";
83+
84+ case 8 :
85+ // nothing
86+
87+ case 9 :
88+ // $cmd = "/root/bin/erik.remote.exec.sh \"/root/bin/dns.restart.sh\" ";
89+
90+ // $mess_ok = "erik-dns.restart ok ";
91+ // $mess_er = "erik-dns.restart-restart error ";
92+ case 10 :
93+ // $cmd = "/root/bin/trinity.local.exec2.sh \"/root/bin/dns.restart.sh\" ";
94+
95+ // $mess_ok = "trinity-dns-restart ok ";
96+ // $mess_er = "trinity-dns-restart error ";
97+ case 11 :
98+ // $cmd = "/root/bin/artemis.remote.exec2.sh \"/root/bin/dns.restart.sh\" ";
99+
100+ // $mess_ok = "artemis-dns-server-restart ok ";
101+ // $mess_er = "artemis-dns-server-restart error ";
102+ case 12 :
103+ // $cmd = "/root/bin/c.ns.remote.exec2.sh \"/root/bin/dns.restart.sh\" ";
104+
105+ // $mess_ok = "c.ns.simelon.net-dns-server-restart ok ";
106+ // $mess_er = "c.ns.simelon.net-dns-server-restart error ";
107+ case 13 :
108+ // gateway-wifi (ros) - shaper (client's tariffs)
109+
110+ // "/root/bin/trinity.local.exec2.sh \"php /var/www/html/htdocs.ssl/adminator2/mk_control/mk_qos_handler.php 10.128.0.2\" ";
111+
112+ // $mess_ok = "gateway-wifi-shaper-restart ok ";
113+ // $mess_er = "gateway-wifi-shaper-restart error ";
63114
64115 command .cmd = "php"
65- command .args = []string {"-v" }
66- ExecuteCommand (command .cmd , command .args )
116+ command .args = []string {fmt .Sprintf ("%v/mk_qos_handler.php" , work_items_path ), getIpAddress (flag .ConfigGatewayWifiFqdn )}
117+ err = ExecuteCommand (command .cmd , command .args )
118+ case 14 :
119+ // $cmd = "/root/bin/trinity.local.exec2.sh \"/root/bin/scripts_wifi_network/rb.filter_v2.pl\" ";
120+
121+ // $mess_ok = "trinity-filtrace-IP-on-Mtik's-restart ok ";
122+ // $mess_er = "trinity-filtrace-IP-on-Mtik's-restart error ";
123+
124+ case 15 :
125+ //trinity - Monitoring I - Footer-restart (alarms)
126+
127+ // $cmd = "/root/bin/monitoring.remote.exec2.sh \"/var/www/cgi-bin/mon1-footer.pl\" ";
128+
129+ // $mess_ok = "monitoring-I-Footer-restart ok ";
130+ // $mess_er = "monitoring-I-Footer-restart error ";
131+
132+ case 16 :
133+ // $cmd = "/root/bin/trinity.local.exec2.sh \"/var/www/cgi-bin/cgi-mon/footer_php.pl\" ";
67134
135+ // $mess_ok = "trinity-monitoring-I-Footer-PHP-restart ok ";
136+ // $mess_er = "trinity-monitoring-I-Footer-PHP-restart error ";
137+
138+ case 17 :
139+ // $cmd = "/root/bin/trinity.local.exec2.sh \"/var/www/cgi-bin/cgi-mon/footer_cat.pl\" ";
140+
141+ // $mess_ok = "trinity-monitoring-I-Footer-cat-restart ok ";
142+ // $mess_er = "trinity-monitoring-I-Footer-cat-restart error ";
143+ case 18 :
144+ // $cmd = "/root/bin/monitoring.remote.exec2.sh \"/var/www/cgi-bin/mon2-feeder.pl\" ";
145+
146+ // $mess_ok = "monitoring - Monitoring II - Feeder-restart ok ";
147+ // $mess_er = "monitoring - Monitoring II - Feeder-restart error ";
148+ case 19 :
149+ // nothing
150+ case 20 :
151+ // $cmd = "/root/bin/trinity.local.exec2.sh \"php /var/www/html/htdocs.ssl/adminator2/mk_control/mk_qos_handler.php 10.128.0.3\" ";
152+
153+ // $mess_ok = "gateway-3 (ros) - shaper (client's tariffs) - restart ok ";
154+ // $mess_er = "gateway-3 (ros) - shaper (client's tariffs) - restart error ";
155+
156+ command .cmd = "php"
157+ command .args = []string {fmt .Sprintf ("%v/mk_qos_handler.php" , work_items_path ), getIpAddress (flag .ConfigGateway3Fqdn )}
158+ err = ExecuteCommand (command .cmd , command .args )
159+
160+ case 21 :
161+ // $cmd = "/root/bin/artemis.remote.exec2.sh \"/root/bin/radius.restart.sh\" ";
162+
163+ // $mess_ok = "artemis-radius-restart ok ";
164+ // $mess_er = "artemis-radius-restart error ";
165+ case 22 :
166+ // $cmd = "/root/bin/monitoring.remote.exec2.sh \"/var/www/cgi-bin/mon2-checker.pl\" ";
167+
168+ // $mess_ok = "monitoring - Monitoring II - Feeder-restart ok ";
169+ // $mess_er = "monitoring - Monitoring II - Feeder-restart error ";
170+ case 23 :
171+ // $cmd = "/root/bin/trinity.local.exec2.sh \"php /var/www/html/htdocs.ssl/adminator2/mk_control/mk_qos_handler.php 10.128.0.15\" ";
172+
173+ // $mess_ok = "gateway-5-shaper-restart ok ";
174+ // $mess_er = "gateway-5-shaper-restart error ";
175+ case 24 :
176+ // $cmd = "/root/bin/trinity.local.exec2.sh \"php /var/www/html/htdocs.ssl/adminator2/mk_control/mk_rh_restriction.php 10.128.0.15\" ";
177+
178+ // $mess_ok = "gateway-5-iptables-restart ok ";
179+ // $mess_er = "gateway-5-iptables-restart error ";
68180 default :
69181 // unknown itemId
70182 errM := fmt .Errorf ("unsupported ItemId (%d)" , itemId )
71183
72184 logger .Error (errM )
73- return fmt .Errorf ("%s " , errM )
185+ return fmt .Errorf ("%w " , errM )
74186 }
75187
76- // TODO: add more commands to case below from original code
77- // https://github.com/H-Software/isp-net-adminator/pull/260/files#diff-bf99864cec6493c7e3d8e681dd2fc01c1ffc7480b5728411b20c7af4bbf88b37L874
188+ if err != nil {
189+ errM := fmt .Sprintf ("runCommand failed: %v, (itemId: %d)" , err , itemId )
190+ logger .Error (errM )
191+ return err
192+ }
78193
79194 return nil
80195}
@@ -109,13 +224,13 @@ func ExecuteCommand(inputCommand string, inputCommandArgs []string) error {
109224 cmd .Stdout = nil
110225 continue
111226 }
112- logger .Info ( fmt . Printf ( "COMMAND STDOUT: %v \n " , line ) )
227+ logger .Infof ( "COMMAND STDOUT: %s" , line )
113228 case line , open := <- cmd .Stderr :
114229 if ! open {
115230 cmd .Stderr = nil
116231 continue
117232 }
118- logger .Error ( fmt . Printf ( "COMMAND STDERR: %v \n " , line ) )
233+ logger .Errorf ( "COMMAND STDERR: %s " , line )
119234 }
120235 }
121236 }()
@@ -129,14 +244,22 @@ func ExecuteCommand(inputCommand string, inputCommandArgs []string) error {
129244 logger .Debugf ("command executed (PID: %v, complete: %v, exit code: %v)" ,
130245 status .PID , status .Complete , status .Exit )
131246
132- if ( status .Exit != 0 || ! status .Complete ) {
133- errM = fmt .Errorf ("command failed! (exitCode: %d, complete: %t)" , status .Exit , status .Complete );
247+ if status .Exit != 0 || ! status .Complete {
248+ errM = fmt .Errorf ("command failed! (exitCode: %d, complete: %t)" , status .Exit , status .Complete )
134249 }
135250
136251 if errM != nil {
137252 logger .Error (errM )
138- return fmt .Errorf ("%s " , errM )
253+ return fmt .Errorf ("%w " , errM )
139254 }
140255
141256 return nil
142257}
258+
259+ func getIpAddress (fqdn string ) string {
260+ addr , err := net .ResolveIPAddr ("ip" , fqdn )
261+ if err != nil {
262+ panic (err )
263+ }
264+ return addr .String ()
265+ }
0 commit comments