Skip to content

Commit

Permalink
Fix NGINX Plus Instance, NGINX Version (#661)
Browse files Browse the repository at this point in the history
* fix nginx plus version
  • Loading branch information
aphralG authored May 3, 2024
1 parent 1750510 commit 01ef41d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 42 deletions.
37 changes: 6 additions & 31 deletions internal/service/instance/nginx_instance_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@ import (
"github.com/nginx/agent/v3/internal/uuid"
)

var (
ossVersionRegex = regexp.MustCompile(`(?P<name>\S+)/(?P<version>\S+)`)
plusVersionRegex = regexp.MustCompile(`(?P<name>\S+)/(?P<version>\S+).\((?P<plus>\S+plus\S+)\)`)
)
var versionRegex = regexp.MustCompile(`(?P<name>\S+)\/(?P<version>.*)`)

type Info struct {
ProcessID int32
Version string
PlusVersion string
Prefix string
ConfPath string
ConfigureArgs map[string]interface{}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand All @@ -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)
}
Expand All @@ -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{} {
Expand Down
18 changes: 8 additions & 10 deletions internal/service/instance/nginx_instance_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion test/protos/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 01ef41d

Please sign in to comment.