kube-forge v2: embede python with ansible (kubespray) and cobra as cli building tool
This commit is contained in:
@@ -2,8 +2,11 @@ package kubespray
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"kube-forge/internal/config"
|
||||
"kube-forge/internal/logging"
|
||||
"kube-forge/internal/python"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
@@ -16,7 +19,7 @@ func getPlaybookParameters(tags string) playbook.AnsiblePlaybookOptions {
|
||||
cfg := config.GetConfig()
|
||||
|
||||
ansiblePlaybookOptions := playbook.AnsiblePlaybookOptions{
|
||||
Inventory: filepath.Join(cfg.WorkDir, "kubespray/inventory/hosts"),
|
||||
Inventory: filepath.Join(cfg.InventoryDir, "hosts"),
|
||||
Tags: tags,
|
||||
User: cfg.Credentials.User,
|
||||
Become: true,
|
||||
@@ -33,18 +36,16 @@ func getPlaybookParameters(tags string) playbook.AnsiblePlaybookOptions {
|
||||
return ansiblePlaybookOptions
|
||||
}
|
||||
|
||||
func CopyK8SAdminConfig(pathInDataDir string) {
|
||||
func CopyK8SAdminConfig(outFile string) {
|
||||
config := config.GetConfig()
|
||||
dataDir := config.WorkDir
|
||||
var adminDefaultConfigPath = filepath.Join(dataDir, "kubespray/inventory/artifacts/admin.conf")
|
||||
var adminOutConfigPath = filepath.Join(dataDir, pathInDataDir)
|
||||
var adminDefaultConfigPath = filepath.Join(config.InventoryDir, "artifacts/admin.conf")
|
||||
source, err := os.Open(adminDefaultConfigPath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer source.Close()
|
||||
|
||||
destination, err := os.Create(adminOutConfigPath)
|
||||
destination, err := os.Create(outFile)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -53,26 +54,58 @@ func CopyK8SAdminConfig(pathInDataDir string) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
logging.Log.Info(fmt.Sprintf("K8s admin config: %s", outFile))
|
||||
}
|
||||
|
||||
func runPlaybook(playbookPath string, tags string) {
|
||||
config := config.GetConfig()
|
||||
var callbackExecute execute.Executor
|
||||
epExec := python.NewPythonExec()
|
||||
|
||||
envVars := map[string]string{
|
||||
"ANSIBLE_DISPLAY_SKIPPED_HOSTS": "false",
|
||||
"ANSIBLE_DISPLAY_OK_HOSTS": "false",
|
||||
"ANSIBLE_FORCE_COLOR": "true",
|
||||
"ANSIBLE_HOST_KEY_CHECKING": "false",
|
||||
}
|
||||
|
||||
playbookOptions := getPlaybookParameters(tags)
|
||||
|
||||
playbookCmd := playbook.NewAnsiblePlaybookCmd(
|
||||
playbook.WithPlaybooks(playbookPath),
|
||||
playbook.WithPlaybooks(
|
||||
filepath.Join(epExec.ResourcesFsPath, playbookPath),
|
||||
),
|
||||
playbook.WithPlaybookOptions(&playbookOptions),
|
||||
playbook.WithBinary(
|
||||
filepath.Join(epExec.PythonLibFsPath, "bin", "ansible-playbook"),
|
||||
),
|
||||
)
|
||||
|
||||
execute := execute.NewDefaultExecute(
|
||||
execute.WithCmd(playbookCmd),
|
||||
execute.WithErrorEnrich(playbook.NewAnsiblePlaybookErrorEnrich()),
|
||||
execute.WithExecutable(epExec),
|
||||
)
|
||||
|
||||
if config.Verbose {
|
||||
callbackExecute = stdoutcallback.NewDebugStdoutCallbackExecute(execute)
|
||||
envVars["ANSIBLE_STDOUT_CALLBACK"] = "unixy"
|
||||
envVars["ANSIBLE_VERBOSITY"] = "3"
|
||||
envVars["ANSIBLE_DISPLAY_SKIPPED_HOSTS"] = "true"
|
||||
envVars["ANSIBLE_DISPLAY_OK_HOSTS"] = "true"
|
||||
} else {
|
||||
callbackExecute = stdoutcallback.NewDenseStdoutCallbackExecute(execute)
|
||||
envVars["ANSIBLE_STDOUT_CALLBACK"] = "dense"
|
||||
envVars["ANSIBLE_ACTION_WARNINGS"] = "false"
|
||||
envVars["ANSIBLE_DEVEL_WARNING"] = "false"
|
||||
envVars["ANSIBLE_DEPRECATION_WARNINGS"] = "false"
|
||||
envVars["ANSIBLE_DUPLICATE_YAML_DICT_KEY"] = "ignore"
|
||||
envVars["ANSIBLE_HOST_PATTERN_MISMATCH"] = "ignore"
|
||||
envVars["ANSIBLE_SYSTEM_WARNINGS"] = "false"
|
||||
}
|
||||
|
||||
callbackExecute = stdoutcallback.NewDefaultStdoutCallbackExecute(execute)
|
||||
|
||||
for key, value := range envVars {
|
||||
os.Setenv(key, value)
|
||||
}
|
||||
|
||||
err := callbackExecute.Execute(context.Background())
|
||||
|
||||
Reference in New Issue
Block a user