github.com/rigado/snapd@v2.42.5-go-mod+incompatible/seed/validate.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-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 seed
    21  
    22  import (
    23  	"bytes"
    24  	"fmt"
    25  	"path/filepath"
    26  
    27  	"github.com/snapcore/snapd/snap"
    28  )
    29  
    30  // ValidateFromYaml validates the given seed.yaml file and surrounding seed.
    31  func ValidateFromYaml(seedYamlFile string) error {
    32  	seed, err := ReadYaml(seedYamlFile)
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	var errs []error
    38  	// read the snaps info
    39  	snapInfos := make(map[string]*snap.Info)
    40  	for _, seedSnap := range seed.Snaps {
    41  		fn := filepath.Join(filepath.Dir(seedYamlFile), "snaps", seedSnap.File)
    42  		snapf, err := snap.Open(fn)
    43  		if err != nil {
    44  			errs = append(errs, err)
    45  		} else {
    46  			info, err := snap.ReadInfoFromSnapFile(snapf, nil)
    47  			if err != nil {
    48  				errs = append(errs, fmt.Errorf("cannot use snap %s: %v", fn, err))
    49  			} else {
    50  				snapInfos[info.InstanceName()] = info
    51  			}
    52  		}
    53  	}
    54  
    55  	// ensure we have either "core" or "snapd"
    56  	_, haveCore := snapInfos["core"]
    57  	_, haveSnapd := snapInfos["snapd"]
    58  	if !(haveCore || haveSnapd) {
    59  		errs = append(errs, fmt.Errorf("the core or snapd snap must be part of the seed"))
    60  	}
    61  
    62  	if errs2 := snap.ValidateBasesAndProviders(snapInfos); errs2 != nil {
    63  		errs = append(errs, errs2...)
    64  	}
    65  	if errs != nil {
    66  		var buf bytes.Buffer
    67  		for _, err := range errs {
    68  			fmt.Fprintf(&buf, "\n- %s", err)
    69  		}
    70  		return fmt.Errorf("cannot validate seed:%s", buf.Bytes())
    71  	}
    72  
    73  	return nil
    74  }