server

package
v0.21.1 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2024 License: Apache-2.0 Imports: 33 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ServeCmd = &cobra.Command{
	Use:   "serve",
	Short: "Run the server process in the current terminal session",
	Args:  cobra.NoArgs,
	Run: func(cmd *cobra.Command, args []string) {
		if log.GetLevel() < log.InfoLevel {

			log.SetLevel(log.InfoLevel)
		}

		configDir, err := server.GetConfigDir()
		if err != nil {
			log.Fatal(err)
		}

		c, err := server.GetConfig()
		if err != nil {
			log.Fatal(err)
		}

		apiServer := api.NewApiServer(api.ApiServerConfig{
			ApiPort: int(c.ApiPort),
		})

		logsDir, err := server.GetWorkspaceLogsDir()
		if err != nil {
			log.Fatal(err)
		}
		loggerFactory := logs.NewLoggerFactory(logsDir)

		dbPath, err := getDbPath()
		if err != nil {
			log.Fatal(err)
		}

		dbConnection := db.GetSQLiteConnection(dbPath)
		apiKeyStore, err := db.NewApiKeyStore(dbConnection)
		if err != nil {
			log.Fatal(err)
		}
		containerRegistryStore, err := db.NewContainerRegistryStore(dbConnection)
		if err != nil {
			log.Fatal(err)
		}
		gitProviderConfigStore, err := db.NewGitProviderConfigStore(dbConnection)
		if err != nil {
			log.Fatal(err)
		}
		providerTargetStore, err := db.NewProviderTargetStore(dbConnection)
		if err != nil {
			log.Fatal(err)
		}
		workspaceStore, err := db.NewWorkspaceStore(dbConnection)
		if err != nil {
			log.Fatal(err)
		}
		profileDataStore, err := db.NewProfileDataStore(dbConnection)
		if err != nil {
			log.Fatal(err)
		}

		headscaleServer := headscale.NewHeadscaleServer(&headscale.HeadscaleServerConfig{
			ServerId:      c.Id,
			FrpsDomain:    c.Frps.Domain,
			FrpsProtocol:  c.Frps.Protocol,
			HeadscalePort: c.HeadscalePort,
		})
		err = headscaleServer.Init()
		if err != nil {
			log.Fatal(err)
		}

		containerRegistryService := containerregistries.NewContainerRegistryService(containerregistries.ContainerRegistryServiceConfig{
			Store: containerRegistryStore,
		})

		var localContainerRegistry server.ILocalContainerRegistry

		if c.BuilderRegistryServer != "local" {
			_, err := containerRegistryService.Find(c.BuilderRegistryServer)
			if err != nil {
				log.Errorf("Failed to find container registry credentials for builder registry server %s\n", c.BuilderRegistryServer)
				log.Errorf("Defaulting to local container registry. To use %s as the builder registry, add credentials for the registry server with 'daytona container-registry set' and restart the server\n", c.BuilderRegistryServer)
				c.BuilderRegistryServer = "local"
			}
		}

		if c.BuilderRegistryServer == "local" {
			localContainerRegistry = registry.NewLocalContainerRegistry(&registry.LocalContainerRegistryConfig{
				DataPath: filepath.Join(configDir, "registry"),
				Port:     c.LocalBuilderRegistryPort,
			})
			c.BuilderRegistryServer = util.GetFrpcRegistryDomain(c.Id, c.Frps.Domain)
		}

		providerTargetService := providertargets.NewProviderTargetService(providertargets.ProviderTargetServiceConfig{
			TargetStore: providerTargetStore,
		})
		apiKeyService := apikeys.NewApiKeyService(apikeys.ApiKeyServiceConfig{
			ApiKeyStore: apiKeyStore,
		})

		headscaleUrl := util.GetFrpcHeadscaleUrl(c.Frps.Protocol, c.Id, c.Frps.Domain)

		providerManager := manager.NewProviderManager(manager.ProviderManagerConfig{
			LogsDir:               logsDir,
			ProviderTargetService: providerTargetService,
			ApiUrl:                util.GetFrpcApiUrl(c.Frps.Protocol, c.Id, c.Frps.Domain),
			DaytonaDownloadUrl:    getDaytonaScriptUrl(c),
			ServerUrl:             headscaleUrl,
			RegistryUrl:           c.RegistryUrl,
			BaseDir:               c.ProvidersDir,
			CreateProviderNetworkKey: func(providerName string) (string, error) {
				return headscaleServer.CreateAuthKey()
			},
			ServerPort: c.HeadscalePort,
			ApiPort:    c.ApiPort,
		})

		buildImageNamespace := c.BuildImageNamespace
		if buildImageNamespace != "" {
			buildImageNamespace = fmt.Sprintf("/%s", buildImageNamespace)
		}
		buildImageNamespace = strings.TrimSuffix(buildImageNamespace, "/")

		builderFactory := builder.NewBuilderFactory(builder.BuilderConfig{
			ServerConfigFolder:              configDir,
			ContainerRegistryServer:         c.BuilderRegistryServer,
			BasePath:                        filepath.Join(configDir, "builds"),
			BuildImageNamespace:             buildImageNamespace,
			LoggerFactory:                   loggerFactory,
			DefaultProjectImage:             c.DefaultProjectImage,
			DefaultProjectUser:              c.DefaultProjectUser,
			DefaultProjectPostStartCommands: c.DefaultProjectPostStartCommands,
			Image:                           c.BuilderImage,
			ContainerRegistryService:        containerRegistryService,
		})
		provisioner := provisioner.NewProvisioner(provisioner.ProvisionerConfig{
			ProviderManager: providerManager,
		})
		gitProviderService := gitproviders.NewGitProviderService(gitproviders.GitProviderServiceConfig{
			ConfigStore: gitProviderConfigStore,
		})

		workspaceService := workspaces.NewWorkspaceService(workspaces.WorkspaceServiceConfig{
			WorkspaceStore:                  workspaceStore,
			TargetStore:                     providerTargetStore,
			ApiKeyService:                   apiKeyService,
			GitProviderService:              gitProviderService,
			ContainerRegistryService:        containerRegistryService,
			ServerApiUrl:                    util.GetFrpcApiUrl(c.Frps.Protocol, c.Id, c.Frps.Domain),
			ServerUrl:                       headscaleUrl,
			DefaultProjectImage:             c.DefaultProjectImage,
			DefaultProjectUser:              c.DefaultProjectUser,
			DefaultProjectPostStartCommands: c.DefaultProjectPostStartCommands,
			Provisioner:                     provisioner,
			LoggerFactory:                   loggerFactory,
			BuilderFactory:                  builderFactory,
		})
		profileDataService := profiledata.NewProfileDataService(profiledata.ProfileDataServiceConfig{
			ProfileDataStore: profileDataStore,
		})

		server := server.GetInstance(&server.ServerInstanceConfig{
			Config:                   *c,
			TailscaleServer:          headscaleServer,
			ProviderTargetService:    providerTargetService,
			ContainerRegistryService: containerRegistryService,
			LocalContainerRegistry:   localContainerRegistry,
			ApiKeyService:            apiKeyService,
			WorkspaceService:         workspaceService,
			GitProviderService:       gitProviderService,
			ProviderManager:          providerManager,
			ProfileDataService:       profileDataService,
		})

		errCh := make(chan error)

		err = server.Start(errCh)
		if err != nil {
			log.Fatal(err)
		}

		go func() {
			err := apiServer.Start()
			if err != nil {
				log.Fatal(err)
			}
		}()

		go func() {
			err := <-errCh
			if err != nil {
				log.Fatal(err)
			}
		}()

		err = waitForServerToStart(apiServer)

		if err != nil {
			log.Fatal(err)
		}

		printServerStartedMessage(c, false)

		err = setDefaultConfig(server, c.ApiPort)
		if err != nil {
			log.Fatal(err)
		}

		err = <-errCh
		if err != nil {
			log.Fatal(err)
		}
	},
}
View Source
var ServerCmd = &cobra.Command{
	Use:   "server",
	Short: "Start the server process in daemon mode",
	Args:  cobra.NoArgs,
	Run: func(cmd *cobra.Command, args []string) {
		confirmCheck := true

		if !yesFlag {
			view.ConfirmPrompt(&confirmCheck)
			if !confirmCheck {
				views.RenderInfoMessage("Operation cancelled.")
				return
			}
		}

		if log.GetLevel() < log.InfoLevel {

			log.SetLevel(log.InfoLevel)
		}

		c, err := server.GetConfig()
		if err != nil {
			log.Fatal(err)
		}

		apiServer := api.NewApiServer(api.ApiServerConfig{
			ApiPort: int(c.ApiPort),
		})

		views.RenderInfoMessageBold("Starting the Daytona Server daemon...")
		err = daemon.Start(c.LogFilePath)
		if err != nil {
			log.Fatal(err)
		}
		err = waitForServerToStart(apiServer)
		if err != nil {
			log.Fatal(err)
		}
		printServerStartedMessage(c, true)
	},
}

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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