github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/agent/main/agent.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/pem"
     5  	"flag"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"os"
     9  	"time"
    10  
    11  	"github.com/evergreen-ci/evergreen/agent"
    12  	_ "github.com/evergreen-ci/evergreen/plugin/config"
    13  	"github.com/mongodb/grip"
    14  	"github.com/mongodb/grip/level"
    15  	"github.com/pkg/errors"
    16  )
    17  
    18  func init() {
    19  	flag.Usage = func() {
    20  		fmt.Fprintf(os.Stderr, "%s pulls tasks from the API server and runs them.\n", os.Args[0])
    21  		fmt.Fprintf(os.Stderr, "This program is designed to be started by the Evergreen taskrunner, not manually.\n\n")
    22  		fmt.Fprintf(os.Stderr, "Usage:\n  %s [flags]\n\n", os.Args[0])
    23  		fmt.Fprintf(os.Stderr, "Supported flags are:\n")
    24  		flag.PrintDefaults()
    25  	}
    26  }
    27  
    28  const (
    29  	statsPort  = 2285
    30  	agentSleep = time.Minute * 1
    31  )
    32  
    33  // getHTTPSCertFile fetches the contents of the file at httpsCertFile and
    34  // attempts to decode the pem encoded data contained therein. Returns the
    35  // decoded data.
    36  func getHTTPSCertFile(httpsCertFile string) (string, error) {
    37  	var httpsCert []byte
    38  	var err error
    39  
    40  	if httpsCertFile != "" {
    41  		httpsCert, err = ioutil.ReadFile(httpsCertFile)
    42  		if err != nil {
    43  			return "", errors.Wrapf(err, "error reading certficate file %v", httpsCertFile)
    44  		}
    45  		// If we don't test the cert here, we won't know if
    46  		// the cert is invalid unil much later
    47  		decoded_cert, _ := pem.Decode(httpsCert)
    48  		if decoded_cert == nil {
    49  			return "", errors.Errorf("could not decode certficate file (%v)", httpsCertFile)
    50  		}
    51  	}
    52  	return string(httpsCert), nil
    53  }
    54  
    55  func main() {
    56  	// Get the basic info needed to run the agent from command line flags.
    57  	hostId := flag.String("host_id", "", "id of machine agent is running on")
    58  	hostSecret := flag.String("host_secret", "", "secret for the current host")
    59  	apiServer := flag.String("api_server", "", "URL of API server")
    60  	httpsCertFile := flag.String("https_cert", "", "path to a self-signed private cert")
    61  	logPrefix := flag.String("log_prefix", "evg-agent", "prefix for the agent's log filename")
    62  	port := flag.Int("status_port", statsPort, "port to run the status server on")
    63  	flag.Parse()
    64  
    65  	grip.CatchEmergencyFatal(agent.SetupLogging("agent-startup", "init"))
    66  	grip.SetDefaultLevel(level.Info)
    67  	grip.SetThreshold(level.Debug)
    68  	grip.SetName("evg-agent")
    69  
    70  	httpsCert, err := getHTTPSCertFile(*httpsCertFile)
    71  	if err != nil {
    72  		grip.EmergencyFatalf("could not decode https certificate file: %+v", err)
    73  	}
    74  
    75  	// all we need is the host id and host secret
    76  	initialOptions := agent.Options{
    77  		APIURL:      *apiServer,
    78  		Certificate: httpsCert,
    79  		HostId:      *hostId,
    80  		HostSecret:  *hostSecret,
    81  		StatusPort:  *port,
    82  		LogPrefix:   *logPrefix,
    83  	}
    84  
    85  	agt, err := agent.New(initialOptions)
    86  	if err != nil {
    87  		grip.EmergencyFatalf("could not create new agent: %+v", err)
    88  	}
    89  	grip.CatchEmergencyFatal(agt.Run())
    90  }