Skip to content

Commit

Permalink
clair: remove types package
Browse files Browse the repository at this point in the history
This removes the `types` package instead moving the contents to the
top-level clair package.
This change also renames the `Priority` type to `Severity` in order to
reduce confusion.
This change also removes the IsValid method and replaces it with a safe
constructor to avoid the creation of invalid values.
Many docstrings were tweaked in the making of this commit.
  • Loading branch information
jzelinskie committed Jan 23, 2017
1 parent 02e2c58 commit 343e24e
Show file tree
Hide file tree
Showing 24 changed files with 363 additions and 356 deletions.
10 changes: 5 additions & 5 deletions api/v1/models.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015 clair authors
// Copyright 2017 clair authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -24,9 +24,9 @@ import (
"github.com/coreos/pkg/capnslog"
"github.com/fernet/fernet-go"

"github.com/coreos/clair"
"github.com/coreos/clair/database"
"github.com/coreos/clair/ext/versionfmt"
"github.com/coreos/clair/utils/types"
)

var log = capnslog.NewPackageLogger("github.com/coreos/clair", "v1")
Expand Down Expand Up @@ -109,9 +109,9 @@ type Vulnerability struct {
}

func (v Vulnerability) DatabaseModel() (database.Vulnerability, error) {
severity := types.Priority(v.Severity)
if !severity.IsValid() {
return database.Vulnerability{}, errors.New("Invalid severity")
severity, err := clair.NewSeverity(v.Severity)
if err != nil {
return database.Vulnerability{}, err
}

var dbFeatures []database.FeatureVersion
Expand Down
75 changes: 0 additions & 75 deletions clair.go

This file was deleted.

96 changes: 72 additions & 24 deletions cmd/clair/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@ package main

import (
"flag"
"math/rand"
"os"
"os/signal"
"runtime/pprof"
"strings"
"syscall"
"time"

"github.com/coreos/pkg/capnslog"

"github.com/coreos/clair"
"github.com/coreos/clair/api"
"github.com/coreos/clair/api/context"
"github.com/coreos/clair/config"
"github.com/coreos/clair/database"
"github.com/coreos/clair/notifier"
"github.com/coreos/clair/updater"
"github.com/coreos/clair/utils"

// Register database driver.
_ "github.com/coreos/clair/database/pgsql"
Expand All @@ -50,13 +59,74 @@ import (

var log = capnslog.NewPackageLogger("github.com/coreos/clair/cmd/clair", "main")

func waitForSignals(signals ...os.Signal) {
interrupts := make(chan os.Signal, 1)
signal.Notify(interrupts, signals...)
<-interrupts
}

func startCPUProfiling(path string) *os.File {
f, err := os.Create(path)
if err != nil {
log.Fatalf("failed to create profile file: %s", err)
}

err = pprof.StartCPUProfile(f)
if err != nil {
log.Fatalf("failed to start CPU profiling: %s", err)
}

log.Info("started CPU profiling")

return f
}

func stopCPUProfiling(f *os.File) {
pprof.StopCPUProfile()
f.Close()
log.Info("stopped CPU profiling")
}

// Boot starts Clair instance with the provided config.
func Boot(config *config.Config) {
rand.Seed(time.Now().UnixNano())
st := utils.NewStopper()

// Open database
db, err := database.Open(config.Database)
if err != nil {
log.Fatal(err)
}
defer db.Close()

// Start notifier
st.Begin()
go notifier.Run(config.Notifier, db, st)

// Start API
st.Begin()
go api.Run(config.API, &context.RouteContext{db, config.API}, st)
st.Begin()
go api.RunHealth(config.API, &context.RouteContext{db, config.API}, st)

// Start updater
st.Begin()
go updater.Run(config.Updater, db, st)

// Wait for interruption and shutdown gracefully.
waitForSignals(syscall.SIGINT, syscall.SIGTERM)
log.Info("Received interruption, gracefully stopping ...")
st.Stop()
}

func main() {
// Parse command-line arguments
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flagConfigPath := flag.String("config", "/etc/clair/config.yaml", "Load configuration from the specified file.")
flagCPUProfilePath := flag.String("cpu-profile", "", "Write a CPU profile to the specified file before exiting.")
flagLogLevel := flag.String("log-level", "info", "Define the logging level.")
flag.Parse()

// Load configuration
config, err := config.Load(*flagConfigPath)
if err != nil {
Expand All @@ -73,27 +143,5 @@ func main() {
defer stopCPUProfiling(startCPUProfiling(*flagCPUProfilePath))
}

clair.Boot(config)
}

func startCPUProfiling(path string) *os.File {
f, err := os.Create(path)
if err != nil {
log.Fatalf("failed to create profile file: %s", err)
}

err = pprof.StartCPUProfile(f)
if err != nil {
log.Fatalf("failed to start CPU profiling: %s", err)
}

log.Info("started CPU profiling")

return f
}

func stopCPUProfiling(f *os.File) {
pprof.StopCPUProfile()
f.Close()
log.Info("stopped CPU profiling")
Boot(config)
}
Loading

0 comments on commit 343e24e

Please sign in to comment.