+ install: check static ip

* move code to network_utils

+ add tests

'dhcp' package has its own network_utils for now

+ doc: add static ip configuration algorithm on Ubuntu
This commit is contained in:
Simon Zolin
2019-11-08 17:53:12 +03:00
parent 207e0236f7
commit 5c385521c4
7 changed files with 806 additions and 312 deletions

View File

@@ -22,6 +22,14 @@ type firstRunData struct {
Interfaces map[string]interface{} `json:"interfaces"`
}
type netInterfaceJSON struct {
Name string `json:"name"`
MTU int `json:"mtu"`
HardwareAddr string `json:"hardware_address"`
Addresses []string `json:"ip_addresses"`
Flags string `json:"flags"`
}
// Get initial installation settings
func handleInstallGetAddresses(w http.ResponseWriter, r *http.Request) {
data := firstRunData{}
@@ -36,7 +44,14 @@ func handleInstallGetAddresses(w http.ResponseWriter, r *http.Request) {
data.Interfaces = make(map[string]interface{})
for _, iface := range ifaces {
data.Interfaces[iface.Name] = iface
ifaceJSON := netInterfaceJSON{
Name: iface.Name,
MTU: iface.MTU,
HardwareAddr: iface.HardwareAddr,
Addresses: iface.Addresses,
Flags: iface.Flags,
}
data.Interfaces[iface.Name] = ifaceJSON
}
w.Header().Set("Content-Type", "application/json")
@@ -48,9 +63,10 @@ func handleInstallGetAddresses(w http.ResponseWriter, r *http.Request) {
}
type checkConfigReqEnt struct {
Port int `json:"port"`
IP string `json:"ip"`
Autofix bool `json:"autofix"`
Port int `json:"port"`
IP string `json:"ip"`
Autofix bool `json:"autofix"`
SetStaticIP bool `json:"set_static_ip"`
}
type checkConfigReq struct {
Web checkConfigReqEnt `json:"web"`
@@ -61,9 +77,15 @@ type checkConfigRespEnt struct {
Status string `json:"status"`
CanAutofix bool `json:"can_autofix"`
}
type staticIPJSON struct {
Static string `json:"static"`
IP string `json:"ip"`
Error string `json:"error"`
}
type checkConfigResp struct {
Web checkConfigRespEnt `json:"web"`
DNS checkConfigRespEnt `json:"dns"`
Web checkConfigRespEnt `json:"web"`
DNS checkConfigRespEnt `json:"dns"`
StaticIP staticIPJSON `json:"static_ip"`
}
// Check if ports are available, respond with results
@@ -108,6 +130,33 @@ func handleInstallCheckConfig(w http.ResponseWriter, r *http.Request) {
if err != nil {
respData.DNS.Status = fmt.Sprintf("%v", err)
} else {
interfaceName := getInterfaceByIP(reqData.DNS.IP)
staticIPStatus := "yes"
if len(interfaceName) == 0 {
staticIPStatus = "error"
respData.StaticIP.Error = fmt.Sprintf("Couldn't find network interface by IP %s", reqData.DNS.IP)
} else if reqData.DNS.SetStaticIP {
err = setStaticIP(interfaceName)
staticIPStatus = "error"
respData.StaticIP.Error = err.Error()
} else {
// check if we have a static IP
isStaticIP, err := hasStaticIP(interfaceName)
if err != nil {
staticIPStatus = "error"
respData.StaticIP.Error = err.Error()
} else if !isStaticIP {
staticIPStatus = "no"
respData.StaticIP.IP = getFullIP(interfaceName)
}
}
respData.StaticIP.Static = staticIPStatus
}
}