Skip to content

Commit 236b1e0

Browse files
authored
add config for gateways, add the rest of commands for workItems (#11)
1 parent 1e510e1 commit 236b1e0

File tree

8 files changed

+218
-46
lines changed

8 files changed

+218
-46
lines changed

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ COPY --from=builder /app/bin/app /app/
290290

291291
COPY external_scripts /app/external_scripts
292292

293+
COPY config /app/config
294+
293295
RUN cd /app/external_scripts/AdminatorWorkItems \
294296
&& composer install --no-dev
295297

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
# isp-net-adminator-queue
22
task manager for [ISP-Adminator](https://github.com/H-Software/isp-net-adminator)
33

4+
# development
5+
6+
## env-vars for adminator
7+
```
8+
export REDIS_ADDR=127.0.0.1:16379
9+
10+
export MYSQL_SERVER=127.0.0.1
11+
12+
export MYSQL_USER=root
13+
export MYSQL_PASSWD=isp-net-passwd
14+
15+
export POSTGRES_SERVER=127.0.0.1
16+
export POSTGRES_USER=adminator
17+
export POSTGRES_PASSWD=isp-net-passwd
18+
export POSTGRES_DB=adminator.new
19+
```
20+
21+
## env-vars for adminator - devcontainers
22+
```
23+
export REDIS_ADDR=host.docker.internal:16379
24+
25+
export MYSQL_SERVER=host.docker.internal
26+
27+
export MYSQL_USER=root
28+
export MYSQL_PASSWD=isp-net-passwd
29+
30+
export POSTGRES_SERVER=host.docker.internal
31+
export POSTGRES_USER=adminator
32+
export POSTGRES_PASSWD=isp-net-passwd
33+
export POSTGRES_DB=adminator.new
34+
```
35+
436
# links
537
- https://github.com/czhujer/h-platform-automation-cc-server/blob/master/main.go
638
## boilerplates

config/config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
gateways:
2+
gateway-wifi:
3+
fqdn: 10.128.0.2
4+
gateway-3:
5+
fqdn: 10.128.0.3

external_scripts/AdminatorWorkItems/src/mk_qos_handler.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@
3838
$sc_speed_koef = 1.1;
3939

4040
//ip adress device
41-
if (isset($_GET["ip"])) {
42-
$ip = $_GET["ip"];
43-
} elseif (isset($_SERVER['argv']['1'])) {
41+
if (isset($_SERVER['argv']['1'])) {
4442
$ip = $_SERVER['argv']['1'];
4543
} else {
4644
echo "ERROR: Missing IP Adress. Aborting... \n";

external_scripts/AdminatorWorkItems/src/mk_rh_restriction.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@
3434
//api vars
3535

3636
//ip adress device
37-
if (isset($_GET["ip"])) {
38-
$ip = $_GET["ip"];
39-
} elseif (isset($_SERVER['argv']['1'])) {
37+
if (isset($_SERVER['argv']['1'])) {
4038
$ip = $_SERVER['argv']['1'];
4139
} else {
4240
echo "ERROR: Missing IP Adress. Aborting... \n";

internal/command/command.go

Lines changed: 151 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,28 @@ package command
22

33
import (
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

1011
type Command struct {
1112
cmd string
1213
args []string
1314
}
1415

15-
var (
16-
logger *log.Logger
17-
command Command
18-
)
19-
2016
const (
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+
2627
func 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+
}

internal/flag/flag.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@ package flag
33
import (
44
"strings"
55
"time"
6-
6+
"fmt"
77
flag "github.com/spf13/pflag"
88
"github.com/spf13/viper"
99
)
1010

1111
var (
1212
// for testing
1313
FlagHelp bool
14-
FlagPhp bool
14+
FlagPhp bool
1515

1616
// for asyncq
17-
FlagRedisAddr string
17+
FlagRedisAddr string
1818
FlagGroupGracePeriod time.Duration
19-
FlagGroupMaxDelay time.Duration
20-
FlagGroupMaxSize int
19+
FlagGroupMaxDelay time.Duration
20+
FlagGroupMaxSize int
21+
22+
ConfigGatewayWifiFqdn string
23+
ConfigGateway3Fqdn string
2124
)
2225

2326
func init() {
@@ -40,14 +43,27 @@ func init() {
4043

4144
// environments variables support
4245
viper.BindEnv("redis-addr")
43-
46+
4447
FlagHelp = viper.GetBool("help")
4548
FlagPhp = viper.GetBool("php-version")
4649

4750
FlagRedisAddr = viper.GetString("redis-addr")
4851
FlagGroupGracePeriod = viper.GetDuration("asynq-grace-period")
4952
FlagGroupMaxDelay = viper.GetDuration("asynq-max-delay")
50-
FlagGroupMaxSize = viper.GetInt("asynq-max-size")
53+
FlagGroupMaxSize = viper.GetInt("asynq-max-size")
54+
55+
viper.SetConfigName("config") // name of config file (without extension)
56+
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
57+
viper.AddConfigPath("config") // path to look for the config file in
58+
59+
err := viper.ReadInConfig() // Find and read the config file
60+
if err != nil { // Handle errors reading the config file
61+
panic(fmt.Errorf("fatal error config file: %w", err))
62+
}
63+
64+
ConfigGatewayWifiFqdn = viper.GetString("gateways.gateway-wifi.fqdn")
65+
ConfigGateway3Fqdn = viper.GetString("gateways.gateway-3.fqdn")
66+
5167
}
5268

5369
func PrintDefaults() {

0 commit comments

Comments
 (0)