159 lines
4.3 KiB
Go
159 lines
4.3 KiB
Go
package github
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/Pingu-Studio/MaidnCLI/internal/assets"
|
|
"github.com/Pingu-Studio/MaidnCLI/internal/utils"
|
|
)
|
|
|
|
func BuildFluxConfig(chartOwner, gitURL, manifestsRepo string) string {
|
|
return fmt.Sprintf(`---
|
|
apiVersion: source.toolkit.fluxcd.io/v1beta2
|
|
kind: HelmRepository
|
|
metadata:
|
|
name: ghcr-charts
|
|
namespace: flux-system
|
|
spec:
|
|
interval: 5m
|
|
type: oci
|
|
url: oci://ghcr.io/%s
|
|
secretRef:
|
|
name: ghcr-auth
|
|
---
|
|
apiVersion: source.toolkit.fluxcd.io/v1
|
|
kind: GitRepository
|
|
metadata:
|
|
name: cicd-deployment-manifests
|
|
namespace: flux-system
|
|
spec:
|
|
interval: 1m0s
|
|
url: %s
|
|
ref:
|
|
branch: main
|
|
secretRef:
|
|
name: github-auth
|
|
---
|
|
apiVersion: kustomize.toolkit.fluxcd.io/v1
|
|
kind: Kustomization
|
|
metadata:
|
|
name: namespaces
|
|
namespace: flux-system
|
|
spec:
|
|
dependsOn:
|
|
- name: vault-instance
|
|
- name: democratic-csi
|
|
- name: metallb-config
|
|
interval: 10m
|
|
path: ./namespaces
|
|
prune: true
|
|
sourceRef:
|
|
kind: GitRepository
|
|
name: cicd-deployment-manifests
|
|
---
|
|
apiVersion: kustomize.toolkit.fluxcd.io/v1
|
|
kind: Kustomization
|
|
metadata:
|
|
name: preview-apps
|
|
namespace: flux-system
|
|
spec:
|
|
dependsOn:
|
|
- name: namespaces
|
|
interval: 2m0s
|
|
path: ./apps/previews
|
|
prune: true
|
|
sourceRef:
|
|
kind: GitRepository
|
|
name: cicd-deployment-manifests
|
|
---
|
|
apiVersion: kustomize.toolkit.fluxcd.io/v1
|
|
kind: Kustomization
|
|
metadata:
|
|
name: staging-apps
|
|
namespace: flux-system
|
|
spec:
|
|
dependsOn:
|
|
- name: namespaces
|
|
interval: 2m0s
|
|
path: ./apps/staging
|
|
prune: true
|
|
sourceRef:
|
|
kind: GitRepository
|
|
name: cicd-deployment-manifests
|
|
---
|
|
apiVersion: kustomize.toolkit.fluxcd.io/v1
|
|
kind: Kustomization
|
|
metadata:
|
|
name: production-apps
|
|
namespace: flux-system
|
|
spec:
|
|
dependsOn:
|
|
- name: namespaces
|
|
interval: 2m0s
|
|
path: ./apps/production
|
|
prune: true
|
|
sourceRef:
|
|
kind: GitRepository
|
|
name: cicd-deployment-manifests
|
|
`, chartOwner, gitURL)
|
|
}
|
|
|
|
type RepoManager struct {
|
|
OrgName string
|
|
ManifestsRepoName string
|
|
FluxRepoName string
|
|
}
|
|
|
|
func NewRepoManager(org, manifestsRepo, fluxRepo string) *RepoManager {
|
|
return &RepoManager{OrgName: org, ManifestsRepoName: manifestsRepo, FluxRepoName: fluxRepo}
|
|
}
|
|
|
|
func (rm *RepoManager) InitializeManifestsRepo() {}
|
|
func (rm *RepoManager) InitializeFluxRepo() {}
|
|
func (rm *RepoManager) InitializeAll() error { return nil }
|
|
|
|
func WriteManifestsStructure(baseDir, repoName string) error {
|
|
manifestsReadme := fmt.Sprintf(assets.ManifestsReadmeTmpl, repoName)
|
|
utils.WriteFile(filepath.Join(baseDir, "README.md"), manifestsReadme)
|
|
namespacesDir := filepath.Join(baseDir, "namespaces")
|
|
os.MkdirAll(namespacesDir, 0755)
|
|
utils.WriteFile(filepath.Join(namespacesDir, "staging.yaml"), "apiVersion: v1\nkind: Namespace\nmetadata:\n name: staging\n")
|
|
utils.WriteFile(filepath.Join(namespacesDir, "production.yaml"), "apiVersion: v1\nkind: Namespace\nmetadata:\n name: production\n")
|
|
utils.WriteFile(filepath.Join(namespacesDir, "kustomization.yaml"), "apiVersion: kustomize.config.k8s.io/v1beta1\nkind: Kustomization\nresources:\n - staging.yaml\n - production.yaml\n")
|
|
for _, env := range []string{"staging", "production", "previews"} {
|
|
dirPath := filepath.Join(baseDir, "apps", env)
|
|
os.MkdirAll(dirPath, 0755)
|
|
utils.WriteFile(filepath.Join(dirPath, ".gitkeep"), "")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func WriteFluxStructure(baseDir, repoName, clusterPath, fluxConfig string) error {
|
|
fluxReadme := fmt.Sprintf(assets.FluxReadmeTmpl, repoName)
|
|
utils.WriteFile(filepath.Join(baseDir, "README.md"), fluxReadme)
|
|
clusterDir := filepath.Join(baseDir, strings.TrimPrefix(clusterPath, "./"))
|
|
os.MkdirAll(clusterDir, 0755)
|
|
utils.WriteFile(filepath.Join(clusterDir, "cicd-manifests-repo.yaml"), fluxConfig)
|
|
return nil
|
|
}
|
|
|
|
func CommitAndPush(tempDir string) error {
|
|
utils.RunCommandInDir(tempDir, "git", "config", "user.name", "Maidn")
|
|
utils.RunCommandInDir(tempDir, "git", "config", "user.email", "maidn@free-maidn.com")
|
|
utils.RunCommandInDir(tempDir, "git", "add", ".")
|
|
statusCmd := exec.Command("git", "status", "--porcelain")
|
|
statusCmd.Dir = tempDir
|
|
output, _ := statusCmd.Output()
|
|
if len(output) == 0 {
|
|
return nil
|
|
}
|
|
if err := utils.RunCommandInDir(tempDir, "git", "commit", "-m", "feat: initialize repository structure for CI/CD"); err != nil {
|
|
return err
|
|
}
|
|
return utils.RunCommandInDir(tempDir, "git", "push", "origin", "main")
|
|
}
|