yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/publisher.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package azure
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"yunion.io/x/cloudmux/pkg/apis"
    22  )
    23  
    24  type SPublisherDriver struct {
    25  	OsType       string
    26  	GetOffers    func() []string
    27  	GetSkus      func(offer string) []string
    28  	GetOsDist    func(offser, sku, version string) string
    29  	GetOsVersion func(offser, sku, version string) string
    30  	GetOsArch    func(offser, sku, version string) string
    31  	GetName      func(offser, sku, version string) string
    32  }
    33  
    34  var publisherDrivers = map[string]SPublisherDriver{
    35  	// Microsoft Windows Server
    36  	"microsoftwindowsserver": {
    37  		OsType: "Windows",
    38  		GetOffers: func() []string {
    39  			return []string{"WindowsServer", "2019-Datacenter"}
    40  		},
    41  		GetSkus: func(offer string) []string {
    42  			switch offer {
    43  			case "WindowsServer":
    44  				return []string{"2016-Datacenter", "2019-Datacenter"}
    45  			default:
    46  				return []string{}
    47  			}
    48  		},
    49  		GetOsDist: func(offer, sku, version string) string {
    50  			parts := strings.Split(sku, "-")
    51  			return fmt.Sprintf("Windows Server %s", strings.Join(parts, " "))
    52  		},
    53  		GetOsVersion: func(offer, sku, version string) string {
    54  			parts := strings.Split(sku, "-")
    55  			return parts[0]
    56  		},
    57  		GetOsArch: func(offer, sku, version string) string {
    58  			return apis.OS_ARCH_X86_64
    59  		},
    60  		GetName: func(offer, sku, version string) string {
    61  			return fmt.Sprintf("%s-%s-%s", offer, sku, version)
    62  		},
    63  	},
    64  	// RHEL
    65  	"redhat": {
    66  		OsType: "Linux",
    67  		GetOffers: func() []string {
    68  			return []string{"rhel-75"}
    69  		},
    70  		GetSkus: func(offer string) []string {
    71  			switch offer {
    72  			case "rhel-75":
    73  				return []string{"standard"}
    74  			default:
    75  				return []string{}
    76  			}
    77  		},
    78  		GetOsDist: func(offer, sku, version string) string {
    79  			return "RHEL"
    80  		},
    81  		GetOsVersion: func(offer, sku, version string) string {
    82  			return sku
    83  		},
    84  		GetOsArch: func(offer, sku, version string) string {
    85  			return apis.OS_ARCH_X86_64
    86  		},
    87  		GetName: func(offer, sku, version string) string {
    88  			return fmt.Sprintf("%s-%s", offer, version)
    89  		},
    90  	},
    91  	// Ubuntu
    92  	"canonical": {
    93  		OsType: "Linux",
    94  		GetOffers: func() []string {
    95  			return []string{"UbuntuServer"}
    96  		},
    97  		GetSkus: func(offer string) []string {
    98  			switch offer {
    99  			case "UbuntuServer":
   100  				return []string{"14.04.5-LTS", "16.04-LTS", "17.10", "18.04-LTS", "18_04-lts-gen2", "19.04", "19_04-gen2"}
   101  			default:
   102  				return []string{}
   103  			}
   104  		},
   105  		GetOsDist: func(offer, sku, version string) string {
   106  			return "Ubuntu"
   107  		},
   108  		GetOsVersion: func(offer, sku, version string) string {
   109  			return sku
   110  		},
   111  		GetOsArch: func(offer, sku, version string) string {
   112  			return apis.OS_ARCH_X86_64
   113  		},
   114  		GetName: func(offer, sku, version string) string {
   115  			return fmt.Sprintf("%s-%s", offer, version)
   116  		},
   117  	},
   118  	// CentOS
   119  	"openlogic": {
   120  		OsType: "Linux",
   121  		GetOffers: func() []string {
   122  			return []string{"CentOS"}
   123  		},
   124  		GetSkus: func(offer string) []string {
   125  			switch offer {
   126  			case "CentOS":
   127  				return []string{"6.9", "7.3", "7.4", "7.5", "7.6", "7.7", "8.0", "7_4-gen2", "7_5-gen2", "7_6-gen2", "7_7-gen2", "8_0-gen2", "8_1-gen2"}
   128  			default:
   129  				return []string{}
   130  			}
   131  		},
   132  		GetOsDist: func(offer, sku, version string) string {
   133  			return "CentOS"
   134  		},
   135  		GetOsVersion: func(offer, sku, version string) string {
   136  			return sku
   137  		},
   138  		GetOsArch: func(offer, sku, version string) string {
   139  			return apis.OS_ARCH_X86_64
   140  		},
   141  		GetName: func(offer, sku, version string) string {
   142  			return fmt.Sprintf("%s-%s", offer, version)
   143  		},
   144  	},
   145  	// SUSE
   146  	"suse": {
   147  		OsType: "Linux",
   148  		GetOffers: func() []string {
   149  			return []string{"SLES"}
   150  		},
   151  		GetSkus: func(offer string) []string {
   152  			switch offer {
   153  			case "SLES":
   154  				return []string{"12-SP4", "12-SP4-gen2"}
   155  			default:
   156  				return []string{}
   157  			}
   158  		},
   159  		GetOsDist: func(offer, sku, version string) string {
   160  			return "SUSE"
   161  		},
   162  		GetOsVersion: func(offer, sku, version string) string {
   163  			return sku
   164  		},
   165  		GetOsArch: func(offer, sku, version string) string {
   166  			return apis.OS_ARCH_X86_64
   167  		},
   168  		GetName: func(offer, sku, version string) string {
   169  			return fmt.Sprintf("%s-%s-%s", offer, sku, version)
   170  		},
   171  	},
   172  	// CoreOS
   173  	"coreos": {
   174  		OsType: "Linux",
   175  		GetOffers: func() []string {
   176  			return []string{"CoreOS"}
   177  		},
   178  		GetSkus: func(offer string) []string {
   179  			switch offer {
   180  			case "CoreOS":
   181  				return []string{"Alpha", "Beta", "Stable"}
   182  			default:
   183  				return []string{}
   184  			}
   185  		},
   186  		GetOsDist: func(offer, sku, version string) string {
   187  			return "CoreOS"
   188  		},
   189  		GetOsVersion: func(offer, sku, version string) string {
   190  			return version
   191  		},
   192  		GetOsArch: func(offer, sku, version string) string {
   193  			return apis.OS_ARCH_X86_64
   194  		},
   195  		GetName: func(offer, sku, version string) string {
   196  			return fmt.Sprintf("%s-%s-%s", offer, sku, version)
   197  		},
   198  	},
   199  	// Debian
   200  	"credativ": {
   201  		OsType: "Linux",
   202  		GetOffers: func() []string {
   203  			return []string{"Debian"}
   204  		},
   205  		GetSkus: func(offer string) []string {
   206  			switch offer {
   207  			case "Debian":
   208  				return []string{"8", "9"}
   209  			default:
   210  				return []string{}
   211  			}
   212  		},
   213  		GetOsDist: func(offer, sku, version string) string {
   214  			return "Debian"
   215  		},
   216  		GetOsVersion: func(offer, sku, version string) string {
   217  			return sku
   218  		},
   219  		GetOsArch: func(offer, sku, version string) string {
   220  			return apis.OS_ARCH_X86_64
   221  		},
   222  		GetName: func(offer, sku, version string) string {
   223  			return fmt.Sprintf("%s-%s", offer, version)
   224  		},
   225  	},
   226  	// FreeBSD
   227  	"microsoftostc": {
   228  		OsType: "FreeBSD",
   229  		GetOffers: func() []string {
   230  			return []string{"FreeBSD"}
   231  		},
   232  		GetSkus: func(offer string) []string {
   233  			switch offer {
   234  			case "FreeBSD":
   235  				return []string{"10.4", "11.2", "12.0"}
   236  			default:
   237  				return []string{}
   238  			}
   239  		},
   240  		GetOsDist: func(offer, sku, version string) string {
   241  			return "FreeBSD"
   242  		},
   243  		GetOsVersion: func(offer, sku, version string) string {
   244  			return sku
   245  		},
   246  		GetOsArch: func(offer, sku, version string) string {
   247  			return apis.OS_ARCH_X86_64
   248  		},
   249  		GetName: func(offer, sku, version string) string {
   250  			return fmt.Sprintf("%s-%s", offer, version)
   251  		},
   252  	},
   253  }
   254  
   255  var knownPublishers []string
   256  
   257  func init() {
   258  	knownPublishers = make([]string, len(publisherDrivers))
   259  	i := 0
   260  	for k := range publisherDrivers {
   261  		knownPublishers[i] = strings.ToLower(k)
   262  		i += 1
   263  	}
   264  }
   265  
   266  func publisherGetName(publisher, offer, sku, version string) string {
   267  	driver, ok := publisherDrivers[publisher]
   268  	if ok {
   269  		return driver.GetName(offer, sku, version)
   270  	}
   271  	return fmt.Sprintf("%s-%s-%s", offer, sku, version)
   272  }
   273  
   274  func publisherGetOsType(publisher string) string {
   275  	for _publisher, driver := range publisherDrivers {
   276  		if strings.ToLower(_publisher) == strings.ToLower(publisher) {
   277  			return driver.OsType
   278  		}
   279  	}
   280  	return "Linux"
   281  }
   282  
   283  func publisherGetOsDist(publisher, offer, sku, version string) string {
   284  	driver, ok := publisherDrivers[publisher]
   285  	if ok {
   286  		return driver.GetOsDist(offer, sku, version)
   287  	}
   288  	return offer
   289  }
   290  
   291  func publisherGetOsVersion(publisher, offer, sku, version string) string {
   292  	driver, ok := publisherDrivers[publisher]
   293  	if ok {
   294  		return driver.GetOsVersion(offer, sku, version)
   295  	}
   296  	return sku
   297  }
   298  
   299  func publisherGetOsArch(publisher, offer, sku, version string) string {
   300  	driver, ok := publisherDrivers[publisher]
   301  	if ok {
   302  		return driver.GetOsArch(offer, sku, version)
   303  	}
   304  	return apis.OS_ARCH_X86_64
   305  }