github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/interfaces/helpers.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 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 interfaces
    21  
    22  import (
    23  	"fmt"
    24  
    25  	"github.com/snapcore/snapd/snap"
    26  	"github.com/snapcore/snapd/timings"
    27  )
    28  
    29  // SetupMany generates profiles of snaps using either SetupMany() method of the security backend (if implemented), or Setup(). All errors are logged.
    30  // The return value indicates if all profiles were successfully generated.
    31  func SetupMany(repo *Repository, backend SecurityBackend, snaps []*snap.Info, confinementOpts func(snapName string) ConfinementOptions, tm timings.Measurer) []error {
    32  	var errors []error
    33  	// use .SetupMany() if implemented by the backend, otherwise fall back to .Setup()
    34  	if setupManyInterface, ok := backend.(SecurityBackendSetupMany); ok {
    35  		timings.Run(tm, "setup-security-backend[many]", fmt.Sprintf("setup security backend %q for %d snaps", backend.Name(), len(snaps)), func(nesttm timings.Measurer) {
    36  			errors = setupManyInterface.SetupMany(snaps, confinementOpts, repo, nesttm)
    37  		})
    38  	} else {
    39  		// For each snap:
    40  		for _, snapInfo := range snaps {
    41  			snapName := snapInfo.InstanceName()
    42  			// Compute confinement options
    43  			opts := confinementOpts(snapName)
    44  
    45  			// Refresh security of this snap and backend
    46  			timings.Run(tm, "setup-security-backend", fmt.Sprintf("setup security backend %q for snap %q", backend.Name(), snapInfo.InstanceName()), func(nesttm timings.Measurer) {
    47  				if err := backend.Setup(snapInfo, opts, repo, nesttm); err != nil {
    48  					errors = append(errors, err)
    49  				}
    50  			})
    51  		}
    52  	}
    53  	return errors
    54  }