Esempio n. 1
0
func main() {
	// Get the basic info needed to run the agent from command line flags.
	taskId := flag.String("task_id", "", "id of task to run")
	taskSecret := flag.String("task_secret", "", "secret of task to run")
	apiServer := flag.String("api_server", "", "URL of API server")
	httpsCertFile := flag.String("https_cert", "", "path to a self-signed private cert")
	logPrefix := flag.String("log_prefix", "", "prefix for the agent's log filename")
	pidFile := flag.String("pid_file", "", "path to pid file")
	flag.Parse()

	httpsCert, err := getHTTPSCertFile(*httpsCertFile)
	if err != nil {
		fmt.Fprintf(os.Stderr, "could not decode https certificate file: %v\n", err)
		os.Exit(1)
	}

	logFile := *logPrefix + logSuffix()

	agt, err := agent.New(*apiServer, *taskId, *taskSecret, logFile, httpsCert, *pidFile)
	if err != nil {
		fmt.Fprintf(os.Stderr, "could not create new agent: %v\n", err)
		os.Exit(1)
	}
	if err := agt.CreatePidFile(*pidFile); err != nil {
		fmt.Fprintf(os.Stderr, "error creating pidFile: %v\n", err)
		os.Exit(1)
	}

	// enable debug traces on SIGQUIT signaling
	go agent.DumpStackOnSIGQUIT(&agt)

	exitCode := 0
	// run all tasks until an API server's response has RunNext set to false
	for {
		resp, err := agt.RunTask()
		if err != nil {
			fmt.Fprintf(os.Stderr, "error running task: %v\n", err)
			exitCode = 1
			break
		}

		if resp == nil {
			fmt.Fprintf(os.Stderr, "received nil response from API server\n")
			exitCode = 1
			break
		}

		if !resp.RunNext {
			break
		}

		agt, err = agent.New(*apiServer, resp.TaskId, resp.TaskSecret, logFile, httpsCert, *pidFile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "could not create new agent for next task '%v': %v\n", resp.TaskId, err)
			exitCode = 1
			break
		}
	}
	agent.ExitAgent(nil, exitCode, *pidFile)
}
Esempio n. 2
0
func main() {
	// Get the basic info needed to run the agent from command line flags.
	taskId := flag.String("task_id", "", "id of task to run")
	taskSecret := flag.String("task_secret", "", "secret of task to run")
	apiServer := flag.String("api_server", "", "URL of API server")
	httpsCertFile := flag.String("https_cert", "", "path to a self-signed private cert")
	logFile := flag.String("log_file", "", "log file for agent")
	flag.Parse()

	httpsCert, err := getHTTPSCertFile(*httpsCertFile)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Could decode https certificate file: %v\n", err)
		os.Exit(1)
	}

	agt, err := agent.New(*apiServer, *taskId, *taskSecret, *logFile, httpsCert)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Could not create new agent: %v\n", err)
		os.Exit(1)
	}

	// run all tasks until an API server's response has RunNext set to false
	for {
		resp, err := agt.RunTask()
		if err != nil {
			fmt.Fprintf(os.Stderr, "error running task: %v\n", err)
			os.Exit(1)
		}

		if resp == nil {
			fmt.Fprintf(os.Stderr, "received nil response from API server\n")
			os.Exit(1)
		}

		if !resp.RunNext {
			os.Exit(0)
		}

		agt, err = agent.New(*apiServer, resp.TaskId, resp.TaskSecret, *logFile, httpsCert)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Could not create new agent: %v\n", err)
			os.Exit(1)
		}
	}
}