2015-12-07 21:38:50 +00:00
|
|
|
// 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 clair implements the ability to boot Clair with your own imports
|
|
|
|
// that can dynamically register additional functionality.
|
|
|
|
package clair
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2016-03-07 21:38:10 +00:00
|
|
|
"syscall"
|
2015-12-07 21:38:50 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/coreos/clair/api"
|
2016-01-27 19:07:58 +00:00
|
|
|
"github.com/coreos/clair/api/context"
|
2015-12-07 21:38:50 +00:00
|
|
|
"github.com/coreos/clair/config"
|
2016-05-02 22:33:03 +00:00
|
|
|
"github.com/coreos/clair/database"
|
2016-01-21 23:09:23 +00:00
|
|
|
"github.com/coreos/clair/notifier"
|
2016-01-20 00:17:08 +00:00
|
|
|
"github.com/coreos/clair/updater"
|
2015-12-07 21:38:50 +00:00
|
|
|
"github.com/coreos/clair/utils"
|
|
|
|
"github.com/coreos/pkg/capnslog"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = capnslog.NewPackageLogger("github.com/coreos/clair", "main")
|
|
|
|
|
|
|
|
// Boot starts Clair. By exporting this function, anyone can import their own
|
|
|
|
// custom fetchers/updaters into their own package and then call clair.Boot.
|
|
|
|
func Boot(config *config.Config) {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
st := utils.NewStopper()
|
|
|
|
|
|
|
|
// Open database
|
2016-05-02 22:33:03 +00:00
|
|
|
db, err := database.Open(config.Database)
|
2015-12-07 21:38:50 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
defer db.Close()
|
2015-12-07 21:38:50 +00:00
|
|
|
|
|
|
|
// Start notifier
|
2016-01-21 23:09:23 +00:00
|
|
|
st.Begin()
|
|
|
|
go notifier.Run(config.Notifier, db, st)
|
2015-12-07 21:38:50 +00:00
|
|
|
|
|
|
|
// Start API
|
|
|
|
st.Begin()
|
2016-02-05 21:22:17 +00:00
|
|
|
go api.Run(config.API, &context.RouteContext{db, config.API}, st)
|
2015-12-07 21:38:50 +00:00
|
|
|
st.Begin()
|
2016-02-05 21:22:17 +00:00
|
|
|
go api.RunHealth(config.API, &context.RouteContext{db, config.API}, st)
|
2015-12-07 21:38:50 +00:00
|
|
|
|
|
|
|
// Start updater
|
2016-01-20 00:17:08 +00:00
|
|
|
st.Begin()
|
|
|
|
go updater.Run(config.Updater, db, st)
|
2015-12-07 21:38:50 +00:00
|
|
|
|
|
|
|
// Wait for interruption and shutdown gracefully.
|
2016-03-07 21:38:10 +00:00
|
|
|
waitForSignals(syscall.SIGINT, syscall.SIGTERM)
|
2015-12-07 21:38:50 +00:00
|
|
|
log.Info("Received interruption, gracefully stopping ...")
|
|
|
|
st.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForSignals(signals ...os.Signal) {
|
|
|
|
interrupts := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interrupts, signals...)
|
|
|
|
<-interrupts
|
|
|
|
}
|