2022-06-15 17:29:45 +02:00
|
|
|
package templater
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-06-29 17:07:43 +02:00
|
|
|
"log"
|
2022-06-15 17:29:45 +02:00
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SystemPackage struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Action string `json:"action"`
|
|
|
|
OS string `json:"os"`
|
|
|
|
Distribution string `json:"distribution"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SystemPackage) SetDistribution() error {
|
|
|
|
OSConfig, err := utils.ReadOSRelease()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Distribution = OSConfig["ID_LIKE"]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (p *SystemPackage) SetOS() error {
|
|
|
|
p.OS = runtime.GOOS
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SystemPackage) Manage() error {
|
|
|
|
var pkErr error
|
|
|
|
var stdErr []byte
|
|
|
|
|
|
|
|
if p.OS == "" {
|
|
|
|
if err := p.SetOS(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Distribution == "" {
|
|
|
|
if err := p.SetDistribution(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 17:08:12 +02:00
|
|
|
log.Printf("\tInstalling %s package\n", p.Name)
|
2022-06-15 17:29:45 +02:00
|
|
|
switch os := p.Distribution; os {
|
|
|
|
case "debian", "ubuntu":
|
|
|
|
_, stdErr, pkErr = utils.RunSystemCommand("apt", "install", "-y", p.Name)
|
|
|
|
case "alpine":
|
|
|
|
_, stdErr, pkErr = utils.RunSystemCommand("apk", "add", p.Name)
|
|
|
|
case "redhat":
|
|
|
|
_, stdErr, pkErr = utils.RunSystemCommand("yum", "install", "-y", p.Name)
|
|
|
|
case "arch":
|
|
|
|
_, stdErr, pkErr = utils.RunSystemCommand("pacman", "-Suy", p.Name)
|
|
|
|
default:
|
|
|
|
pkErr = fmt.Errorf("Unsupported OS %s [%s]", p.OS, stdErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pkErr != nil {
|
|
|
|
var msg string
|
|
|
|
if len(stdErr) != 0 {
|
|
|
|
msg = string(stdErr)
|
|
|
|
} else {
|
|
|
|
msg = pkErr.Error()
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Package %s, os %s, failed with error: %v", p.Name, p.OS, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|