Skip to content

Commit

Permalink
utils: rm exec.go
Browse files Browse the repository at this point in the history
This change also adds a dependency check at startup, rather than
runtime.
  • Loading branch information
jzelinskie committed Jan 23, 2017
1 parent e7f72ef commit c2f4a44
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 55 deletions.
9 changes: 9 additions & 0 deletions cmd/clair/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"flag"
"math/rand"
"os"
"os/exec"
"os/signal"
"runtime/pprof"
"strings"
Expand Down Expand Up @@ -127,6 +128,14 @@ func main() {
flagLogLevel := flag.String("log-level", "info", "Define the logging level.")
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
config, err := config.Load(*flagConfigPath)
if err != nil {
Expand Down
8 changes: 3 additions & 5 deletions ext/featurefmt/rpm/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"bufio"
"io/ioutil"
"os"
"os/exec"
"strings"

"github.com/coreos/pkg/capnslog"
Expand All @@ -29,7 +30,6 @@ import (
"github.com/coreos/clair/ext/versionfmt/rpm"
"github.com/coreos/clair/pkg/commonerr"
"github.com/coreos/clair/pkg/tarutil"
"github.com/coreos/clair/utils"
)

var log = capnslog.NewPackageLogger("github.com/coreos/clair", "ext/featurefmt/rpm")
Expand Down Expand Up @@ -63,10 +63,8 @@ func (l lister) ListFeatures(files tarutil.FilesMap) ([]database.FeatureVersion,
return []database.FeatureVersion{}, commonerr.ErrFilesystem
}

// Query RPM
// We actually extract binary package names instead of source package names here because RHSA refers to package names
// In the dpkg system, we extract the source instead
out, err := utils.Exec(tmpDir, "rpm", "--dbpath", tmpDir, "-qa", "--qf", "%{NAME} %{EPOCH}:%{VERSION}-%{RELEASE}\n")
// Extract binary package names because RHSA refers to binary package names.
out, err := exec.Command("rpm", "--dbpath", tmpDir, "-qa", "--qf", "%{NAME} %{EPOCH}:%{VERSION}-%{RELEASE}\n").CombinedOutput()
if err != nil {
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,
Expand Down
17 changes: 11 additions & 6 deletions ext/vulnsrc/alpine/alpine.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"io/ioutil"
"os"
"os/exec"
"strings"

"gopkg.in/yaml.v2"
Expand All @@ -33,7 +34,6 @@ import (
"github.com/coreos/clair/ext/versionfmt/dpkg"
"github.com/coreos/clair/ext/vulnsrc"
"github.com/coreos/clair/pkg/commonerr"
"github.com/coreos/clair/utils"
)

const (
Expand Down Expand Up @@ -169,20 +169,25 @@ func (u *updater) pullRepository() (commit string, err error) {
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()
log.Errorf("could not pull alpine-secdb repository: %s. output: %s", err, out)
return "", commonerr.ErrCouldNotDownload
}
} else {
// The repository exists and it needs to be refreshed via a pull.
_, err := utils.Exec(u.repositoryLocalPath, "git", "pull")
if err != nil {
// The repository already exists and it needs to be refreshed via a pull.
cmd := exec.Command("git", "pull")
cmd.Dir = u.repositoryLocalPath
if _, err := cmd.CombinedOutput(); err != nil {
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 {
return "", vulnsrc.ErrGitFailure
}
Expand Down
20 changes: 15 additions & 5 deletions ext/vulnsrc/ubuntu/ubuntu.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"io/ioutil"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
Expand All @@ -35,7 +36,6 @@ import (
"github.com/coreos/clair/ext/versionfmt/dpkg"
"github.com/coreos/clair/ext/vulnsrc"
"github.com/coreos/clair/pkg/commonerr"
"github.com/coreos/clair/utils"
)

const (
Expand Down Expand Up @@ -174,7 +174,9 @@ func (u *updater) pullRepository() (err error) {
}

// 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)
return commonerr.ErrCouldNotDownload
}
Expand All @@ -183,7 +185,9 @@ func (u *updater) pullRepository() (err error) {
}

// 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)

log.Errorf("could not pull Ubuntu repository: %s. output: %s", err, out)
Expand All @@ -194,16 +198,20 @@ func (u *updater) pullRepository() (err 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 {
log.Errorf("could not get Ubuntu repository's revision number: %s. output: %s", err, out)
return 0, commonerr.ErrCouldNotDownload
}

revno, err := strconv.Atoi(strings.TrimSpace(string(out)))
if err != nil {
log.Errorf("could not parse Ubuntu repository's revision number: %s. output: %s", err, out)
return 0, commonerr.ErrCouldNotDownload
}

return revno, nil
}

Expand Down Expand Up @@ -252,7 +260,9 @@ func collectModifiedVulnerabilities(revision int, dbRevision, repositoryLocalPat
}

// 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 {
log.Errorf("could not get Ubuntu vulnerabilities repository logs: %s. output: %s", err, out)
return nil, commonerr.ErrCouldNotDownload
Expand Down
39 changes: 0 additions & 39 deletions utils/exec.go

This file was deleted.

0 comments on commit c2f4a44

Please sign in to comment.