Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ COPY --from=builder /app/bin/app /app/

COPY external_scripts /app/external_scripts

COPY config /app/config

RUN cd /app/external_scripts/AdminatorWorkItems \
&& composer install --no-dev

Expand Down
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
# isp-net-adminator-queue
task manager for [ISP-Adminator](https://github.com/H-Software/isp-net-adminator)

# development

## env-vars for adminator
```
export REDIS_ADDR=127.0.0.1:16379

export MYSQL_SERVER=127.0.0.1

export MYSQL_USER=root
export MYSQL_PASSWD=isp-net-passwd

export POSTGRES_SERVER=127.0.0.1
export POSTGRES_USER=adminator
export POSTGRES_PASSWD=isp-net-passwd
export POSTGRES_DB=adminator.new
```

## env-vars for adminator - devcontainers
```
export REDIS_ADDR=host.docker.internal:16379

export MYSQL_SERVER=host.docker.internal

export MYSQL_USER=root
export MYSQL_PASSWD=isp-net-passwd

export POSTGRES_SERVER=host.docker.internal
export POSTGRES_USER=adminator
export POSTGRES_PASSWD=isp-net-passwd
export POSTGRES_DB=adminator.new
```

# links
- https://github.com/czhujer/h-platform-automation-cc-server/blob/master/main.go
## boilerplates
Expand Down
5 changes: 5 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
gateways:
gateway-wifi:
fqdn: 10.128.0.2
gateway-3:
fqdn: 10.128.0.3
4 changes: 1 addition & 3 deletions external_scripts/AdminatorWorkItems/src/mk_qos_handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
$sc_speed_koef = 1.1;

//ip adress device
if (isset($_GET["ip"])) {
$ip = $_GET["ip"];
} elseif (isset($_SERVER['argv']['1'])) {
if (isset($_SERVER['argv']['1'])) {
$ip = $_SERVER['argv']['1'];
} else {
echo "ERROR: Missing IP Adress. Aborting... \n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
//api vars

//ip adress device
if (isset($_GET["ip"])) {
$ip = $_GET["ip"];
} elseif (isset($_SERVER['argv']['1'])) {
if (isset($_SERVER['argv']['1'])) {
$ip = $_SERVER['argv']['1'];
} else {
echo "ERROR: Missing IP Adress. Aborting... \n";
Expand Down
179 changes: 151 additions & 28 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@ package command

import (
"fmt"

"net"
"github.com/go-cmd/cmd"
"github.com/h-software/isp-net-adminator-queue/internal/log"
"github.com/h-software/isp-net-adminator-queue/internal/flag"
)

type Command struct {
cmd string
args []string
}

var (
logger *log.Logger
command Command
)

const (
gateway_wifi_fqdn = "10.128.0.2"
gateway_3_fqdn = "10.128.0.3"
ext_scripts_path = "external_scripts"
)

var (
logger *log.Logger
command Command
err error
work_items_path = fmt.Sprintf("%s/AdminatorWorkItems/src", ext_scripts_path)
)

func init() {
logger = log.NewLogger(nil)
}
Expand All @@ -35,46 +36,160 @@ func RunCommand(itemId int) error {
case 1:
// gateway-3 - restriction (net-n/sikana)
command.cmd = "php"
command.args = []string{fmt.Sprintf("%v/AdminatorWorkItems/mk_rh_restriction.php", ext_scripts_path), gateway_3_fqdn}
command.args = []string{fmt.Sprintf("%v/mk_rh_restriction.php", work_items_path), getIpAddress(flag.ConfigGateway3Fqdn)}

ExecuteCommand(command.cmd, command.args)
err = ExecuteCommand(command.cmd, command.args)

// $mess_ok = "gateway-3-restriction ok ";
// $mess_er = "gateway-3-restriction error ";
case 2, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 33:
case 2:
// gateway-wifi (1) - restrictions (net-n/sikana)

command.cmd = "php"
command.args = []string{fmt.Sprintf("%v/AdminatorWorkItems/mk_rh_restriction.php", ext_scripts_path), gateway_wifi_fqdn}

ExecuteCommand(command.cmd, command.args)

// executeCommand(command.cmd, command.args)
command.args = []string{fmt.Sprintf("%v/mk_rh_restriction.php", work_items_path), getIpAddress(flag.ConfigGatewayWifiFqdn)}

err = ExecuteCommand(command.cmd, command.args)

// $mess_ok = "gateway-wifi-iptables-restart ok ";
// $mess_er = "gateway-wifi-iptables-restart error ";
case 3, 4, 5, 6, 7, 8, 9, 10:
case 3:
//gateway-fiber (2) - iptables (net-n/sikana)

// $cmd = "/root/bin/gateway-fiber.remote.exec2.sh \"/etc/init.d/iptables-adminator restart\" ";

// $mess_ok = "gateway-fiber.iptables ok ";
// $mess_er = "gateway-fiber.iptables error ";
case 4:
//gateway-fiber - radius
// $cmd = "/root/bin/reinhard-fiber.remote.exec2.sh \"/root/bin/radius.restart.sh\"";

// $mess_ok = "reinhard-fiber.radius ok ";
// $mess_er = "reinhard-fiber.radius error ";
case 5:
// $cmd = "/root/bin/gateway-fiber.remote.exec2.sh \"/etc/init.d/shaper restart\" ";

// $mess_ok = "reinhard-fiber.shaper ok ";
// $mess_er = "reinhard-fiber.shaper error ";
case 6:
// $cmd = "/root/bin/trinity.local.exec2.sh \"/root/bin/mikrotik.dhcp.leases.erase.sh\" ";

// $mess_ok = "(trinity) mikrotik.dhcp.leases.erase ok ";
// $mess_er = "(trinity) mikrotik.dhcp.leases.erase error ";

case 7:
// $cmd = "/root/bin/trinity.local.exec2.sh \"/root/bin/scripts_fiber/sw.h3c.vlan.set.pl update\" ";

// $mess_ok = "trinity.sw.h3c.vlan.set ok ";
// $mess_er = "trinity.sw.h3c.vlan.set error ";

case 8:
// nothing

case 9:
// $cmd = "/root/bin/erik.remote.exec.sh \"/root/bin/dns.restart.sh\" ";

// $mess_ok = "erik-dns.restart ok ";
// $mess_er = "erik-dns.restart-restart error ";
case 10:
// $cmd = "/root/bin/trinity.local.exec2.sh \"/root/bin/dns.restart.sh\" ";

// $mess_ok = "trinity-dns-restart ok ";
// $mess_er = "trinity-dns-restart error ";
case 11:
// $cmd = "/root/bin/artemis.remote.exec2.sh \"/root/bin/dns.restart.sh\" ";

// $mess_ok = "artemis-dns-server-restart ok ";
// $mess_er = "artemis-dns-server-restart error ";
case 12:
// $cmd = "/root/bin/c.ns.remote.exec2.sh \"/root/bin/dns.restart.sh\" ";

// $mess_ok = "c.ns.simelon.net-dns-server-restart ok ";
// $mess_er = "c.ns.simelon.net-dns-server-restart error ";
case 13:
// gateway-wifi (ros) - shaper (client's tariffs)

// "/root/bin/trinity.local.exec2.sh \"php /var/www/html/htdocs.ssl/adminator2/mk_control/mk_qos_handler.php 10.128.0.2\" ";

// $mess_ok = "gateway-wifi-shaper-restart ok ";
// $mess_er = "gateway-wifi-shaper-restart error ";

command.cmd = "php"
command.args = []string{"-v"}
ExecuteCommand(command.cmd, command.args)
command.args = []string{fmt.Sprintf("%v/mk_qos_handler.php", work_items_path), getIpAddress(flag.ConfigGatewayWifiFqdn)}
err = ExecuteCommand(command.cmd, command.args)
case 14:
// $cmd = "/root/bin/trinity.local.exec2.sh \"/root/bin/scripts_wifi_network/rb.filter_v2.pl\" ";

// $mess_ok = "trinity-filtrace-IP-on-Mtik's-restart ok ";
// $mess_er = "trinity-filtrace-IP-on-Mtik's-restart error ";

case 15:
//trinity - Monitoring I - Footer-restart (alarms)

// $cmd = "/root/bin/monitoring.remote.exec2.sh \"/var/www/cgi-bin/mon1-footer.pl\" ";

// $mess_ok = "monitoring-I-Footer-restart ok ";
// $mess_er = "monitoring-I-Footer-restart error ";

case 16:
// $cmd = "/root/bin/trinity.local.exec2.sh \"/var/www/cgi-bin/cgi-mon/footer_php.pl\" ";

// $mess_ok = "trinity-monitoring-I-Footer-PHP-restart ok ";
// $mess_er = "trinity-monitoring-I-Footer-PHP-restart error ";

case 17:
// $cmd = "/root/bin/trinity.local.exec2.sh \"/var/www/cgi-bin/cgi-mon/footer_cat.pl\" ";

// $mess_ok = "trinity-monitoring-I-Footer-cat-restart ok ";
// $mess_er = "trinity-monitoring-I-Footer-cat-restart error ";
case 18:
// $cmd = "/root/bin/monitoring.remote.exec2.sh \"/var/www/cgi-bin/mon2-feeder.pl\" ";

// $mess_ok = "monitoring - Monitoring II - Feeder-restart ok ";
// $mess_er = "monitoring - Monitoring II - Feeder-restart error ";
case 19:
// nothing
case 20:
// $cmd = "/root/bin/trinity.local.exec2.sh \"php /var/www/html/htdocs.ssl/adminator2/mk_control/mk_qos_handler.php 10.128.0.3\" ";

// $mess_ok = "gateway-3 (ros) - shaper (client's tariffs) - restart ok ";
// $mess_er = "gateway-3 (ros) - shaper (client's tariffs) - restart error ";

command.cmd = "php"
command.args = []string{fmt.Sprintf("%v/mk_qos_handler.php", work_items_path), getIpAddress(flag.ConfigGateway3Fqdn)}
err = ExecuteCommand(command.cmd, command.args)

case 21:
// $cmd = "/root/bin/artemis.remote.exec2.sh \"/root/bin/radius.restart.sh\" ";

// $mess_ok = "artemis-radius-restart ok ";
// $mess_er = "artemis-radius-restart error ";
case 22:
// $cmd = "/root/bin/monitoring.remote.exec2.sh \"/var/www/cgi-bin/mon2-checker.pl\" ";

// $mess_ok = "monitoring - Monitoring II - Feeder-restart ok ";
// $mess_er = "monitoring - Monitoring II - Feeder-restart error ";
case 23:
// $cmd = "/root/bin/trinity.local.exec2.sh \"php /var/www/html/htdocs.ssl/adminator2/mk_control/mk_qos_handler.php 10.128.0.15\" ";

// $mess_ok = "gateway-5-shaper-restart ok ";
// $mess_er = "gateway-5-shaper-restart error ";
case 24:
// $cmd = "/root/bin/trinity.local.exec2.sh \"php /var/www/html/htdocs.ssl/adminator2/mk_control/mk_rh_restriction.php 10.128.0.15\" ";

// $mess_ok = "gateway-5-iptables-restart ok ";
// $mess_er = "gateway-5-iptables-restart error ";
default:
// unknown itemId
errM := fmt.Errorf("unsupported ItemId (%d)", itemId)

logger.Error(errM)
return fmt.Errorf("%s", errM)
return fmt.Errorf("%w", errM)
}

// TODO: add more commands to case below from original code
// https://github.com/H-Software/isp-net-adminator/pull/260/files#diff-bf99864cec6493c7e3d8e681dd2fc01c1ffc7480b5728411b20c7af4bbf88b37L874
if err != nil {
errM := fmt.Sprintf("runCommand failed: %v, (itemId: %d)", err, itemId)
logger.Error(errM)
return err
}

return nil
}
Expand Down Expand Up @@ -109,13 +224,13 @@ func ExecuteCommand(inputCommand string, inputCommandArgs []string) error {
cmd.Stdout = nil
continue
}
logger.Info(fmt.Printf("COMMAND STDOUT: %v \n",line))
logger.Infof("COMMAND STDOUT: %s", line)
case line, open := <-cmd.Stderr:
if !open {
cmd.Stderr = nil
continue
}
logger.Error(fmt.Printf("COMMAND STDERR: %v \n", line))
logger.Errorf("COMMAND STDERR: %s", line)
}
}
}()
Expand All @@ -129,14 +244,22 @@ func ExecuteCommand(inputCommand string, inputCommandArgs []string) error {
logger.Debugf("command executed (PID: %v, complete: %v, exit code: %v)",
status.PID, status.Complete, status.Exit)

if (status.Exit != 0 || !status.Complete) {
errM = fmt.Errorf("command failed! (exitCode: %d, complete: %t)", status.Exit, status.Complete);
if status.Exit != 0 || !status.Complete {
errM = fmt.Errorf("command failed! (exitCode: %d, complete: %t)", status.Exit, status.Complete)
}

if errM != nil {
logger.Error(errM)
return fmt.Errorf("%s", errM)
return fmt.Errorf("%w", errM)
}

return nil
}

func getIpAddress (fqdn string) string {
addr, err := net.ResolveIPAddr("ip", fqdn)
if err != nil {
panic(err)
}
return addr.String()
}
30 changes: 23 additions & 7 deletions internal/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ package flag
import (
"strings"
"time"

"fmt"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
)

var (
// for testing
FlagHelp bool
FlagPhp bool
FlagPhp bool

// for asyncq
FlagRedisAddr string
FlagRedisAddr string
FlagGroupGracePeriod time.Duration
FlagGroupMaxDelay time.Duration
FlagGroupMaxSize int
FlagGroupMaxDelay time.Duration
FlagGroupMaxSize int

ConfigGatewayWifiFqdn string
ConfigGateway3Fqdn string
)

func init() {
Expand All @@ -40,14 +43,27 @@ func init() {

// environments variables support
viper.BindEnv("redis-addr")

FlagHelp = viper.GetBool("help")
FlagPhp = viper.GetBool("php-version")

FlagRedisAddr = viper.GetString("redis-addr")
FlagGroupGracePeriod = viper.GetDuration("asynq-grace-period")
FlagGroupMaxDelay = viper.GetDuration("asynq-max-delay")
FlagGroupMaxSize = viper.GetInt("asynq-max-size")
FlagGroupMaxSize = viper.GetInt("asynq-max-size")

viper.SetConfigName("config") // name of config file (without extension)
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
viper.AddConfigPath("config") // path to look for the config file in

err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %w", err))
}

ConfigGatewayWifiFqdn = viper.GetString("gateways.gateway-wifi.fqdn")
ConfigGateway3Fqdn = viper.GetString("gateways.gateway-3.fqdn")

}

func PrintDefaults() {
Expand Down
Loading