This repository has been archived by the owner on Mar 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
device-config.go
86 lines (77 loc) · 2.89 KB
/
device-config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Ubuntu Core Configuration
// Copyright 2020 Canonical Ltd. All rights reserved.
package main
import (
"flag"
"fmt"
"github.com/CanonicalLtd/device-config/config"
"github.com/CanonicalLtd/device-config/datastore/memory"
"github.com/CanonicalLtd/device-config/service"
"github.com/CanonicalLtd/device-config/service/dbus"
"github.com/CanonicalLtd/device-config/service/network"
"github.com/CanonicalLtd/device-config/service/snapd"
"github.com/CanonicalLtd/device-config/service/system"
"github.com/CanonicalLtd/device-config/service/transfer"
"github.com/CanonicalLtd/device-config/web"
"log"
"os"
"strings"
)
func main() {
// Get the application parameters
settings := config.ReadParameters()
configure(settings)
// Set up the dependency chain
memorySrv := memory.NewStore()
authSrv := service.NewAuthService(memorySrv)
snapdClient := snapd.NewClientAdapter()
dBus, err := dbus.NewDBus()
if err != nil {
log.Fatal(err)
}
timeSrv := service.NewTime(dBus)
systemSrv := system.NewSystem()
netSrv := network.Factory(settings, dBus)
xferSrv := transfer.NewTransfer(dBus, snapdClient)
srv := web.NewWebService(settings, authSrv, netSrv, snapdClient, timeSrv, systemSrv, xferSrv)
// Start the web service
log.Fatal(srv.Start())
}
func configure(cfg *config.Settings) {
var (
configureOnly bool
iface string
listenOn string
factoryReset bool
snapControl bool
useNM bool
hideIfaces string
)
flag.BoolVar(&configureOnly, "configure", false, "Configure the application and exit")
flag.StringVar(&iface, "interface", config.DefaultInterfaceIP, "The default network interface for the service")
flag.StringVar(&listenOn, "listenon", config.DefaultInterfaceDevice, "Force the service to listen a specific network device e.g. eth0")
flag.BoolVar(&factoryReset, "factoryreset", config.DefaultFactoryReset, "Display option that allows factory reset of the device")
flag.BoolVar(&snapControl, "snapcontrol", config.DefaultSnapControl, "Display configuration that needs the snapd-control interface")
flag.BoolVar(&useNM, "nm", config.DefaultUseNetworkManager, "Use network manager instead of netplan")
flag.StringVar(&hideIfaces, "hide", "", "Comma-separated list of interfaces to hide")
flag.Parse()
log.Printf("Device config: configure=%v, snapcontrol=%v, interface=%v, nm=%v, hide=%v, factoryreset=%v", configureOnly, snapControl, iface, useNM, hideIfaces, factoryReset)
if !configureOnly {
// No changes if we're not configuring the app
return
}
// Update the settings
cfg.NetworkInterfaceIP = iface
cfg.NetworkInterfaceDevice = listenOn
cfg.SnapControl = snapControl
cfg.FactoryReset = factoryReset
cfg.UseNetworkManager = useNM
cfg.HideInterfaces = strings.Split(hideIfaces, ",")
err := config.StoreParameters(cfg)
if err != nil {
fmt.Println("Error saving parameters:", err)
os.Exit(1)
}
fmt.Println("Device Config configured successfully")
os.Exit(0)
}