func registerAgentFromCli(c *cli.Context) *buildbox.Agent { // Init debugging if c.Bool("debug") { buildbox.LoggerInitDebug() } agentRegistrationToken := c.String("agent-registration-token") // Should we look to the environment for the agent registration token? if agentRegistrationToken == AgentRegistrationTokenDefault { agentRegistrationToken = os.Getenv(AgentRegistrationTokenDefault) } if agentRegistrationToken == "" { fmt.Println("buildbox-agent: missing agent registration token\nSee 'buildbox-agent register --help'") os.Exit(1) } // Create a client so we can register the agent var client buildbox.Client client.AuthorizationToken = agentRegistrationToken client.URL = c.String("url") // Register the agent agentAccessToken, err := client.AgentRegister(c.String("name"), c.StringSlice("meta-data")) if err != nil { buildbox.Logger.Fatal(err) } // Start the agent using the token we have return setupAgent("register", agentAccessToken, c.String("bootstrap-script"), c.String("url")) }
func setupAgentFromCli(c *cli.Context, command string) *buildbox.Agent { // Init debugging if c.Bool("debug") { buildbox.LoggerInitDebug() } agentAccessToken := c.String("agent-access-token") // Should we look to the environment for the agent access token? if agentAccessToken == AgentAccessTokenDefault { agentAccessToken = os.Getenv(AgentAccessTokenEnv) } if agentAccessToken == "" { fmt.Println("buildbox-data: missing agent access token\nSee 'buildbox-data start --help'") os.Exit(1) } // Set the agent options var agent buildbox.Agent // Client specific options agent.Client.AuthorizationToken = agentAccessToken agent.Client.URL = c.String("url") return &agent }
func setupAgentFromCli(c *cli.Context, command string) *buildbox.Agent { // Init debugging if c.Bool("debug") { buildbox.LoggerInitDebug() } return setupAgent(command, c.String("access-token"), c.String("bootstrap-script"), c.String("url")) }
func setupAgentFromCli(c *cli.Context, command string) *buildbox.Agent { // Init debugging if c.Bool("debug") { buildbox.LoggerInitDebug() } agentAccessToken := c.String("access-token") // Should we look to the environment for the agent access token? if agentAccessToken == AgentAccessTokenDefault { agentAccessToken = os.Getenv(AgentAccessTokenEnv) } if agentAccessToken == "" { fmt.Println("buildbox-agent: missing agent access token\nSee 'buildbox-agent start --help'") os.Exit(1) } bootstrapScript := c.String("bootstrap-script") // Expand the envionment variable. if bootstrapScript == BootstrapScriptDefault { bootstrapScript = os.ExpandEnv(bootstrapScript) } // Make sure the boostrap script exists. if _, err := os.Stat(bootstrapScript); os.IsNotExist(err) { print("buildbox-agent: no such file " + bootstrapScript + "\n") os.Exit(1) } // Set the agent options var agent buildbox.Agent agent.BootstrapScript = bootstrapScript // Client specific options agent.Client.AgentAccessToken = agentAccessToken agent.Client.URL = c.String("url") // Setup the agent agent.Setup() // A nice welcome message buildbox.Logger.WithFields(logrus.Fields{ "pid": os.Getpid(), "version": buildbox.Version, }).Infof("Started buildbox-agent `%s`", agent.Name) return &agent }
func main() { cli.AppHelpTemplate = AppHelpTemplate cli.CommandHelpTemplate = CommandHelpTemplate app := cli.NewApp() app.Name = "buildbox-artifact" app.Version = buildbox.Version // Define the actions for our CLI app.Commands = []cli.Command{ { Name: "upload", Usage: "Upload the following artifacts to the build", Description: UploadHelpDescription, Flags: []cli.Flag{ cli.StringFlag{"job", JobIdDefault, "Which job should the artifacts be uploaded to"}, cli.StringFlag{"agent-access-token", AgentAccessTokenDefault, "The access token used to identify the agent"}, cli.StringFlag{"url", "https://agent.buildbox.io/v2", "The agent API endpoint"}, cli.BoolFlag{"debug", "Enable debug mode"}, }, Action: func(c *cli.Context) { // Init debugging if c.Bool("debug") { buildbox.LoggerInitDebug() } agentAccessToken := c.String("agent-access-token") // Should we look to the environment for the agent access token? if agentAccessToken == AgentAccessTokenDefault { agentAccessToken = os.Getenv(AgentAccessTokenEnv) } if agentAccessToken == "" { fmt.Printf("%s: missing agent access token\nSee '%s help upload'\n", app.Name, app.Name) os.Exit(1) } jobId := c.String("job") // Should we look to the environment for the job id? if jobId == JobIdDefault { jobId = os.Getenv(JobIdEnv) } if jobId == "" { fmt.Printf("%s: missing job\nSee '%s help upload'\n", app.Name, app.Name) os.Exit(1) } // Grab the first argument and use as paths to download paths := c.Args().First() if paths == "" { fmt.Printf("%s: missing upload paths\nSee '%s help upload'\n", app.Name, app.Name) os.Exit(1) } // Do we have a custom destination destination := "" if len(c.Args()) > 1 { destination = c.Args()[1] } // Set the agent options var agent buildbox.Agent // Client specific options agent.Client.AuthorizationToken = agentAccessToken agent.Client.URL = c.String("url") // Setup the agent agent.Setup() // Find the actual job now job, err := agent.Client.JobFind(jobId) if err != nil { buildbox.Logger.Fatalf("Could not find job: %s", jobId) } // Create artifact structs for all the files we need to upload artifacts, err := buildbox.CollectArtifacts(job, paths) if err != nil { buildbox.Logger.Fatalf("Failed to collect artifacts: %s", err) } if len(artifacts) == 0 { buildbox.Logger.Infof("No files matched paths: %s", paths) } else { buildbox.Logger.Infof("Found %d files that match \"%s\"", len(artifacts), paths) err := buildbox.UploadArtifacts(agent.Client, job, artifacts, destination) if err != nil { buildbox.Logger.Fatalf("Failed to upload artifacts: %s", err) } } }, }, { Name: "download", Usage: "Download the following artifacts", Description: DownloadHelpDescription, Flags: []cli.Flag{ cli.StringFlag{"job", "", "Which job should the artifacts be downloaded from"}, cli.StringFlag{"build", BuildIdDefault, "Which build should the artifacts be downloaded from"}, cli.StringFlag{"agent-access-token", AgentAccessTokenDefault, "The access token used to identify the agent"}, cli.StringFlag{"url", "https://agent.buildbox.io/v2", "The agent API endpoint"}, cli.BoolFlag{"debug", "Enable debug mode"}, }, Action: func(c *cli.Context) { // Init debugging if c.Bool("debug") { buildbox.LoggerInitDebug() } agentAccessToken := c.String("agent-access-token") // Should we look to the environment for the agent access token? if agentAccessToken == AgentAccessTokenDefault { agentAccessToken = os.Getenv(AgentAccessTokenEnv) } if agentAccessToken == "" { fmt.Printf("%s: missing agent access token\nSee '%s help download'\n", app.Name, app.Name) os.Exit(1) } if len(c.Args()) != 2 { fmt.Printf("%s: invalid usage\nSee '%s help download'\n", app.Name, app.Name) os.Exit(1) } // Find the build id buildId := c.String("build") if buildId == BuildIdDefault { buildId = os.Getenv(BuildIdEnv) } if buildId == "" { fmt.Printf("%s: missing build\nSee '%s help download'\n", app.Name, app.Name) os.Exit(1) } // Get our search query and download destination searchQuery := c.Args()[0] downloadDestination := c.Args()[1] jobQuery := c.String("job") // Turn the download destination into an absolute path and confirm it exists downloadDestination, _ = filepath.Abs(downloadDestination) fileInfo, err := os.Stat(downloadDestination) if err != nil { buildbox.Logger.Fatalf("Could not find information about destination: %s", downloadDestination) } if !fileInfo.IsDir() { buildbox.Logger.Fatalf("%s is not a directory", downloadDestination) } // Set the agent options var agent buildbox.Agent // Client specific options agent.Client.AuthorizationToken = agentAccessToken agent.Client.URL = c.String("url") // Setup the agent agent.Setup() if jobQuery == "" { buildbox.Logger.Infof("Searching for artifacts: \"%s\"", searchQuery) } else { buildbox.Logger.Infof("Searching for artifacts: \"%s\" within job: \"%s\"", searchQuery, jobQuery) } // Search for artifacts (only those that have finished uploaded) to download artifacts, err := agent.Client.SearchArtifacts(buildId, searchQuery, jobQuery, "finished") if err != nil { buildbox.Logger.Fatalf("Failed to find artifacts: %s", err) } if len(artifacts) == 0 { buildbox.Logger.Info("No artifacts found for downloading") } else { buildbox.Logger.Infof("Found %d artifacts. Starting to download to: %s", len(artifacts), downloadDestination) err := buildbox.DownloadArtifacts(artifacts, downloadDestination) if err != nil { buildbox.Logger.Fatalf("Failed to download artifacts: %s", err) } } }, }, } // Default the default action app.Action = func(c *cli.Context) { cli.ShowAppHelp(c) os.Exit(1) } app.Run(os.Args) }