github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/overlord/snapstate/devicectx.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018-2019 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package snapstate
    21  
    22  import (
    23  	"github.com/snapcore/snapd/asserts"
    24  	"github.com/snapcore/snapd/boot"
    25  	"github.com/snapcore/snapd/overlord/state"
    26  )
    27  
    28  // A DeviceContext provides for operating as a given device and with
    29  // its brand store either for normal operation or over a remodeling.
    30  type DeviceContext interface {
    31  	// Model returns the governing device model assertion for the context.
    32  	Model() *asserts.Model
    33  
    34  	// GroundContext returns a context corresponding to the
    35  	// original model of the device for a remodel, or a context
    36  	// equivalent to this one otherwise, except in both cases
    37  	// Store cannot be used and must panic.
    38  	GroundContext() DeviceContext
    39  
    40  	// Store returns the store service to use under this context or nil if the snapstate store is appropriate.
    41  	Store() StoreService
    42  
    43  	// ForRemodeling returns whether this context is for use over a remodeling.
    44  	ForRemodeling() bool
    45  
    46  	// SystemMode returns the system  mode (run,install,recover,...).
    47  	SystemMode() string
    48  
    49  	// DeviceContext should be usable as boot.Device
    50  	boot.Device
    51  }
    52  
    53  // Hook setup by devicestate to pick a device context from state,
    54  // optional task or an optionally pre-provided one. It's expected to
    55  // return ErrNoState if a model assertion is not yet known.
    56  var (
    57  	DeviceCtx func(st *state.State, task *state.Task, providedDeviceCtx DeviceContext) (DeviceContext, error)
    58  )
    59  
    60  // Hook setup by devicestate to know whether a remodeling is in progress.
    61  var (
    62  	Remodeling func(st *state.State) bool
    63  )
    64  
    65  // ModelFromTask returns a model assertion through the device context for the task.
    66  func ModelFromTask(task *state.Task) (*asserts.Model, error) {
    67  	deviceCtx, err := DeviceCtx(task.State(), task, nil)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	return deviceCtx.Model(), nil
    72  }
    73  
    74  // DevicePastSeeding returns a device context if a model assertion is
    75  // available and the device is seeded, at that point the device store
    76  // is known and seeding done. Otherwise it returns a
    77  // ChangeConflictError about being too early unless a pre-provided
    78  // DeviceContext is passed in. It will again return a conflict error
    79  // during remodeling unless the providedDeviceCtx is for it.
    80  func DevicePastSeeding(st *state.State, providedDeviceCtx DeviceContext) (DeviceContext, error) {
    81  	var seeded bool
    82  	err := st.Get("seeded", &seeded)
    83  	if err != nil && err != state.ErrNoState {
    84  		return nil, err
    85  	}
    86  	if Remodeling(st) {
    87  		// a remodeling is in progress and this is not called
    88  		// as part of it. The 2nd check should not be needed
    89  		// in practice.
    90  		if providedDeviceCtx == nil || !providedDeviceCtx.ForRemodeling() {
    91  			return nil, &ChangeConflictError{
    92  				Message: "remodeling in progress, no other " +
    93  					"changes allowed until this is done",
    94  				ChangeKind: "remodel",
    95  			}
    96  		}
    97  	}
    98  	devCtx, err := DeviceCtx(st, nil, providedDeviceCtx)
    99  	if err != nil && err != state.ErrNoState {
   100  		return nil, err
   101  	}
   102  	// when seeded devCtx should not be nil except in the rare
   103  	// case of upgrades from a snapd before the introduction of
   104  	// the fallback generic/generic-classic model
   105  	if !seeded || devCtx == nil {
   106  		return nil, &ChangeConflictError{
   107  			Message: "too early for operation, device not yet" +
   108  				" seeded or device model not acknowledged",
   109  			ChangeKind: "seed",
   110  		}
   111  	}
   112  
   113  	return devCtx, nil
   114  }
   115  
   116  // DeviceCtxFromState returns a device context if a model assertion is
   117  // available. Otherwise it returns a ChangeConflictError about being
   118  // too early unless an pre-provided DeviceContext is passed in.
   119  func DeviceCtxFromState(st *state.State, providedDeviceCtx DeviceContext) (DeviceContext, error) {
   120  	deviceCtx, err := DeviceCtx(st, nil, providedDeviceCtx)
   121  	if err != nil {
   122  		if err == state.ErrNoState {
   123  			return nil, &ChangeConflictError{
   124  				Message:    "too early for operation, device model not yet acknowledged",
   125  				ChangeKind: "seed",
   126  			}
   127  		}
   128  		return nil, err
   129  	}
   130  	return deviceCtx, nil
   131  }