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

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 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 patch
    21  
    22  import (
    23  	"github.com/snapcore/snapd/logger"
    24  	"github.com/snapcore/snapd/overlord/snapstate"
    25  	"github.com/snapcore/snapd/overlord/state"
    26  	"github.com/snapcore/snapd/snap"
    27  	"github.com/snapcore/snapd/timings"
    28  	"github.com/snapcore/snapd/wrappers"
    29  )
    30  
    31  func init() {
    32  	patches[5] = []PatchFunc{patch5}
    33  }
    34  
    35  type log struct{}
    36  
    37  func (log) Notify(status string) {
    38  	logger.Noticef("patch 5: %s", status)
    39  }
    40  
    41  // patch5:
    42  //  - regenerate generated .service files
    43  func patch5(st *state.State) error {
    44  	log := log{}
    45  
    46  	snapStates, err := snapstate.All(st)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	// create timings to satisfy StartServices/StopServices API, but don't save them
    52  	tm := timings.New(nil)
    53  	for snapName, snapst := range snapStates {
    54  		if !snapst.Active {
    55  			continue
    56  		}
    57  
    58  		info, err := snapst.CurrentInfo()
    59  		if err != nil {
    60  			return err
    61  		}
    62  
    63  		svcs := info.Services()
    64  		if len(svcs) == 0 {
    65  			logger.Debugf("patch 5: skipping for %q: no services", snapName)
    66  			continue
    67  		}
    68  
    69  		err = wrappers.StopServices(svcs, nil, snap.StopReasonRefresh, log, tm)
    70  		if err != nil {
    71  			return err
    72  		}
    73  
    74  		err = wrappers.AddSnapServices(info, nil, nil, log)
    75  		if err != nil {
    76  			return err
    77  		}
    78  
    79  		err = wrappers.StartServices(svcs, nil, nil, log, tm)
    80  		if err != nil {
    81  			return err
    82  		}
    83  
    84  		logger.Noticef("patch 5: %q updated", snapName)
    85  	}
    86  
    87  	return nil
    88  }