Skip to content

Commit

Permalink
Imporving the git bootstrapper and adding documentation on how to use
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasvinther committed Apr 28, 2022
1 parent 5ab3dcb commit 1f2490d
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 977 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: test build compile
.PHONY: test build compile start-nomad

test:
go test -v ./...
Expand All @@ -14,4 +14,7 @@ compile:
CGO_ENABLED=0 GOOS=freebsd GOARCH=386 go build -o bin/nomoperator-freebsd-386 main.go
CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -o bin/nomoperator.exe main.go

start-nomad:
./scripts/start-nomad.sh

all: test build
47 changes: 43 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
# nomad-gitops-operator
A GitOps operator for Hashicorp Nomad
# Nomoporator
Nomoporator is a GitOps operator for Hashicorp Nomad.

## How to use

### Setting up Nomoporator

#### Environment variables
It's also possible configure Nomoporator via environment variables by setting them like this:
```
NOMAD_ADDR - Required to overide the default of http://127.0.0.1:4646.
NOMAD_TOKEN - Required with ACLs enabled.
NOMAD_CACERT - Required with TLS enabled.
NOMAD_CLIENT_CERT - Required with TLS enabled.
NOMAD_CLIENT_KEY - Required with TLS enabled.
```

### Bootstrapping using a git repository

> Get help with `./nomoporator bootstrap git -h`
```
Bootstrap Nomad using a git repository
Usage:
nomoperator bootstrap git [git repo] [flags]
Flags:
--branch string git branch (default "main")
-h, --help help for git
--path string path relative to the repository root (default "/")
--url string git repository URL
Global Flags:
-a, --address string Address of the Nomad server
```

Use it like this:
```
./nomoperator bootstrap git --url https://github.com/jonasvinther/nomad-state.git --path /jobs --branch main
```

## Run as Nomad job
```yaml
Expand All @@ -11,10 +50,10 @@ job "nomoperator" {
driver = "exec"
config {
command = "nomoperator"
args = ["bootstrap", "git", "--url", "https://github.com/jonasvinther/nomad-state.git"]
args = ["bootstrap", "git", "--url", "https://github.com/jonasvinther/nomad-state.git", "--branch", "main", "--path", "/prod-env"]
}
artifact {
source = "https://github.com/jonasvinther/nomad-gitops-operator/releases/download/v0.0.1-pre/nomad-gitops-operator_0.0.1-pre_linux_amd64.tar.gz"
source = "https://github.com/jonasvinther/nomad-gitops-operator/releases/download/v0.0.1/nomad-gitops-operator_0.0.1_linux_amd64.tar.gz"
destination = "local"
mode = "any"
}
Expand Down
74 changes: 2 additions & 72 deletions cmd/bootstrap.go
Original file line number Diff line number Diff line change
@@ -1,87 +1,17 @@
package cmd

import (
"context"
"fmt"
"io"
"net/url"
"time"

"github.com/go-git/go-git/v5"
"github.com/spf13/cobra"

"nomad-gitops-operator/pkg/nomad"
"nomad-gitops-operator/pkg/repository"
)

type gitFlags struct {
url string
branch string
path string
username string
password string
}

var gitArgs gitFlags
import "github.com/spf13/cobra"

func init() {
rootCmd.AddCommand(bootstrapCmd)
bootstrapCmd.Flags().StringVar(&gitArgs.url, "url", "", "git repository URL")
bootstrapCmd.Flags().StringVar(&gitArgs.branch, "branch", "", "git branch [default \"main\"]")
bootstrapCmd.Flags().StringVar(&gitArgs.path, "path", "", "path relative to the repository root")
}

var bootstrapCmd = &cobra.Command{
Use: "bootstrap [git repo]",
Use: "bootstrap",
Short: "bootstrap a yaml file into a Vault instance",
Long: ``,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
repositoryURL, err := url.Parse(gitArgs.url)
if err != nil {
return err
}

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Minute*5))
defer cancel()

worktree, err := repository.CLone(ctx, repositoryURL)

if err != nil {
fmt.Printf("Error: %s\n", err)
}

// Reconcile
for true {
worktree.Pull(&git.PullOptions{RemoteName: "origin"})

fs := worktree.Filesystem
path := "/jobs/"
files, err := fs.ReadDir(path)
if err != nil {
return err
}

for _, file := range files {
filePath := fs.Join(path, file.Name())
f, err := fs.Open(filePath)
if err != nil {
return err
}

b, err := io.ReadAll(f)
if err != nil {
return err
}

status, err := nomad.ApplyJob(string(b))
if err != nil {
return err
}
fmt.Println(status)
}
time.Sleep(30 * time.Second)
}

return nil
},
Expand Down
88 changes: 88 additions & 0 deletions cmd/bootstrap_git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package cmd

import (
"context"
"fmt"
"io"
"net/url"
"time"

"github.com/go-git/go-git/v5"
"github.com/spf13/cobra"

"nomad-gitops-operator/pkg/nomad"
"nomad-gitops-operator/pkg/repository"
)

type gitFlags struct {
url string
branch string
path string
username string
password string
}

var gitArgs gitFlags

func init() {
bootstrapCmd.AddCommand(bootstrapGitCmd)
bootstrapGitCmd.Flags().StringVar(&gitArgs.url, "url", "", "git repository URL")
bootstrapGitCmd.Flags().StringVar(&gitArgs.branch, "branch", "main", "git branch")
bootstrapGitCmd.Flags().StringVar(&gitArgs.path, "path", "/", "path relative to the repository root")
}

var bootstrapGitCmd = &cobra.Command{
Use: "git [git repo]",
Short: "Bootstrap Nomad using a git repository",
Long: ``,
// Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
repositoryURL, err := url.Parse(gitArgs.url)
if err != nil {
return err
}

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Minute*5))
defer cancel()

worktree, err := repository.CLone(ctx, repositoryURL, gitArgs.branch)

if err != nil {
fmt.Printf("Error: %s\n", err)
}

// Reconcile
for true {
worktree.Pull(&git.PullOptions{RemoteName: "origin"})

fs := worktree.Filesystem
path := gitArgs.path
files, err := fs.ReadDir(path)
if err != nil {
return err
}

for _, file := range files {
filePath := fs.Join(path, file.Name())
f, err := fs.Open(filePath)
if err != nil {
return err
}

b, err := io.ReadAll(f)
if err != nil {
return err
}

status, err := nomad.ApplyJob(string(b))
if err != nil {
return err
}
fmt.Println(status)
}
time.Sleep(30 * time.Second)
}

return nil
},
}
6 changes: 6 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -13,6 +15,10 @@ Created by Jonas Vinther.`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// Apply the viper config value to the flag when the flag is not set and viper has a value
address, _ := cmd.Flags().GetString("address")
if address != "" {
os.Setenv("NOMAD_ADDR", address)
}

if viper.IsSet("NOMAD_ADDR") && address == "" {
value := viper.Get("NOMAD_ADDR").(string)
err := cmd.Flags().Set("address", value)
Expand Down
39 changes: 38 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module nomad-gitops-operator

go 1.13
go 1.18

require (
github.com/go-git/go-billy/v5 v5.3.1
Expand All @@ -9,3 +9,40 @@ require (
github.com/spf13/cobra v0.0.2-0.20171109065643-2da4a54c5cee
github.com/spf13/viper v1.10.0
)

require (
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/cronexpr v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.1-0.20201016140508-a07e7d50bbee // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit 1f2490d

Please sign in to comment.