package msi import ( "bytes" "fmt" "regexp" "strings" "text/template" ) type wxsCfg struct { GrafanaVersion string UpgradeCode string ProductName string Title string Manufacturer string License string } var semverRegex = regexp.MustCompile(`^(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)(?:-(?P(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`) // WxsVersion converts a grafana version string (no v) to a 4-digit MSI version. func WxsVersion(ersion string) string { match := semverRegex.FindStringSubmatch(ersion) result := make(map[string]string) for i, name := range semverRegex.SubexpNames() { if i != 0 && name != "" { result[name] = match[i] } } var major, minor, patch string if v, ok := result["major"]; ok { major = v } if v, ok := result["minor"]; ok { minor = v } if v, ok := result["patch"]; ok { patch = v } if v, ok := result["buildmetadata"]; ok && v != "" { return fmt.Sprintf("%s.%s.%s.%s", result["major"], result["minor"], result["patch"], strings.TrimPrefix(v, "security-")) } if v, ok := result["prerelease"]; ok && v != "" { v := strings.TrimPrefix(v, "beta") v = strings.TrimPrefix(v, "pre") if v == "local" { v = "0" } if len(v) > 5 { v = v[len(v)-5:] } return fmt.Sprintf("%s.%s.%s.%s", major, minor, patch, v) } return fmt.Sprintf("%s.%s.%s.0", major, minor, patch) } type WXSFile struct { Name string Contents string } func WXSFiles(version string, enterprise bool) ([]WXSFile, error) { upgradeCode := "35c7d2a9-6e23-4645-b975-e8693a1cef10" prodName := "GrafanaOSS" title := "Grafana OSS" license := "LICENSE.rtf" if enterprise { upgradeCode = "d534ec50-476b-4edc-a25e-fe854c949f4f" prodName = "GrafanaEnterprise" title = "Grafana Enterprise" license = "EE_LICENSE.rtf" } ersion := strings.TrimPrefix(version, "v") cfg := wxsCfg{ GrafanaVersion: WxsVersion(ersion), UpgradeCode: upgradeCode, ProductName: prodName, Title: title, Manufacturer: "Grafana Labs", License: license, } files := make([]WXSFile, len(wxsTemplates)) for i, t := range wxsTemplates { name := fmt.Sprintf("grafana-%s.wxs", t.Name()) buf := bytes.NewBuffer(nil) if err := t.Execute(buf, cfg); err != nil { return nil, err } files[i] = WXSFile{ Name: name, Contents: buf.String(), } } return files, nil } var wxsTemplates = []*template.Template{ template.Must(template.New("firewall").Parse(firewallTemplate)), template.Must(template.New("service").Parse(svcTemplate)), template.Must(template.New("product").Parse(prodTemplate)), } const firewallTemplate = ` ` const prodTemplate = ` {{ $version := .GrafanaVersion }} ` const svcTemplate = ` LOG_LEVEL=DEBUG `