daemon.go 6.2 KB
Newer Older
J
jingxiaolu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
// Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
// isula-build licensed under the Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//     http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
// See the Mulan PSL v2 for more details.
// Author: iSula Team
// Create: 2020-01-20
// Description: This file is used for daemon setting

// Package daemon is used for isula-build daemon
package daemon

import (
	"context"
	"os"
	"path/filepath"
	"sync"
	"time"

	"github.com/containerd/containerd/sys"
	"github.com/gofrs/flock"
	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"
	"golang.org/x/sys/unix"

	pb "isula.org/isula-build/api/services"
	"isula.org/isula-build/builder"
	"isula.org/isula-build/pkg/gc"
	"isula.org/isula-build/pkg/stack"
	"isula.org/isula-build/pkg/systemd"
	"isula.org/isula-build/store"
	"isula.org/isula-build/util"
)

const lockFileName = "isula-builder.lock"

// Options carries the options configured to daemon
type Options struct {
	ConfigFile    string
	Debug         bool
	LogLevel      string
	DataRoot      string
	RunRoot       string
	StorageDriver string
	StorageOpts   []string
	RuntimePath   string
}

// Daemon struct carries the main contents in daemon
type Daemon struct {
	sync.RWMutex
	opts       *Options
	builders   map[string]builder.Builder
	backend    *Backend
	grpc       *GrpcServer
	localStore store.Store
}

// NewDaemon new a daemon instance
func NewDaemon(opts Options, store store.Store) *Daemon {
	return &Daemon{
		opts:       &opts,
		builders:   make(map[string]builder.Builder),
		localStore: store,
	}
}

// Run runs the daemon process
func (d *Daemon) Run() error {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	gc := gc.NewGC()
	gc.StartGC(ctx)

H
holyfei 已提交
79 80 81 82
	if err := d.registerSubReaper(gc); err != nil {
		return err
	}

J
jingxiaolu 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
	logrus.Debugf("Daemon start with option %#v", d.opts)

	// Ensure we have only one daemon running at the same time
	lock, err := setDaemonLock(d.opts.RunRoot, lockFileName)
	if err != nil {
		return err
	}
	defer func() {
		if uerr := lock.Unlock(); uerr != nil {
			logrus.Errorf("Unlock file %s failed: %v", lock.Path(), uerr)
		} else if rerr := os.RemoveAll(lock.Path()); rerr != nil {
			logrus.Errorf("Remove lock file %s failed: %v", lock.Path(), rerr)
		}
	}()

	stack.Setup(d.opts.RunRoot)

	d.NewBackend()

	if err = d.NewGrpcServer(); err != nil {
		return err
	}
	d.backend.Register(d.grpc.server)
	// after the daemon is done setting up we can notify systemd api
	systemd.NotifySystemReady()

	errCh := make(chan error)
	if err = d.grpc.Run(ctx, errCh, cancel); err != nil {
		logrus.Error("Running GRPC server failed: ", err)
	}

	select {
	case serverErr, ok := <-errCh:
		if !ok {
			logrus.Errorf("Channel errCh closed, check grpc server err")
		}
		err = serverErr
		cancel()
	// channel closed is what we expected since it's daemon normal behavior
	case <-ctx.Done():
		logrus.Infof("Context finished with: %v", ctx.Err())
	}

	systemd.NotifySystemStopping()
	d.grpc.server.GracefulStop()
	return err
}

// NewBuilder returns the builder with request sent from GRPC service
func (d *Daemon) NewBuilder(ctx context.Context, req *pb.BuildRequest) (b builder.Builder, err error) {
	// buildDir is used to set directory which is used to store data
	buildDir := filepath.Join(d.opts.DataRoot, req.BuildID)
	// runDir is used to store such as container bundle directories
	runDir := filepath.Join(d.opts.RunRoot, req.BuildID)

	// this key with BuildDir will be used by exporter to save blob temporary
	// NOTE: keep it be updated before NewBuilder. ctx will be taken by Builder
	ctx = context.WithValue(ctx, util.BuildDirKey(util.BuildDir), buildDir)
	b, err = builder.NewBuilder(ctx, d.localStore, req, d.opts.RuntimePath, buildDir, runDir)
	if err != nil {
		return nil, errors.Wrap(err, "failed to new builder")
	}

	d.Lock()
	d.builders[req.BuildID] = b
	d.Unlock()

	return b, nil
}

// Builder returns an Builder to caller. Caller should check the return value if it is nil
func (d *Daemon) Builder(buildID string) (builder.Builder, error) {
	d.RLock()
	defer d.RUnlock()
	if _, ok := d.builders[buildID]; !ok {
		return nil, errors.Errorf("could not find builder with build job %s", buildID)
	}
	return d.builders[buildID], nil
}

// deleteBuilder deletes builder from daemon
func (d *Daemon) deleteBuilder(buildID string) {
	d.Lock()
	delete(d.builders, buildID)
	d.Unlock()
}

// deleteAllBuilders deletes all Builders stored in daemon
func (d *Daemon) deleteAllBuilders() {
	d.Lock()
	d.builders = make(map[string]builder.Builder)
	d.Unlock()
}

// Cleanup cleans the resource
func (d *Daemon) Cleanup() error {
	if d.backend != nil {
		d.backend.deleteAllStatus()
	}
	d.deleteAllBuilders()
	d.localStore.CleanContainerStore()
	_, err := d.localStore.Shutdown(false)
	return err
}

H
holyfei 已提交
188
func (d *Daemon) registerSubReaper(g *gc.GarbageCollector) error {
J
jingxiaolu 已提交
189 190 191 192
	if err := unix.Prctl(unix.PR_SET_CHILD_SUBREAPER, uintptr(1), 0, 0, 0); err != nil { //nolint, gomod
		return errors.Errorf("set subreaper failed: %v", err)
	}

H
holyfei 已提交
193 194 195 196 197 198 199
	childProcessReap := func(i interface{}) error {
		var err error

		daemonTmp := i.(*Daemon)
		daemonTmp.Lock()
		defer daemonTmp.Unlock()

J
jingxiaolu 已提交
200
		// if any of image build process is running, skip reap
H
holyfei 已提交
201 202
		if len(daemonTmp.builders) != 0 {
			return nil
J
jingxiaolu 已提交
203 204 205 206
		}
		if _, err = sys.Reap(false); err != nil {
			logrus.Errorf("Reap child process error: %v", err)
		}
H
holyfei 已提交
207
		return err
J
jingxiaolu 已提交
208
	}
H
holyfei 已提交
209 210 211 212 213 214 215 216 217

	opt := &gc.RegisterOption{
		Name:        "subReaper",
		Interval:    10 * time.Second,
		RecycleData: d,
		RecycleFunc: childProcessReap,
	}

	return g.RegisterGC(opt)
J
jingxiaolu 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
}

// setDaemonLock will check if there is another daemon running and return error if any
func setDaemonLock(root, fileName string) (*flock.Flock, error) {
	lockPath := filepath.Join(root, fileName)
	lock := flock.New(lockPath)
	locked, err := lock.TryLock()
	if err != nil {
		return nil, errors.Wrapf(err, "could not lock %s", lockPath)
	}
	if !locked {
		return nil, errors.Errorf("lock %s failed, check if there is another daemon running", lockPath)
	}
	return lock, nil
}