github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/cmd/chore/gen-executables/main.go (about)

     1  //go:generate goderive .
     2  
     3  package main
     4  
     5  import (
     6  	"flag"
     7  	"fmt"
     8  	"os"
     9  	"path/filepath"
    10  	"runtime"
    11  	"strings"
    12  
    13  	"github.com/caos/orbos/internal/executables"
    14  )
    15  
    16  func main() {
    17  
    18  	version := flag.String("version", "none", "Value shown by orbctl --version")
    19  	commit := flag.String("commit", "none", "Commit SHA shown by orbctl --version")
    20  	githubClientID := flag.String("githubclientid", "none", "ClientID used for OAuth with github as store")
    21  	githubClientSecret := flag.String("githubclientsecret", "none", "ClientSecret used for OAuth with github as store")
    22  	orbctldir := flag.String("orbctl", "", "Build orbctl binaries to this directory")
    23  	debug := flag.Bool("debug", false, "Compile executables with debugging features enabled")
    24  	dev := flag.Bool("dev", false, "Compile executables with debugging features enabled")
    25  	containeronly := flag.Bool("containeronly", false, "Compile orbctl binaries only for in-container usage")
    26  	hostBinsOnly := flag.Bool("host-bins-only", false, "Build only daemon binaries running on host machines")
    27  
    28  	flag.Parse()
    29  
    30  	defer func() {
    31  		err := recover()
    32  		if err != nil {
    33  			fmt.Println(err)
    34  			os.Exit(1)
    35  		}
    36  	}()
    37  
    38  	if *orbctldir == "" {
    39  		panic("flag orbctldir not provided")
    40  	}
    41  
    42  	_, selfPath, _, _ := runtime.Caller(0)
    43  	cmdPath := filepath.Join(filepath.Dir(selfPath), "../..")
    44  	path := curryJoinPath(cmdPath)
    45  
    46  	builtExecutables := executables.Build(
    47  		*debug, *commit, *version, *githubClientID, *githubClientSecret,
    48  		executables.Buildable{OutDir: filepath.Join(*orbctldir, "nodeagent"), MainDir: path("nodeagent"), Env: map[string]string{"GOOS": "linux", "GOARCH": "amd64", "CGO_ENABLED": "0"}},
    49  		executables.Buildable{OutDir: filepath.Join(*orbctldir, "health"), MainDir: path("health"), Env: map[string]string{"GOOS": "linux", "GOARCH": "amd64", "CGO_ENABLED": "0"}},
    50  	)
    51  
    52  	if *hostBinsOnly {
    53  		for range builtExecutables {
    54  		}
    55  		return
    56  	}
    57  
    58  	packableExecutables := executables.PackableBuilds(builtExecutables)
    59  
    60  	packableFiles := executables.PackableFiles(toChan([]string{
    61  		filepath.Join(cmdPath, "../internal/operator/orbiter/kinds/clusters/kubernetes/networks/calico.yaml"),
    62  		filepath.Join(cmdPath, "../internal/operator/orbiter/kinds/clusters/kubernetes/networks/cilium.yaml"),
    63  		filepath.Join(cmdPath, "../internal/operator/orbiter/kinds/providers/gce/kubernetes_gce.yaml"),
    64  	}))
    65  
    66  	if err := executables.PreBuild(deriveJoinPackables(packableExecutables, packableFiles)); err != nil {
    67  		panic(err)
    68  	}
    69  
    70  	// Use all available CPUs from now on
    71  	runtime.GOMAXPROCS(runtime.NumCPU())
    72  
    73  	orbctlMain := path("orbctl")
    74  
    75  	orbctls := []executables.Buildable{
    76  		orbctlBin(orbctlMain, *orbctldir, "darwin", "amd64"),
    77  		orbctlBin(orbctlMain, *orbctldir, "freebsd", "amd64"),
    78  		orbctlBin(orbctlMain, *orbctldir, "linux", "amd64"),
    79  		orbctlBin(orbctlMain, *orbctldir, "openbsd", "amd64"),
    80  		orbctlBin(orbctlMain, *orbctldir, "windows", "amd64"),
    81  		orbctlBin(orbctlMain, *orbctldir, "darwin", "arm64"),
    82  	}
    83  	if *dev {
    84  		orbctls = []executables.Buildable{orbctlBin(orbctlMain, *orbctldir, runtime.GOOS, "amd64")}
    85  	}
    86  
    87  	if *containeronly {
    88  		orbctls = []executables.Buildable{orbctlBin(orbctlMain, *orbctldir, "linux", "amd64")}
    89  	}
    90  
    91  	var hasErr bool
    92  	for orbctl := range executables.Build(*debug, *commit, *version, *githubClientID, *githubClientSecret, orbctls...) {
    93  		if _, err := orbctl(); err != nil {
    94  			hasErr = true
    95  		}
    96  	}
    97  	if hasErr {
    98  		panic("Building orbctl failed")
    99  	}
   100  }
   101  
   102  func orbctlBin(mainPath, outPath, goos, goarch string) executables.Buildable {
   103  
   104  	arch := "x86_64"
   105  	switch goarch {
   106  	case "arm64":
   107  		arch = "ARM64"
   108  	}
   109  	os := strings.ToUpper(goos[0:1]) + goos[1:]
   110  	switch goos {
   111  	case "freebsd":
   112  		os = "FreeBSD"
   113  	case "openbsd":
   114  		os = "OpenBSD"
   115  	}
   116  
   117  	outdir := filepath.Join(outPath, fmt.Sprintf("orbctl-%s-%s", os, arch))
   118  	if goos == "windows" {
   119  		outdir += ".exe"
   120  	}
   121  
   122  	return executables.Buildable{
   123  		MainDir: mainPath,
   124  		OutDir:  outdir,
   125  		Env:     map[string]string{"GOOS": goos, "GOARCH": goarch, "CGO_ENABLED": "0"},
   126  	}
   127  }
   128  
   129  func curryJoinPath(cmdPath string) func(dir string) string {
   130  	return func(dir string) string {
   131  		return filepath.Join(cmdPath, dir)
   132  	}
   133  }
   134  
   135  func toChan(args []string) <-chan string {
   136  	ch := make(chan string)
   137  	go func() {
   138  		for _, arg := range args {
   139  			ch <- arg
   140  		}
   141  		close(ch)
   142  	}()
   143  	return ch
   144  }