Skip to content

Commit

Permalink
move initialization code from init() to main(), see #32
Browse files Browse the repository at this point in the history
  • Loading branch information
jlowellwofford committed May 9, 2021
1 parent 55ef6f0 commit 023b5df
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 49 deletions.
54 changes: 54 additions & 0 deletions cmd/imageapi-server/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// initialize internal objects
// we keep this separated from main.go to minimize the amount of modification to generated code.

package main

import (
"os"
"time"

internal "github.com/kraken-hpc/imageapi/internal/api"
"github.com/sirupsen/logrus"
)

const collectTime = time.Second * 1

func initInternal() {
internal.Log = logrus.New()
// fixme: read from os.Env?
logLevel := logrus.InfoLevel
if val, ok := os.LookupEnv("IMAGEAPI_LOGLEVEL"); ok {
if ll, ok := internal.LogStringToLL[val]; ok {
logLevel = ll
}
}
internal.MountDir = "/var/run/imageapi/mounts"
internal.LogDir = "/var/run/imageapi/logs"
if val, ok := os.LookupEnv("IMAGEAPI_MOUNTDIR"); ok {
internal.MountDir = val
}
if val, ok := os.LookupEnv("IMAGEAPI_LOGDIR"); ok {
internal.LogDir = val
}

internal.ForkInit()

internal.Log.Level = logLevel
internal.Log.Info("initializing imageapi-server")
internal.Rbds = &internal.RbdsType{}
internal.Rbds.Init()
internal.MountsRbd = &internal.MountsRBDType{}
internal.MountsRbd.Init()
internal.MountsOverlay = &internal.MountsOverlayType{}
internal.MountsOverlay.Init()
internal.Containers = &internal.ContainersType{}
internal.Containers.Init()
internal.Log.WithField("collectTime", collectTime).Debug("starting garbage collection")

go func() {
for {
time.Sleep(collectTime)
internal.GarbageCollect()
}
}()
}
8 changes: 4 additions & 4 deletions cmd/imageapi-server/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Code generated by go-swagger; DO NOT EDIT.
// This file was generated by the swagger tool.
// Make sure not to overwrite this file after you generated it because all your edits would be lost!

package main

Expand All @@ -13,10 +14,9 @@ import (
"github.com/kraken-hpc/imageapi/restapi/operations"
)

// This file was generated by the swagger tool.
// Make sure not to overwrite this file after you generated it because all your edits would be lost!

func main() {
// IMPORTANT: if you regenerate code, this line needs to be re-added!!!
initInternal()

swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/api/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ func (c *ContainersType) Create(ctn *models.Container) (r *models.Container, err
ctn.ID = c.next

// set up logger
if err = os.MkdirAll(logDir, 0700); err != nil {
if err = os.MkdirAll(LogDir, 0700); err != nil {
l.WithError(err).Error("could not make log directory")
return nil, fmt.Errorf("could not create log directory: %v", err)
}
ctn.Logfile = path.Join(logDir, fmt.Sprintf("%d-%d.log", ctn.ID, time.Now().Unix()))
ctn.Logfile = path.Join(LogDir, fmt.Sprintf("%d-%d.log", ctn.ID, time.Now().Unix()))
f, err := os.Create(ctn.Logfile)
if err != nil {
l.WithError(err).Error("failed to creat elog file")
Expand Down Expand Up @@ -653,7 +653,7 @@ func validateImage(newRoot string) (err error) {
return
}

func init() {
func ForkInit() {
fork.RegisterFunc("containerInit", containerInit)
fork.Init()
}
43 changes: 7 additions & 36 deletions internal/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package api

import (
"errors"
"os"
"time"

"github.com/sirupsen/logrus"
)
Expand All @@ -14,22 +12,18 @@ var MountsOverlay *MountsOverlayType
var Containers *ContainersType
var Log *logrus.Logger

const mountDir = "/var/run/imageapi/mounts"
const logDir = "/var/run/imageapi/logs"
const collectTime = time.Second * 1
var MountDir string = "/var/run/imageapi/mounts"
var LogDir string = "/var/run/imageapi/logs"

var ERRNOTFOUND = errors.New("not found")

func garbageCollect() {
for {
time.Sleep(collectTime)
MountsOverlay.Collect()
MountsRbd.Collect()
Rbds.Collect()
}
func GarbageCollect() {
MountsOverlay.Collect()
MountsRbd.Collect()
Rbds.Collect()
}

var llStringToLL = map[string]logrus.Level{
var LogStringToLL = map[string]logrus.Level{
"PANIC": logrus.PanicLevel,
"FATAL": logrus.FatalLevel,
"ERROR": logrus.ErrorLevel,
Expand All @@ -38,26 +32,3 @@ var llStringToLL = map[string]logrus.Level{
"DEBUG": logrus.DebugLevel,
"TRACE": logrus.TraceLevel,
}

func init() {
Log = logrus.New()
// fixme: read from os.Env?
logLevel := logrus.InfoLevel
if val, ok := os.LookupEnv("IMAGEAPI_LOGLEVEL"); ok {
if ll, ok := llStringToLL[val]; ok {
logLevel = ll
}
}
Log.Level = logLevel
Log.Info("initializing imageapi-server")
Rbds = &RbdsType{}
Rbds.Init()
MountsRbd = &MountsRBDType{}
MountsRbd.Init()
MountsOverlay = &MountsOverlayType{}
MountsOverlay.Init()
Containers = &ContainersType{}
Containers.Init()
Log.WithField("collectTime", collectTime).Debug("starting garbage collection")
go garbageCollect()
}
8 changes: 4 additions & 4 deletions internal/api/mount_overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,21 @@ func (m *MountsOverlayType) Mount(mnt *models.MountOverlay) (r *models.MountOver

// ok, we're good to attempt the mount
// make a mountpoint/upperdir/workdir
if err = os.MkdirAll(mountDir, 0700); err != nil {
if err = os.MkdirAll(MountDir, 0700); err != nil {
l.WithError(err).Error("could not create base directory")
return nil, fmt.Errorf("could not create base mount directory: %v", err)
}
if mnt.Mountpoint, err = ioutil.TempDir(mountDir, "mount_"); err != nil {
if mnt.Mountpoint, err = ioutil.TempDir(MountDir, "mount_"); err != nil {
l.WithError(err).Error("could not create mountpoint")
return nil, fmt.Errorf("could not create mountpoint: %v", err)
}
os.Chmod(mnt.Mountpoint, os.FileMode(0755))
if mnt.Upperdir, err = ioutil.TempDir(mountDir, "upper_"); err != nil {
if mnt.Upperdir, err = ioutil.TempDir(MountDir, "upper_"); err != nil {
l.WithError(err).Error("could not create upperdir")
return nil, fmt.Errorf("could not create upperdir: %v", err)
}
os.Chmod(mnt.Upperdir, os.FileMode(0755))
if mnt.Workdir, err = ioutil.TempDir(mountDir, "work_"); err != nil {
if mnt.Workdir, err = ioutil.TempDir(MountDir, "work_"); err != nil {
l.WithError(err).Error("could not create workdir")
return nil, fmt.Errorf("could not create workdir: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/api/mount_rbd.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ func (m *MountsRBDType) Mount(mnt *models.MountRbd) (ret *models.MountRbd, err e
// HERE!!!!
// ok, we're good to attempt the mount
// make a mountpoint
if err = os.MkdirAll(mountDir, 0700); err != nil {
if err = os.MkdirAll(MountDir, 0700); err != nil {
l.WithError(err).Error("failed to make mount directory")
return nil, fmt.Errorf("could not create base mount directory: %v", err)
}
if mnt.Mountpoint, err = ioutil.TempDir(mountDir, "mount_"); err != nil {
if mnt.Mountpoint, err = ioutil.TempDir(MountDir, "mount_"); err != nil {
l.WithError(err).Error("failed to make pointpoint")
return nil, fmt.Errorf("could not create mountpoint: %v", err)
}
Expand Down

0 comments on commit 023b5df

Please # to comment.