Moved from ini to toml, fixed installation and new-domain permissions issues
This commit is contained in:
62
git.go
62
git.go
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -98,18 +99,6 @@ func createGiteaRepo(domain string, giteaClient *gitea.Client) string {
|
||||
fmt.Printf("Domain %s config does not exist\n", domain)
|
||||
return ""
|
||||
}
|
||||
//options := gitea.CreateRepoFromTemplateOption{
|
||||
// Avatar: true,
|
||||
// Description: "Certificates storage for " + domain,
|
||||
// GitContent: true,
|
||||
// GitHooks: true,
|
||||
// Labels: true,
|
||||
// Name: domain + domainConfig.GetAsString("Repo.repo_suffix"),
|
||||
// Owner: config.GetAsString("Git.org_name"),
|
||||
// Private: true,
|
||||
// Topics: true,
|
||||
// Webhooks: true,
|
||||
//}
|
||||
options := gitea.CreateRepoOption{
|
||||
Name: domain + domainConfig.GetString("Repo.repo_suffix"),
|
||||
Description: "Certificate storage for " + domain,
|
||||
@@ -174,6 +163,22 @@ func cloneRepo(url string, ws *GitWorkspace) error {
|
||||
fmt.Printf("Error getting worktree from cloned repo: %v\n", err)
|
||||
return err
|
||||
}
|
||||
serverIdFile, err := ws.FS.OpenFile("/SERVER_ID", os.O_RDWR, 0640)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
fmt.Printf("Server ID file not found for %s, adopting domain\n", url)
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
serverIdBytes, err := io.ReadAll(serverIdFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
serverId := strings.TrimSpace(string(serverIdBytes))
|
||||
if serverId != config.GetString("App.uuid") {
|
||||
return fmt.Errorf("domain is already managed by server with uuid %s", serverId)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -205,7 +210,7 @@ func addAndPushCerts(domain string, ws *GitWorkspace) error {
|
||||
fmt.Printf("Error copying file to memfs: %v\n", err)
|
||||
return err
|
||||
}
|
||||
certFile, err := os.ReadFile(filepath.Join(certsDir, file.Name()))
|
||||
certFile, err := os.ReadFile(filepath.Join(certsDir, entry.Name()))
|
||||
if err != nil {
|
||||
fmt.Printf("Error reading file to memfs: %v\n", err)
|
||||
file.Close()
|
||||
@@ -228,6 +233,29 @@ func addAndPushCerts(domain string, ws *GitWorkspace) error {
|
||||
fmt.Printf("Error closing file: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
file, err := ws.FS.Create("/SERVER_ID")
|
||||
if err != nil {
|
||||
fmt.Printf("Error creating file in memfs: %v\n", err)
|
||||
return err
|
||||
}
|
||||
_, err = file.Write([]byte(config.GetString("App.uuid")))
|
||||
if err != nil {
|
||||
fmt.Printf("Error writing to memfs: %v\n", err)
|
||||
file.Close()
|
||||
return err
|
||||
}
|
||||
_, err = ws.WorkTree.Add(file.Name())
|
||||
if err != nil {
|
||||
fmt.Printf("Error adding file %v: %v\n", file.Name(), err)
|
||||
file.Close()
|
||||
return err
|
||||
}
|
||||
err = file.Close()
|
||||
if err != nil {
|
||||
fmt.Printf("Error closing file: %v\n", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
status, err := ws.WorkTree.Status()
|
||||
@@ -246,7 +274,7 @@ func addAndPushCerts(domain string, ws *GitWorkspace) error {
|
||||
Email: config.GetString("Certificates.email"),
|
||||
When: time.Now(),
|
||||
}
|
||||
commitHash, err := ws.WorkTree.Commit("Update "+domain+" @ "+time.Now().Format("Mon Jan _2 2006 15:04:05 MST"), &git.CommitOptions{Author: signature, Committer: signature})
|
||||
_, err = ws.WorkTree.Commit("Update "+domain+" @ "+time.Now().Format("Mon Jan _2 2006 15:04:05 MST"), &git.CommitOptions{Author: signature, Committer: signature})
|
||||
if err != nil {
|
||||
fmt.Printf("Error committing certs: %v\n", err)
|
||||
return err
|
||||
@@ -270,12 +298,6 @@ func addAndPushCerts(domain string, ws *GitWorkspace) error {
|
||||
|
||||
fmt.Println("Successfully uploaded to " + config.GetString("Git.server") + "/" + config.GetString("Git.org_name") + "/" + domain + domainConfig.GetString("Repo.repo_suffix") + ".git")
|
||||
|
||||
err = writeCommitHash(commitHash.String(), domainConfig)
|
||||
if err != nil {
|
||||
fmt.Printf("Error writing commit hash: %v\n", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user