Example #1
0
func main() {
	flag.Parse()
	tricorder.RegisterFlags()
	if os.Geteuid() == 0 {
		fmt.Fprintln(os.Stderr, "Do not run the Image Server as root")
		os.Exit(1)
	}
	if *archiveMode && *imageServerHostname == "" {
		fmt.Fprintln(os.Stderr, "-imageServerHostname required in archive mode")
		os.Exit(1)
	}
	circularBuffer := logbuf.New()
	logger := log.New(circularBuffer, "", log.LstdFlags)
	if err := setupserver.SetupTls(); err != nil {
		logger.Println(err)
		circularBuffer.Flush()
		if !*permitInsecureMode {
			os.Exit(1)
		}
	}
	objSrv, err := filesystem.NewObjectServer(*objectDir, logger)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Cannot create ObjectServer: %s\n", err)
		os.Exit(1)
	}
	cleanupUnreferencedObjects := true
	if *imageServerHostname != "" {
		cleanupUnreferencedObjects = false
	}
	imdb, err := scanner.LoadImageDataBase(*imageDir, objSrv,
		cleanupUnreferencedObjects, logger)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Cannot load image database: %s\n", err)
		os.Exit(1)
	}
	tricorder.RegisterMetric("/image-count",
		func() uint { return imdb.CountImages() },
		units.None, "number of images")
	imgSrvRpcHtmlWriter := imageserverRpcd.Setup(imdb, *imageServerHostname,
		logger)
	objSrvRpcHtmlWriter := objectserverRpcd.Setup(objSrv, logger)
	httpd.AddHtmlWriter(imdb)
	httpd.AddHtmlWriter(&imageObjectServersType{imdb, objSrv})
	httpd.AddHtmlWriter(imgSrvRpcHtmlWriter)
	httpd.AddHtmlWriter(objSrvRpcHtmlWriter)
	httpd.AddHtmlWriter(circularBuffer)
	if *imageServerHostname != "" {
		go replicator(fmt.Sprintf("%s:%d", *imageServerHostname,
			*imageServerPortNum), imdb, objSrv, *archiveMode, logger)
	}
	if err = httpd.StartServer(*portNum, imdb, objSrv, false); err != nil {
		fmt.Fprintf(os.Stderr, "Unable to create http server: %s\n", err)
		os.Exit(1)
	}
}
Example #2
0
func newObjectServer(objectsDir string, logger *log.Logger) (
	*objectserver.ObjectServer, error) {
	fi, err := os.Stat(objectsDir)
	if err != nil {
		if err := os.Mkdir(objectsDir, dirPerms); err != nil {
			return nil, err
		}
	} else if !fi.IsDir() {
		return nil, fmt.Errorf("%s is not a directory\n", objectsDir)
	}
	return objectserver.NewObjectServer(objectsDir, logger)
}