-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Imporving the git bootstrapper and adding documentation on how to use
- Loading branch information
1 parent
5ab3dcb
commit 1f2490d
Showing
10 changed files
with
203 additions
and
977 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.