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"
30 VPPDirEnvVar = "VPP_DIR"
31 versionScriptPath = "./src/scripts/version"
34 // ResolveVPPVersion resolves version of the VPP for target directory.
36 // Version resolved here can be overriden by setting VPP_VERSION env var.
37 func ResolveVPPVersion(apidir string) string {
38 // check env variable override
39 if ver := os.Getenv(VPPVersionEnvVar); ver != "" {
40 logrus.Infof("VPP version was manually set to %q via %s env var", ver, VPPVersionEnvVar)
44 // assuming VPP package is installed
45 if path.Clean(apidir) == DefaultDir {
46 version, err := GetVPPVersionInstalled()
48 logrus.Warnf("resolving VPP version from installed package failed: %v", err)
50 logrus.Debugf("resolved VPP version from installed package: %v", version)
55 // check if inside VPP repo
56 repoDir, err := findGitRepoRootDir(apidir)
58 logrus.Warnf("checking VPP git repo failed: %v", err)
60 logrus.Debugf("resolved git repo root directory: %v", repoDir)
61 version, err := GetVPPVersionRepo(repoDir)
63 logrus.Warnf("resolving VPP version from version script failed: %v", err)
65 logrus.Infof("resolved VPP version from version script: %v", version)
70 // try to read VPP_VERSION file
71 data, err := ioutil.ReadFile(path.Join(repoDir, "VPP_VERSION"))
73 return strings.TrimSpace(string(data))
76 logrus.Warnf("VPP version could not be resolved, you can set it manually using %s env var", VPPVersionEnvVar)
80 // GetVPPVersionInstalled retrieves VPP version of installed package using dpkg-query.
81 func GetVPPVersionInstalled() (string, error) {
82 cmd := exec.Command("dpkg-query", "-f", "${Version}", "-W", "vpp")
83 out, err := cmd.CombinedOutput()
85 return "", fmt.Errorf("dpkg-query command failed: %v\noutput: %s", err, out)
87 return strings.TrimSpace(string(out)), nil
90 // GetVPPVersionRepo retrieves VPP version using script in repo directory.
91 func GetVPPVersionRepo(repoDir string) (string, error) {
92 scriptPath := path.Join(repoDir, versionScriptPath)
93 if _, err := os.Stat(scriptPath); err != nil {
96 cmd := exec.Command(scriptPath)
98 out, err := cmd.CombinedOutput()
100 return "", fmt.Errorf("version script failed: %v\noutput: %s", err, out)
102 return strings.TrimSpace(string(out)), nil
105 func findGitRepoRootDir(dir string) (string, error) {
106 if conf := os.Getenv(VPPDirEnvVar); conf != "" {
107 logrus.Infof("VPP directory was manually set to %q via %s env var", conf, VPPDirEnvVar)
110 cmd := exec.Command("git", "rev-parse", "--show-toplevel")
112 out, err := cmd.CombinedOutput()
114 return "", fmt.Errorf("git command failed: %v\noutput: %s", err, out)
116 return strings.TrimSpace(string(out)), nil