diff --git a/internal/service/instance/nginx_instance_service.go b/internal/service/instance/nginx_instance_service.go index df6c804003..77a3e0c607 100644 --- a/internal/service/instance/nginx_instance_service.go +++ b/internal/service/instance/nginx_instance_service.go @@ -23,15 +23,11 @@ import ( "github.com/nginx/agent/v3/internal/uuid" ) -var ( - ossVersionRegex = regexp.MustCompile(`(?P\S+)/(?P\S+)`) - plusVersionRegex = regexp.MustCompile(`(?P\S+)/(?P\S+).\((?P\S+plus\S+)\)`) -) +var versionRegex = regexp.MustCompile(`(?P\S+)\/(?P.*)`) type Info struct { ProcessID int32 Version string - PlusVersion string Prefix string ConfPath string ConfigureArgs map[string]interface{} @@ -132,7 +128,7 @@ func convertInfoToProcess(nginxInfo Info) *v1.Instance { nginxType := v1.InstanceMeta_INSTANCE_TYPE_NGINX version := nginxInfo.Version - if nginxInfo.PlusVersion == "" { + if !strings.Contains(nginxInfo.Version, "plus") { instanceRuntime = &v1.InstanceRuntime{ ProcessId: nginxInfo.ProcessID, BinaryPath: nginxInfo.ExePath, @@ -165,7 +161,7 @@ func convertInfoToProcess(nginxInfo Info) *v1.Instance { } nginxType = v1.InstanceMeta_INSTANCE_TYPE_NGINX_PLUS - version = nginxInfo.PlusVersion + version = nginxInfo.Version } return &v1.Instance{ @@ -190,7 +186,7 @@ func parseNginxVersionCommandOutput(ctx context.Context, output *bytes.Buffer) * line := strings.TrimSpace(scanner.Text()) switch { case strings.HasPrefix(line, "nginx version"): - nginxInfo.Version, nginxInfo.PlusVersion = parseNginxVersion(line) + nginxInfo.Version = parseNginxVersion(line) case strings.HasPrefix(line, "configure arguments"): nginxInfo.ConfigureArgs = parseConfigureArguments(line) } @@ -202,29 +198,8 @@ func parseNginxVersionCommandOutput(ctx context.Context, output *bytes.Buffer) * return nginxInfo } -func parseNginxVersion(line string) (version, plusVersion string) { - ossVersionMatches := ossVersionRegex.FindStringSubmatch(line) - plusVersionMatches := plusVersionRegex.FindStringSubmatch(line) - - ossSubNames := ossVersionRegex.SubexpNames() - plusSubNames := plusVersionRegex.SubexpNames() - - for index, value := range ossVersionMatches { - if ossSubNames[index] == "version" { - version = value - } - } - - for index, value := range plusVersionMatches { - switch plusSubNames[index] { - case "plus": - plusVersion = value - case "version": - version = value - } - } - - return version, plusVersion +func parseNginxVersion(line string) string { + return strings.TrimPrefix(versionRegex.FindString(line), "nginx/") } func parseConfigureArguments(line string) map[string]interface{} { diff --git a/internal/service/instance/nginx_instance_service_test.go b/internal/service/instance/nginx_instance_service_test.go index 99c3356b2a..c020efe178 100644 --- a/internal/service/instance/nginx_instance_service_test.go +++ b/internal/service/instance/nginx_instance_service_test.go @@ -214,11 +214,10 @@ func TestGetInfo(t *testing.T) { Exe: exePath, }, expected: &Info{ - Version: "1.25.3", - PlusVersion: "", - Prefix: "/usr/local/Cellar/nginx/1.25.3", - ConfPath: "/usr/local/etc/nginx/nginx.conf", - ExePath: exePath, + Version: "1.25.3", + Prefix: "/usr/local/Cellar/nginx/1.25.3", + ConfPath: "/usr/local/etc/nginx/nginx.conf", + ExePath: exePath, ConfigureArgs: map[string]interface{}{ "conf-path": "/usr/local/etc/nginx/nginx.conf", "error-log-path": "/usr/local/var/log/nginx/error.log", @@ -281,11 +280,10 @@ func TestGetInfo(t *testing.T) { Exe: exePath, }, expected: &Info{ - Version: "1.25.3", - PlusVersion: "nginx-plus-r31-p1", - Prefix: "/etc/nginx", - ConfPath: "/etc/nginx/nginx.conf", - ExePath: exePath, + Version: "1.25.3 (nginx-plus-r31-p1)", + Prefix: "/etc/nginx", + ConfPath: "/etc/nginx/nginx.conf", + ExePath: exePath, ConfigureArgs: map[string]interface{}{ "build": "nginx-plus-r31-p1", "conf-path": "/etc/nginx/nginx.conf", diff --git a/test/protos/instances.go b/test/protos/instances.go index 7d3d779965..b014e94209 100644 --- a/test/protos/instances.go +++ b/test/protos/instances.go @@ -56,7 +56,7 @@ func GetNginxPlusInstance(expectedModules []string) *v1.Instance { InstanceMeta: &v1.InstanceMeta{ InstanceId: plusInstanceID, InstanceType: v1.InstanceMeta_INSTANCE_TYPE_NGINX_PLUS, - Version: "nginx-plus-r31-p1", + Version: "1.25.3 (nginx-plus-r31-p1)", }, InstanceRuntime: &v1.InstanceRuntime{ ProcessId: processID,