Skip to content

Commit

Permalink
Added poc
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasvinther committed Dec 19, 2021
1 parent 374cc88 commit f3095b5
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 34 deletions.
61 changes: 58 additions & 3 deletions cmd/bootstrap.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
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() {
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{
Expand All @@ -18,16 +37,52 @@ var bootstrapCmd = &cobra.Command{
Long: ``,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
repo := args[0]
repositoryURL, err := url.Parse(gitArgs.url)
if err != nil {
return err
}

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

_, err := nomad.Apply()
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
},
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module nomad-gitops-operator
go 1.13

require (
github.com/go-git/go-billy/v5 v5.3.1
github.com/go-git/go-git/v5 v5.4.2
github.com/hashicorp/nomad-openapi v0.0.0-20211206195704-37d950c8b53d
github.com/spf13/cobra v0.0.2-0.20171109065643-2da4a54c5cee
github.com/spf13/viper v1.10.0
Expand Down
Loading

0 comments on commit f3095b5

Please sign in to comment.