Fernando K pushed to branch main at Root / CLI / netboxadm

Commits:

7 changed files:

Changes:

  • cmd/backup/update.go
    ... ... @@ -32,10 +32,11 @@ var UpdateCmd = &cobra.Command{
    32 32
     	PreRunE: config.AssertGitlabConfigured,
    
    33 33
     }
    
    34 34
     
    
    35
    +const BACKUP_REPO_URL = "https://gitlab.c3sl.ufpr.br/root/services/vmbackups.git"
    
    36
    +
    
    35 37
     func updateBackup(cmd *cobra.Command, args []string) error {
    
    36 38
     	localPath := "/tmp/backup"
    
    37 39
     	callRenderScript := func() error {
    
    38
    -		// run the rendering script
    
    39 40
     		netboxURL := viper.GetString("netbox.url")
    
    40 41
     		netboxToken := viper.GetString("netbox.token")
    
    41 42
     		vmbackups.Render(localPath, netboxURL, netboxToken)
    
    ... ... @@ -44,7 +45,7 @@ func updateBackup(cmd *cobra.Command, args []string) error {
    44 45
     	}
    
    45 46
     
    
    46 47
     	return nethuh.PerformPipelineUpdateWorkflow(
    
    47
    -		viper.GetString("backup.repoURL"),
    
    48
    +		BACKUP_REPO_URL,
    
    48 49
     		localPath,
    
    49 50
     		callRenderScript,
    
    50 51
     	)
    

  • cmd/config/config.go
    ... ... @@ -19,9 +19,7 @@ package config
    19 19
     
    
    20 20
     import (
    
    21 21
     	"errors"
    
    22
    -	"os"
    
    23 22
     	"os/exec"
    
    24
    -	"path/filepath"
    
    25 23
     	"strings"
    
    26 24
     
    
    27 25
     	"github.com/spf13/cobra"
    
    ... ... @@ -54,29 +52,17 @@ func InitConfig() error {
    54 52
     	viper.SetConfigName("config")
    
    55 53
     	viper.SetConfigType("yml")
    
    56 54
     
    
    57
    -	home, err := os.UserHomeDir()
    
    58
    -	if err != nil {
    
    59
    -		return err
    
    60
    -	}
    
    61
    -
    
    62
    -	configPath := filepath.Join(home, ".config", "netboxadm")
    
    63
    -	viper.AddConfigPath(configPath)
    
    64
    -	viper.AddConfigPath(".")
    
    65 55
     	viper.MustBindEnv("netbox.url", "NETBOX_URL")
    
    66 56
     	viper.MustBindEnv("netbox.token", "NETBOX_TOKEN")
    
    67 57
     	viper.MustBindEnv("gitlab.accessToken", "GITLAB_ACCESS_TOKEN")
    
    68 58
     	viper.MustBindEnv("gitlab.username", "GITLAB_USERNAME")
    
    69 59
     
    
    70
    -	if err := viper.ReadInConfig(); err != nil {
    
    71
    -		return errors.New("failed to read config.yml")
    
    72
    -	}
    
    73
    -
    
    74 60
     	if viper.GetString("netbox.url") == "" {
    
    75
    -		return errors.New("netbox.url should be set either in config or environment variable NETBOX_URL")
    
    61
    +		return errors.New("netbox.url should be set either in environment variable NETBOX_URL")
    
    76 62
     	}
    
    77 63
     
    
    78 64
     	if viper.GetString("netbox.token") == "" {
    
    79
    -		return errors.New("netbox.token should be set either in config or environment variable NETBOX_TOKEN")
    
    65
    +		return errors.New("netbox.token should be set either in environment variable NETBOX_TOKEN")
    
    80 66
     	}
    
    81 67
     
    
    82 68
     	return nil
    
    ... ... @@ -84,11 +70,11 @@ func InitConfig() error {
    84 70
     
    
    85 71
     func AssertGitlabConfigured(cmd *cobra.Command, args []string) error {
    
    86 72
     	if viper.GetString("gitlab.accessToken") == "" {
    
    87
    -		return errors.New("gitlab.accessToken should be set either in config or environment variable GITLAB_ACCESS_TOKEN")
    
    73
    +		return errors.New("gitlab.accessToken should be set either in environment variable GITLAB_ACCESS_TOKEN")
    
    88 74
     	}
    
    89 75
     
    
    90 76
     	if viper.GetString("gitlab.username") == "" {
    
    91
    -		return errors.New("gitlab.username should be set either in config or environment variable GITLAB_USERNAME")
    
    77
    +		return errors.New("gitlab.username should be set either in environment variable GITLAB_USERNAME")
    
    92 78
     	}
    
    93 79
     
    
    94 80
     	return nil
    

  • cmd/dhcp/update.go
    ... ... @@ -18,14 +18,11 @@
    18 18
     package dhcp
    
    19 19
     
    
    20 20
     import (
    
    21
    -	"fmt"
    
    22
    -	"os"
    
    23
    -	"os/exec"
    
    24
    -
    
    25 21
     	"github.com/spf13/cobra"
    
    26 22
     	"github.com/spf13/viper"
    
    27 23
     	"gitlab.c3sl.ufpr.br/root/netboxadm/cmd/config"
    
    28 24
     	"gitlab.c3sl.ufpr.br/root/netboxadm/internal/nethuh"
    
    25
    +	"gitlab.c3sl.ufpr.br/root/services/dhcp"
    
    29 26
     )
    
    30 27
     
    
    31 28
     var UpdateCmd = &cobra.Command{
    
    ... ... @@ -35,27 +32,20 @@ var UpdateCmd = &cobra.Command{
    35 32
     	PreRunE: config.AssertGitlabConfigured,
    
    36 33
     }
    
    37 34
     
    
    35
    +const DHCP_REPO_URL = "https://gitlab.c3sl.ufpr.br/root/services/dhcp.git"
    
    36
    +
    
    38 37
     func updateDHCP(cmd *cobra.Command, args []string) error {
    
    39 38
     	localPath := "/tmp/dhcp"
    
    40 39
     	callRenderScript := func() error {
    
    41
    -		// run the rendering script
    
    42
    -		// won't work in a workspace without the requirements
    
    43
    -		// they are: go
    
    44
    -		cmdRender := exec.Command("go", "run", "render.go", "--suppress_output")
    
    45
    -		cmdRender.Dir = localPath
    
    46
    -		cmdRender.Stdout = os.Stdout
    
    47
    -		cmdRender.Stderr = os.Stderr
    
    48
    -
    
    49
    -		err := cmdRender.Run()
    
    50
    -		if err != nil {
    
    51
    -			return fmt.Errorf("error running DHCP render script: %w", err)
    
    52
    -		}
    
    40
    +		netboxURL := viper.GetString("netbox.url")
    
    41
    +		netboxToken := viper.GetString("netbox.token")
    
    42
    +		dhcp.Render(localPath, netboxURL, netboxToken)
    
    53 43
     
    
    54 44
     		return nil
    
    55 45
     	}
    
    56 46
     
    
    57 47
     	return nethuh.PerformPipelineUpdateWorkflow(
    
    58
    -		viper.GetString("dhcp.repoURL"),
    
    48
    +		DHCP_REPO_URL,
    
    59 49
     		localPath,
    
    60 50
     		callRenderScript,
    
    61 51
     	)
    

  • cmd/dns/update.go
    ... ... @@ -18,14 +18,11 @@
    18 18
     package dns
    
    19 19
     
    
    20 20
     import (
    
    21
    -	"fmt"
    
    22
    -	"os"
    
    23
    -	"os/exec"
    
    24
    -
    
    25 21
     	"github.com/spf13/cobra"
    
    26
    -	"github.com/spf13/viper"
    
    27 22
     	"gitlab.c3sl.ufpr.br/root/netboxadm/cmd/config"
    
    28 23
     	"gitlab.c3sl.ufpr.br/root/netboxadm/internal/nethuh"
    
    24
    +	"gitlab.c3sl.ufpr.br/root/services/dns"
    
    25
    +	"github.com/spf13/viper"
    
    29 26
     )
    
    30 27
     
    
    31 28
     var UpdateCmd = &cobra.Command{
    
    ... ... @@ -35,24 +32,20 @@ var UpdateCmd = &cobra.Command{
    35 32
     	PreRunE: config.AssertGitlabConfigured,
    
    36 33
     }
    
    37 34
     
    
    35
    +const DNS_REPO_URL = "https://gitlab.c3sl.ufpr.br/root/services/dns.git"
    
    36
    +
    
    38 37
     func updateDNS(cmd *cobra.Command, args []string) error {
    
    39 38
     	localPath := "/tmp/dns"
    
    40 39
     	callRenderScript := func() error {
    
    41
    -		cmdRender := exec.Command("go", "run", "render.go", "--suppress_output")
    
    42
    -		cmdRender.Dir = localPath
    
    43
    -		cmdRender.Stdout = os.Stdout
    
    44
    -		cmdRender.Stderr = os.Stderr
    
    45
    -
    
    46
    -		err := cmdRender.Run()
    
    47
    -		if err != nil {
    
    48
    -			return fmt.Errorf("error running DNS render script: %w", err)
    
    49
    -		}
    
    40
    +		netboxURL := viper.GetString("netbox.url")
    
    41
    +		netboxToken := viper.GetString("netbox.token")
    
    42
    +		dns.Render(localPath, netboxURL, netboxToken)
    
    50 43
     
    
    51 44
     		return nil
    
    52 45
     	}
    
    53 46
     
    
    54 47
     	return nethuh.PerformPipelineUpdateWorkflow(
    
    55
    -		viper.GetString("dns.repoURL"),
    
    48
    +		DNS_REPO_URL,
    
    56 49
     		localPath,
    
    57 50
     		callRenderScript,
    
    58 51
     	)
    

  • config.yml deleted
    1
    -netbox:
    
    2
    -  url: https://ronnie.c3sl.ufpr.br
    
    3
    -
    
    4
    -dns:
    
    5
    -  repoURL: https://gitlab.c3sl.ufpr.br/root/services/dns.git
    
    6
    -
    
    7
    -dhcp:
    
    8
    -  repoURL: https://gitlab.c3sl.ufpr.br/root/services/dhcp.git
    
    9
    -
    
    10
    -backup:
    
    11
    -  repoURL: https://gitlab.c3sl.ufpr.br/root/services/vmbackups.git

  • go.mod
    ... ... @@ -13,11 +13,17 @@ require (
    13 13
     	github.com/seancfoley/ipaddress-go v1.7.1
    
    14 14
     	github.com/spf13/cobra v1.9.1
    
    15 15
     	github.com/spf13/viper v1.20.1
    
    16
    +	gitlab.c3sl.ufpr.br/root/services/dhcp v1.0.2
    
    17
    +	gitlab.c3sl.ufpr.br/root/services/dns v1.0.2
    
    16 18
     	gitlab.c3sl.ufpr.br/root/services/vmbackups v1.0.0
    
    17 19
     )
    
    18 20
     
    
    19 21
     replace gitlab.c3sl.ufpr.br/root/services/vmbackups => gitlab.c3sl.ufpr.br/root/services/vmbackups.git v1.0.0
    
    20 22
     
    
    23
    +replace gitlab.c3sl.ufpr.br/root/services/dns => gitlab.c3sl.ufpr.br/root/services/dns.git v1.0.2
    
    24
    +
    
    25
    +replace gitlab.c3sl.ufpr.br/root/services/dhcp => gitlab.c3sl.ufpr.br/root/services/dhcp.git v1.0.2
    
    26
    +
    
    21 27
     require (
    
    22 28
     	dario.cat/mergo v1.0.0 // indirect
    
    23 29
     	github.com/Microsoft/go-winio v0.6.2 // indirect
    

  • go.sum
    ... ... @@ -188,6 +188,10 @@ github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM
    188 188
     github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
    
    189 189
     github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
    
    190 190
     github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
    
    191
    +gitlab.c3sl.ufpr.br/root/services/dhcp.git v1.0.2 h1:Wi9gBfwattPz10KdZgUhCyO1n2wNn/LzT85qbMh30Tw=
    
    192
    +gitlab.c3sl.ufpr.br/root/services/dhcp.git v1.0.2/go.mod h1:1sv2r7hZ1dPsYwn71xBoBIzVgUlPBkhwu1McUXDnxwg=
    
    193
    +gitlab.c3sl.ufpr.br/root/services/dns.git v1.0.2 h1:8x8pNllXTwyf5G9YvztymS+n53kNIxxB60DIuSLilAM=
    
    194
    +gitlab.c3sl.ufpr.br/root/services/dns.git v1.0.2/go.mod h1:DgzAim9cbJw3WxecQh2KDPdM2/GwEuUzVwgVz0cZ7Aw=
    
    191 195
     gitlab.c3sl.ufpr.br/root/services/vmbackups.git v1.0.0 h1:e5q7dA8aNDqTI0+7mXs+J8Xm3nejXItK6XVvEZbsNXg=
    
    192 196
     gitlab.c3sl.ufpr.br/root/services/vmbackups.git v1.0.0/go.mod h1:eyNqTBI4DuroKzEbX7a1kjLuDB272vbp4s+TIT3xR3I=
    
    193 197
     go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=