github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/overlord/configstate/configcore/experimental.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 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 configcore
    21  
    22  import (
    23  	"os"
    24  	"strings"
    25  
    26  	"github.com/snapcore/snapd/dirs"
    27  	"github.com/snapcore/snapd/features"
    28  	"github.com/snapcore/snapd/osutil"
    29  	"github.com/snapcore/snapd/overlord/configstate/config"
    30  	"github.com/snapcore/snapd/sysconfig"
    31  )
    32  
    33  func init() {
    34  	for _, feature := range features.KnownFeatures() {
    35  		snapName, confName := feature.ConfigOption()
    36  		supportedConfigurations[snapName+"."+confName] = true
    37  	}
    38  }
    39  
    40  func earlyExperimentalSettingsFilter(values, early map[string]interface{}) {
    41  	for key, v := range values {
    42  		if strings.HasPrefix(key, "experimental.") && supportedConfigurations["core."+key] {
    43  			early[key] = v
    44  		}
    45  	}
    46  }
    47  
    48  func validateExperimentalSettings(tr config.ConfGetter) error {
    49  	for k := range supportedConfigurations {
    50  		if !strings.HasPrefix(k, "core.experimental.") {
    51  			continue
    52  		}
    53  		if err := validateBoolFlag(tr, strings.TrimPrefix(k, "core.")); err != nil {
    54  			return err
    55  		}
    56  	}
    57  	return nil
    58  }
    59  
    60  func doExportExperimentalFlags(_ sysconfig.Device, tr config.ConfGetter, opts *fsOnlyContext) error {
    61  	var dir string
    62  	if opts != nil {
    63  		dir = dirs.FeaturesDirUnder(opts.RootDir)
    64  	} else {
    65  		dir = dirs.FeaturesDir
    66  	}
    67  	if err := os.MkdirAll(dir, 0755); err != nil {
    68  		return err
    69  	}
    70  	feat := features.KnownFeatures()
    71  	content := make(map[string]osutil.FileState, len(feat))
    72  	for _, feature := range feat {
    73  		if !feature.IsExported() {
    74  			continue
    75  		}
    76  		isEnabled, err := features.Flag(tr, feature)
    77  		if err != nil {
    78  			return err
    79  		}
    80  		if isEnabled {
    81  			content[feature.String()] = &osutil.MemoryFileState{Mode: 0644}
    82  		}
    83  	}
    84  	_, _, err := osutil.EnsureDirState(dir, "*", content)
    85  	return err
    86  }
    87  
    88  func ExportExperimentalFlags(tr config.ConfGetter) error {
    89  	return doExportExperimentalFlags(nil, tr, nil)
    90  }