github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/cmd/builder-tool/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  	"time"
     8  
     9  	"github.com/Cloud-Foundations/Dominator/lib/constants"
    10  	"github.com/Cloud-Foundations/Dominator/lib/flags/commands"
    11  	"github.com/Cloud-Foundations/Dominator/lib/flags/loadflags"
    12  	"github.com/Cloud-Foundations/Dominator/lib/flagutil"
    13  	"github.com/Cloud-Foundations/Dominator/lib/log/cmdlogger"
    14  	"github.com/Cloud-Foundations/Dominator/lib/srpc"
    15  	"github.com/Cloud-Foundations/Dominator/lib/srpc/setupclient"
    16  )
    17  
    18  var (
    19  	alwaysShowBuildLog = flag.Bool("alwaysShowBuildLog", false,
    20  		"If true, show build log even for successful builds")
    21  	bindMounts         flagutil.StringList
    22  	imaginatorHostname = flag.String("imaginatorHostname", "localhost",
    23  		"Hostname of image build server")
    24  	imaginatorPortNum = flag.Uint("imaginatorPortNum",
    25  		constants.ImaginatorPortNumber,
    26  		"Port number of image build server")
    27  	expiresIn = flag.Duration("expiresIn", time.Hour,
    28  		"How long before the image expires (auto deletes)")
    29  	imageFilename = flag.String("imageFilename", "",
    30  		"Name of file to write image to")
    31  	imageServerHostname = flag.String("imageServerHostname", "localhost",
    32  		"Hostname of image server")
    33  	imageServerPortNum = flag.Uint("imageServerPortNum",
    34  		constants.ImageServerPortNumber,
    35  		"Port number of image server")
    36  	maxSourceAge = flag.Duration("maxSourceAge", time.Hour,
    37  		"Maximum age of a source image before it is rebuilt")
    38  	rawSize flagutil.Size
    39  
    40  	minimumExpiration = 5 * time.Minute
    41  )
    42  
    43  func init() {
    44  	flag.Var(&bindMounts, "bindMounts",
    45  		"Comma separated list of directories to bind mount into build workspace")
    46  	flag.Var(&rawSize, "rawSize", "Size of RAW file to create")
    47  }
    48  
    49  func printUsage() {
    50  	w := flag.CommandLine.Output()
    51  	fmt.Fprintln(w, "Usage: builder-tool [flags...] command [args...]")
    52  	fmt.Fprintln(w, "Common flags:")
    53  	flag.PrintDefaults()
    54  	fmt.Fprintln(w, "Commands:")
    55  	commands.PrintCommands(w, subcommands)
    56  }
    57  
    58  var subcommands = []commands.Command{
    59  	{"build-from-manifest", "manifestDir stream-name", 2, 2,
    60  		buildFromManifestSubcommand},
    61  	{"build-image", "stream-name [git-branch]", 1, 2, buildImageSubcommand},
    62  	{"build-raw-from-manifest", "manifestDir rawFile", 2, 2,
    63  		buildRawFromManifestSubcommand},
    64  	{"build-tree-from-manifest", "manifestDir", 1, 1,
    65  		buildTreeFromManifestSubcommand},
    66  	{"process-manifest", "rootDir", 2, 2, processManifestSubcommand},
    67  }
    68  
    69  var imaginatorSrpcClient *srpc.Client
    70  var imageServerSrpcClient *srpc.Client
    71  
    72  func getImaginatorClient() *srpc.Client {
    73  	if imaginatorSrpcClient == nil {
    74  		var err error
    75  		clientName := fmt.Sprintf("%s:%d",
    76  			*imaginatorHostname, *imaginatorPortNum)
    77  		imaginatorSrpcClient, err = srpc.DialHTTP("tcp", clientName, 0)
    78  		if err != nil {
    79  			fmt.Fprintf(os.Stderr, "Error dialing: %s: %s\n", clientName, err)
    80  			os.Exit(1)
    81  		}
    82  	}
    83  	return imaginatorSrpcClient
    84  }
    85  
    86  func getImageServerClient() *srpc.Client {
    87  	if imageServerSrpcClient == nil {
    88  		var err error
    89  		clientName := fmt.Sprintf("%s:%d",
    90  			*imageServerHostname, *imageServerPortNum)
    91  		imageServerSrpcClient, err = srpc.DialHTTP("tcp", clientName, 0)
    92  		if err != nil {
    93  			fmt.Fprintf(os.Stderr, "Error dialing: %s: %s\n", clientName, err)
    94  			os.Exit(1)
    95  		}
    96  	}
    97  	return imageServerSrpcClient
    98  }
    99  
   100  func doMain() int {
   101  	if err := loadflags.LoadForCli("builder-tool"); err != nil {
   102  		fmt.Fprintln(os.Stderr, err)
   103  		return 1
   104  	}
   105  	flag.Usage = printUsage
   106  	flag.Parse()
   107  	if flag.NArg() < 1 {
   108  		printUsage()
   109  		return 3
   110  	}
   111  	if *expiresIn > 0 && *expiresIn < minimumExpiration {
   112  		fmt.Fprintf(os.Stderr, "Minimum expiration: %s\n", minimumExpiration)
   113  		return 2
   114  	}
   115  	logger := cmdlogger.New()
   116  	if err := setupclient.SetupTls(true); err != nil {
   117  		fmt.Fprintln(os.Stderr, err)
   118  		return 1
   119  	}
   120  	os.Unsetenv("LANG")
   121  	return commands.RunCommands(subcommands, printUsage, logger)
   122  }
   123  
   124  func main() {
   125  	os.Exit(doMain())
   126  }