* Copyright (c) 2024 Bocloud Technologies Co., Ltd.
* installer is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain n copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
******************************************************************/
package source
import (
_ "embed"
"fmt"
"net/url"
"os"
"runtime"
"strings"
"github.com/pkg/errors"
"github.com/shirou/gopsutil/v3/host"
"gopkg.openfuyao.cn/cluster-api-provider-bke/common/utils"
"gopkg.openfuyao.cn/cluster-api-provider-bke/common/warehouse"
)
const (
minimumVersionParts = 2
)
var (
yumRepos = "/etc/yum.repos.d"
bkeRepoFile = "bke.repo"
aptSourcesDir = "/etc/apt/sources.list.d"
aptPrefsDir = "/etc/apt/preferences.d"
bkeListFile = "bke.list"
bkePrefsFile = "bke"
centos = "CentOS"
kylin = "Kylin"
ubuntu = "Ubuntu"
openeuler = "OpenEuler"
hopeos = "HopeOS"
euleros = "EulerOS"
yumSource = `[bke]
name = OpenFuyao Repository
baseurl = {{.baseurl}}
enabled = 1
gpgcheck = 0
priority = 1
`
aptSource = `deb [trusted=yes] {{.baseurl}} ./
`
aptPrefs = `Package: *
Pin: origin {{.origin}}
Pin-Priority: 1001
`
)
func GetCustomDownloadPath(url string) string {
if strings.HasSuffix(url, "/") {
return url + "files"
}
return url + "/files"
}
func GetRPMDownloadPath(url string) (string, error) {
baseurl := url
h, err := host.Info()
if err != nil {
return baseurl, err
}
if !strings.HasSuffix(baseurl, "/") {
baseurl += "/"
}
switch strings.ToLower(h.Platform) {
case "centos":
baseurl += centos
case "kylin":
baseurl += kylin
case "ubuntu":
baseurl += ubuntu
case "openeuler":
baseurl += openeuler
case "hopeos":
baseurl += hopeos
case "euleros":
baseurl += euleros
default:
return baseurl, errors.New(fmt.Sprintf("The operating system is not supported %s", h.Platform))
}
ver := ""
if !strings.Contains(h.PlatformVersion, ".") {
ver = strings.ToUpper(h.PlatformVersion)
} else {
version := strings.Split(h.PlatformVersion, ".")
if len(version) < minimumVersionParts {
return baseurl, errors.New(fmt.Sprintf("Error getting system version %s", h.PlatformVersion))
}
ver = version[0]
}
baseurl += "/" + ver
baseurl += "/" + runtime.GOARCH
return baseurl, nil
}
func SetSource(rawURL string) error {
baseurl, err := GetRPMDownloadPath(rawURL)
if err != nil {
return err
}
if strings.Contains(baseurl, "/"+ubuntu+"/") {
return writeAptSource(baseurl)
}
return writeYumSource(baseurl)
}
func writeYumSource(baseurl string) error {
newSource := strings.Replace(yumSource, "{{.baseurl}}", baseurl, -1)
return os.WriteFile(yumRepos+"/"+bkeRepoFile, []byte(newSource), warehouse.FilePerm)
}
func writeAptSource(baseurl string) error {
newSource := strings.Replace(aptSource, "{{.baseurl}}", baseurl, -1)
listPath := aptSourcesDir + "/" + bkeListFile
if err := os.WriteFile(listPath, []byte(newSource), warehouse.FilePerm); err != nil {
return err
}
origin := originFromURL(baseurl)
newPrefs := strings.Replace(aptPrefs, "{{.origin}}", origin, -1)
prefsPath := aptPrefsDir + "/" + bkePrefsFile
return os.WriteFile(prefsPath, []byte(newPrefs), warehouse.FilePerm)
}
func originFromURL(rawURL string) string {
u, err := url.Parse(rawURL)
if err != nil || u.Hostname() == "" {
return rawURL
}
return u.Hostname()
}
func ResetSource() error {
h, err := host.Info()
if err != nil {
return err
}
switch strings.ToLower(h.Platform) {
case "centos", "kylin", "openeuler", "hopeos", "euleros":
return resetYumSource()
case "ubuntu":
return resetAptSource()
default:
return errors.New(fmt.Sprintf("The operating system is not supported %s", h.Platform))
}
}
func resetYumSource() error {
repoPath := yumRepos + "/" + bkeRepoFile
if !utils.Exists(repoPath) {
return nil
}
return os.Remove(repoPath)
}
func resetAptSource() error {
listPath := aptSourcesDir + "/" + bkeListFile
if utils.Exists(listPath) {
if err := os.Remove(listPath); err != nil {
return err
}
}
prefsPath := aptPrefsDir + "/" + bkePrefsFile
if utils.Exists(prefsPath) {
if err := os.Remove(prefsPath); err != nil {
return err
}
}
return nil
}