92 lines
2.5 KiB
Go
92 lines
2.5 KiB
Go
package kubespray
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"kube-forge/pkg/config"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/apenella/go-ansible/pkg/execute"
|
|
"github.com/apenella/go-ansible/pkg/inventory"
|
|
"github.com/apenella/go-ansible/pkg/options"
|
|
"github.com/apenella/go-ansible/pkg/playbook"
|
|
)
|
|
|
|
func getPlaybookParameters(tags string) config.AnsiblePlaybookConfig {
|
|
cfg := config.GetConfig()
|
|
ansibleInventoryOptions := inventory.AnsibleInventoryOptions{
|
|
Graph: true,
|
|
Inventory: "kubespray/inventory/hosts",
|
|
Vars: true,
|
|
Yaml: false,
|
|
}
|
|
|
|
ansiblePlaybookOptions := playbook.AnsiblePlaybookOptions{
|
|
Inventory: ansibleInventoryOptions.Inventory,
|
|
Tags: tags,
|
|
}
|
|
|
|
ansiblePlaybookConnectionOptions := options.AnsibleConnectionOptions{
|
|
User: cfg.Credentials.User,
|
|
}
|
|
|
|
if cfg.Credentials.PrivateKeyFile != "" {
|
|
ansiblePlaybookConnectionOptions.PrivateKey = cfg.Credentials.PrivateKeyFile
|
|
}
|
|
|
|
ansiblePlaybookPrivilegeEscalationOptions := options.AnsiblePrivilegeEscalationOptions{
|
|
Become: true,
|
|
}
|
|
|
|
if cfg.Credentials.AskSudoPassword {
|
|
ansiblePlaybookPrivilegeEscalationOptions.AskBecomePass = true
|
|
}
|
|
|
|
return config.AnsiblePlaybookConfig{
|
|
ConnectionOptions: ansiblePlaybookConnectionOptions,
|
|
PrivilegeEscalationOptions: ansiblePlaybookPrivilegeEscalationOptions,
|
|
PlaybookOptions: ansiblePlaybookOptions,
|
|
}
|
|
}
|
|
|
|
func CopyK8SAdminConfig(pathInDataDir string) {
|
|
config := config.GetConfig()
|
|
dataDir := config.DataDir
|
|
var adminDefaultConfigPath = filepath.Join(dataDir, "kubespray/inventory/artifacts/admin.conf")
|
|
var adminOutConfigPath = filepath.Join(dataDir, pathInDataDir)
|
|
source, err := os.Open(adminDefaultConfigPath)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer source.Close()
|
|
|
|
destination, err := os.Create(adminOutConfigPath)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer destination.Close()
|
|
_, err = io.Copy(destination, source)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func runPlaybook(playbookPath string, playbookConfig config.AnsiblePlaybookConfig) {
|
|
playbook := &playbook.AnsiblePlaybookCmd{
|
|
Playbooks: []string{playbookPath},
|
|
ConnectionOptions: &playbookConfig.ConnectionOptions,
|
|
PrivilegeEscalationOptions: &playbookConfig.PrivilegeEscalationOptions,
|
|
Options: &playbookConfig.PlaybookOptions,
|
|
Exec: execute.NewDefaultExecute(
|
|
execute.WithEnvVar("ANSIBLE_FORCE_COLOR", "true"),
|
|
// execute.WithEnvVar("ANSIBLE_STDOUT_CALLBACK", "true"),
|
|
),
|
|
}
|
|
|
|
err := playbook.Run(context.TODO())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|