From 9f81b71a4a6ca42a1679944cd884575d0086316f Mon Sep 17 00:00:00 2001 From: Tanmoy Sarkar <57363826+tanmoysrt@users.noreply.github.com> Date: Sat, 23 Mar 2024 21:41:24 +0530 Subject: [PATCH] fix: use image registry locally also (#417) --- container_manager/container.go | 17 ++ container_manager/service.go | 184 +++++++++++------ swiftwave_service/cmd/init.go | 144 ++++++++++--- swiftwave_service/cmd/localregistry.go | 192 ++++++++++++++++++ swiftwave_service/cmd/root.go | 3 +- swiftwave_service/cmd/start.go | 29 +++ swiftwave_service/cmd/tls.go | 52 +++-- swiftwave_service/cmd/types.go | 1 + swiftwave_service/cmd/utils.go | 10 +- .../config/local_config/default.go | 7 +- .../config/local_config/types.go | 67 +++--- .../config/local_config/utils.go | 20 ++ .../config/system_config/bootstrap/utils.go | 3 +- swiftwave_service/config/utils.go | 21 ++ swiftwave_service/graphql/server.resolvers.go | 10 + swiftwave_service/main.go | 44 +--- .../process_application_build_request.go | 12 +- .../process_application_deploy_request.go | 23 +-- .../process_ingress_rule_delete_request.go | 5 - .../worker/process_setup_proxy_request.go | 5 - .../worker/process_setup_server_request.go | 1 + 21 files changed, 632 insertions(+), 218 deletions(-) create mode 100644 swiftwave_service/cmd/localregistry.go diff --git a/container_manager/container.go b/container_manager/container.go index a3b9f36924..5359e171e0 100644 --- a/container_manager/container.go +++ b/container_manager/container.go @@ -63,3 +63,20 @@ func (m Manager) RunCommandInServiceContainers(serviceName string, command []str } return nil } + +// IsContainerRunning checks if a container is running +func (m Manager) IsContainerRunning(containerName string) (bool, error) { + containers, err := m.client.ContainerList(m.ctx, container.ListOptions{ + All: true, + Filters: filters.NewArgs( + filters.Arg("name", containerName), + ), + }) + if err != nil { + return false, errors.New("Failed to list containers " + err.Error()) + } + if len(containers) == 0 { + return false, nil + } + return containers[0].State == "running", nil +} diff --git a/container_manager/service.go b/container_manager/service.go index 120c843de6..d273486586 100644 --- a/container_manager/service.go +++ b/container_manager/service.go @@ -2,6 +2,7 @@ package containermanger import ( "errors" + "fmt" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" @@ -9,8 +10,11 @@ import ( "github.com/docker/docker/api/types/swarm" "io" "strings" + "time" ) +const maxRetriesForVersionConflict = 5 + func (m Manager) GetService(serviceName string) (Service, error) { serviceData, _, err := m.client.ServiceInspectWithRaw(m.ctx, serviceName, types.ServiceInspectOptions{}) if err != nil { @@ -102,62 +106,96 @@ func (m Manager) UpdateService(service Service, username string, password string if err != nil { return errors.New("failed to generate auth header") } - serviceData, _, err := m.client.ServiceInspectWithRaw(m.ctx, service.Name, types.ServiceInspectOptions{}) - if err != nil { - return errors.New("error getting swarm server version") - } - version := swarm.Version{ - Index: serviceData.Version.Index, - } - if err != nil { - return errors.New("error getting swarm server version") - } - _, err = m.client.ServiceUpdate(m.ctx, service.Name, version, m.serviceToServiceSpec(service), types.ServiceUpdateOptions{ - EncodedRegistryAuth: authHeader, - QueryRegistry: queryRegistry, - }) - if err != nil { - return errors.New("error updating service") + + maxRetries := maxRetriesForVersionConflict + for { + serviceData, _, err := m.client.ServiceInspectWithRaw(m.ctx, service.Name, types.ServiceInspectOptions{}) + if err != nil { + return errors.New("error getting swarm server version") + } + version := swarm.Version{ + Index: serviceData.Version.Index, + } + _, err = m.client.ServiceUpdate(m.ctx, service.Name, version, m.serviceToServiceSpec(service), types.ServiceUpdateOptions{ + EncodedRegistryAuth: authHeader, + QueryRegistry: queryRegistry, + }) + if err != nil { + if strings.Contains(err.Error(), "update out of sequence") { + if maxRetries == 0 { + return fmt.Errorf("error updating service due to version out of sync [retried %d times]", maxRetriesForVersionConflict) + } + <-time.After(3 * time.Second) + maxRetries-- + continue + } + return errors.New("error updating service") + } else { + return nil + } } - return nil } func (m Manager) RestartService(serviceName string) error { - serviceData, _, err := m.client.ServiceInspectWithRaw(m.ctx, serviceName, types.ServiceInspectOptions{}) - if err != nil { - return errors.New("error getting swarm server version") - } - version := swarm.Version{ - Index: serviceData.Version.Index, - } - if err != nil { - return errors.New("error getting swarm server version") - } - spec := serviceData.Spec - spec.TaskTemplate.ForceUpdate++ - _, err = m.client.ServiceUpdate(m.ctx, serviceName, version, spec, types.ServiceUpdateOptions{}) - if err != nil { - return errors.New("error updating service") + maxRetries := 5 + for { + serviceData, _, err := m.client.ServiceInspectWithRaw(m.ctx, serviceName, types.ServiceInspectOptions{}) + if err != nil { + return errors.New("error getting swarm server version") + } + version := swarm.Version{ + Index: serviceData.Version.Index, + } + if err != nil { + return errors.New("error getting swarm server version") + } + spec := serviceData.Spec + spec.TaskTemplate.ForceUpdate++ + _, err = m.client.ServiceUpdate(m.ctx, serviceName, version, spec, types.ServiceUpdateOptions{}) + if err != nil { + if strings.Contains(err.Error(), "update out of sequence") { + if maxRetries == 0 { + return fmt.Errorf("error updating service due to version out of sync [retried %d times]", maxRetriesForVersionConflict) + } + <-time.After(3 * time.Second) + maxRetries-- + continue + } + return errors.New("error updating service") + } else { + return nil + } } - return nil } func (m Manager) RollbackService(serviceName string) error { - serviceData, _, err := m.client.ServiceInspectWithRaw(m.ctx, serviceName, types.ServiceInspectOptions{}) - if err != nil { - return errors.New("error getting swarm server version") - } - version := swarm.Version{ - Index: serviceData.Version.Index, - } - if err != nil { - return errors.New("error getting swarm server version") - } - _, err = m.client.ServiceUpdate(m.ctx, serviceName, version, *serviceData.PreviousSpec, types.ServiceUpdateOptions{}) - if err != nil { - return errors.New("error updating service") + maxRetries := 5 + for { + serviceData, _, err := m.client.ServiceInspectWithRaw(m.ctx, serviceName, types.ServiceInspectOptions{}) + if err != nil { + return errors.New("error getting swarm server version") + } + version := swarm.Version{ + Index: serviceData.Version.Index, + } + if err != nil { + return errors.New("error getting swarm server version") + } + _, err = m.client.ServiceUpdate(m.ctx, serviceName, version, *serviceData.PreviousSpec, types.ServiceUpdateOptions{}) + if err != nil { + if strings.Contains(err.Error(), "update out of sequence") { + if maxRetries == 0 { + return fmt.Errorf("error updating service due to version out of sync [retried %d times]", maxRetriesForVersionConflict) + } + <-time.After(3 * time.Second) + maxRetries-- + continue + } + return errors.New("error updating service") + } else { + return nil + } } - return nil } func (m Manager) RemoveService(serviceName string) error { @@ -169,27 +207,39 @@ func (m Manager) RemoveService(serviceName string) error { } func (m Manager) SetServiceReplicaCount(serviceName string, replicas int) error { - serviceData, _, err := m.client.ServiceInspectWithRaw(m.ctx, serviceName, types.ServiceInspectOptions{}) - if err != nil { - return errors.New("error getting swarm server version") - } - version := swarm.Version{ - Index: serviceData.Version.Index, - } - if err != nil { - return errors.New("error getting swarm server version") - } - spec := serviceData.Spec - if spec.Mode.Replicated == nil { - return errors.New("service is not a replicated service") - } - replicaCount := uint64(replicas) - spec.Mode.Replicated.Replicas = &replicaCount - _, err = m.client.ServiceUpdate(m.ctx, serviceName, version, spec, types.ServiceUpdateOptions{}) - if err != nil { - return errors.New("error updating service") + maxRetries := 5 + for { + serviceData, _, err := m.client.ServiceInspectWithRaw(m.ctx, serviceName, types.ServiceInspectOptions{}) + if err != nil { + return errors.New("error getting swarm server version") + } + version := swarm.Version{ + Index: serviceData.Version.Index, + } + if err != nil { + return errors.New("error getting swarm server version") + } + spec := serviceData.Spec + if spec.Mode.Replicated == nil { + return errors.New("service is not a replicated service") + } + replicaCount := uint64(replicas) + spec.Mode.Replicated.Replicas = &replicaCount + _, err = m.client.ServiceUpdate(m.ctx, serviceName, version, spec, types.ServiceUpdateOptions{}) + if err != nil { + if strings.Contains(err.Error(), "update out of sequence") { + if maxRetries == 0 { + return fmt.Errorf("error updating service due to version out of sync [retried %d times]", maxRetriesForVersionConflict) + } + <-time.After(3 * time.Second) + maxRetries-- + continue + } + return errors.New("error updating service") + } else { + return nil + } } - return nil } func (m Manager) RealtimeInfoRunningServices() (map[string]ServiceRealtimeInfo, error) { diff --git a/swiftwave_service/cmd/init.go b/swiftwave_service/cmd/init.go index 569c755465..4e96549af8 100644 --- a/swiftwave_service/cmd/init.go +++ b/swiftwave_service/cmd/init.go @@ -4,6 +4,7 @@ import ( _ "embed" "fmt" "github.com/spf13/cobra" + config2 "github.com/swiftwave-org/swiftwave/swiftwave_service/config" "github.com/swiftwave-org/swiftwave/swiftwave_service/config/local_config" "io" "log" @@ -15,8 +16,10 @@ import ( func init() { initCmd.Flags().SortFlags = false + initCmd.Flags().String("domain", "", "Domain name to use") initCmd.Flags().Bool("auto-domain", false, "Resolve domain name automatically") initCmd.Flags().Bool("remote-postgres", false, "Opt for remote postgres server") + initCmd.Flags().Bool("overwrite", false, "Overwrite existing config") } var initCmd = &cobra.Command{ @@ -25,37 +28,84 @@ var initCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { isAutoDomainResolve := cmd.Flag("auto-domain").Value.String() == "true" isLocalPostgres := cmd.Flag("remote-postgres").Value.String() == "false" - // Try to fetch local config - _, err := local_config.Fetch() - if err == nil { - printError("Config already exists at " + local_config.LocalConfigPath) - printInfo("Run `swiftwave config` to edit the config file") - os.Exit(1) - } - - // Get Domain Name - domainName, err := resolveQuickDNSDomain() - if err != nil { - printError(err.Error()) - if isAutoDomainResolve { + isOverWrite := cmd.Flag("overwrite").Value.String() == "true" + predefinedDomain := cmd.Flag("domain").Value.String() + if isOverWrite { + printWarning("Overwriting existing config! Restart the service to apply changes") + // try to fetch local config + val, err := local_config.Fetch() + if err == nil { + config = &config2.Config{ + LocalConfig: val, + SystemConfig: nil, + } + } + } else { + // Try to fetch local config + _, err := local_config.Fetch() + if err == nil { + printError("Config already exists at " + local_config.LocalConfigPath) + printInfo("Run `swiftwave config` to edit the config file") os.Exit(1) } } - if !isAutoDomainResolve { - // Ask user to enter domain name with default value as resolved domain name - fmt.Print("Domain Name [default: " + domainName + "]: ") + var domainName string - var inputDomainName string - _, err = fmt.Scanln(&inputDomainName) + if strings.Compare(predefinedDomain, "") == 0 { + // Get Domain Name + domainName, err := resolveQuickDNSDomain() if err != nil { - inputDomainName = "" + printError(err.Error()) + if isAutoDomainResolve { + os.Exit(1) + } } - if strings.TrimSpace(inputDomainName) != "" { - domainName = inputDomainName - printInfo("Domain name set to " + domainName) + + if !isAutoDomainResolve { + // Ask user to enter domain name with default value as resolved domain name + fmt.Print("Domain Name [default: " + domainName + "]: ") + + var inputDomainName string + _, err = fmt.Scanln(&inputDomainName) + if err != nil { + inputDomainName = "" + } + if strings.TrimSpace(inputDomainName) != "" { + domainName = inputDomainName + printInfo("Domain name set to " + domainName) + } } + } else { + printWarning("Using predefined domain name: " + predefinedDomain) + domainName = predefinedDomain } + + currentPostgresHost := "" + currentPostgresPort := 0 + currentPostgresUser := "" + currentPostgresPassword := "" + currentPostgresDatabase := "" + currentPostgresTimeZone := "" + currentPostgresSSLMode := "" + + currentLocalImageRegistryPort := 0 + currentLocalImageRegistryUser := "" + currentLocalImageRegistryPassword := "" + + if config != nil && config.LocalConfig != nil { + currentPostgresHost = config.LocalConfig.PostgresqlConfig.Host + currentPostgresPort = config.LocalConfig.PostgresqlConfig.Port + currentPostgresUser = config.LocalConfig.PostgresqlConfig.User + currentPostgresPassword = config.LocalConfig.PostgresqlConfig.Password + currentPostgresDatabase = config.LocalConfig.PostgresqlConfig.Database + currentPostgresTimeZone = config.LocalConfig.PostgresqlConfig.TimeZone + currentPostgresSSLMode = config.LocalConfig.PostgresqlConfig.SSLMode + currentLocalImageRegistryPort = config.LocalConfig.LocalImageRegistryConfig.Port + currentLocalImageRegistryUser = config.LocalConfig.LocalImageRegistryConfig.Username + currentLocalImageRegistryPassword = config.LocalConfig.LocalImageRegistryConfig.Password + } + // Create config newConfig := &local_config.Config{ IsDevelopmentMode: false, @@ -64,17 +114,22 @@ var initCmd = &cobra.Command{ ManagementNodeAddress: domainName, }, PostgresqlConfig: local_config.PostgresqlConfig{ - Host: "127.0.0.1", - Port: 5432, - User: generateRandomString(8), - Password: generateRandomString(20), - Database: "db_" + generateRandomString(8), - TimeZone: "Asia/Kolkata", - SSLMode: "disable", + Host: defaultString(currentPostgresHost, "127.0.0.1"), + Port: defaultInt(currentPostgresPort, 5432), + User: defaultString(currentPostgresUser, "user_"+generateRandomString(8)), + Password: defaultString(currentPostgresPassword, generateRandomString(20)), + Database: defaultString(currentPostgresDatabase, "db_"+generateRandomString(8)), + TimeZone: defaultString(currentPostgresTimeZone, "Asia/Kolkata"), + SSLMode: defaultString(currentPostgresSSLMode, "disable"), RunLocalPostgres: isLocalPostgres, }, + LocalImageRegistryConfig: local_config.LocalImageRegistryConfig{ + Port: defaultInt(currentLocalImageRegistryPort, 3334), + Username: defaultString(currentLocalImageRegistryUser, "user_"+generateRandomString(8)), + Password: defaultString(currentLocalImageRegistryPassword, generateRandomString(20)), + }, } - err = local_config.FillDefaults(newConfig) + err := local_config.FillDefaults(newConfig) if err != nil { printError(err.Error()) os.Exit(1) @@ -88,6 +143,10 @@ var initCmd = &cobra.Command{ newConfig.ServiceConfig.PVBackupDirectoryPath, newConfig.ServiceConfig.PVRestoreDirectoryPath, newConfig.ServiceConfig.SSLCertDirectoryPath, + newConfig.ServiceConfig.LocalImageRegistryDirectoryPath, + newConfig.LocalImageRegistryConfig.CertPath, + newConfig.LocalImageRegistryConfig.AuthPath, + newConfig.LocalImageRegistryConfig.DataPath, newConfig.ServiceConfig.HAProxyDataDirectoryPath, newConfig.ServiceConfig.HAProxyUnixSocketDirectory, newConfig.ServiceConfig.UDPProxyDataDirectoryPath, @@ -96,7 +155,7 @@ var initCmd = &cobra.Command{ } // create folders for _, folder := range requiredFolders { - err = createFolder(folder) + err := createFolder(folder) if err != nil { printError("Failed to create folder " + folder) os.Exit(1) @@ -111,11 +170,20 @@ var initCmd = &cobra.Command{ os.Exit(1) } printSuccess("Config created at " + local_config.LocalConfigPath) + config = &config2.Config{ + LocalConfig: newConfig, + SystemConfig: nil, + } if !isLocalPostgres { printInfo("You have opted to use your own postgres server") printInfo("Configure postgresql credentials to connect to your own postgres server") printInfo("Run `swiftwave config` to edit the config file") } + if isOverWrite { + printInfo("Config has been overwritten") + printInfo("Try to restarting the service to apply changes [as overwriting config]") + restartServiceCmd.Run(serviceCmd, []string{}) + } }, } @@ -163,3 +231,17 @@ func generateRandomString(length int) string { } return string(result) } + +func defaultString(value, defaultValue string) string { + if strings.Compare(value, "") == 0 { + return defaultValue + } + return value +} + +func defaultInt(value, defaultValue int) int { + if value == 0 { + return defaultValue + } + return value +} diff --git a/swiftwave_service/cmd/localregistry.go b/swiftwave_service/cmd/localregistry.go new file mode 100644 index 0000000000..ca45f10380 --- /dev/null +++ b/swiftwave_service/cmd/localregistry.go @@ -0,0 +1,192 @@ +package cmd + +import ( + "context" + "errors" + "fmt" + "github.com/spf13/cobra" + containermanger "github.com/swiftwave-org/swiftwave/container_manager" + "os" + "os/exec" +) + +var localRegistryContainerName = "swiftwave-image-registry" + +func init() { + localRegistryCmd.AddCommand(startLocalRegistryCmd) + localRegistryCmd.AddCommand(stopLocalRegistryCmd) + localRegistryCmd.AddCommand(isLocalRegistryRunningCmd) + localRegistryCmd.AddCommand(restartLocalRegistryCmd) +} + +var localRegistryCmd = &cobra.Command{ + Use: "localregistry", + Short: "Manage local image registry for swiftwave service", + Long: `Manage local image registry for swiftwave service`, + Run: func(cmd *cobra.Command, args []string) { + // print help + err := cmd.Help() + if err != nil { + return + } + }, +} + +var startLocalRegistryCmd = &cobra.Command{ + Use: "start", + Short: "Start local image registry", + Long: `Start local image registry`, + Run: func(cmd *cobra.Command, args []string) { + if err := startLocalRegistry(context.Background()); err != nil { + printError(err.Error()) + } else { + printSuccess("Local image registry started") + } + }, +} + +var stopLocalRegistryCmd = &cobra.Command{ + Use: "stop", + Short: "Stop local image registry", + Long: `Stop local image registry`, + Run: func(cmd *cobra.Command, args []string) { + if err := stopLocalRegistry(context.Background()); err != nil { + printError(err.Error()) + } else { + printSuccess("Local image registry stopped") + } + }, +} + +var isLocalRegistryRunningCmd = &cobra.Command{ + Use: "status", + Short: "Check if local image registry is running", + Long: `Check if local image registry is running`, + Run: func(cmd *cobra.Command, args []string) { + if isRunning, err := isLocalRegistryRunning(context.Background()); err != nil { + printError(err.Error()) + } else { + if isRunning { + printSuccess("Local image registry is running") + } else { + printError("Local image registry is not running") + } + } + }, +} + +var restartLocalRegistryCmd = &cobra.Command{ + Use: "restart", + Short: "Restart local image registry", + Long: `Restart local image registry`, + Run: func(cmd *cobra.Command, args []string) { + if err := restartLocalRegistry(context.Background()); err != nil { + printError(err.Error()) + } else { + printSuccess("Local image registry restarted") + } + }, +} + +// private functions +func isLocalRegistryRequired() (bool, error) { + if config.SystemConfig == nil { + return false, errors.New("system config is not loaded") + } + return !config.SystemConfig.ImageRegistryConfig.IsConfigured(), nil +} + +func isLocalRegistryRunning(ctx context.Context) (bool, error) { + if _, err := isLocalRegistryRequired(); err != nil { + return false, err + } + dockerManager, err := containermanger.NewLocalClient(ctx) + if err != nil { + return false, err + } + return dockerManager.IsContainerRunning(localRegistryContainerName) + +} + +func startLocalRegistry(ctx context.Context) error { + isRunning, err := isLocalRegistryRunning(ctx) + if err != nil { + return err + } + if !isRunning { + // generate htpasswd file + htpasswdString, err := config.LocalConfig.LocalImageRegistryConfig.Htpasswd() + if err != nil { + return err + } + htpasswdString = htpasswdString + "\n" + // write htpasswd file + err = os.WriteFile(config.LocalConfig.LocalImageRegistryConfig.AuthPath+"/htpasswd", []byte(htpasswdString), 0611) + if err != nil { + return err + } + + var dockerCmd *exec.Cmd + if config.LocalConfig.ServiceConfig.UseTLS { + printInfo("Using TLS for local image registry") + dockerCmd = exec.Command("docker", "run", "-d", + "-p", fmt.Sprintf("%d:5000", config.LocalConfig.LocalImageRegistryConfig.Port), + "--restart", "always", + "-e", "REGISTRY_AUTH=htpasswd", + "-e", "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd", + "-e", "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm", + "-e", "REGISTRY_HTTP_TLS_CERTIFICATE=/cert/certificate.crt", + "-e", "REGISTRY_HTTP_TLS_KEY=/cert/private.key", + "-v", fmt.Sprintf("%s:/cert", config.LocalConfig.LocalImageRegistryConfig.CertPath), + "-v", fmt.Sprintf("%s:/auth", config.LocalConfig.LocalImageRegistryConfig.AuthPath), + "-v", fmt.Sprintf("%s:/var/lib/registry", config.LocalConfig.LocalImageRegistryConfig.DataPath), + "--name", localRegistryContainerName, "registry:2.8") + } else { + printInfo("Using Non-TLS for local image registry") + dockerCmd = exec.Command("docker", "run", "-d", + "-p", fmt.Sprintf("%d:5000", config.LocalConfig.LocalImageRegistryConfig.Port), + "--restart", "always", + "-e", "REGISTRY_AUTH=htpasswd", + "-e", "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd", + "-e", "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm", + "-v", fmt.Sprintf("%s:/auth", config.LocalConfig.LocalImageRegistryConfig.AuthPath), + "-v", fmt.Sprintf("%s:/var/lib/registry", config.LocalConfig.LocalImageRegistryConfig.DataPath), + "--name", localRegistryContainerName, "registry:2.8") + } + dockerCmd.Stdout = os.Stdout + dockerCmd.Stderr = os.Stderr + err = dockerCmd.Run() + if err != nil { + return err + } + } else { + printSuccess("Local image registry is already running") + } + return nil +} + +func stopLocalRegistry(ctx context.Context) error { + isRunning, err := isLocalRegistryRunning(ctx) + if err != nil { + return err + } + if isRunning { + dockerCmd := exec.Command("docker", "rm", localRegistryContainerName, "--force") + dockerCmd.Stderr = os.Stderr + err := dockerCmd.Run() + if err != nil { + return err + } + } else { + printSuccess("Local image registry is not running") + } + return nil +} + +func restartLocalRegistry(ctx context.Context) error { + err := stopLocalRegistry(ctx) + if err != nil { + return err + } + return startLocalRegistry(context.Background()) +} diff --git a/swiftwave_service/cmd/root.go b/swiftwave_service/cmd/root.go index d5bb97843c..d41fe1f4ec 100644 --- a/swiftwave_service/cmd/root.go +++ b/swiftwave_service/cmd/root.go @@ -24,6 +24,7 @@ func init() { rootCmd.AddCommand(startCmd) rootCmd.AddCommand(serviceCmd) rootCmd.AddCommand(postgresCmd) + rootCmd.AddCommand(localRegistryCmd) rootCmd.AddCommand(updateCmd) rootCmd.AddCommand(autoUpdaterCmd) } @@ -90,7 +91,7 @@ func Execute() { loadSystemConfig := false // if it's start command, and system setup is required, don't load complete config - if len(os.Args) > 1 && os.Args[1] == "start" { + if len(os.Args) > 1 && (os.Args[1] == "start" || os.Args[1] == "localregistry") { setupRequired, err := bootstrap.IsSystemSetupRequired() if err != nil { printError("Failed to check if system setup is required: " + err.Error()) diff --git a/swiftwave_service/cmd/start.go b/swiftwave_service/cmd/start.go index f1117d4084..d123b1cced 100644 --- a/swiftwave_service/cmd/start.go +++ b/swiftwave_service/cmd/start.go @@ -6,6 +6,7 @@ import ( "github.com/spf13/cobra" swiftwave "github.com/swiftwave-org/swiftwave/swiftwave_service" "github.com/swiftwave-org/swiftwave/swiftwave_service/config/system_config/bootstrap" + "os" ) var startCmd = &cobra.Command{ @@ -25,6 +26,7 @@ var startCmd = &cobra.Command{ if err != nil { printError("Failed to check if system setup is required") printError(err.Error()) + os.Exit(1) return } if setupRequired { @@ -35,6 +37,33 @@ var startCmd = &cobra.Command{ printError(err.Error()) } } else { + isRequired, err := isLocalRegistryRequired() + if err != nil { + printError("Failed to check if local registry is required") + printError(err.Error()) + os.Exit(1) + return + } + if isRequired { + color.Yellow("Local registry will be used for image storage") + isRunning, err := isLocalRegistryRunning(cmd.Context()) + if err != nil { + printError("Failed to check if local registry is running") + printError(err.Error()) + os.Exit(1) + return + } + if !isRunning { + color.Yellow("Starting local registry") + err := startLocalRegistry(cmd.Context()) + if err != nil { + printError("Failed to start local registry") + printError(err.Error()) + os.Exit(1) + return + } + } + } swiftwave.StartSwiftwave(config) } }, diff --git a/swiftwave_service/cmd/tls.go b/swiftwave_service/cmd/tls.go index 01bbe544ed..0bf55ca18a 100644 --- a/swiftwave_service/cmd/tls.go +++ b/swiftwave_service/cmd/tls.go @@ -67,6 +67,8 @@ var tlsEnableCmd = &cobra.Command{ } printSuccess("TLS has been enabled") restartSysctlService("swiftwave") + // Restart local registry if required + restartLocalRegistryIfRequired(cmd.Context()) }, } @@ -89,6 +91,8 @@ var tlsDisableCmd = &cobra.Command{ } printSuccess("TLS has been disabled") restartSysctlService("swiftwave") + // Restart local registry if required + restartLocalRegistryIfRequired(cmd.Context()) }, } @@ -170,25 +174,17 @@ var generateCertificateCommand = &cobra.Command{ return } } - // Store private key and certificate in the service.ssl_certificate_dir/ folder - dir := filepath.Join(config.LocalConfig.ServiceConfig.SSLCertDirectoryPath, domain) - if !checkIfFolderExists(dir) { - err = createFolder(dir) - if err != nil { - printError("Failed to create folder " + dir) - os.Exit(1) - return - } - } + // Store private key and certificate in the service.ssl_certificate_dir folder + certDir := config.LocalConfig.ServiceConfig.SSLCertDirectoryPath // Store private key - err = os.WriteFile(filepath.Join(dir, "private.key"), []byte(privateKey), 0644) + err = os.WriteFile(filepath.Join(certDir, "private.key"), []byte(privateKey), 0600) if err != nil { printError("Failed to store private key") os.Exit(1) return } // Store certificate - err = os.WriteFile(filepath.Join(dir, "certificate.crt"), []byte(certificate), 0644) + err = os.WriteFile(filepath.Join(certDir, "certificate.crt"), []byte(certificate), 0600) if err != nil { printError("Failed to store certificate") os.Exit(1) @@ -209,6 +205,8 @@ var generateCertificateCommand = &cobra.Command{ } // Restart swiftwave service restartSysctlService("swiftwave") + // Restart local registry if required + restartLocalRegistryIfRequired(cmd.Context()) }, } @@ -218,7 +216,7 @@ var renewCertificateCommand = &cobra.Command{ Long: `This command renews TLS certificates for swiftwave endpoints. It's not for renewing certificates for domain of hosted applications`, Run: func(cmd *cobra.Command, args []string) { - sslCertificatePath := filepath.Join(config.LocalConfig.ServiceConfig.SSLCertDirectoryPath, config.LocalConfig.ServiceConfig.ManagementNodeAddress, "certificate.crt") + sslCertificatePath := filepath.Join(config.LocalConfig.ServiceConfig.SSLCertDirectoryPath, "certificate.crt") if _, err := os.Stat(sslCertificatePath); os.IsNotExist(err) { printError("No TLS certificate found") printInfo("Use `swiftwave tls generate` to generate a new certificate") @@ -307,3 +305,31 @@ func isRenewalImminent(certPath string) (bool, error) { return daysRemaining <= 30, nil } + +func restartLocalRegistryIfRequired(ctx context.Context) { + if config == nil || config.LocalConfig == nil || config.SystemConfig == nil { + return + } + isRequired, err := isLocalRegistryRequired() + if err != nil { + printError("Failed to check if local registry is required") + printError(err.Error()) + return + } + if isRequired { + isRunning, err := isLocalRegistryRunning(ctx) + if err != nil { + printError("Failed to check if local registry is running") + printError(err.Error()) + return + } + if !isRunning { + err := restartLocalRegistry(ctx) + if err != nil { + printError("Failed to restart local registry") + printError(err.Error()) + return + } + } + } +} diff --git a/swiftwave_service/cmd/types.go b/swiftwave_service/cmd/types.go index 3a00effb85..554e8c0958 100644 --- a/swiftwave_service/cmd/types.go +++ b/swiftwave_service/cmd/types.go @@ -3,3 +3,4 @@ package cmd var CrossSymbol = "\u2717" var TickSymbol = "\u2713" var InfoSymbol = "\u21D2" +var WarningSymbol = "\u26A0" diff --git a/swiftwave_service/cmd/utils.go b/swiftwave_service/cmd/utils.go index be912282bf..615ef757ff 100644 --- a/swiftwave_service/cmd/utils.go +++ b/swiftwave_service/cmd/utils.go @@ -9,12 +9,6 @@ import ( "time" ) -func checkIfFolderExists(folder string) bool { - cmd := exec.Command("ls", folder) - err := cmd.Run() - return err == nil -} - func createFolder(folder string) error { return os.MkdirAll(folder, 0711) } @@ -107,3 +101,7 @@ func printError(message string) { func printInfo(message string) { color.Blue(InfoSymbol + " " + message) } + +func printWarning(message string) { + color.Yellow(WarningSymbol + " " + message) +} diff --git a/swiftwave_service/config/local_config/default.go b/swiftwave_service/config/local_config/default.go index 2c50273cc7..fc36484df9 100644 --- a/swiftwave_service/config/local_config/default.go +++ b/swiftwave_service/config/local_config/default.go @@ -4,6 +4,7 @@ import "path/filepath" var defaultBindAddress = "0.0.0.0" var defaultBindPort = 3333 +var defaultImageRegistryPort = 3334 var defaultSocketPathDirectory = "/var/run/swiftwave" var defaultDataDirectory = "/var/lib/swiftwave" var defaultNetworkName = "swiftwave_network" @@ -15,7 +16,11 @@ var defaultUDPProxyServiceName = "swiftwave_udpproxy" var defaultUDPProxyDataDirectoryPath = filepath.Join(defaultDataDirectory, "udpproxy") var defaultUDPProxyUnixSocketDirectory = filepath.Join(defaultSocketPathDirectory, "udpproxy") var defaultUDPProxyUnixSocketPath = filepath.Join(defaultUDPProxyUnixSocketDirectory, "api.sock") -var defaultSSLCertDirectoryPath = filepath.Join(defaultDataDirectory, "certs") +var defaultSSLCertDirectoryPath = filepath.Join(defaultDataDirectory, "cert") +var defaultLocalImageRegistryDirectoryPath = filepath.Join(defaultDataDirectory, "registry") +var defaultLocalImageRegistryDataDirectoryPath = filepath.Join(defaultLocalImageRegistryDirectoryPath, "data") +var defaultLocalImageRegistryAuthDirectoryPath = filepath.Join(defaultLocalImageRegistryDirectoryPath, "auth") +var defaultLocalImageRegistryCertDirectoryPath = defaultSSLCertDirectoryPath var defaultPVBackupDirectoryPath = filepath.Join(defaultDataDirectory, "pvbackup") var defaultPVRestoreDirectoryPath = filepath.Join(defaultDataDirectory, "pvrestore") var defaultTarballDirectoryPath = filepath.Join(defaultDataDirectory, "tarball") diff --git a/swiftwave_service/config/local_config/types.go b/swiftwave_service/config/local_config/types.go index 592026fd65..36717ec69a 100644 --- a/swiftwave_service/config/local_config/types.go +++ b/swiftwave_service/config/local_config/types.go @@ -6,36 +6,38 @@ import _ "embed" var softwareVersion string type Config struct { - IsDevelopmentMode bool `yaml:"dev_mode"` - Version string `yaml:"-"` - ServiceConfig ServiceConfig `yaml:"service"` - PostgresqlConfig PostgresqlConfig `yaml:"postgresql"` + IsDevelopmentMode bool `yaml:"dev_mode"` + Version string `yaml:"-"` + ServiceConfig ServiceConfig `yaml:"service"` + PostgresqlConfig PostgresqlConfig `yaml:"postgresql"` + LocalImageRegistryConfig LocalImageRegistryConfig `yaml:"local_image_registry"` } type ServiceConfig struct { - UseTLS bool `yaml:"use_tls"` - ManagementNodeAddress string `yaml:"management_node_address"` - BindAddress string `yaml:"bind_address"` - BindPort int `yaml:"bind_port"` - SocketPathDirectory string `yaml:"-"` - DataDirectory string `yaml:"-"` - LocalPostgresDataDirectory string `yaml:"-"` - TarballDirectoryPath string `yaml:"-"` - PVBackupDirectoryPath string `yaml:"-"` - PVRestoreDirectoryPath string `yaml:"-"` - NetworkName string `yaml:"-"` - HAProxyServiceName string `yaml:"-"` - HAProxyUnixSocketDirectory string `yaml:"-"` - HAProxyUnixSocketPath string `yaml:"-"` - HAProxyDataDirectoryPath string `yaml:"-"` - UDPProxyServiceName string `yaml:"-"` - UDPProxyUnixSocketDirectory string `yaml:"-"` - UDPProxyUnixSocketPath string `yaml:"-"` - UDPProxyDataDirectoryPath string `yaml:"-"` - SSLCertDirectoryPath string `yaml:"-"` - LogDirectoryPath string `yaml:"-"` - InfoLogFilePath string `yaml:"-"` - ErrorLogFilePath string `yaml:"-"` + UseTLS bool `yaml:"use_tls"` + ManagementNodeAddress string `yaml:"management_node_address"` + BindAddress string `yaml:"bind_address"` + BindPort int `yaml:"bind_port"` + SocketPathDirectory string `yaml:"-"` + DataDirectory string `yaml:"-"` + LocalPostgresDataDirectory string `yaml:"-"` + TarballDirectoryPath string `yaml:"-"` + PVBackupDirectoryPath string `yaml:"-"` + PVRestoreDirectoryPath string `yaml:"-"` + NetworkName string `yaml:"-"` + HAProxyServiceName string `yaml:"-"` + HAProxyUnixSocketDirectory string `yaml:"-"` + HAProxyUnixSocketPath string `yaml:"-"` + HAProxyDataDirectoryPath string `yaml:"-"` + UDPProxyServiceName string `yaml:"-"` + UDPProxyUnixSocketDirectory string `yaml:"-"` + UDPProxyUnixSocketPath string `yaml:"-"` + UDPProxyDataDirectoryPath string `yaml:"-"` + SSLCertDirectoryPath string `yaml:"-"` + LocalImageRegistryDirectoryPath string `yaml:"-"` + LogDirectoryPath string `yaml:"-"` + InfoLogFilePath string `yaml:"-"` + ErrorLogFilePath string `yaml:"-"` } type PostgresqlConfig struct { @@ -48,3 +50,14 @@ type PostgresqlConfig struct { SSLMode string `yaml:"ssl_mode"` RunLocalPostgres bool `yaml:"run_local_postgres"` } + +type LocalImageRegistryConfig struct { + // TLS of this depends on the TLS status of the service + // both will use same certificate + Port int `yaml:"port"` + Username string `yaml:"username"` + Password string `yaml:"password"` + DataPath string `yaml:"-"` + CertPath string `yaml:"-"` + AuthPath string `yaml:"-"` +} diff --git a/swiftwave_service/config/local_config/utils.go b/swiftwave_service/config/local_config/utils.go index d85d9e2fb5..269c3bb00d 100644 --- a/swiftwave_service/config/local_config/utils.go +++ b/swiftwave_service/config/local_config/utils.go @@ -3,6 +3,7 @@ package local_config import ( "errors" "fmt" + "golang.org/x/crypto/bcrypt" "gopkg.in/yaml.v3" "io" "os" @@ -93,6 +94,9 @@ func FillDefaults(config *Config) error { if config.ServiceConfig.ManagementNodeAddress == "" { return errors.New("management_node_address is required in config") } + if config.LocalImageRegistryConfig.Port == 0 { + config.LocalImageRegistryConfig.Port = defaultImageRegistryPort + } config.ServiceConfig.SocketPathDirectory = defaultSocketPathDirectory config.ServiceConfig.DataDirectory = defaultDataDirectory config.ServiceConfig.LocalPostgresDataDirectory = defaultLocalPostgresDataDirectory @@ -107,11 +111,15 @@ func FillDefaults(config *Config) error { config.ServiceConfig.UDPProxyUnixSocketPath = defaultUDPProxyUnixSocketPath config.ServiceConfig.UDPProxyDataDirectoryPath = defaultUDPProxyDataDirectoryPath config.ServiceConfig.SSLCertDirectoryPath = defaultSSLCertDirectoryPath + config.ServiceConfig.LocalImageRegistryDirectoryPath = defaultLocalImageRegistryDirectoryPath config.ServiceConfig.LogDirectoryPath = LogDirectoryPath config.ServiceConfig.InfoLogFilePath = InfoLogFilePath config.ServiceConfig.ErrorLogFilePath = ErrorLogFilePath config.ServiceConfig.PVBackupDirectoryPath = defaultPVBackupDirectoryPath config.ServiceConfig.PVRestoreDirectoryPath = defaultPVRestoreDirectoryPath + config.LocalImageRegistryConfig.CertPath = defaultLocalImageRegistryCertDirectoryPath + config.LocalImageRegistryConfig.AuthPath = defaultLocalImageRegistryAuthDirectoryPath + config.LocalImageRegistryConfig.DataPath = defaultLocalImageRegistryDataDirectoryPath return nil } @@ -127,3 +135,15 @@ func (config *Config) String() (string, error) { } return string(out), nil } + +func (config *Config) GetRegistryURL() string { + return fmt.Sprintf("%s:%d", config.ServiceConfig.ManagementNodeAddress, config.LocalImageRegistryConfig.Port) +} + +func (l *LocalImageRegistryConfig) Htpasswd() (string, error) { + hashedPassword, err := bcrypt.GenerateFromPassword([]byte(l.Password), bcrypt.DefaultCost) + if err != nil { + return "", err + } + return fmt.Sprintf("%s:%s", l.Username, string(hashedPassword)), nil +} diff --git a/swiftwave_service/config/system_config/bootstrap/utils.go b/swiftwave_service/config/system_config/bootstrap/utils.go index 588a12af64..11db2c6fa7 100644 --- a/swiftwave_service/config/system_config/bootstrap/utils.go +++ b/swiftwave_service/config/system_config/bootstrap/utils.go @@ -53,8 +53,6 @@ func portsStringToArray(ports string) []int64 { } // add default ports portsMap[22] = true - portsMap[80] = true - portsMap[443] = true portsMap[2376] = true portsMap[2377] = true portsMap[4789] = true @@ -65,6 +63,7 @@ func portsStringToArray(ports string) []int64 { } bindPort := localConfig.ServiceConfig.BindPort portsMap[int64(bindPort)] = true + portsMap[int64(localConfig.LocalImageRegistryConfig.Port)] = true // convert map to array portsArr := pq.Int64Array{} for k := range portsMap { diff --git a/swiftwave_service/config/utils.go b/swiftwave_service/config/utils.go index 532093645f..2cc76f4da4 100644 --- a/swiftwave_service/config/utils.go +++ b/swiftwave_service/config/utils.go @@ -34,3 +34,24 @@ func Fetch() (*Config, error) { SystemConfig: systemConfig, }, nil } + +func (config *Config) ImageRegistryURI() string { + if config.SystemConfig.ImageRegistryConfig.IsConfigured() { + return config.SystemConfig.ImageRegistryConfig.URI() + } + return config.LocalConfig.GetRegistryURL() +} + +func (config *Config) ImageRegistryUsername() string { + if config.SystemConfig.ImageRegistryConfig.IsConfigured() { + return config.SystemConfig.ImageRegistryConfig.Username + } + return config.LocalConfig.LocalImageRegistryConfig.Username +} + +func (config *Config) ImageRegistryPassword() string { + if config.SystemConfig.ImageRegistryConfig.IsConfigured() { + return config.SystemConfig.ImageRegistryConfig.Password + } + return config.LocalConfig.LocalImageRegistryConfig.Password +} diff --git a/swiftwave_service/graphql/server.resolvers.go b/swiftwave_service/graphql/server.resolvers.go index a49a6be954..ec1be341b0 100644 --- a/swiftwave_service/graphql/server.resolvers.go +++ b/swiftwave_service/graphql/server.resolvers.go @@ -421,6 +421,16 @@ func (r *mutationResolver) EnableProxyOnServer(_ context.Context, id uint) (bool } // Enable the proxy server.ProxyConfig.SetupRunning = true + // For backup proxy, atleast 1 active proxy is required + if server.ProxyConfig.Type == core.BackupProxy { + activeProxies, err := core.FetchProxyActiveServers(&r.ServiceManager.DbClient) + if err != nil { + return false, err + } + if len(activeProxies) == 0 { + return false, errors.New("for adding backup proxy, atleast 1 active proxy is required") + } + } err = core.UpdateServer(&r.ServiceManager.DbClient, server) if err != nil { return false, err diff --git a/swiftwave_service/main.go b/swiftwave_service/main.go index d533ce880d..a7a1c0e09e 100644 --- a/swiftwave_service/main.go +++ b/swiftwave_service/main.go @@ -2,7 +2,6 @@ package swiftwave import ( "context" - "crypto/tls" "fmt" "github.com/fatih/color" "github.com/golang-jwt/jwt/v5" @@ -11,10 +10,10 @@ import ( "github.com/swiftwave-org/swiftwave/swiftwave_service/console" "github.com/swiftwave-org/swiftwave/swiftwave_service/core" "github.com/swiftwave-org/swiftwave/swiftwave_service/dashboard" + "github.com/swiftwave-org/swiftwave/swiftwave_service/logger" "github.com/swiftwave-org/swiftwave/swiftwave_service/service_manager" "log" "net/http" - "os" "strings" "github.com/labstack/echo/v4" @@ -73,7 +72,7 @@ func StartSwiftwave(config *config.Config) { func echoLogger(_ echo.Context, err error, stack []byte) error { color.Red("Recovered from panic: %s\n", err) - fmt.Println(string(stack)) + logger.HTTPLoggerError.Println("Swiftwave server is facing error : ", err.Error(), "\n", string(stack)) return nil } @@ -249,44 +248,19 @@ func StartServer(config *config.Config, manager *service_manager.ServiceManager, // Initialize GraphQL Server graphqlServer.Initialize() - // StartSwiftwave the server + // Start the server address := fmt.Sprintf("%s:%d", config.LocalConfig.ServiceConfig.BindAddress, config.LocalConfig.ServiceConfig.BindPort) if config.LocalConfig.ServiceConfig.UseTLS { println("TLS Server Started on " + address) - - tlsCfg := &tls.Config{ - Certificates: fetchCertificates(config.LocalConfig.ServiceConfig.SSLCertDirectoryPath), - } - s := http.Server{ - Addr: address, - Handler: echoServer, - TLSConfig: tlsCfg, + Addr: address, + Handler: echoServer, } - echoServer.Logger.Fatal(s.ListenAndServeTLS("", "")) + certFilePath := fmt.Sprintf("%s/certificate.crt", config.LocalConfig.ServiceConfig.SSLCertDirectoryPath) + keyFilePath := fmt.Sprintf("%s/private.key", config.LocalConfig.ServiceConfig.SSLCertDirectoryPath) + echoServer.Logger.Fatal(s.ListenAndServeTLS(certFilePath, keyFilePath)) } else { + println("Server Started on " + address) echoServer.Logger.Fatal(echoServer.Start(address)) } } - -// private functions -func fetchCertificates(certFolderPath string) []tls.Certificate { - var certificates []tls.Certificate - // fetch all folders in the cert folder - files, err := os.ReadDir(certFolderPath) - if err != nil { - return certificates - } - for _, file := range files { - if file.IsDir() { - // fetch the certificate - cert, err := tls.LoadX509KeyPair(fmt.Sprintf("%s/%s/certificate.crt", certFolderPath, file.Name()), fmt.Sprintf("%s/%s/private.key", certFolderPath, file.Name())) - if err != nil { - log.Println("Error loading certificate: ", err) - continue - } - certificates = append(certificates, cert) - } - } - return certificates -} diff --git a/swiftwave_service/worker/process_application_build_request.go b/swiftwave_service/worker/process_application_build_request.go index fe4d29c250..75acb37cb3 100644 --- a/swiftwave_service/worker/process_application_build_request.go +++ b/swiftwave_service/worker/process_application_build_request.go @@ -108,7 +108,7 @@ func (m Manager) buildApplicationHelper(request BuildApplicationRequest, ctx con } } // Push image to registry - if m.Config.SystemConfig.ImageRegistryConfig.IsConfigured() && (deployment.UpstreamType == core.UpstreamTypeGit || deployment.UpstreamType == core.UpstreamTypeSourceCode) { + if deployment.UpstreamType == core.UpstreamTypeGit || deployment.UpstreamType == core.UpstreamTypeSourceCode { err = m.pushImageToRegistry(deployment, *db, dbWithoutTx, pubSubClient, ctx, cancelContext, dockerManager) if err != nil { return err @@ -134,7 +134,7 @@ func (m Manager) buildApplicationHelper(request BuildApplicationRequest, ctx con return err } -func (m Manager) buildApplicationForDockerImage(deployment *core.Deployment, db gorm.DB, dbWithoutTx gorm.DB, pubSubClient pubsub.Client, ctx context.Context, _ context.CancelFunc, _ *containermanger.Manager) error { +func (m Manager) buildApplicationForDockerImage(deployment *core.Deployment, _ gorm.DB, dbWithoutTx gorm.DB, pubSubClient pubsub.Client, _ context.Context, _ context.CancelFunc, _ *containermanger.Manager) error { addDeploymentLog(dbWithoutTx, pubSubClient, deployment.ID, "As the upstream type is image, no build is required\n", false) return nil } @@ -203,7 +203,7 @@ func (m Manager) buildApplicationForGit(deployment *core.Deployment, db gorm.DB, } // start building docker image - scanner, err := dockerManager.CreateImageWithContext(ctx, deployment.Dockerfile, buildArgsMap, tempDirectory, deployment.CodePath, deployment.DeployableDockerImageURI(m.Config.SystemConfig.ImageRegistryConfig.URI())) + scanner, err := dockerManager.CreateImageWithContext(ctx, deployment.Dockerfile, buildArgsMap, tempDirectory, deployment.CodePath, deployment.DeployableDockerImageURI(m.Config.ImageRegistryURI())) if err != nil { addDeploymentLog(dbWithoutTx, pubSubClient, deployment.ID, "Failed to build docker image\n", true) return err @@ -278,7 +278,7 @@ func (m Manager) buildApplicationForTarball(deployment *core.Deployment, db gorm } // start building docker image - scanner, err := dockerManager.CreateImageWithContext(ctx, deployment.Dockerfile, buildArgsMap, tempDirectory, deployment.CodePath, deployment.DeployableDockerImageURI(m.Config.SystemConfig.ImageRegistryConfig.URI())) + scanner, err := dockerManager.CreateImageWithContext(ctx, deployment.Dockerfile, buildArgsMap, tempDirectory, deployment.CodePath, deployment.DeployableDockerImageURI(m.Config.ImageRegistryURI())) if err != nil { addDeploymentLog(dbWithoutTx, pubSubClient, deployment.ID, "Failed to build docker image\n", true) return err @@ -318,8 +318,8 @@ func (m Manager) buildApplicationForTarball(deployment *core.Deployment, db gorm } func (m Manager) pushImageToRegistry(deployment *core.Deployment, _ gorm.DB, dbWithoutTx gorm.DB, pubSubClient pubsub.Client, ctx context.Context, _ context.CancelFunc, dockerManager *containermanger.Manager) error { - addDeploymentLog(dbWithoutTx, pubSubClient, deployment.ID, "Image : "+deployment.DeployableDockerImageURI(m.Config.SystemConfig.ImageRegistryConfig.URI())+"\n", false) - scanner, err := dockerManager.PushImage(ctx, deployment.DeployableDockerImageURI(m.Config.SystemConfig.ImageRegistryConfig.URI()), m.Config.SystemConfig.ImageRegistryConfig.Username, m.Config.SystemConfig.ImageRegistryConfig.Password) + addDeploymentLog(dbWithoutTx, pubSubClient, deployment.ID, "Image : "+deployment.DeployableDockerImageURI(m.Config.ImageRegistryURI())+"\n", false) + scanner, err := dockerManager.PushImage(ctx, deployment.DeployableDockerImageURI(m.Config.ImageRegistryURI()), m.Config.ImageRegistryUsername(), m.Config.ImageRegistryPassword()) if err != nil { addDeploymentLog(dbWithoutTx, pubSubClient, deployment.ID, "Failed to push image to registry\n", true) return err diff --git a/swiftwave_service/worker/process_application_deploy_request.go b/swiftwave_service/worker/process_application_deploy_request.go index 53192f53c5..9578ade22c 100644 --- a/swiftwave_service/worker/process_application_deploy_request.go +++ b/swiftwave_service/worker/process_application_deploy_request.go @@ -127,10 +127,10 @@ func (m Manager) deployApplicationHelper(request DeployApplicationRequest, docke command = strings.Split(application.Command, " ") } // docker image info - dockerImageUri := deployment.DeployableDockerImageURI(m.Config.SystemConfig.ImageRegistryConfig.URI()) + dockerImageUri := deployment.DeployableDockerImageURI(m.Config.ImageRegistryURI()) refetchImage := false - imageRegistryUsername := "" - imageRegistryPassword := "" + imageRegistryUsername := m.Config.ImageRegistryUsername() + imageRegistryPassword := m.Config.ImageRegistryPassword() if deployment.UpstreamType == core.UpstreamTypeImage { // fetch image registry credential @@ -146,26 +146,11 @@ func (m Manager) deployApplicationHelper(request DeployApplicationRequest, docke } addDeploymentLog(dbWithoutTx, pubSubClient, deployment.ID, "Image will be fetched from upstream at the time of deployment\n", false) refetchImage = true - } else { - // set the image uri only if remote private registry is used - if m.Config.SystemConfig.ImageRegistryConfig.IsConfigured() { - refetchImage = true - imageRegistryUsername = m.Config.SystemConfig.ImageRegistryConfig.Username - imageRegistryPassword = m.Config.SystemConfig.ImageRegistryConfig.Password - addDeploymentLog(dbWithoutTx, pubSubClient, deployment.ID, "Image will be fetched from the configured private registry\n", false) - } else { - refetchImage = false - addDeploymentLog(dbWithoutTx, pubSubClient, deployment.ID, "Image will be fetched from the local (If exists)\n", false) - addDeploymentLog(dbWithoutTx, pubSubClient, deployment.ID, "[Notice] If you have connected remote server and not using any image registry, then the deployment can failed due to accessible image\n", false) - } } if refetchImage { - addDeploymentLog(dbWithoutTx, pubSubClient, deployment.ID, "[Notice] Image will be fetched during deployment\n", false) - } else { - addDeploymentLog(dbWithoutTx, pubSubClient, deployment.ID, "[Notice] Image will not be fetched from the upstream\n", false) + addDeploymentLog(dbWithoutTx, pubSubClient, deployment.ID, "[Notice] Image will be fetched from remote during deployment\n", false) } - // create service service := containermanger.Service{ Name: application.Name, diff --git a/swiftwave_service/worker/process_ingress_rule_delete_request.go b/swiftwave_service/worker/process_ingress_rule_delete_request.go index 9b8f117bbb..918ec08749 100644 --- a/swiftwave_service/worker/process_ingress_rule_delete_request.go +++ b/swiftwave_service/worker/process_ingress_rule_delete_request.go @@ -13,11 +13,6 @@ import ( func (m Manager) IngressRuleDelete(request IngressRuleDeleteRequest, ctx context.Context, _ context.CancelFunc) error { dbWithoutTx := m.ServiceManager.DbClient - // restricted ports - restrictedPorts := make([]int, 0) - for _, port := range m.Config.SystemConfig.RestrictedPorts { - restrictedPorts = append(restrictedPorts, int(port)) - } // fetch ingress rule var ingressRule core.IngressRule err := ingressRule.FindById(ctx, dbWithoutTx, request.Id) diff --git a/swiftwave_service/worker/process_setup_proxy_request.go b/swiftwave_service/worker/process_setup_proxy_request.go index ac88c83e10..ad9b481570 100644 --- a/swiftwave_service/worker/process_setup_proxy_request.go +++ b/swiftwave_service/worker/process_setup_proxy_request.go @@ -93,11 +93,6 @@ func (m Manager) setupAndEnableProxy(request SetupAndEnableProxyRequest, ctx con logText += "Failed to fetch all proxy servers: " + err.Error() + "\n" return err } - // don't attempt if no proxy servers are active - if len(servers) == 0 { - logText += "No proxy servers are active\n" - return errors.New("no proxy servers are active") - } if len(servers) > 0 { var chosenServer core.Server diff --git a/swiftwave_service/worker/process_setup_server_request.go b/swiftwave_service/worker/process_setup_server_request.go index 0dce904e69..6cf52c3272 100644 --- a/swiftwave_service/worker/process_setup_server_request.go +++ b/swiftwave_service/worker/process_setup_server_request.go @@ -89,6 +89,7 @@ func (m Manager) setupServerHelper(request SetupServerRequest, ctx context.Conte m.Config.LocalConfig.ServiceConfig.UDPProxyDataDirectoryPath, m.Config.LocalConfig.ServiceConfig.UDPProxyUnixSocketDirectory, m.Config.LocalConfig.ServiceConfig.SSLCertDirectoryPath, + m.Config.LocalConfig.ServiceConfig.LocalImageRegistryDirectoryPath, } for _, dir := range directories {