systemd: parse systemdVersion in more situations

there have been cases observed where instead of `v$VER.0-$OS` the systemdVersion returned is just `$VER`, or `$VER-1`.
handle these cases

Signed-off-by: Peter Hunt <pehunt@redhat.com>
This commit is contained in:
Peter Hunt 2020-06-17 16:27:39 -04:00
parent 406298fdf0
commit 6369e38871
1 changed files with 18 additions and 11 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"math" "math"
"os" "os"
"regexp"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -365,22 +366,28 @@ func systemdVersion(conn *systemdDbus.Conn) (int, error) {
return return
} }
// verStr is like "v245.4-1.fc32" (including quotes) version, versionErr = systemdVersionAtoi(verStr)
if !strings.HasPrefix(verStr, `"v`) { return
versionErr = fmt.Errorf("can't parse version %s", verStr)
return
}
// remove `"v` prefix and everything after the first dot
ver, err := strconv.Atoi(strings.SplitN(verStr[2:], ".", 2)[0])
if err != nil {
versionErr = err
}
version = ver
}) })
return version, versionErr return version, versionErr
} }
func systemdVersionAtoi(verStr string) (int, error) {
// verStr should be of the form:
// "v245.4-1.fc32", "245", "v245-1.fc32", "245-1.fc32"
// all the input strings include quotes, and the output int should be 245
// thus, we unconditionally remove the `"v`
// and then match on the first integer we can grab
re := regexp.MustCompile(`"?v?([0-9]+)`)
matches := re.FindStringSubmatch(verStr)
if len(matches) < 2 {
return 0, errors.Errorf("can't parse version %s: incorrect number of matches %v", verStr, matches)
}
ver, err := strconv.Atoi(matches[1])
return ver, errors.Wrapf(err, "can't parse version %s", verStr)
}
func addCpuQuota(conn *systemdDbus.Conn, properties *[]systemdDbus.Property, quota int64, period uint64) { func addCpuQuota(conn *systemdDbus.Conn, properties *[]systemdDbus.Property, quota int64, period uint64) {
if period != 0 { if period != 0 {
// systemd only supports CPUQuotaPeriodUSec since v242 // systemd only supports CPUQuotaPeriodUSec since v242