Skip to content

Commit

Permalink
dev: design system interface config
Browse files Browse the repository at this point in the history
  • Loading branch information
wwhai committed Aug 16, 2023
1 parent 9019b5f commit ceaf2cc
Show file tree
Hide file tree
Showing 15 changed files with 318 additions and 238 deletions.
11 changes: 11 additions & 0 deletions plugin/http_server/dto/etcnet_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package service

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"`
}
16 changes: 16 additions & 0 deletions plugin/http_server/dto/netplan_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package service

type hwInterface struct {
Dhcp4 bool `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]hwInterface `yaml:"ethernets" json:"ethernets,omitempty"`
}
type NetplanConfigDto struct {
Network network `yaml:"network" json:"network,omitempty"`
}
1 change: 1 addition & 0 deletions plugin/http_server/http_api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (s *HttpApiServer) registerModel() {
&model.MGenericGroup{},
&model.MGenericGroupRelation{},
&model.MProtocolApp{},
&model.MNetworkConfig{},
)
}

Expand Down
8 changes: 0 additions & 8 deletions plugin/http_server/model/dto.go

This file was deleted.

9 changes: 9 additions & 0 deletions plugin/http_server/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,12 @@ type MProtocolApp struct {
Type string `gorm:"not null"` // 类型: IN OUT DEVICE APP
Content string `gorm:"not null"` // 协议包的内容
}

/*
*
* 系统配置参数, 直接以String保存,完了以后再加工成Dto结构体
*
*/
type MNetworkConfig struct {
Network string `yaml:"network" json:"network,omitempty"`
}
207 changes: 0 additions & 207 deletions plugin/http_server/service/system_config_service.go

This file was deleted.

75 changes: 75 additions & 0 deletions plugin/http_server/service/ubuntu16_network_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package service

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

// # /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)
}

/*
*
* 将结构体写入配置文件
sudo systemctl restart networking
sudo service networking restart
*
*/
func ApplyConfig(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, func(dhcpEnabled bool) string {
if dhcpEnabled {
return "dhcp"
}
return "static"
}(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\n", strings.Join(iface.DNS, " ")))
}

configText := strings.Join(configLines, "\n")
return os.WriteFile("/etc/network/interfaces", []byte(configText), 0644)
}
Loading

0 comments on commit ceaf2cc

Please sign in to comment.