This commit is contained in:
Philippe Caseiro 2022-06-29 16:18:28 +02:00
parent 1a180113cb
commit c0f49a7bb4
6 changed files with 94 additions and 36 deletions

View File

@ -29,6 +29,12 @@
"name": "grafana", "name": "grafana",
"url": "https://grafana.github.io/helm-charts", "url": "https://grafana.github.io/helm-charts",
"enabled":true "enabled":true
},
"AlpineTesting": {
"type": "apk",
"name": "testing",
"url": "http://mirror.arvancloud.com/alpine/edge/testing",
"enabled": true
} }
}, },
"Packages": { "Packages": {

View File

@ -1,6 +1,7 @@
package templater package templater
import ( import (
"bufio"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@ -9,14 +10,48 @@ import (
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils" "forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
) )
var APKConfigFile = "/etc/apk/repositories"
type APKRepository struct { type APKRepository struct {
Name string `json:"name"` Repository
Type string `json:"type"` }
URL string `json:"url"`
Enabled bool `json:"enabled"` func (hr *APKRepository) urlIsPresent() (bool, error) {
f, err := os.Open(APKConfigFile)
if err != nil {
return false, err
}
defer f.Close()
// Splits on newlines by default.
scanner := bufio.NewScanner(f)
line := 1
for scanner.Scan() {
if strings.Contains(scanner.Text(), hr.URL) {
return true, nil
}
line++
}
if err := scanner.Err(); err != nil {
return false, err
}
return false, nil
} }
func (hr *APKRepository) Add() error { func (hr *APKRepository) Add() error {
URLIsPresent, err := hr.urlIsPresent()
if err != nil {
return err
}
if URLIsPresent {
return nil
} else {
data := fmt.Sprintf("%s", hr.URL) data := fmt.Sprintf("%s", hr.URL)
file, err := os.OpenFile("/etc/apk/repositories", os.O_APPEND|os.O_WRONLY, 0644) file, err := os.OpenFile("/etc/apk/repositories", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil { if err != nil {
@ -29,6 +64,8 @@ func (hr *APKRepository) Add() error {
return nil return nil
} }
}
func (hr *APKRepository) Update() error { func (hr *APKRepository) Update() error {
if _, stdErr, err := utils.RunSystemCommand("apk", "update"); err != nil { if _, stdErr, err := utils.RunSystemCommand("apk", "update"); err != nil {
return fmt.Errorf("%s [%s]", stdErr, err) return fmt.Errorf("%s [%s]", stdErr, err)
@ -48,6 +85,7 @@ func (hr *APKRepository) Delete() error {
} }
return nil return nil
} }
func (hr *APKRepository) Manage() error { func (hr *APKRepository) Manage() error {
if hr.Enabled { if hr.Enabled {
return hr.Add() return hr.Add()

View File

@ -8,10 +8,7 @@ import (
) )
type DebRepository struct { type DebRepository struct {
Name string `json:"name"` Repository
Type string `json:"type"`
URL string `json:"url"`
Enabled bool `json:"enabled"`
} }
func (hr *DebRepository) Add() error { func (hr *DebRepository) Add() error {

View File

@ -1,10 +1,7 @@
package templater package templater
type HelmRepository struct { type HelmRepository struct {
Name string `json:"name"` Repository
Type string `json:"type"`
URL string `json:"url"`
Enabled bool `json:"enabled"`
} }
func (hr *HelmRepository) Add() error { func (hr *HelmRepository) Add() error {
@ -18,6 +15,11 @@ func (hr *HelmRepository) Update() error {
func (hr *HelmRepository) Delete() error { func (hr *HelmRepository) Delete() error {
return nil return nil
} }
func (hr *HelmRepository) Manage() error { func (hr *HelmRepository) Manage() error {
return nil if hr.Enabled {
return hr.Add()
} else {
return hr.Delete()
}
} }

View File

@ -1,8 +1,17 @@
package templater package templater
type PackageRepository interface { // création de L'interface Forme type PackageRepository interface {
Manage() error // signature de la méthode Perimetre() Manage() error
Update() error Update() error
Add() error Add() error
Delete() error Delete() error
} }
type Repository struct {
Actions PackageRepository
Name string `json:"name"`
Type string `json:"type"`
URL string `json:"url"`
Enabled bool `json:"enabled"`
}

View File

@ -4,8 +4,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"path/filepath" "path/filepath"
"github.com/mitchellh/mapstructure"
) )
type Service struct { type Service struct {
@ -13,29 +11,37 @@ type Service struct {
Vars map[string]interface{} `json:"Vars"` Vars map[string]interface{} `json:"Vars"`
Daemons map[string]SystemService `json:"Daemons"` Daemons map[string]SystemService `json:"Daemons"`
Users map[string]SystemUser `json:"Users"` Users map[string]SystemUser `json:"Users"`
Repos map[string]interface{} `json:"Repositories"` Repos map[string]Repository `json:"Repositories"`
Packages map[string]SystemPackage `json:"Packages"` Packages map[string]SystemPackage `json:"Packages"`
} }
func (s *Service) manageRepo(repo interface{}) error { func (s *Service) manageRepos(repos map[string]Repository) error {
fmt.Printf("%v\n", repo) for _, repo := range s.Repos {
if repo["type"] == "helm" { if repo.Type == "helm" {
var rp HelmRepository rp := HelmRepository{repo}
mapstructure.Decode(repo, rp) return rp.Manage()
}
if repo.Type == "apk" {
rp := APKRepository{repo}
return rp.Manage()
}
if repo.Type == "deb" {
rp := DebRepository{}
return rp.Manage()
} }
}
return nil return nil
} }
func (s *Service) Manage(templateDir string) error { func (s *Service) Manage(templateDir string) error {
for _, repo := range s.Repos { // Manage packages repositories
if err := s.manageRepo(repo); err != nil { err := s.manageRepos(s.Repos)
if err != nil {
return err return err
} }
}
// Manage system packages // Manage system packages
for _, pack := range s.Packages { for _, pack := range s.Packages {
err := pack.Manage() err := pack.Manage()
@ -44,7 +50,7 @@ func (s *Service) Manage(templateDir string) error {
} }
} }
err := processConfigFiles(s.ConfigFiles, s.Vars, templateDir) err = processConfigFiles(s.ConfigFiles, s.Vars, templateDir)
if err != nil { if err != nil {
return fmt.Errorf("ProcessingTemplatesFailed with error: %v", err) return fmt.Errorf("ProcessingTemplatesFailed with error: %v", err)
} }