Files
snapd/overlord/patch/patch5.go
Philip Meulengracht a199598051 o/servicestate,wrappers: handle quota changes on a service level instead of on a snap level (2/3) (#12395)
* o/servicestate,wrappers: handle services individually to avoid affecting all services in cases where you modify a group with only one service.

* o/servicestate: add unit tests for AffectedSnapServices to make it easier to verify the new behavior

* o/servicestate,wrappers: fix failing unit tests, fix a logic mistake in MakeServiceQuotaMap

* multiple: a bit of code cleanup and missing doc

* multiple: i hopefully stop misspelling separate

* o/ifacestate: pass snap.Info instead of instance name to avoid doing a lookup

* o/ifacestate: pass snap.Info instead of instance name to avoid doing a lookup

* multiple: filter services out instead of passing the relevant services to SnapServiceOptions. Also update doc comment

* o/servicestate: rename filterApps to filterSnapServices

* o/snapstate/backend: change usage to EnsureSnapServices to avoid needless copying

* multiple: review feedback

rename SnapServiceOptions to SnapServicesOptions. And rename FindQuotaGroupForServiceInSubGroups to GroupForService.

* many: review feedback

Move MakeServiceQuotaMap, remove usage of AddSnapServices and move it into services_test.go instead where it can be used as a test helper instead. SnapServicesOptions is now requiredx

* multiple: simplify arguments to MakeServiceQuotaMap

* o/snapstate: rename function pointer as well

* wrappers,o/servicestate: only build a map of services that has a quota group

* o/snapstate/backend: fix unit tests in link_test.go

* wrappers,overlord: refactor AddSnapServiceOptions into a new structure, and remove the original usage. Remove some earlier changes requiring SnapServicesOptions to be provided, no longer necessary with the changes done to the service map

* root: remove log.txt

* o/patch: set nil SnapServicesOptions

* wrappers,o/servicestate: Add unit test that test across layers to verify correct service files are touched when we change a service group.
Change the approach on how we filter services in wrappers, we now provide a filter list, which if provided, controls which services are generated.

* review feedback: allow nil entries in the service map, verify the quota groups provided are sane, add unit tests

* multiple: move MakeServiceQuotaMap to snap/quota, and add unit tests

* snap/quota: no need for BaseTest :-)

* multiple: remove the ServiceQuotaMap as the wrappers layer has all the information available anyway.

* many: unrename SnapServiceOptions

* multiple: more code cleanup

* multiple: review feedback

replace GroupForService with ServiceMap that provides us with a map of group replacements, and then replace usage of GroupForService with this. Update a couple doc strings.

* snap/quota: restructure the unit tests a bit

* o/servicestate: fix test that started failing. It was missing systemctl calls for the service group.

* wrappers: review feedback

correct doc string for QuotaGroup in generateSnapServicesOptions

* wrappers,o/servicestate: review feedback

small cleanup, use sort.Strings() instead of sort.Slice. Undo some whiteline changes. Use strings.SplitN instead of split
2022-12-15 11:41:26 +01:00

91 lines
2.0 KiB
Go

// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package patch
import (
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/timings"
"github.com/snapcore/snapd/wrappers"
)
func init() {
patches[5] = []PatchFunc{patch5}
}
type log struct{}
func (log) Notify(status string) {
logger.Noticef("patch 5: %s", status)
}
// patch5:
// - regenerate generated .service files
func patch5(st *state.State) error {
log := log{}
snapStates, err := snapstate.All(st)
if err != nil {
return err
}
// create timings to satisfy StartServices/StopServices API, but don't save them
tm := timings.New(nil)
for snapName, snapst := range snapStates {
if !snapst.Active {
continue
}
info, err := snapst.CurrentInfo()
if err != nil {
return err
}
svcs := info.Services()
if len(svcs) == 0 {
logger.Debugf("patch 5: skipping for %q: no services", snapName)
continue
}
err = wrappers.StopServices(svcs, nil, snap.StopReasonRefresh, log, tm)
if err != nil {
return err
}
err = wrappers.EnsureSnapServices(map[*snap.Info]*wrappers.SnapServiceOptions{
info: nil,
}, nil, nil, log)
if err != nil {
return err
}
err = wrappers.StartServices(svcs, nil, nil, log, tm)
if err != nil {
return err
}
logger.Noticef("patch 5: %q updated", snapName)
}
return nil
}