Worky
This commit is contained in:
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
122
main.go
122
main.go
@@ -4,12 +4,16 @@ import (
|
|||||||
"code.gitea.io/sdk/gitea"
|
"code.gitea.io/sdk/gitea"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.nevets.tech/Steven/ezconf"
|
"git.nevets.tech/Steven/ezconf"
|
||||||
|
"github.com/go-git/go-git/v5/plumbing/object"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-git/go-billy/v5"
|
billy "github.com/go-git/go-billy/v5"
|
||||||
"github.com/go-git/go-git/v5"
|
memfs "github.com/go-git/go-billy/v5/memfs"
|
||||||
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
git "github.com/go-git/go-git/v5"
|
||||||
"github.com/go-git/go-git/v5/storage/memory"
|
http "github.com/go-git/go-git/v5/plumbing/transport/http"
|
||||||
|
memory "github.com/go-git/go-git/v5/storage/memory"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -34,7 +38,7 @@ func main() {
|
|||||||
if hasConfig {
|
if hasConfig {
|
||||||
config = ezconf.NewConfiguration(args[configIndex+1])
|
config = ezconf.NewConfiguration(args[configIndex+1])
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("Error, no config passed. Please add '-c /path/to/config.ini' to the command")
|
fmt.Printf("Error, no config passed. Please add '-c /path/to/config.ini' to the command\n")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +47,7 @@ func main() {
|
|||||||
if hasDomain {
|
if hasDomain {
|
||||||
domain = args[domainIndex+1]
|
domain = args[domainIndex+1]
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("Error, no domain passed. Please add '-d domain.tld' to the command")
|
fmt.Printf("Error, no domain passed. Please add '-d domain.tld' to the command\n")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +73,7 @@ func main() {
|
|||||||
err = os.Setenv("CF_API_TOKEN", config.GetAsString("Cloudflare.cf_api_token"))
|
err = os.Setenv("CF_API_TOKEN", config.GetAsString("Cloudflare.cf_api_token"))
|
||||||
err = os.Setenv("CF_EMAIL", config.GetAsString("Cloudflare.cf_email"))
|
err = os.Setenv("CF_EMAIL", config.GetAsString("Cloudflare.cf_email"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error setting environment variable: %v", err)
|
fmt.Printf("Error setting environment variable: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,23 +81,28 @@ func main() {
|
|||||||
Username: config.GetAsString("Git.username"),
|
Username: config.GetAsString("Git.username"),
|
||||||
Password: config.GetAsString("Git.api_token"),
|
Password: config.GetAsString("Git.api_token"),
|
||||||
}
|
}
|
||||||
giteaClient, err = gitea.NewClient(config.GetAsString("Git.server"), gitea.SetBasicAuth(config.GetAsString("Git.username"), config.GetAsString("Git.api_token")))
|
giteaClient, err = gitea.NewClient(config.GetAsString("Git.server"), gitea.SetToken(config.GetAsString("Git.api_token")))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error connecting to gitea instance: %v", err)
|
fmt.Printf("Error connecting to gitea instance: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
storage = memory.NewStorage()
|
||||||
|
fs = memfs.New()
|
||||||
|
|
||||||
|
var cmd *exec.Cmd
|
||||||
switch args[len(args)-1] {
|
switch args[len(args)-1] {
|
||||||
case "gen":
|
case "gen":
|
||||||
{
|
{
|
||||||
url := createGiteaRepo()
|
url := createGiteaRepo()
|
||||||
cloneRepo(url)
|
cloneRepo(url)
|
||||||
exec.Command("lego", legoNewSiteArgs...)
|
fixUpdateSh()
|
||||||
|
cmd = exec.Command("lego", legoNewSiteArgs...)
|
||||||
}
|
}
|
||||||
case "renew":
|
case "renew":
|
||||||
{
|
{
|
||||||
cloneRepo(config.GetAsString("Git.server") + "/" + config.GetAsString("Git.org_name"))
|
cloneRepo(config.GetAsString("Git.server") + "/" + config.GetAsString("Git.org_name"))
|
||||||
exec.Command("lego", legoRenewSiteArgs...)
|
cmd = exec.Command("lego", legoRenewSiteArgs...)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
@@ -101,18 +110,32 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fmt.Printf("Env Vars: %v", cmd.Env)
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error creating certs with lego: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
fmt.Println(string(out))
|
||||||
|
addAndPushCerts()
|
||||||
}
|
}
|
||||||
|
|
||||||
func createGiteaRepo() string {
|
func createGiteaRepo() string {
|
||||||
options := gitea.CreateRepoFromTemplateOption{
|
options := gitea.CreateRepoFromTemplateOption{
|
||||||
Owner: config.GetAsString("Git.repo_owner"),
|
Avatar: true,
|
||||||
Name: domain + "-certificates",
|
|
||||||
Description: "Certificates storage for " + domain,
|
Description: "Certificates storage for " + domain,
|
||||||
|
GitContent: true,
|
||||||
|
GitHooks: true,
|
||||||
|
Labels: true,
|
||||||
|
Name: domain + "-certificates",
|
||||||
|
Owner: config.GetAsString("Git.org_name"),
|
||||||
Private: true,
|
Private: true,
|
||||||
|
Topics: true,
|
||||||
|
Webhooks: true,
|
||||||
}
|
}
|
||||||
giteaRepo, _, err := giteaClient.CreateRepoFromTemplate(config.GetAsString("Git.org_name"), config.GetAsString("Git.template_name"), options)
|
giteaRepo, _, err := giteaClient.CreateRepoFromTemplate(config.GetAsString("Git.org_name"), config.GetAsString("Git.template_name"), options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error creating repo: %v", err)
|
fmt.Printf("Error creating repo: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
return giteaRepo.CloneURL
|
return giteaRepo.CloneURL
|
||||||
@@ -122,35 +145,84 @@ func cloneRepo(url string) {
|
|||||||
var err error
|
var err error
|
||||||
repo, err = git.Clone(storage, fs, &git.CloneOptions{URL: url, Auth: creds})
|
repo, err = git.Clone(storage, fs, &git.CloneOptions{URL: url, Auth: creds})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error clone git repo: %v", err)
|
fmt.Printf("Error clone git repo: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
workTree, err = repo.Worktree()
|
workTree, err = repo.Worktree()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error getting worktree from repo: %v", err)
|
fmt.Printf("Error getting worktree from repo: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func fixUpdateSh() {
|
||||||
|
updateSh, err := fs.Open("update.sh")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error opening update.sh: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
content := "#!/bin/env bash\necho Starting cert pull\ngit pull https://Steven:07026d2d4e99614ec98fc2a8357f108f78f52682@git.nevets.tech/Keys/" + domain + "-certificates.git --force --no-rebase\nexit 0"
|
||||||
|
fmt.Printf("Update.sh Content: %v\n", content)
|
||||||
|
_, err = updateSh.Write([]byte(content))
|
||||||
|
err = updateSh.Close()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error writing update.sh: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func addAndPushCerts() {
|
func addAndPushCerts() {
|
||||||
// Copy certs to memfs
|
//TODO integrate SOPS api when stable release
|
||||||
|
certs, err := os.ReadDir(config.GetAsString("Certificates.certs_path"))
|
||||||
//file, err := fs.Create("")
|
|
||||||
//if err != nil {
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
_, err = workTree.Add(domain + "*")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error adding certificates to workTree: %v", err)
|
fmt.Printf("Error reading from directory: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
for _, cert := range certs {
|
||||||
|
if strings.HasPrefix(cert.Name(), domain) {
|
||||||
|
file, err := fs.Create(config.GetAsString("Certificates.certs_path" + "/" + cert.Name()))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error copying cert to memfs: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
certFile, err := os.ReadFile(config.GetAsString("Certificates.certs_path" + "/" + cert.Name()))
|
||||||
|
_, err = file.Write(certFile)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error writing to memfs: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = workTree.Add(".")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error adding certificates to workTree: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
status, err := workTree.Status()
|
status, err := workTree.Status()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error getting repo status: %v", err)
|
fmt.Printf("Error getting repo status: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Println(status.String())
|
fmt.Println(status.String())
|
||||||
|
signature := &object.Signature{
|
||||||
|
Name: "Cert Manager",
|
||||||
|
Email: "certs@nevets.tech",
|
||||||
|
When: time.Now(),
|
||||||
|
}
|
||||||
|
_, err = workTree.Commit("Update "+domain+" @ "+time.Now().String(), &git.CommitOptions{Author: signature, Committer: signature})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error committing certs: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
err = repo.Push(&git.PushOptions{Auth: creds, Force: true, RemoteName: "origin"})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error pushing to origin: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Successfully uploaded to repo")
|
||||||
}
|
}
|
||||||
|
|
||||||
func contains(slice []string, value string) (sliceHas bool, index int) {
|
func contains(slice []string, value string) (sliceHas bool, index int) {
|
||||||
|
|||||||
Reference in New Issue
Block a user