agent

package
v0.52.0-alpha.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2025 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AgentCmd = &cobra.Command{
	Use:   "agent",
	Short: "Start the agent process",
	Args:  cobra.NoArgs,
	RunE: func(cmd *cobra.Command, args []string) error {
		setLogLevel()

		agentMode := config.ModeWorkspace

		if targetModeFlag {
			agentMode = config.ModeTarget
		}

		c, err := config.GetConfig(agentMode)
		if err != nil {
			return err
		}

		telemetryEnabled := os.Getenv("DAYTONA_TELEMETRY_ENABLED") == "true"

		var ws *models.Workspace

		c.WorkspaceDir = os.Getenv("HOME")

		if agentMode == config.ModeWorkspace {
			ws, err = getWorkspace(c, telemetryEnabled)
			if err != nil {
				return err
			}

			c.WorkspaceDir = filepath.Join(os.Getenv("HOME"), ws.WorkspaceFolderName())
		}

		if workspaceDir := os.Getenv("DAYTONA_WORKSPACE_DIR"); workspaceDir != "" {
			c.WorkspaceDir = workspaceDir
		}

		if _, err := os.Stat(c.WorkspaceDir); os.IsNotExist(err) {
			if err := os.MkdirAll(c.WorkspaceDir, 0755); err != nil {
				return fmt.Errorf("failed to create workspace directory: %w", err)
			}
		}

		gitLogWriter := io.MultiWriter(os.Stdout)
		dockerCredHelperLogWriter := io.MultiWriter(os.Stdout)
		var agentLogWriter io.Writer
		if c.LogFilePath != nil {
			logFile, err := os.OpenFile(*c.LogFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
			if err != nil {
				return err
			}
			defer logFile.Close()
			gitLogWriter = io.MultiWriter(os.Stdout, logFile)
			dockerCredHelperLogWriter = io.MultiWriter(os.Stdout, logFile)
			agentLogWriter = logFile
		}

		git := &git.Service{
			WorkspaceDir:      c.WorkspaceDir,
			GitConfigFileName: filepath.Join(os.Getenv("HOME"), ".gitconfig"),
			LogWriter:         gitLogWriter,
		}

		dockerCredHelper := &docker.DockerCredHelper{
			DockerConfigFileName: filepath.Join(os.Getenv("HOME"), ".docker", "config.json"),
			LogWriter:            dockerCredHelperLogWriter,
			HomeDir:              os.Getenv("HOME"),
		}

		sshServer := &ssh.Server{
			WorkspaceDir:        c.WorkspaceDir,
			DefaultWorkspaceDir: os.Getenv("HOME"),
		}

		tailscaleHostname := common.GetTailscaleHostname(c.TargetId)
		if agentMode == config.ModeWorkspace {
			tailscaleHostname = common.GetTailscaleHostname(ws.Id)
		}

		toolBoxServer := &toolbox.Server{
			WorkspaceDir: c.WorkspaceDir,
		}

		tailscaleServer := &tailscale.Server{
			Hostname:         tailscaleHostname,
			Server:           c.Server,
			TelemetryEnabled: telemetryEnabled,
			ClientId:         c.ClientId,
		}

		agent := agent.Agent{
			Config:           c,
			Git:              git,
			DockerCredHelper: dockerCredHelper,
			Ssh:              sshServer,
			Toolbox:          toolBoxServer,
			Tailscale:        tailscaleServer,
			LogWriter:        agentLogWriter,
			TelemetryEnabled: telemetryEnabled,
			Workspace:        ws,
		}

		return agent.Start()
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL