Compare commits

...

2 Commits

Author SHA1 Message Date
Philippe Caseiro c0f49a7bb4 Try 07 2022-06-29 16:18:28 +02:00
Philippe Caseiro 1a180113cb Try 06 2022-06-29 12:13:32 +02:00
8 changed files with 155 additions and 76 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
}
},

95
pkg/templater/repo-apk.go Normal file
View File

@ -0,0 +1,95 @@
package templater
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
)
var APKConfigFile = "/etc/apk/repositories"
type APKRepository struct {
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 {
return err
}
defer file.Close()
if _, err := file.WriteString(data); err != nil {
return err
}
return nil
}
}
func (hr *APKRepository) Update() error {
if _, stdErr, err := utils.RunSystemCommand("apk", "update"); err != nil {
return fmt.Errorf("%s [%s]", stdErr, err)
}
return nil
}
func (hr *APKRepository) Delete() error {
fileBytes, err := ioutil.ReadFile("/etc/apk/repositories")
if err != nil {
return err
}
lines := strings.Split(string(fileBytes), "\n")
for _, line := range lines {
fmt.Println(line)
}
return nil
}
func (hr *APKRepository) Manage() error {
if hr.Enabled {
return hr.Add()
} else {
return hr.Delete()
}
}

View File

@ -8,17 +8,14 @@ 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 {
//deb http://fr.archive.ubuntu.com/ubuntu/ focal main restricted
data := fmt.Sprintf("deb %s", hr.URL)
if err := os.WriteFile("/etc/apt/source.list.d", []byte(data)); err != nil {
if err := os.WriteFile("/etc/apt/source.list.d", []byte(data), 0600); err != nil {
return err
}

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()
}
}

17
pkg/templater/repo.go Normal file
View File

@ -0,0 +1,17 @@
package templater
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

@ -1,57 +0,0 @@
package templater
import (
"fmt"
"io/ioutil"
"os"
"strings"
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
)
type APKRepository struct {
Name string `json:"name"`
Type string `json:"type"`
URL string `json:"url"`
Enabled bool `json:"enabled"`
}
func (hr *APKRepository) Add() error {
data := fmt.Sprintf("%s", hr.URL)
file, err := os.OpenFile("/etc/apk/repositories", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
if _, err := file.WriteString(data); err != nil {
return err
}
return nil
}
func (hr *APKRepository) Update() error {
if _, stdErr, err := utils.RunSystemCommand("apk", "update"); err != nil {
return fmt.Errorf("%s [%s]", stdErr, err)
}
return nil
}
func (hr *APKRepository) Delete() error {
fileBytes, err := ioutil.ReadFile("/etc/apk/repositories")
if err != nil {
return err
}
lines := strings.Split(string(fileBytes), "\n")
for i, line := range lines {
}
return nil
}
func (hr *APKRepository) Manage() error {
if hr.Enabled {
return hr.Add()
} else {
return hr.Delete()
}
}

View File

@ -1,8 +0,0 @@
package templater
type PackageRepository interface { // création de L'interface Forme
Manage() error // signature de la méthode Perimetre()
Update() error
Add() error
Delete() error
}

View File

@ -11,10 +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]Repository `json:"Repositories"`
Packages map[string]SystemPackage `json:"Packages"`
}
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 {
// Manage packages repositories
err := s.manageRepos(s.Repos)
if err != nil {
return err
}
// Manage system packages
for _, pack := range s.Packages {
err := pack.Manage()
@ -23,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)
}