The following pull request was submitted through Github. It can be accessed and reviewed at: https://github.com/lxc/lxd/pull/6410
This e-mail was sent by the LXC bot, direct replies will not reach the author unless they happen to be subscribed to this list. === Description (from pull-request) === This adds support for a nil state which may occur when using the lxd-agent. Signed-off-by: Thomas Hipp <thomas.h...@canonical.com>
From 03c42aaaabf173b7eea7a371a71795bcefea30f6 Mon Sep 17 00:00:00 2001 From: Thomas Hipp <thomas.h...@canonical.com> Date: Wed, 6 Nov 2019 15:46:37 +0100 Subject: [PATCH] lxd/operations: Support nil state This adds support for a nil state which may occur when using the lxd-agent. Signed-off-by: Thomas Hipp <thomas.h...@canonical.com> --- lxd/operations/operations.go | 62 +++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/lxd/operations/operations.go b/lxd/operations/operations.go index e9fba58d30..dfe768341a 100644 --- a/lxd/operations/operations.go +++ b/lxd/operations/operations.go @@ -157,21 +157,31 @@ func OperationCreate(state *state.State, project string, opClass operationClass, operations[op.id] = &op operationsLock.Unlock() - err = op.state.Cluster.Transaction(func(tx *db.ClusterTx) error { - _, err := tx.OperationAdd(project, op.id, opType) - return err - }) - if err != nil { - return nil, errors.Wrapf(err, "failed to add Operation %s to database", op.id) + if op.state != nil { + err = op.state.Cluster.Transaction(func(tx *db.ClusterTx) error { + _, err := tx.OperationAdd(project, op.id, opType) + return err + }) + if err != nil { + return nil, errors.Wrapf(err, "failed to add Operation %s to database", op.id) + } } logger.Debugf("New %s Operation: %s", op.class.String(), op.id) _, md, _ := op.Render() - op.state.Events.Send(op.project, "operation", md) + op.sendEvent(md) return &op, nil } +func (op *Operation) sendEvent(eventMessage interface{}) { + if op.state == nil { + return + } + + op.state.Events.Send(op.project, "operation", eventMessage) +} + func (op *Operation) done() { if op.readonly { return @@ -196,6 +206,10 @@ func (op *Operation) done() { delete(operations, op.id) operationsLock.Unlock() + if op.state == nil { + return + } + err := op.state.Cluster.Transaction(func(tx *db.ClusterTx) error { return tx.OperationRemove(op.id) }) @@ -231,7 +245,7 @@ func (op *Operation) Run() (chan error, error) { logger.Debugf("Failure for %s operation: %s: %s", op.class.String(), op.id, err) _, md, _ := op.Render() - op.state.Events.Send(op.project, "operation", md) + op.sendEvent(md) return } @@ -244,7 +258,7 @@ func (op *Operation) Run() (chan error, error) { op.lock.Lock() logger.Debugf("Success for %s operation: %s", op.class.String(), op.id) _, md, _ := op.Render() - op.state.Events.Send(op.project, "operation", md) + op.sendEvent(md) op.lock.Unlock() }(op, chanRun) } @@ -252,8 +266,7 @@ func (op *Operation) Run() (chan error, error) { logger.Debugf("Started %s operation: %s", op.class.String(), op.id) _, md, _ := op.Render() - op.state.Events.Send(op.project, "operation", md) - + op.sendEvent(md) return chanRun, nil } @@ -286,7 +299,7 @@ func (op *Operation) Cancel() (chan error, error) { logger.Debugf("Failed to cancel %s Operation: %s: %s", op.class.String(), op.id, err) _, md, _ := op.Render() - op.state.Events.Send(op.project, "operation", md) + op.sendEvent(md) return } @@ -298,13 +311,13 @@ func (op *Operation) Cancel() (chan error, error) { logger.Debugf("Cancelled %s Operation: %s", op.class.String(), op.id) _, md, _ := op.Render() - op.state.Events.Send(op.project, "operation", md) + op.sendEvent(md) }(op, oldStatus, chanCancel) } logger.Debugf("Cancelling %s Operation: %s", op.class.String(), op.id) _, md, _ := op.Render() - op.state.Events.Send(op.project, "operation", md) + op.sendEvent(md) if op.canceler != nil { err := op.canceler.Cancel() @@ -323,7 +336,7 @@ func (op *Operation) Cancel() (chan error, error) { logger.Debugf("Cancelled %s Operation: %s", op.class.String(), op.id) _, md, _ = op.Render() - op.state.Events.Send(op.project, "operation", md) + op.sendEvent(md) return chanCancel, nil } @@ -398,12 +411,15 @@ func (op *Operation) Render() (string, *api.Operation, error) { // Local server name var err error var serverName string - err = op.state.Cluster.Transaction(func(tx *db.ClusterTx) error { - serverName, err = tx.NodeName() - return err - }) - if err != nil { - return "", nil, err + + if op.state != nil { + err = op.state.Cluster.Transaction(func(tx *db.ClusterTx) error { + serverName, err = tx.NodeName() + return err + }) + if err != nil { + return "", nil, err + } } return op.url, &api.Operation{ @@ -469,7 +485,7 @@ func (op *Operation) UpdateResources(opResources map[string][]string) error { logger.Debugf("Updated resources for %s Operation: %s", op.class.String(), op.id) _, md, _ := op.Render() - op.state.Events.Send(op.project, "operation", md) + op.sendEvent(md) return nil } @@ -497,7 +513,7 @@ func (op *Operation) UpdateMetadata(opMetadata interface{}) error { logger.Debugf("Updated metadata for %s Operation: %s", op.class.String(), op.id) _, md, _ := op.Render() - op.state.Events.Send(op.project, "operation", md) + op.sendEvent(md) return nil }
_______________________________________________ lxc-devel mailing list lxc-devel@lists.linuxcontainers.org http://lists.linuxcontainers.org/listinfo/lxc-devel