gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/overlord/hookstate/ctlcmd/system_mode.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2021 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 ctlcmd
    21  
    22  import (
    23  	"gopkg.in/yaml.v2"
    24  
    25  	"github.com/snapcore/snapd/i18n"
    26  	"github.com/snapcore/snapd/overlord/devicestate"
    27  	"github.com/snapcore/snapd/strutil"
    28  )
    29  
    30  type systemModeCommand struct {
    31  	baseCommand
    32  }
    33  
    34  var shortSystemModeHelp = i18n.G("Get the current system mode and associated details")
    35  
    36  var longSystemModeHelp = i18n.G(`
    37  The system-mode command returns information about the device's current system mode.
    38  
    39  This information includes the mode itself and whether the model snaps have been installed from the seed (seed-loaded). The system mode is either run, recover, or install.
    40  
    41  Retrieved information can also include "factory mode" details: 'factory: true' declares whether the device booted an image flagged as for factory use. This flag can be set for convenience when building the image. No security sensitive decisions should be based on this bit alone.
    42  
    43  The output is in YAML format. Example output:
    44      $ snapctl system-mode
    45      system-mode: install
    46      seed-loaded: true
    47      factory: true
    48  `)
    49  
    50  func init() {
    51  	addCommand("system-mode", shortSystemModeHelp, longSystemModeHelp, func() command { return &systemModeCommand{} })
    52  }
    53  
    54  var devicestateSystemModeInfoFromState = devicestate.SystemModeInfoFromState
    55  
    56  type systemModeResult struct {
    57  	SystemMode string `yaml:"system-mode,omitempty"`
    58  	Seeded     bool   `yaml:"seed-loaded"`
    59  	Factory    bool   `yaml:"factory,omitempty"`
    60  }
    61  
    62  func (c *systemModeCommand) Execute(args []string) error {
    63  	context, err := c.ensureContext()
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	st := context.State()
    69  	st.Lock()
    70  	defer st.Unlock()
    71  
    72  	smi, err := devicestateSystemModeInfoFromState(st)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	res := systemModeResult{
    78  		SystemMode: smi.Mode,
    79  		Seeded:     smi.Seeded,
    80  	}
    81  	if strutil.ListContains(smi.BootFlags, "factory") {
    82  		res.Factory = true
    83  	}
    84  
    85  	b, err := yaml.Marshal(res)
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	c.printf("%s", string(b))
    91  
    92  	return nil
    93  }