kube-forge v2: embede python with ansible (kubespray) and cobra as cli building tool

This commit is contained in:
2025-06-22 00:37:47 +03:00
parent c2df2abc78
commit 4142a9db61
830 changed files with 874 additions and 2047 deletions

View File

@@ -0,0 +1,27 @@
package main
import (
"runtime"
"strings"
"github.com/kluctl/go-embed-python/pip"
)
var platforms = map[string][]string{
"darwin-amd64": {"macosx_11_0_x86_64", "macosx_12_0_x86_64"},
"darwin-arm64": {"macosx_11_0_arm64", "macosx_12_0_arm64"},
"linux-amd64": {"manylinux_2_17_x86_64", "manylinux_2_28_x86_64", "manylinux2014_x86_64"},
"linux-arm64": {"manylinux_2_17_aarch64", "manylinux_2_28_aarch64", "manylinux2014_aarch64"},
"windows-amd64": {"win_amd64"},
}
func main() {
os := runtime.GOOS
arch := runtime.GOARCH
pipPlatform := platforms[strings.Join([]string{os, arch}, "-")]
err := pip.CreateEmbeddedPipPackages("requirements.txt", os, arch, pipPlatform, "./data/")
if err != nil {
panic(err)
}
}

3
internal/python/main.go Normal file
View File

@@ -0,0 +1,3 @@
package python
//go:generate go run ./generate

View File

@@ -0,0 +1,4 @@
ansible
cryptography
jmespath
netaddr

161
internal/python/utils.go Normal file
View File

@@ -0,0 +1,161 @@
package python
import (
"context"
"fmt"
"io"
"kube-forge/internal/python/data"
"kube-forge/internal/resources"
"os"
"os/exec"
"path/filepath"
executable "github.com/apenella/go-ansible/v2/pkg/execute/exec"
"github.com/kluctl/go-embed-python/embed_util"
"github.com/kluctl/go-embed-python/python"
)
type PythonExec struct {
ep *python.EmbeddedPython
PythonLibFsPath string
ResourcesFsPath string
}
type CmdWrapper struct {
*exec.Cmd
}
func (c *CmdWrapper) CombinedOutput() ([]byte, error) {
return c.Cmd.CombinedOutput()
}
func (c *CmdWrapper) Environ() []string {
return c.Cmd.Environ()
}
func (c *CmdWrapper) Output() ([]byte, error) {
return c.Cmd.Output()
}
func (c *CmdWrapper) Run() error {
return c.Cmd.Run()
}
func (c *CmdWrapper) Start() error {
return c.Cmd.Start()
}
func (c *CmdWrapper) StderrPipe() (io.ReadCloser, error) {
return c.Cmd.StderrPipe()
}
func (c *CmdWrapper) StdinPipe() (io.WriteCloser, error) {
return c.Cmd.StdinPipe()
}
func (c *CmdWrapper) StdoutPipe() (io.ReadCloser, error) {
return c.Cmd.StdoutPipe()
}
func (c *CmdWrapper) String() string {
return c.Cmd.String()
}
func (c *CmdWrapper) Wait() error {
return c.Cmd.Wait()
}
type ErrorCmd struct {
err error
}
func (e *ErrorCmd) CombinedOutput() ([]byte, error) {
return nil, e.err
}
func (e *ErrorCmd) Environ() []string {
return nil
}
func (e *ErrorCmd) Output() ([]byte, error) {
return nil, e.err
}
func (e *ErrorCmd) Run() error {
return e.err
}
func (e *ErrorCmd) Start() error {
return e.err
}
func (e *ErrorCmd) StderrPipe() (io.ReadCloser, error) {
return nil, e.err
}
func (e *ErrorCmd) StdinPipe() (io.WriteCloser, error) {
return nil, e.err
}
func (e *ErrorCmd) StdoutPipe() (io.ReadCloser, error) {
return nil, e.err
}
func (e *ErrorCmd) String() string {
return fmt.Sprintf("ErrorCmd: %v", e.err)
}
func (e *ErrorCmd) Wait() error {
return e.err
}
func NewPythonExec() *PythonExec {
tmpDir := filepath.Join(os.TempDir(), "go-ansible")
pythonDir := tmpDir + "-python"
pythonLibDir := tmpDir + "-python-lib"
resourcesDir := tmpDir + "-resources"
pythonLibFs, err := embed_util.NewEmbeddedFilesWithTmpDir(data.Data, pythonLibDir, true)
if err != nil {
panic(err)
}
pythonLibFsPath := pythonLibFs.GetExtractedPath()
resourcesFs, _ := embed_util.NewEmbeddedFilesWithTmpDir(resources.Kubespray, resourcesDir, true)
resourcesFsPath := resourcesFs.GetExtractedPath()
ep, _ := python.NewEmbeddedPythonWithTmpDir(pythonDir, true)
ep.AddPythonPath(pythonLibFs.GetExtractedPath())
ep.AddPythonPath(resourcesFs.GetExtractedPath())
return &PythonExec{
ep: ep,
PythonLibFsPath: pythonLibFsPath,
ResourcesFsPath: resourcesFsPath,
}
}
func (e *PythonExec) Command(name string, arg ...string) executable.Cmder {
cmd := append([]string{name}, arg...)
execCmd, err := e.ep.PythonCmd2(cmd)
if err != nil {
return &ErrorCmd{err: fmt.Errorf("failed to create python command: %w", err)}
}
return &CmdWrapper{Cmd: execCmd}
}
func (e *PythonExec) CommandContext(ctx context.Context, name string, arg ...string) executable.Cmder {
cmd := append([]string{name}, arg...)
execCmd, err := e.ep.PythonCmd2(cmd)
if err != nil {
return &ErrorCmd{err: fmt.Errorf("failed to create python command: %w", err)}
}
ctxCmd := exec.CommandContext(ctx, execCmd.Path, execCmd.Args[1:]...)
ctxCmd.Env = execCmd.Env
ctxCmd.Dir = execCmd.Dir
ctxCmd.Stdin = execCmd.Stdin
ctxCmd.Stdout = execCmd.Stdout
ctxCmd.Stderr = execCmd.Stderr
return &CmdWrapper{Cmd: ctxCmd}
}