github.com/Cloud-Foundations/Dominator@v0.3.4/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  	digraphExcludes flagutil.StringList
    23  	digraphIncludes flagutil.StringList
    24  	disableFor      = flag.Duration("disableFor", 5*time.Minute,
    25  		"How long to disable")
    26  	expiresIn = flag.Duration("expiresIn", time.Hour,
    27  		"How long before the image expires (auto deletes)")
    28  	imaginatorHostname = flag.String("imaginatorHostname", "localhost",
    29  		"Hostname of image build server")
    30  	imaginatorPortNum = flag.Uint("imaginatorPortNum",
    31  		constants.ImaginatorPortNumber,
    32  		"Port number of image build server")
    33  	imageFilename = flag.String("imageFilename", "",
    34  		"Name of file to write image to")
    35  	imageServerHostname = flag.String("imageServerHostname", "localhost",
    36  		"Hostname of image server")
    37  	imageServerPortNum = flag.Uint("imageServerPortNum",
    38  		constants.ImageServerPortNumber,
    39  		"Port number of image server")
    40  	maxSourceAge = flag.Duration("maxSourceAge", time.Hour,
    41  		"Maximum age of a source image before it is rebuilt")
    42  	mtimesCopyFilterFile = flag.String("mtimesCopyFilterFile", "",
    43  		"Filter file to apply when copying mtimes")
    44  	rawSize      flagutil.Size
    45  	showFetchLog = flag.Bool("showFetchLog", false,
    46  		"If true, show fetch log when getting directed graph")
    47  	variablesFilename = flag.String("variablesFilename", "",
    48  		"Name of file to read variables from for local builds")
    49  
    50  	minimumExpiration = 5 * time.Minute
    51  )
    52  
    53  func init() {
    54  	flag.Var(&bindMounts, "bindMounts",
    55  		"Comma separated list of directories to bind mount into build workspace")
    56  	flag.Var(&digraphExcludes, "digraphExcludes",
    57  		"Comma separated list of excludes when generating digraph")
    58  	flag.Var(&digraphIncludes, "digraphIncludes",
    59  		"Comma separated list of includes when generating digraph")
    60  	flag.Var(&rawSize, "rawSize", "Size of RAW file to create")
    61  }
    62  
    63  func printUsage() {
    64  	w := flag.CommandLine.Output()
    65  	fmt.Fprintln(w, "Usage: builder-tool [flags...] command [args...]")
    66  	fmt.Fprintln(w, "Common flags:")
    67  	flag.PrintDefaults()
    68  	fmt.Fprintln(w, "Commands:")
    69  	commands.PrintCommands(w, subcommands)
    70  }
    71  
    72  var subcommands = []commands.Command{
    73  	{"build-from-manifest", "manifestDir stream-name", 2, 2,
    74  		buildFromManifestSubcommand},
    75  	{"build-image", "stream-name [git-branch]", 1, 2, buildImageSubcommand},
    76  	{"build-raw-from-manifest", "manifestDir rawFile", 2, 2,
    77  		buildRawFromManifestSubcommand},
    78  	{"build-tree-from-manifest", "manifestDir", 1, 1,
    79  		buildTreeFromManifestSubcommand},
    80  	{"disable-auto-builds", "", 0, 0, disableAutoBuildsSubcommand},
    81  	{"disable-build-requests", "", 0, 0, disableBuildRequestsSubcommand},
    82  	{"get-dependencies", "", 0, 0, getDependenciesSubcommand},
    83  	{"get-digraph", "", 0, 0, getDirectedGraphSubcommand},
    84  	{"process-manifest", "manifestDir rootDir", 2, 2,
    85  		processManifestSubcommand},
    86  	{"replace-idle-slaves", "", 0, 0, replaceIdleSlavesSubcommand},
    87  }
    88  
    89  var imaginatorSrpcClient *srpc.Client
    90  var imageServerSrpcClient *srpc.Client
    91  
    92  func getImaginatorClient() *srpc.Client {
    93  	if imaginatorSrpcClient == nil {
    94  		var err error
    95  		clientName := fmt.Sprintf("%s:%d",
    96  			*imaginatorHostname, *imaginatorPortNum)
    97  		imaginatorSrpcClient, err = srpc.DialHTTP("tcp", clientName, 0)
    98  		if err != nil {
    99  			fmt.Fprintf(os.Stderr, "Error dialing: %s: %s\n", clientName, err)
   100  			os.Exit(1)
   101  		}
   102  	}
   103  	return imaginatorSrpcClient
   104  }
   105  
   106  func getImageServerClient() *srpc.Client {
   107  	if imageServerSrpcClient == nil {
   108  		var err error
   109  		clientName := fmt.Sprintf("%s:%d",
   110  			*imageServerHostname, *imageServerPortNum)
   111  		imageServerSrpcClient, err = srpc.DialHTTP("tcp", clientName, 0)
   112  		if err != nil {
   113  			fmt.Fprintf(os.Stderr, "Error dialing: %s: %s\n", clientName, err)
   114  			os.Exit(1)
   115  		}
   116  	}
   117  	return imageServerSrpcClient
   118  }
   119  
   120  func doMain() int {
   121  	if err := loadflags.LoadForCli("builder-tool"); err != nil {
   122  		fmt.Fprintln(os.Stderr, err)
   123  		return 1
   124  	}
   125  	flag.Usage = printUsage
   126  	flag.Parse()
   127  	if flag.NArg() < 1 {
   128  		printUsage()
   129  		return 3
   130  	}
   131  	if *expiresIn > 0 && *expiresIn < minimumExpiration {
   132  		fmt.Fprintf(os.Stderr, "Minimum expiration: %s\n", minimumExpiration)
   133  		return 2
   134  	}
   135  	logger := cmdlogger.New()
   136  	srpc.SetDefaultLogger(logger)
   137  	if err := setupclient.SetupTls(true); err != nil {
   138  		fmt.Fprintln(os.Stderr, err)
   139  		return 1
   140  	}
   141  	os.Unsetenv("LANG")
   142  	return commands.RunCommands(subcommands, printUsage, logger)
   143  }
   144  
   145  func main() {
   146  	os.Exit(doMain())
   147  }