github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/compilers/names.go (about)

     1  package compilers
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/emc-advanced-dev/pkg/errors"
     8  )
     9  
    10  const (
    11  	Rump = "rump"
    12  )
    13  
    14  type CompilerType string
    15  
    16  func (c CompilerType) Base() string {
    17  	return strings.Split(string(c), "-")[0]
    18  }
    19  func (c CompilerType) Language() string {
    20  	return strings.Split(string(c), "-")[1]
    21  }
    22  func (c CompilerType) Provider() string {
    23  	return strings.Split(string(c), "-")[2]
    24  }
    25  func (c CompilerType) String() string {
    26  	return string(c)
    27  }
    28  
    29  var (
    30  	//available compilers
    31  	RUMP_C_XEN        = compilerName("rump", "c", "xen")
    32  	RUMP_C_AWS        = compilerName("rump", "c", "aws")
    33  	RUMP_C_VIRTUALBOX = compilerName("rump", "c", "virtualbox")
    34  	RUMP_C_VSPHERE    = compilerName("rump", "c", "vsphere")
    35  	RUMP_C_QEMU       = compilerName("rump", "c", "qemu")
    36  	RUMP_C_PHOTON     = compilerName("rump", "c", "photon")
    37  	RUMP_C_OPENSTACK  = compilerName("rump", "c", "openstack")
    38  
    39  	RUMP_GO_XEN        = compilerName("rump", "go", "xen")
    40  	RUMP_GO_AWS        = compilerName("rump", "go", "aws")
    41  	RUMP_GO_VIRTUALBOX = compilerName("rump", "go", "virtualbox")
    42  	RUMP_GO_VSPHERE    = compilerName("rump", "go", "vsphere")
    43  	RUMP_GO_QEMU       = compilerName("rump", "go", "qemu")
    44  	RUMP_GO_PHOTON     = compilerName("rump", "go", "photon")
    45  	RUMP_GO_OPENSTACK  = compilerName("rump", "go", "openstack")
    46  	RUMP_GO_GCLOUD     = compilerName("rump", "go", "gcloud")
    47  
    48  	RUMP_NODEJS_XEN        = compilerName("rump", "nodejs", "xen")
    49  	RUMP_NODEJS_AWS        = compilerName("rump", "nodejs", "aws")
    50  	RUMP_NODEJS_VIRTUALBOX = compilerName("rump", "nodejs", "virtualbox")
    51  	RUMP_NODEJS_VSPHERE    = compilerName("rump", "nodejs", "vsphere")
    52  	RUMP_NODEJS_QEMU       = compilerName("rump", "nodejs", "qemu")
    53  	RUMP_NODEJS_OPENSTACK  = compilerName("rump", "nodejs", "openstack")
    54  
    55  	RUMP_PYTHON_XEN        = compilerName("rump", "python", "xen")
    56  	RUMP_PYTHON_AWS        = compilerName("rump", "python", "aws")
    57  	RUMP_PYTHON_VIRTUALBOX = compilerName("rump", "python", "virtualbox")
    58  	RUMP_PYTHON_VSPHERE    = compilerName("rump", "python", "vsphere")
    59  	RUMP_PYTHON_QEMU       = compilerName("rump", "python", "qemu")
    60  	RUMP_PYTHON_OPENSTACK  = compilerName("rump", "python", "openstack")
    61  
    62  	RUMP_JAVA_XEN        = compilerName("rump", "java", "xen")
    63  	RUMP_JAVA_AWS        = compilerName("rump", "java", "aws")
    64  	RUMP_JAVA_VIRTUALBOX = compilerName("rump", "java", "virtualbox")
    65  	RUMP_JAVA_VSPHERE    = compilerName("rump", "java", "vsphere")
    66  	RUMP_JAVA_QEMU       = compilerName("rump", "java", "qemu")
    67  	RUMP_JAVA_OPENSTACK  = compilerName("rump", "java", "openstack")
    68  
    69  	OSV_JAVA_XEN        = compilerName("osv", "java", "xen")
    70  	OSV_JAVA_AWS        = compilerName("osv", "java", "aws")
    71  	OSV_JAVA_VIRTUALBOX = compilerName("osv", "java", "virtualbox")
    72  	OSV_JAVA_VSPHERE    = compilerName("osv", "java", "vsphere")
    73  	OSV_JAVA_QEMU       = compilerName("osv", "java", "qemu")
    74  	OSV_JAVA_OPENSTACK  = compilerName("osv", "java", "openstack")
    75  
    76  	OSV_NODEJS_QEMU      = compilerName("osv", "nodejs", "qemu")
    77  	OSV_NODEJS_OPENSTACK = compilerName("osv", "nodejs", "openstack")
    78  	OSV_NODEJS_AWS       = compilerName("osv", "nodejs", "aws")
    79  
    80  	OSV_NATIVE_QEMU      = compilerName("osv", "native", "qemu")
    81  	OSV_NATIVE_OPENSTACK = compilerName("osv", "native", "openstack")
    82  	OSV_NATIVE_AWS       = compilerName("osv", "native", "aws")
    83  
    84  	INCLUDEOS_CPP_QEMU       = compilerName("includeos", "cpp", "qemu")
    85  	INCLUDEOS_CPP_XEN        = compilerName("includeos", "cpp", "xen")
    86  	INCLUDEOS_CPP_VIRTUALBOX = compilerName("includeos", "cpp", "virtualbox")
    87  	INCLUDEOS_CPP_OPENSTACK  = compilerName("includeos", "cpp", "openstack")
    88  
    89  	MIRAGE_OCAML_XEN  = compilerName("mirage", "ocaml", "xen")
    90  	MIRAGE_OCAML_UKVM = compilerName("mirage", "ocaml", "ukvm")
    91  	MIRAGE_OCAML_QEMU = compilerName("mirage", "ocaml", "qemu")
    92  
    93  	FIRECRACKER_GO = compilerName("firecracker", "go", "firecracker")
    94  )
    95  
    96  var compilers = []CompilerType{
    97  	RUMP_C_XEN,
    98  	RUMP_C_AWS,
    99  	RUMP_C_VIRTUALBOX,
   100  	RUMP_C_VSPHERE,
   101  	RUMP_C_QEMU,
   102  	RUMP_C_PHOTON,
   103  	RUMP_C_OPENSTACK,
   104  
   105  	RUMP_GO_XEN,
   106  	RUMP_GO_AWS,
   107  	RUMP_GO_VIRTUALBOX,
   108  	RUMP_GO_VSPHERE,
   109  	RUMP_GO_QEMU,
   110  	RUMP_GO_PHOTON,
   111  	RUMP_GO_OPENSTACK,
   112  	RUMP_GO_GCLOUD,
   113  
   114  	RUMP_NODEJS_XEN,
   115  	RUMP_NODEJS_AWS,
   116  	RUMP_NODEJS_VIRTUALBOX,
   117  	RUMP_NODEJS_VSPHERE,
   118  	RUMP_NODEJS_QEMU,
   119  	RUMP_NODEJS_OPENSTACK,
   120  
   121  	RUMP_PYTHON_XEN,
   122  	RUMP_PYTHON_AWS,
   123  	RUMP_PYTHON_VIRTUALBOX,
   124  	RUMP_PYTHON_VSPHERE,
   125  	RUMP_PYTHON_QEMU,
   126  	RUMP_PYTHON_OPENSTACK,
   127  
   128  	RUMP_JAVA_XEN,
   129  	RUMP_JAVA_AWS,
   130  	RUMP_JAVA_VIRTUALBOX,
   131  	RUMP_JAVA_VSPHERE,
   132  	RUMP_JAVA_QEMU,
   133  	RUMP_JAVA_OPENSTACK,
   134  
   135  	OSV_JAVA_XEN,
   136  	OSV_JAVA_AWS,
   137  	OSV_JAVA_VIRTUALBOX,
   138  	OSV_JAVA_VSPHERE,
   139  	OSV_JAVA_QEMU,
   140  	OSV_JAVA_OPENSTACK,
   141  
   142  	OSV_NODEJS_QEMU,
   143  	OSV_NODEJS_OPENSTACK,
   144  	OSV_NODEJS_AWS,
   145  
   146  	OSV_NATIVE_QEMU,
   147  	OSV_NATIVE_OPENSTACK,
   148  	OSV_NATIVE_AWS,
   149  
   150  	INCLUDEOS_CPP_QEMU,
   151  	INCLUDEOS_CPP_XEN,
   152  	INCLUDEOS_CPP_VIRTUALBOX,
   153  	INCLUDEOS_CPP_OPENSTACK,
   154  
   155  	MIRAGE_OCAML_XEN,
   156  	MIRAGE_OCAML_UKVM,
   157  	MIRAGE_OCAML_QEMU,
   158  
   159  	FIRECRACKER_GO,
   160  }
   161  
   162  func ValidateCompiler(base, language, provider string) (CompilerType, error) {
   163  	baseMatch := false
   164  	languageMatch := false
   165  	for _, compiler := range compilers {
   166  		if compiler.Base() == base {
   167  			baseMatch = true
   168  		}
   169  		if compiler.Base() == base && compiler.Language() == language {
   170  			languageMatch = true
   171  		}
   172  		if compiler.Base() == base && compiler.Language() == language && compiler.Provider() == provider {
   173  			return compiler, nil
   174  		}
   175  	}
   176  	if !baseMatch {
   177  		return "", errors.New("no compiler found for base "+base+", available bases: "+strings.Join(bases(), " | "), nil)
   178  	}
   179  	if !languageMatch {
   180  		return "", errors.New("language "+language+" not found for base "+base+", available languages: "+strings.Join(langsForBase(base), " | "), nil)
   181  	}
   182  	return "", errors.New("provider "+provider+" not supported for unikernel runtime "+base+"-"+language+", available providers: "+strings.Join(providersForBaseLang(base, language), " | "), nil)
   183  }
   184  
   185  func bases() []string {
   186  	uniqueBases := make(map[string]interface{})
   187  	for _, compiler := range compilers {
   188  		uniqueBases[compiler.Base()] = struct{}{}
   189  	}
   190  	bases := []string{}
   191  	for base := range uniqueBases {
   192  		bases = append(bases, base)
   193  	}
   194  	return bases
   195  }
   196  
   197  func langsForBase(base string) []string {
   198  	uniqueLangs := make(map[string]interface{})
   199  	for _, compiler := range compilers {
   200  		if compiler.Base() == base {
   201  			uniqueLangs[compiler.Language()] = struct{}{}
   202  		}
   203  	}
   204  	langs := []string{}
   205  	for lang := range uniqueLangs {
   206  		langs = append(langs, lang)
   207  	}
   208  	return langs
   209  }
   210  
   211  func providersForBaseLang(base, language string) []string {
   212  	uniqueProviders := make(map[string]interface{})
   213  	for _, compiler := range compilers {
   214  		if (compiler.Base() == base) && (compiler.Language() == language) {
   215  			uniqueProviders[compiler.Provider()] = struct{}{}
   216  		}
   217  	}
   218  	providers := []string{}
   219  	for provider := range uniqueProviders {
   220  		providers = append(providers, provider)
   221  	}
   222  	return providers
   223  }
   224  
   225  func compilerName(base, language, provider string) CompilerType {
   226  	return CompilerType(fmt.Sprintf("%s-%s-%s", base, language, provider))
   227  }