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

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 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 servicestate
    21  
    22  import (
    23  	"fmt"
    24  
    25  	"github.com/snapcore/snapd/overlord/snapstate"
    26  	"github.com/snapcore/snapd/overlord/state"
    27  )
    28  
    29  // ServiceManager is responsible for starting and stopping snap services.
    30  type ServiceManager struct {
    31  	state *state.State
    32  }
    33  
    34  // Manager returns a new service manager.
    35  func Manager(st *state.State, runner *state.TaskRunner) *ServiceManager {
    36  	delayedCrossMgrInit()
    37  	m := &ServiceManager{
    38  		state: st,
    39  	}
    40  	// TODO: undo handler
    41  	runner.AddHandler("service-control", m.doServiceControl, nil)
    42  	return m
    43  }
    44  
    45  // Ensure implements StateManager.Ensure.
    46  func (m *ServiceManager) Ensure() error {
    47  	return nil
    48  }
    49  
    50  func delayedCrossMgrInit() {
    51  	// hook into conflict checks mechanisms
    52  	snapstate.AddAffectedSnapsByAttr("service-action", serviceControlAffectedSnaps)
    53  }
    54  
    55  func serviceControlAffectedSnaps(t *state.Task) ([]string, error) {
    56  	var serviceAction ServiceAction
    57  	if err := t.Get("service-action", &serviceAction); err != nil {
    58  		return nil, fmt.Errorf("internal error: cannot obtain service action from task: %s", t.Summary())
    59  	}
    60  	return []string{serviceAction.SnapName}, nil
    61  }