2021-03-26 00:17:52 +08:00
package main
import (
"fmt"
"github.com/gravitl/netmaker/netclient/functions"
"golang.zx2c4.com/wireguard/wgctrl"
nodepb "github.com/gravitl/netmaker/grpc"
"flag"
"os"
2021-03-28 00:50:27 +08:00
"os/exec"
"strconv"
2021-03-28 01:48:50 +08:00
"strings"
2021-03-26 00:17:52 +08:00
"log"
)
const (
// name of the service
name = "wcdaemon"
description = "Wirecat Daemon Service"
)
var password string
var group string
var server string
var accesskey string
var (
wgclient * wgctrl . Client
)
var (
wcclient nodepb . NodeServiceClient
)
func main ( ) {
tpassword := flag . String ( "p" , "changeme" , "This node's password for accessing the server regularly" )
taccesskey := flag . String ( "k" , "badkey" , "an access key generated by the server and used for one-time access (install only)" )
tserver := flag . String ( "s" , "localhost:50051" , "The location (including port) of the remote gRPC server." )
tgroup := flag . String ( "g" , "badgroup" , "The node group you are attempting to join." )
2021-03-28 01:48:50 +08:00
tnoauto := flag . Bool ( "na" , false , "No auto mode. If true, netmclient will not be installed as a system service and you will have to retrieve updates manually via checkin command." )
tnoforward := flag . Bool ( "nf" , false , "No Forward mode. If true, netclient will not check for IP forwarding. This may break functionality" )
2021-03-26 00:17:52 +08:00
command := flag . String ( "c" , "required" , "The command to run" )
flag . Parse ( )
2021-03-28 00:50:27 +08:00
getID := exec . Command ( "id" , "-u" )
out , err := getID . Output ( )
if err != nil {
log . Fatal ( err )
}
id , err := strconv . Atoi ( string ( out [ : len ( out ) - 1 ] ) )
if err != nil {
log . Fatal ( err )
}
if id != 0 {
log . Fatal ( "This program must be run with elevated privileges (sudo). This program installs a SystemD service and configures WireGuard and networking rules. Please re-run with sudo/root." )
}
2021-03-28 01:48:50 +08:00
_ , err = exec . LookPath ( "wg" )
if err != nil {
log . Println ( err )
log . Fatal ( "WireGuard not installed. Please install WireGuard (wireguard-tools) and try again." )
}
2021-03-26 00:17:52 +08:00
switch * command {
case "required" :
fmt . Println ( "command flag 'c' is required. Pick one of |install|checkin|update|remove|" )
os . Exit ( 1 )
log . Fatal ( "Exiting" )
case "install" :
2021-03-28 01:48:50 +08:00
if ! * tnoforward {
forward := exec . Command ( "sysctl" , "net.ipv4.ip_forward" )
out , err := forward . Output ( )
if err != nil {
log . Fatal ( err )
}
//s := strings.Split(string(out), " ", "\n")
s := strings . Fields ( string ( out ) )
if err != nil {
log . Fatal ( err )
}
if s [ 2 ] != "1" {
log . Fatal ( "It is recommended to enable IP Forwarding. Current status is: " + s [ 2 ] + ", but should be 1. if you would like to run without IP Forwarding, re-run with flag '-nf true'" )
}
}
2021-03-26 00:17:52 +08:00
fmt . Println ( "Beginning agent installation." )
err := functions . Install ( * taccesskey , * tpassword , * tserver , * tgroup , * tnoauto )
if err != nil {
fmt . Println ( "Error installing: " , err )
2021-03-26 10:29:36 +08:00
fmt . Println ( "Cleaning up (uninstall)" )
2021-03-26 00:17:52 +08:00
err = functions . Remove ( )
if err != nil {
2021-03-26 10:29:36 +08:00
fmt . Println ( "Error uninstalling: " , err )
fmt . Println ( "Wiping local." )
err = functions . WipeLocal ( )
if err != nil {
fmt . Println ( "Error removing artifacts: " , err )
}
2021-03-26 22:35:52 +08:00
err = functions . RemoveSystemDServices ( )
if err != nil {
fmt . Println ( "Error removing services: " , err )
}
2021-03-26 00:17:52 +08:00
}
os . Exit ( 1 )
}
case "service-install" :
fmt . Println ( "Beginning service installation." )
err := functions . ConfigureSystemD ( )
if err != nil {
fmt . Println ( "Error installing service: " , err )
os . Exit ( 1 )
}
case "service-uninstall" :
fmt . Println ( "Beginning service uninstall." )
err := functions . RemoveSystemDServices ( )
if err != nil {
fmt . Println ( "Error installing service: " , err )
os . Exit ( 1 )
}
case "checkin" :
fmt . Println ( "Beginning node check in." )
err := functions . CheckIn ( )
if err != nil {
fmt . Println ( "Error checking in: " , err )
os . Exit ( 1 )
}
case "remove" :
fmt . Println ( "Beginning node cleanup." )
err := functions . Remove ( )
if err != nil {
2021-03-28 00:04:44 +08:00
/ *
2021-03-26 10:29:36 +08:00
fmt . Println ( "Error uninstalling: " , err )
fmt . Println ( "Wiping local." )
err = functions . WipeLocal ( )
if err != nil {
fmt . Println ( "Error removing artifacts: " , err )
}
2021-03-26 22:35:52 +08:00
err = functions . RemoveSystemDServices ( )
if err != nil {
fmt . Println ( "Error removing services: " , err )
}
2021-03-28 00:04:44 +08:00
* /
2021-03-26 00:17:52 +08:00
fmt . Println ( "Error deleting node: " , err )
os . Exit ( 1 )
}
}
fmt . Println ( "Command " + * command + " Executed Successfully" )
}