github.com/flanksource/konfigadm@v0.12.0/pkg/phases/os.go (about)

     1  package phases
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/flanksource/konfigadm/pkg/types"
     8  )
     9  
    10  //OS provides an abstraction over different operating systems
    11  type OS interface {
    12  
    13  	// GetVersionCodeName returns the distributions version codename e.g. bionic, xenial, squeeze
    14  	GetVersionCodeName() string
    15  
    16  	//GetPackageManager returns the packagemanager used by the OS
    17  	GetPackageManager() types.PackageManager
    18  
    19  	//GetTags returns all the tags to which this OS applies
    20  	GetTags() []types.Flag
    21  
    22  	GetName() string
    23  
    24  	//DetectAtRuntime will detect if it is compatible with the current running OS
    25  	DetectAtRuntime() bool
    26  
    27  	// Returns the names of the kernel and kernel header packages
    28  	GetKernelPackageNames(version string) (string, string)
    29  
    30  	// Calls the version specific grub config logic
    31  	UpdateDefaultKernel(version string) types.Commands
    32  
    33  	// Check whether defaul kernel matches specific version
    34  	ReadDefaultKernel() string
    35  }
    36  
    37  type OperatingSystemList []OS
    38  
    39  //SupportedOperatingSystems is a list of all supported OS's, used primarily for detecting runtime flags
    40  var SupportedOperatingSystems = OperatingSystemList{
    41  	Debian,
    42  	Redhat,
    43  	Ubuntu,
    44  	AmazonLinux,
    45  	RedhatEnterprise,
    46  	Centos,
    47  	Fedora,
    48  	Photon,
    49  }
    50  
    51  var OperatingSystems = map[string]OS{
    52  	"ubuntu":      Ubuntu,
    53  	"debian":      Debian,
    54  	"redhat":      Redhat,
    55  	"amazonLinux": AmazonLinux,
    56  	"centos":      Centos,
    57  	"fedora":      Fedora,
    58  	"photon":      Photon,
    59  }
    60  
    61  //BaseOperatingSystems is the list of base distributions that are supported, which is currently only debian and redhat
    62  var BaseOperatingSystems = OperatingSystemList{
    63  	Debian,
    64  	Redhat,
    65  	Fedora,
    66  	Photon,
    67  }
    68  
    69  func GetOSForTag(tags ...types.Flag) (OS, error) {
    70  	for _, t := range tags {
    71  		for _, os := range SupportedOperatingSystems {
    72  			if strings.HasPrefix(t.Name, os.GetName()) {
    73  				return os, nil
    74  			}
    75  		}
    76  	}
    77  	return nil, fmt.Errorf("Unable to find OS for %s", tags)
    78  }
    79  
    80  //Detect returns a list of all compatible operating systems at runtime
    81  func (l OperatingSystemList) Detect() []OS {
    82  	var list []OS
    83  	for _, os := range l {
    84  		if os.DetectAtRuntime() {
    85  			list = append(list, os)
    86  		}
    87  	}
    88  	return list
    89  
    90  }