kairos-agent/internal/vpn/setup.go
Ettore Di Giacinto 63cd28d1cb Split off cli into separate binaries (#37)
* 🎨 Split off cli into separate binaries

This commit splits off the cli into 3 binaries:
- agent
- cli
- provider

The provider now is a separate component that can be tested by itself
and have its own lifecycle. This paves the way to a ligher c3os variant,
HA support and other features that can be provided on runtime.

This is working, but still there are low hanging fruit to care about.

Fixes #14

* 🤖 Add provider bin to releases

* ⚙️ Handle signals

* ⚙️ Reduce buildsize footprint

* 🎨 Scan for providers also in /system/providers

* 🤖 Run goreleaser

* 🎨 Refactoring
2022-07-04 22:39:34 +02:00

91 lines
2.2 KiB
Go

package vpn
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/c3os-io/c3os/internal/machine"
"github.com/c3os-io/c3os/internal/machine/systemd"
"github.com/c3os-io/c3os/internal/utils"
"github.com/c3os-io/c3os/pkg/config"
yip "github.com/mudler/yip/pkg/schema"
)
func Setup(instance, apiAddress, rootDir string, start bool, c *config.Config) error {
if c.C3OS == nil || c.C3OS.NetworkToken == "" {
return fmt.Errorf("no network token defined")
}
svc, err := machine.EdgeVPN(instance, rootDir)
if err != nil {
return err
}
apiAddress = strings.ReplaceAll(apiAddress, "https://", "")
apiAddress = strings.ReplaceAll(apiAddress, "http://", "")
vpnOpts := map[string]string{
"EDGEVPNTOKEN": c.C3OS.NetworkToken,
"API": "true",
"APILISTEN": apiAddress,
"EDGEVPNLOWPROFILEVPN": "true",
"DHCP": "true",
"DHCPLEASEDIR": "/usr/local/.c3os/lease",
}
// Override opts with user-supplied
for k, v := range c.VPN {
vpnOpts[k] = v
}
if c.C3OS.DNS {
vpnOpts["DNSADDRESS"] = "127.0.0.1:53"
vpnOpts["DNSFORWARD"] = "true"
if !utils.IsOpenRCBased() {
if _, err := os.Stat("/etc/sysconfig/network/config"); err == nil {
utils.WriteEnv("/etc/sysconfig/network/config", map[string]string{
"NETCONFIG_DNS_STATIC_SERVERS": "127.0.0.1",
})
if utils.Flavor() == "opensuse" {
// TODO: This is dependant on wickedd, move this out in its own network detection block
svc, err := systemd.NewService(systemd.WithName("wickedd"))
if err == nil {
svc.Restart()
}
}
}
}
if err := config.SaveCloudConfig("dns", yip.YipConfig{
Name: "DNS Configuration",
Stages: map[string][]yip.Stage{
config.NetworkStage.String(): {{Dns: yip.DNS{Nameservers: []string{"127.0.0.1"}}}}},
}); err != nil {
fmt.Println("Failed installing DNS")
}
}
os.MkdirAll("/etc/systemd/system.conf.d/", 0600)
// Setup edgevpn instance
err = utils.WriteEnv(filepath.Join(rootDir, "/etc/systemd/system.conf.d/edgevpn-c3os.env"), vpnOpts)
if err != nil {
return err
}
err = svc.WriteUnit()
if err != nil {
return err
}
if start {
err = svc.Start()
if err != nil {
return err
}
return svc.Enable()
}
return nil
}