// TODO(jbd): add link that explains the use of agent.
519390
func main() {
	if len(os.Args) < 2 {
		processes()
		return
	}

	cmd := os.Args[1]

	// See if it is a PID.
	pid, err := strconv.Atoi(cmd)
	if err == nil {
		processInfo(pid)
		return
	}

	if cmd == "help" {
		usage("")
	}

	if cmd == "tree" {
		displayProcessTree()
		return
	}

	fn, ok := cmds[cmd]
	if !ok {
		usage("unknown subcommand")
	}
	if len(os.Args) < 3 {
		usage("Missing PID or address.")
		os.Exit(1)
	}
	addr, err := targetToAddr(os.Args[2])
	if err != nil {
		fmt.Fprintf(os.Stderr, "Couldn't resolve addr or pid %v to TCPAddress: %v\n", os.Args[2], err)
		os.Exit(1)
	}
	var params []string
	if len(os.Args) > 3 {
		params = append(params, os.Args[3:]...)
	}
	if err := fn(*addr, params); err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		os.Exit(1)
	}
}
712927
func ChangeAgentTools(dataDir string, agentName string, vers version.Binary) (*coretools.Tools, error) {
	tools, err := ReadTools(dataDir, vers)
	if err != nil {
		return nil, err
	}
	// build absolute path to toolsDir. Windows implementation of symlink
	// will check for the existence of the source file and error if it does
	// not exists. This is a limitation of junction points (symlinks) on NTFS
	toolPath := ToolsDir(dataDir, tools.Version.String())
	toolsDir := ToolsDir(dataDir, agentName)

	err = symlink.Replace(toolsDir, toolPath)
	if err != nil {
		return nil, fmt.Errorf("cannot replace tools directory: %s", err)
	}
	return tools, nil
}
335651
func (srv *Instance) AgentMetadata(agent string) (agents.Metadata, bool) {
	a, found := srv.agents.Get(agent)
	if !found {
		return agents.Metadata{}, false
	}

	return *a.Metadata(), true
}
410648
func getAgent(ctx context.Context) (*Agent, error) {
	agent := Agent{
		agentAutoStartPath:        "C:\\Users\\{{.User}}\\Start Menu\\Programs\\Startup\\machine-provider-agent.bat",
		agentAutoStartTemplate:    "machine-provider-agent.bat.tmpl",
		logsDir:                   "C:\\logs",
		swarmingAutoStartPath:     "C:\\Users\\{{.User}}\\Start Menu\\Programs\\Startup\\swarming-start-bot.bat",
		swarmingAutoStartTemplate: "swarming-start-bot.bat.tmpl",
		swarmingBotDir:            "C:\\b\\s",
		strategy:                  WindowsStrategy{},
	}
	logging.Infof(ctx, "Using Windows agent.")
	return &agent, nil
}
724201
func (m *Machine) AgentTools() (*tools.Tools, error) {
	if m.doc.Tools == nil {
		return nil, errors.NotFoundf("agent binaries for machine %v", m)
	}
	tools := *m.doc.Tools
	return &tools, nil
}
520569
func UserAgent() string {
	if Prerelease != "" {
		return fmt.Sprintf("triton-go/%s-%s (%s-%s; %s)", Version, Prerelease,
			runtime.GOARCH, runtime.GOOS, runtime.Version())
	}

	return fmt.Sprintf("triton-go/%s (%s-%s; %s)", Version, runtime.GOARCH,
		runtime.GOOS, runtime.Version())
}
427819
func (msg *MsgVersion) AddUserAgent(name string, version string,
	comments ...string) error {

	newUserAgent := fmt.Sprintf("%s:%s", name, version)
	if len(comments) != 0 {
		newUserAgent = fmt.Sprintf("%s(%s)", newUserAgent,
			strings.Join(comments, "; "))
	}
	newUserAgent = fmt.Sprintf("%s%s/", msg.UserAgent, newUserAgent)
	err := validateUserAgent(newUserAgent)
	if err != nil {
		return err
	}
	msg.UserAgent = newUserAgent
	return nil
}
1043179
func ConnectWithAgent(host, username string) (*Client, error) {
	return ConnectWithAgentTimeout(host, username, DefaultTimeout)
}
1317252
func NewMixedAgent(bin string) (*NomadAgent, error) {
	if err := os.MkdirAll(BaseDir, 755); err != nil {
		return nil, err
	}
	dir, err := ioutil.TempDir(BaseDir, "agent")
	if err != nil {
		return nil, err
	}

	vars, err := newAgentTemplateVars()
	if err != nil {
		return nil, err
	}
	vars.EnableClient = true
	vars.EnableServer = true

	conf := filepath.Join(dir, "config.hcl")
	if err := writeConfig(conf, vars); err != nil {
		return nil, err
	}

	na := &NomadAgent{
		BinPath:  bin,
		DataDir:  dir,
		ConfFile: conf,
		Vars:     vars,
		Cmd:      exec.Command(bin, "agent", "-config", conf, "-data-dir", dir),
	}
	return na, nil
}
253962
func (a *LocalKeyAgent) AddKey(key *Key) (*agent.AddedKey, error) {
	// save it to disk (usually into ~/.tsh)
	err := a.keyStore.AddKey(a.proxyHost, a.username, key)
	if err != nil {
		return nil, trace.Wrap(err)
	}

	// load key into the teleport agent and system agent
	return a.LoadKey(*key)
}
1148004
func (a *ACL) AllowAgentRead() bool {
	switch {
	case a.management:
		return true
	case a.agent == PolicyWrite:
		return true
	case a.agent == PolicyRead:
		return true
	default:
		return false
	}
}
959089
func AgentDone(logger loggo.Logger, err error) error {
	err = errors.Cause(err)
	switch err {
	case jworker.ErrTerminateAgent, jworker.ErrRebootMachine, jworker.ErrShutdownMachine:
		// These errors are swallowed here because we want to exit
		// the agent process without error, to avoid the init system
		// restarting us.
		err = nil
	}
	if ug, ok := err.(*upgrader.UpgradeReadyError); ok {
		if err := ug.ChangeAgentTools(); err != nil {
			// Return and let the init system deal with the restart.
			err = errors.Annotate(err, "cannot change agent binaries")
			logger.Infof(err.Error())
			return err
		}
	}
	if err == jworker.ErrRestartAgent {
		logger.Warningf("agent restarting")
	}
	return err
}
207902
func (s *ServiceSpec) Link(scope Scope) error {
	if s.linked() {
		return nil
	}

	if s.parentSrc != nil {
		parent, err := resolveService(*s.parentSrc, scope)
		if err != nil {
			return compileError{
				Target: s.Name,
				Reason: referenceError{
					Target:    s.parentSrc.Name,
					Line:      s.parentSrc.Line,
					ScopeName: scope.GetName(),
					Reason:    err,
				},
			}
		}

		if err := parent.Link(scope); err != nil {
			return compileError{Target: s.Name, Reason: err}
		}

		s.Parent = parent
		s.parentSrc = nil
	}

	for _, function := range s.Functions {
		if err := function.Link(scope); err != nil {
			return compileError{
				Target: s.Name + "." + function.Name,
				Reason: err,
			}
		}
	}

	return nil
}
1074675
func (a *Agent) Join(addr string, wan bool) error {
	r := a.c.newRequest("PUT", "/v1/agent/join/"+addr)
	if wan {
		r.params.Set("wan", "1")
	}
	_, resp, err := requireOK(a.c.doRequest(r))
	if err != nil {
		return err
	}
	resp.Body.Close()
	return nil
}
857756
func RegisterAdditionalAgentProvider(p AgentProvider) {
	aapmu.Lock()
	defer aapmu.Unlock()

	additionalAgentProviders = append(additionalAgentProviders, p)
	build.AgentProviders = append(build.AgentProviders, p.Version())
}
1141027
func (c *connections) ForAgent(agent string) Connections {
	var values []Value
	for _, value := range c.values {
		if value.Agent == agent {
			values = append(values, value)
		}
	}
	return &connections{model: c.model, values: values}
}
966905
func loadAgentFile(filename string) (*redditproto.UserAgent, error) {
	buf, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	agent := &redditproto.UserAgent{}
	return agent, proto.UnmarshalText(bytes.NewBuffer(buf).String(), agent)
}