1 // Copyright (c) 2020 Cisco and/or its affiliates.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
25 "github.com/sirupsen/logrus"
29 VPPVersionEnvVar = "VPP_VERSION"
32 // ResolveVPPVersion resolves version of the VPP for target directory.
34 // Version resolved here can be overriden by setting VPP_VERSION env var.
35 func ResolveVPPVersion(apidir string) string {
36 // check env variable override
37 if ver := os.Getenv(VPPVersionEnvVar); ver != "" {
38 logrus.Debugf("VPP version was manually set to %q via %s env var", ver, VPPVersionEnvVar)
42 // assuming VPP package is installed
43 if path.Clean(apidir) == DefaultDir {
44 version, err := GetVPPVersionInstalled()
46 logrus.Warnf("resolving VPP version from installed package failed: %v", err)
48 logrus.Debugf("resolved VPP version from installed package: %v", version)
53 // check if inside VPP repo
54 repoDir, err := findGitRepoRootDir(apidir)
56 logrus.Warnf("checking VPP git repo failed: %v", err)
58 logrus.Debugf("resolved git repo root directory: %v", repoDir)
59 version, err := GetVPPVersionRepo(repoDir)
61 logrus.Warnf("resolving VPP version from version script failed: %v", err)
63 logrus.Debugf("resolved VPP version from version script: %v", version)
68 // try to read VPP_VERSION file
69 data, err := ioutil.ReadFile(path.Join(repoDir, "VPP_VERSION"))
71 return strings.TrimSpace(string(data))
74 logrus.Warnf("VPP version could not be resolved, you can set it manually using %s env var", VPPVersionEnvVar)
78 // GetVPPVersionInstalled retrieves VPP version of installed package using dpkg-query.
79 func GetVPPVersionInstalled() (string, error) {
80 cmd := exec.Command("dpkg-query", "-f", "${Version}", "-W", "vpp")
81 out, err := cmd.CombinedOutput()
83 return "", fmt.Errorf("dpkg-query command failed: %v\noutput: %s", err, out)
85 return strings.TrimSpace(string(out)), nil
88 const versionScriptPath = "./src/scripts/version"
90 // GetVPPVersionRepo retrieves VPP version using script in repo directory.
91 func GetVPPVersionRepo(repoDir string) (string, error) {
92 if _, err := os.Stat(versionScriptPath); err != nil {
95 cmd := exec.Command(versionScriptPath)
97 out, err := cmd.CombinedOutput()
99 return "", fmt.Errorf("version script failed: %v\noutput: %s", err, out)
101 return strings.TrimSpace(string(out)), nil
104 func findGitRepoRootDir(dir string) (string, error) {
105 cmd := exec.Command("git", "rev-parse", "--show-toplevel")
107 out, err := cmd.CombinedOutput()
109 return "", fmt.Errorf("git command failed: %v\noutput: %s", err, out)
111 return strings.TrimSpace(string(out)), nil