github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/juju/arch/arch.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package arch
     5  
     6  import (
     7  	"regexp"
     8  	"runtime"
     9  	"strings"
    10  )
    11  
    12  // The following constants define the machine architectures supported by Juju.
    13  const (
    14  	AMD64   = "amd64"
    15  	I386    = "i386"
    16  	ARM     = "armhf"
    17  	ARM64   = "arm64"
    18  	PPC64EL = "ppc64el"
    19  
    20  	// Older versions of Juju used "ppc64" instead of ppc64el
    21  	LEGACY_PPC64 = "ppc64"
    22  )
    23  
    24  // AllSupportedArches records the machine architectures recognised by Juju.
    25  var AllSupportedArches = []string{
    26  	AMD64,
    27  	I386,
    28  	ARM,
    29  	ARM64,
    30  	PPC64EL,
    31  }
    32  
    33  // Info records the information regarding each architecture recognised by Juju.
    34  var Info = map[string]ArchInfo{
    35  	AMD64:   {64},
    36  	I386:    {32},
    37  	ARM:     {32},
    38  	ARM64:   {64},
    39  	PPC64EL: {64},
    40  }
    41  
    42  // ArchInfo is a struct containing information about a supported architecture.
    43  type ArchInfo struct {
    44  	// WordSize is the architecture's word size, in bits.
    45  	WordSize int
    46  }
    47  
    48  // archREs maps regular expressions for matching
    49  // `uname -m` to architectures recognised by Juju.
    50  var archREs = []struct {
    51  	*regexp.Regexp
    52  	arch string
    53  }{
    54  	{regexp.MustCompile("amd64|x86_64"), AMD64},
    55  	{regexp.MustCompile("i?[3-9]86"), I386},
    56  	{regexp.MustCompile("(arm$)|(armv.*)"), ARM},
    57  	{regexp.MustCompile("aarch64"), ARM64},
    58  	{regexp.MustCompile("ppc64|ppc64el|ppc64le"), PPC64EL},
    59  }
    60  
    61  // Override for testing.
    62  var HostArch = hostArch
    63  
    64  // hostArch returns the Juju architecture of the machine on which it is run.
    65  func hostArch() string {
    66  	return NormaliseArch(runtime.GOARCH)
    67  }
    68  
    69  // NormaliseArch returns the Juju architecture corresponding to a machine's
    70  // reported architecture. The Juju architecture is used to filter simple
    71  // streams lookup of tools and images.
    72  func NormaliseArch(rawArch string) string {
    73  	rawArch = strings.TrimSpace(rawArch)
    74  	for _, re := range archREs {
    75  		if re.Match([]byte(rawArch)) {
    76  			return re.arch
    77  		}
    78  	}
    79  	return rawArch
    80  }
    81  
    82  // IsSupportedArch returns true if arch is one supported by Juju.
    83  func IsSupportedArch(arch string) bool {
    84  	for _, a := range AllSupportedArches {
    85  		if a == arch {
    86  			return true
    87  		}
    88  	}
    89  	return false
    90  }