Compare commits

..

No commits in common. "c0f49a7bb40b2f612f12055ab0b2b969f9393569" and "eb50773656ef1df5d3355b904615a9ce2ec1be01" have entirely different histories.

8 changed files with 76 additions and 155 deletions

View File

@ -29,12 +29,6 @@
"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,95 +0,0 @@
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

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

@ -0,0 +1,57 @@
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

@ -8,14 +8,17 @@ import (
) )
type DebRepository struct { type DebRepository struct {
Repository Name string `json:"name"`
Type string `json:"type"`
URL string `json:"url"`
Enabled bool `json:"enabled"`
} }
func (hr *DebRepository) Add() error { func (hr *DebRepository) Add() error {
//deb http://fr.archive.ubuntu.com/ubuntu/ focal main restricted //deb http://fr.archive.ubuntu.com/ubuntu/ focal main restricted
data := fmt.Sprintf("deb %s", hr.URL) data := fmt.Sprintf("deb %s", hr.URL)
if err := os.WriteFile("/etc/apt/source.list.d", []byte(data), 0600); err != nil { if err := os.WriteFile("/etc/apt/source.list.d", []byte(data)); err != nil {
return err return err
} }

View File

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

View File

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