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

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

View File

@ -1,6 +1,7 @@
package templater
import (
"bufio"
"fmt"
"io/ioutil"
"os"
@ -9,14 +10,48 @@ import (
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
)
var APKConfigFile = "/etc/apk/repositories"
type APKRepository struct {
Name string `json:"name"`
Type string `json:"type"`
URL string `json:"url"`
Enabled bool `json:"enabled"`
Repository
}
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 {
URLIsPresent, err := hr.urlIsPresent()
if err != nil {
return err
}
if URLIsPresent {
return nil
} else {
data := fmt.Sprintf("%s", hr.URL)
file, err := os.OpenFile("/etc/apk/repositories", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
@ -27,6 +62,8 @@ func (hr *APKRepository) Add() error {
return err
}
return nil
}
}
func (hr *APKRepository) Update() error {
@ -48,6 +85,7 @@ func (hr *APKRepository) Delete() error {
}
return nil
}
func (hr *APKRepository) Manage() error {
if hr.Enabled {
return hr.Add()

View File

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

View File

@ -1,10 +1,7 @@
package templater
type HelmRepository struct {
Name string `json:"name"`
Type string `json:"type"`
URL string `json:"url"`
Enabled bool `json:"enabled"`
Repository
}
func (hr *HelmRepository) Add() error {
@ -18,6 +15,11 @@ func (hr *HelmRepository) Update() error {
func (hr *HelmRepository) Delete() error {
return nil
}
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
type PackageRepository interface { // création de L'interface Forme
Manage() error // signature de la méthode Perimetre()
type PackageRepository interface {
Manage() error
Update() error
Add() 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"
"fmt"
"path/filepath"
"github.com/mitchellh/mapstructure"
)
type Service struct {
@ -13,29 +11,37 @@ type Service struct {
Vars map[string]interface{} `json:"Vars"`
Daemons map[string]SystemService `json:"Daemons"`
Users map[string]SystemUser `json:"Users"`
Repos map[string]interface{} `json:"Repositories"`
Repos map[string]Repository `json:"Repositories"`
Packages map[string]SystemPackage `json:"Packages"`
}
func (s *Service) manageRepo(repo interface{}) error {
fmt.Printf("%v\n", repo)
if repo["type"] == "helm" {
var rp HelmRepository
mapstructure.Decode(repo, rp)
func (s *Service) manageRepos(repos map[string]Repository) error {
for _, repo := range s.Repos {
if repo.Type == "helm" {
rp := HelmRepository{repo}
return rp.Manage()
}
if repo.Type == "apk" {
rp := APKRepository{repo}
return rp.Manage()
}
if repo.Type == "deb" {
rp := DebRepository{}
return rp.Manage()
}
}
return nil
}
func (s *Service) Manage(templateDir string) error {
for _, repo := range s.Repos {
if err := s.manageRepo(repo); err != nil {
// Manage packages repositories
err := s.manageRepos(s.Repos)
if err != nil {
return err
}
}
// Manage system packages
for _, pack := range s.Packages {
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 {
return fmt.Errorf("ProcessingTemplatesFailed with error: %v", err)
}