github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/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  	PPC64 = "ppc64"
    19  )
    20  
    21  // AllSupportedArches records the machine architectures recognised by Juju.
    22  var AllSupportedArches = []string{
    23  	AMD64,
    24  	I386,
    25  	ARM,
    26  	ARM64,
    27  	PPC64,
    28  }
    29  
    30  // Info records the information regarding each architecture recognised by Juju.
    31  var Info = map[string]ArchInfo{
    32  	AMD64: {64},
    33  	I386:  {32},
    34  	ARM:   {32},
    35  	ARM64: {64},
    36  	PPC64: {64},
    37  }
    38  
    39  // ArchInfo is a struct containing information about a supported architecture.
    40  type ArchInfo struct {
    41  	// WordSize is the architecture's word size, in bits.
    42  	WordSize int
    43  }
    44  
    45  // archREs maps regular expressions for matching
    46  // `uname -m` to architectures recognised by Juju.
    47  var archREs = []struct {
    48  	*regexp.Regexp
    49  	arch string
    50  }{
    51  	{regexp.MustCompile("amd64|x86_64"), AMD64},
    52  	{regexp.MustCompile("i?[3-9]86"), I386},
    53  	{regexp.MustCompile("(arm$)|(armv.*)"), ARM},
    54  	{regexp.MustCompile("aarch64"), ARM64},
    55  	{regexp.MustCompile("ppc64el|ppc64le"), PPC64},
    56  }
    57  
    58  // Override for testing.
    59  var HostArch = hostArch
    60  
    61  // hostArch returns the Juju architecture of the machine on which it is run.
    62  func hostArch() string {
    63  	return NormaliseArch(runtime.GOARCH)
    64  }
    65  
    66  // NormaliseArch returns the Juju architecture corresponding to a machine's
    67  // reported architecture. The Juju architecture is used to filter simple
    68  // streams lookup of tools and images.
    69  func NormaliseArch(rawArch string) string {
    70  	rawArch = strings.TrimSpace(rawArch)
    71  	for _, re := range archREs {
    72  		if re.Match([]byte(rawArch)) {
    73  			return re.arch
    74  		}
    75  	}
    76  	return rawArch
    77  }
    78  
    79  // IsSupportedArch returns true if arch is one supported by Juju.
    80  func IsSupportedArch(arch string) bool {
    81  	for _, a := range AllSupportedArches {
    82  		if a == arch {
    83  			return true
    84  		}
    85  	}
    86  	return false
    87  }