Compare commits
1 Commits
fix-tkt-04
...
feat/helm
Author | SHA1 | Date | |
---|---|---|---|
9fb85a9437 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -13,5 +13,5 @@
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
vendor/
|
||||
# vendor/
|
||||
bin/
|
||||
|
49
data/config/loki-stack-helm.json
Normal file
49
data/config/loki-stack-helm.json
Normal file
@ -0,0 +1,49 @@
|
||||
{
|
||||
"Globals": {
|
||||
"Vars": {
|
||||
"PrometheusPort": "9090"
|
||||
}
|
||||
},
|
||||
"Name": "loki-stack",
|
||||
"Services": {
|
||||
"Loki-Stack": {
|
||||
"ConfigFiles": [],
|
||||
"Daemons": {},
|
||||
"Users": {},
|
||||
"Repositories": {
|
||||
"Grafana": {
|
||||
"enabled": true,
|
||||
"name": "grafana",
|
||||
"type": "helm",
|
||||
"url": "https://grafana.github.io/helm-charts"
|
||||
}
|
||||
},
|
||||
"Packages": {
|
||||
"loki": {
|
||||
"type" : "helm",
|
||||
"action": "install",
|
||||
"name": "loki",
|
||||
"repository": "grafana"
|
||||
}
|
||||
},
|
||||
"Vars": {
|
||||
"AlertManagerURL": "http://localhost:9092",
|
||||
"AuthEnabled": false,
|
||||
"GRPCPort": "9095",
|
||||
"Group": "grafana",
|
||||
"HTTPPort": "3099",
|
||||
"LogLevel": "error",
|
||||
"ObjectStore": "filesystem",
|
||||
"S2": {
|
||||
"APIKey": "",
|
||||
"APISecretKey": "",
|
||||
"BucketName": "",
|
||||
"URL": ""
|
||||
},
|
||||
"SharedStore": "filesystem",
|
||||
"StorageRoot": "/var/loki",
|
||||
"User": "loki"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -7,7 +7,6 @@ import (
|
||||
|
||||
"fmt"
|
||||
"os"
|
||||
"os/user"
|
||||
|
||||
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
|
||||
)
|
||||
@ -26,32 +25,8 @@ type ConfigFile struct {
|
||||
// Generate the configuration file from the template (hcl or json)
|
||||
func (cf *ConfigFile) Generate(root string, templateDir string, values []byte) error {
|
||||
var template string
|
||||
|
||||
if utils.DryRun {
|
||||
return nil
|
||||
}
|
||||
|
||||
cf.TemplateDir = templateDir
|
||||
dest := filepath.Join(root, cf.Destination)
|
||||
|
||||
// Get owner and group UID and GID
|
||||
owner, err := user.Lookup(cf.Owner)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Process templates failed with error: %v", err)
|
||||
}
|
||||
group, err := user.LookupGroup(cf.Group)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Process templates failed with error: %v", err)
|
||||
}
|
||||
uid, err := strconv.Atoi(owner.Uid)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Process templates failed with error: %v", err)
|
||||
}
|
||||
gid, err := strconv.Atoi(group.Gid)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Process templates failed with error: %v", err)
|
||||
}
|
||||
|
||||
intMod, err := strconv.ParseInt(cf.Mode, 8, 64)
|
||||
if err != nil {
|
||||
return (err)
|
||||
@ -70,10 +45,6 @@ func (cf *ConfigFile) Generate(root string, templateDir string, values []byte) e
|
||||
if err != nil {
|
||||
return fmt.Errorf("Process templates failed with error: %v", err)
|
||||
}
|
||||
if err = os.Chown(dest, uid, gid); err != nil {
|
||||
return fmt.Errorf("Process templates failed with error: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("\tFile %s generated\n", dest)
|
||||
return nil
|
||||
}
|
||||
|
@ -14,9 +14,22 @@ type SystemPackage struct {
|
||||
Action string `json:"action"`
|
||||
OS string `json:"os"`
|
||||
Distribution string `json:"distribution"`
|
||||
NameSpace string `json:"namespace"`
|
||||
Repository string `json:"repository"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
func (p *SystemPackage) SetDistribution() error {
|
||||
|
||||
// If the package type is helm or kubectl we set distribution to k8s
|
||||
if p.Type == "helm" || p.Type == "kubectl" {
|
||||
p.Distribution = p.Type
|
||||
if len(p.NameSpace) == 0 {
|
||||
p.NameSpace = p.Name
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
OSConfig, err := utils.ReadOSRelease()
|
||||
if err != nil {
|
||||
return err
|
||||
@ -56,6 +69,12 @@ func (p *SystemPackage) Manage() error {
|
||||
_, stdErr, pkErr = utils.RunSystemCommand("yum", "install", "-y", p.Name)
|
||||
case "arch":
|
||||
_, stdErr, pkErr = utils.RunSystemCommand("pacman", "-Suy", p.Name)
|
||||
case "helm":
|
||||
toInstall := fmt.Sprintf("%s/%s", p.Repository, p.Name)
|
||||
nameSpaceOPT := fmt.Sprintf("--namespace=%s", p.NameSpace)
|
||||
_, stdErr, pkErr = utils.RunSystemCommand("helm", "upgrade", "--install", p.Name, nameSpaceOPT, toInstall)
|
||||
case "kubectl":
|
||||
_, stdErr, pkErr = utils.RunSystemCommand("kubectl", "apply", p.URL)
|
||||
default:
|
||||
pkErr = fmt.Errorf("Unsupported OS %s [%s]", p.OS, stdErr)
|
||||
}
|
||||
|
@ -1,11 +1,54 @@
|
||||
package templater
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
|
||||
)
|
||||
|
||||
type HelmRepository struct {
|
||||
Repository
|
||||
}
|
||||
|
||||
func (hr *HelmRepository) repoExists() (bool, error) {
|
||||
var repos []HelmRepository
|
||||
|
||||
stdOut, stdErr, err := utils.RunSystemCommand("helm", "repo", "list", "-o", "json")
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("%s %v", stdErr, err)
|
||||
}
|
||||
err = json.Unmarshal(stdOut, &repos)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
for _, repo := range repos {
|
||||
if repo.Name == hr.Name {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (hr *HelmRepository) Add() error {
|
||||
|
||||
repoExists, err := hr.repoExists()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if repoExists {
|
||||
return nil
|
||||
} else {
|
||||
_, stdErr, err := utils.RunSystemCommand("helm", "repo", "add", hr.Name, hr.URL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s %v", stdErr, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (hr *HelmRepository) Update() error {
|
||||
|
@ -3,7 +3,6 @@ package templater
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os/user"
|
||||
|
||||
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
|
||||
)
|
||||
@ -15,9 +14,17 @@ type SystemUser struct {
|
||||
Shell string `json:"shell"`
|
||||
}
|
||||
|
||||
func (su *SystemUser) exists() (bool, error) {
|
||||
_, _, err := utils.RunSystemCommand("getent", "passwd", su.UserName)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (su *SystemUser) Manage() error {
|
||||
_, err := user.Lookup(su.UserName)
|
||||
if err == nil {
|
||||
exist, _ := su.exists()
|
||||
if exist {
|
||||
log.Printf("\tUser %s already exists", su.UserName)
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user