utils: rm exec.go

This change also adds a dependency check at startup, rather than
runtime.
This commit is contained in:
Jimmy Zelinskie 2017-01-17 21:22:20 -05:00
parent e7f72ef5ad
commit c2f4a44068
5 changed files with 38 additions and 55 deletions

View File

@ -18,6 +18,7 @@ import (
"flag" "flag"
"math/rand" "math/rand"
"os" "os"
"os/exec"
"os/signal" "os/signal"
"runtime/pprof" "runtime/pprof"
"strings" "strings"
@ -127,6 +128,14 @@ func main() {
flagLogLevel := flag.String("log-level", "info", "Define the logging level.") flagLogLevel := flag.String("log-level", "info", "Define the logging level.")
flag.Parse() flag.Parse()
// Check for dependencies.
for _, bin := range []string{"git", "bzr", "rpm", "xz"} {
_, err := exec.LookPath(bin)
if err != nil {
log.Fatalf("failed to find dependency: %s", bin)
}
}
// Load configuration // Load configuration
config, err := config.Load(*flagConfigPath) config, err := config.Load(*flagConfigPath)
if err != nil { if err != nil {

View File

@ -19,6 +19,7 @@ import (
"bufio" "bufio"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec"
"strings" "strings"
"github.com/coreos/pkg/capnslog" "github.com/coreos/pkg/capnslog"
@ -29,7 +30,6 @@ import (
"github.com/coreos/clair/ext/versionfmt/rpm" "github.com/coreos/clair/ext/versionfmt/rpm"
"github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/pkg/commonerr"
"github.com/coreos/clair/pkg/tarutil" "github.com/coreos/clair/pkg/tarutil"
"github.com/coreos/clair/utils"
) )
var log = capnslog.NewPackageLogger("github.com/coreos/clair", "ext/featurefmt/rpm") var log = capnslog.NewPackageLogger("github.com/coreos/clair", "ext/featurefmt/rpm")
@ -63,10 +63,8 @@ func (l lister) ListFeatures(files tarutil.FilesMap) ([]database.FeatureVersion,
return []database.FeatureVersion{}, commonerr.ErrFilesystem return []database.FeatureVersion{}, commonerr.ErrFilesystem
} }
// Query RPM // Extract binary package names because RHSA refers to binary package names.
// We actually extract binary package names instead of source package names here because RHSA refers to package names out, err := exec.Command("rpm", "--dbpath", tmpDir, "-qa", "--qf", "%{NAME} %{EPOCH}:%{VERSION}-%{RELEASE}\n").CombinedOutput()
// In the dpkg system, we extract the source instead
out, err := utils.Exec(tmpDir, "rpm", "--dbpath", tmpDir, "-qa", "--qf", "%{NAME} %{EPOCH}:%{VERSION}-%{RELEASE}\n")
if err != nil { if err != nil {
log.Errorf("could not query RPM: %s. output: %s", err, string(out)) log.Errorf("could not query RPM: %s. output: %s", err, string(out))
// Do not bubble up because we probably won't be able to fix it, // Do not bubble up because we probably won't be able to fix it,

View File

@ -21,6 +21,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec"
"strings" "strings"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -33,7 +34,6 @@ import (
"github.com/coreos/clair/ext/versionfmt/dpkg" "github.com/coreos/clair/ext/versionfmt/dpkg"
"github.com/coreos/clair/ext/vulnsrc" "github.com/coreos/clair/ext/vulnsrc"
"github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/pkg/commonerr"
"github.com/coreos/clair/utils"
) )
const ( const (
@ -169,20 +169,25 @@ func (u *updater) pullRepository() (commit string, err error) {
return "", vulnsrc.ErrFilesystem return "", vulnsrc.ErrFilesystem
} }
if out, err := utils.Exec(u.repositoryLocalPath, "git", "clone", secdbGitURL, "."); err != nil { cmd := exec.Command("git", "clone", secdbGitURL, ".")
cmd.Dir = u.repositoryLocalPath
if out, err := cmd.CombinedOutput(); err != nil {
u.Clean() u.Clean()
log.Errorf("could not pull alpine-secdb repository: %s. output: %s", err, out) log.Errorf("could not pull alpine-secdb repository: %s. output: %s", err, out)
return "", commonerr.ErrCouldNotDownload return "", commonerr.ErrCouldNotDownload
} }
} else { } else {
// The repository exists and it needs to be refreshed via a pull. // The repository already exists and it needs to be refreshed via a pull.
_, err := utils.Exec(u.repositoryLocalPath, "git", "pull") cmd := exec.Command("git", "pull")
if err != nil { cmd.Dir = u.repositoryLocalPath
if _, err := cmd.CombinedOutput(); err != nil {
return "", vulnsrc.ErrGitFailure return "", vulnsrc.ErrGitFailure
} }
} }
out, err := utils.Exec(u.repositoryLocalPath, "git", "rev-parse", "HEAD") cmd := exec.Command("git", "rev-parse", "HEAD")
cmd.Dir = u.repositoryLocalPath
out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return "", vulnsrc.ErrGitFailure return "", vulnsrc.ErrGitFailure
} }

View File

@ -23,6 +23,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -35,7 +36,6 @@ import (
"github.com/coreos/clair/ext/versionfmt/dpkg" "github.com/coreos/clair/ext/versionfmt/dpkg"
"github.com/coreos/clair/ext/vulnsrc" "github.com/coreos/clair/ext/vulnsrc"
"github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/pkg/commonerr"
"github.com/coreos/clair/utils"
) )
const ( const (
@ -174,7 +174,9 @@ func (u *updater) pullRepository() (err error) {
} }
// Branch repository. // Branch repository.
if out, err := utils.Exec(u.repositoryLocalPath, "bzr", "branch", "--use-existing-dir", trackerRepository, "."); err != nil { cmd := exec.Command("bzr", "branch", "--use-existing-dir", trackerRepository, ".")
cmd.Dir = u.repositoryLocalPath
if out, err := cmd.CombinedOutput(); err != nil {
log.Errorf("could not branch Ubuntu repository: %s. output: %s", err, out) log.Errorf("could not branch Ubuntu repository: %s. output: %s", err, out)
return commonerr.ErrCouldNotDownload return commonerr.ErrCouldNotDownload
} }
@ -183,7 +185,9 @@ func (u *updater) pullRepository() (err error) {
} }
// Pull repository. // Pull repository.
if out, err := utils.Exec(u.repositoryLocalPath, "bzr", "pull", "--overwrite"); err != nil { cmd := exec.Command("bzr", "pull", "--overwrite")
cmd.Dir = u.repositoryLocalPath
if out, err := cmd.CombinedOutput(); err != nil {
os.RemoveAll(u.repositoryLocalPath) os.RemoveAll(u.repositoryLocalPath)
log.Errorf("could not pull Ubuntu repository: %s. output: %s", err, out) log.Errorf("could not pull Ubuntu repository: %s. output: %s", err, out)
@ -194,16 +198,20 @@ func (u *updater) pullRepository() (err error) {
} }
func getRevisionNumber(pathToRepo string) (int, error) { func getRevisionNumber(pathToRepo string) (int, error) {
out, err := utils.Exec(pathToRepo, "bzr", "revno") cmd := exec.Command("bzr", "revno")
cmd.Dir = pathToRepo
out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
log.Errorf("could not get Ubuntu repository's revision number: %s. output: %s", err, out) log.Errorf("could not get Ubuntu repository's revision number: %s. output: %s", err, out)
return 0, commonerr.ErrCouldNotDownload return 0, commonerr.ErrCouldNotDownload
} }
revno, err := strconv.Atoi(strings.TrimSpace(string(out))) revno, err := strconv.Atoi(strings.TrimSpace(string(out)))
if err != nil { if err != nil {
log.Errorf("could not parse Ubuntu repository's revision number: %s. output: %s", err, out) log.Errorf("could not parse Ubuntu repository's revision number: %s. output: %s", err, out)
return 0, commonerr.ErrCouldNotDownload return 0, commonerr.ErrCouldNotDownload
} }
return revno, nil return revno, nil
} }
@ -252,7 +260,9 @@ func collectModifiedVulnerabilities(revision int, dbRevision, repositoryLocalPat
} }
// Handle a database that needs upgrading. // Handle a database that needs upgrading.
out, err := utils.Exec(repositoryLocalPath, "bzr", "log", "--verbose", "-r"+strconv.Itoa(dbRevisionInt+1)+"..", "-n0") cmd := exec.Command("bzr", "log", "--verbose", "-r"+strconv.Itoa(dbRevisionInt+1)+"..", "-n0")
cmd.Dir = repositoryLocalPath
out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
log.Errorf("could not get Ubuntu vulnerabilities repository logs: %s. output: %s", err, out) log.Errorf("could not get Ubuntu vulnerabilities repository logs: %s. output: %s", err, out)
return nil, commonerr.ErrCouldNotDownload return nil, commonerr.ErrCouldNotDownload

View File

@ -1,39 +0,0 @@
// Copyright 2015 clair authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package utils simply defines utility functions and types.
package utils
import (
"bytes"
"os/exec"
)
// Exec runs the given binary with arguments
func Exec(dir string, bin string, args ...string) ([]byte, error) {
_, err := exec.LookPath(bin)
if err != nil {
return nil, err
}
cmd := exec.Command(bin, args...)
cmd.Dir = dir
var buf bytes.Buffer
cmd.Stdout = &buf
cmd.Stderr = &buf
err = cmd.Run()
return buf.Bytes(), err
}