Skip to content

Commit

Permalink
dev: ubuntu network config support
Browse files Browse the repository at this point in the history
  • Loading branch information
wwhai committed Aug 15, 2023
1 parent e8467ff commit 9019b5f
Show file tree
Hide file tree
Showing 9 changed files with 349 additions and 57 deletions.
9 changes: 5 additions & 4 deletions engine/runner.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package engine

import (
"os"
"os/signal"
"strings"
"syscall"

mqttserver "github.com/hootrhino/rulex/plugin/mqtt_server"
netdiscover "github.com/hootrhino/rulex/plugin/net_discover"
ttyterminal "github.com/hootrhino/rulex/plugin/ttyd_terminal"
usbmonitor "github.com/hootrhino/rulex/plugin/usb_monitor"
"gopkg.in/ini.v1"
"os"
"os/signal"
"strings"
"syscall"

"github.com/hootrhino/rulex/core"
"github.com/hootrhino/rulex/glogger"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,6 @@ require (
google.golang.org/genproto v0.0.0-20230525234025-438c736192d0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230525234020-1aefcd67740a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 5 additions & 5 deletions plugin/http_server/appstack_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ AppDESCRIPTION = "%s"
`
const defaultLuaMain = `
function Main(arg)
print("Hello World:", applib:Time())
applib:Debug("Hello World:" .. applib:Time())
return 0
end
`
Expand Down Expand Up @@ -216,9 +216,9 @@ func CreateApp(c *gin.Context, hs *HttpApiServer) {
}
// 自启动立即运行
if form.AutoStart {
glogger.GLogger.Debugf("app autoStart allowed:%s-%s-%s", newUUID, form.Version, form.Name)
glogger.GLogger.Debugf("App autoStart allowed:%s-%s-%s", newUUID, form.Version, form.Name)
if err2 := hs.ruleEngine.StartApp(newUUID); err2 != nil {
glogger.GLogger.Error("app autoStart failed:", err2)
glogger.GLogger.Error("App autoStart failed:", err2)
}
}
c.JSON(common.HTTP_OK, common.OkWithData("app create successfully"))
Expand Down Expand Up @@ -284,15 +284,15 @@ func UpdateApp(c *gin.Context, hs *HttpApiServer) {
}
//
if form.AutoStart {
glogger.GLogger.Debugf("app autoStart allowed:%s-%s-%s", form.UUID, form.Version, form.Name)
glogger.GLogger.Debugf("App autoStart allowed:%s-%s-%s", form.UUID, form.Version, form.Name)
// 必须先load后start
if err := hs.ruleEngine.LoadApp(typex.NewApplication(
form.UUID, form.Name, form.Version, path)); err != nil {
c.JSON(common.HTTP_OK, common.Error400(err))
return
}
if err2 := hs.ruleEngine.StartApp(form.UUID); err2 != nil {
glogger.GLogger.Error("app autoStart failed:", err2)
glogger.GLogger.Error("App autoStart failed:", err2)
c.JSON(common.HTTP_OK, common.Error400(err2))
return
}
Expand Down
207 changes: 207 additions & 0 deletions plugin/http_server/service/system_config_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
package service

import (
"encoding/json"
"fmt"
"os"
"strings"

"gopkg.in/yaml.v2"
)

/*
* Ubuntu 18 以后的版本才支持
/etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
enp0s9:
dhcp4: no
addresses:
- 192.168.121.221/24
gateway4: 192.168.121.1
nameservers:
addresses: [8.8.8.8, 1.1.1.1]
*
*/
//
// 读取Ip状态(静态/动态) yaml
type Interface struct {
Dhcp4 string `yaml:"dhcp4" json:"dhcp4,omitempty"`
Addresses []string `yaml:"addresses" json:"addresses,omitempty"`
Gateway4 string `yaml:"gateway4" json:"gateway4,omitempty"`
Nameservers []string `yaml:"nameservers" json:"nameservers,omitempty"`
}
type Network struct {
Version int `yaml:"version" json:"version,omitempty"`
Renderer string `yaml:"renderer" json:"renderer,omitempty"`
Ethernets map[string]Interface `yaml:"ethernets" json:"ethernets,omitempty"`
}
type NetplanConfig struct {
Network Network `yaml:"network" json:"network,omitempty"`
}

func NewNetplanConfig() *NetplanConfig {
return &NetplanConfig{
Network: Network{
Version: 2,
Renderer: "NetworkManager",
Ethernets: map[string]Interface{
"eth0": {
Dhcp4: "no",
Addresses: []string{"192.168.128.1/24"},
Gateway4: "192.168.128.1",
Nameservers: []string{"114.114.114.114"},
},
"eth1": {
Dhcp4: "no",
Addresses: []string{"192.168.128.1/24"},
Gateway4: "192.168.128.2",
Nameservers: []string{"114.114.114.114"},
},
},
},
}
}
func (nc *NetplanConfig) FromJson(jsons string) error {
return json.Unmarshal([]byte(jsons), nc)
}

func (nc *NetplanConfig) FromYaml(jsons string) error {
return yaml.Unmarshal([]byte(jsons), nc)
}

func (nc *NetplanConfig) JsonString() string {
b, _ := json.Marshal(nc)
return string(b)
}
func (nc *NetplanConfig) YAMLString() string {
b, _ := yaml.Marshal(nc)
return string(b)
}

// # /etc/network/interfaces
//-------------------------------------------
// Static
//-------------------------------------------
// auto lo
// iface lo inet loopback
// auto eth0
// iface eth0 inet static
// address 192.168.1.100
// netmask 255.255.255.0
// gateway 192.168.1.1
// dns-nameservers 8.8.8.8 8.8.4.4

//-------------------------------------------
// DHCP
//-------------------------------------------
// auto lo
// iface lo inet loopback
// auto eth0
// iface eth0 inet dhcp

type EtcNetworkConfig struct {
Name string `json:"name,omitempty"`
Interface string `json:"interface,omitempty"`
Address string `json:"address,omitempty"`
Netmask string `json:"netmask,omitempty"`
Gateway string `json:"gateway,omitempty"`
DNS []string `json:"dns,omitempty"`
DHCPEnabled bool `json:"dhcp_enabled,omitempty"`
}

func (nc *EtcNetworkConfig) JsonString() string {
b, _ := json.Marshal(nc)
return string(b)
}

/*
*
* 解析配置文件
*
*/
func ParseEtcFile(content string) []EtcNetworkConfig {
lines := strings.Split(content, "\n")

var interfaces []EtcNetworkConfig
var currentInterface EtcNetworkConfig

for _, line := range lines {
fields := strings.Fields(line)
if len(fields) == 0 {
continue
}

switch fields[0] {
case "auto":
currentInterface = EtcNetworkConfig{Name: fields[1]}
case "iface":
if len(fields) < 3 {
continue
}
currentInterface.Interface = fields[2]
case "address":
if len(fields) < 2 {
continue
}
currentInterface.Address = fields[1]
case "netmask":
if len(fields) < 2 {
continue
}
currentInterface.Netmask = fields[1]
case "gateway":
if len(fields) < 2 {
continue
}
currentInterface.Gateway = fields[1]
case "dns-nameservers":
if len(fields) < 2 {
continue
}
currentInterface.DNS = fields[1:]
case "dhcp":
if len(fields) > 1 && fields[1] == "dhcp" {
currentInterface.DHCPEnabled = true
}
}

if len(currentInterface.Interface) > 0 {
interfaces = append(interfaces, currentInterface)
}
}

return interfaces
}

/*
*
* 将结构体写入配置文件
*
*/
func WriteInterfaceConfig(filePath string, iface EtcNetworkConfig) error {
configLines := []string{
"auto lo",
"iface lo inet loopback",
fmt.Sprintf("auto %s", iface.Name),
fmt.Sprintf("iface %s inet %s", iface.Interface, getInetType(iface.DHCPEnabled)),
}

if !iface.DHCPEnabled {
configLines = append(configLines, fmt.Sprintf(" address %s", iface.Address))
configLines = append(configLines, fmt.Sprintf(" netmask %s", iface.Netmask))
configLines = append(configLines, fmt.Sprintf(" gateway %s", iface.Gateway))
configLines = append(configLines, fmt.Sprintf(" dns-nameservers %s", strings.Join(iface.DNS, " ")))
}

configText := strings.Join(configLines, "\n")
return os.WriteFile(filePath, []byte(configText), 0644)
}
func getInetType(dhcpEnabled bool) string {
if dhcpEnabled {
return "dhcp"
}
return "static"
}
70 changes: 23 additions & 47 deletions plugin/http_server/system_config_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,6 @@ package httpserver

import "github.com/gin-gonic/gin"

// 网络配置结构体
type NetConfig struct {
DHCP string `json:"dhcp"`
Ip []string `json:"ip"`
Gateway string `json:"gateway"`
Names []string `json:"names"`
Version int `json:"version"`
}

// 读取Ip状态(静态/动态) yaml
type T struct {
Network struct {
Ethernets struct {
Eth struct {
DHCP string `yaml:"dhcp4"`
Ip []string `yaml:"addresses"`
Gateway string `yaml:"gateway4"`
Names struct {
Ip []string `yaml:"addresses"`
} `yaml:"names"`
} `yaml:"eth0"`
} `yaml:"ethernets"`
Version int `json:"version"`
} `yaml:"network"`
}

// 读取WIFI状态(静态/动态) yaml
type WT struct {
Network struct {
Ethernets struct {
Eth struct {
DHCP string `yaml:"dhcp4"`
Ip []string `yaml:"addresses"`
Gateway string `yaml:"gateway4"`
Names struct {
Ip []string `yaml:"addresses"`
} `yaml:"names"`
} `yaml:"wlan0"`
} `yaml:"ethernets"`
Version int `json:"version"`
} `yaml:"network"`
}

// 主要是针对WIFI、时区、IP地址设置

/*
*
* WIFI
Expand All @@ -72,8 +27,29 @@ func SetTime(c *gin.Context, hh *HttpApiServer) {
/*
*
* 设置静态网络IP等
*
*/
{
"network": {
"version": 2,
"renderer": "networkd",
"ethernets": {
"enp0s9": {
"dhcp4": "no",
"addresses": [
"192.168.121.221/24"
],
"gateway4": "192.168.121.1",
"nameservers": {
"addresses": [
"8.8.8.8",
"1.1.1.1"
]
}
}
}
}
}
*/
func SetStaticNetwork(c *gin.Context, hh *HttpApiServer) {
type Form struct {
}
Expand Down
4 changes: 4 additions & 0 deletions test/data/etcnet-dhcp.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
8 changes: 8 additions & 0 deletions test/data/etcnet-static.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
11 changes: 11 additions & 0 deletions test/data/netplan.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
network:
version: 2
renderer: networkd
ethernets:
enp0s9:
dhcp4: no
addresses:
- 192.168.121.221/24
gateway4: 192.168.121.1
nameservers:
addresses: [8.8.8.8, 1.1.1.1]
Loading

0 comments on commit 9019b5f

Please sign in to comment.