1
0
mirror of https://github.com/rancher/os.git synced 2025-08-01 15:08:47 +00:00

Support URL when using ros install -c (#2202)

This commit is contained in:
niusmallnan 2018-01-02 17:17:43 +08:00 committed by GitHub
parent b08d29cc2c
commit 3b92e04065
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View File

@ -158,8 +158,14 @@ func installAction(c *cli.Context) error {
} else {
os.MkdirAll("/opt", 0755)
uc := "/opt/user_config.yml"
if err := util.FileCopy(cloudConfig, uc); err != nil {
log.WithFields(log.Fields{"cloudConfig": cloudConfig, "error": err}).Fatal("Failed to copy cloud-config")
if strings.HasPrefix(cloudConfig, "http://") || strings.HasPrefix(cloudConfig, "https://") {
if err := util.HTTPDownloadToFile(cloudConfig, uc); err != nil {
log.WithFields(log.Fields{"cloudConfig": cloudConfig, "error": err}).Fatal("Failed to http get cloud-config")
}
} else {
if err := util.FileCopy(cloudConfig, uc); err != nil {
log.WithFields(log.Fields{"cloudConfig": cloudConfig, "error": err}).Fatal("Failed to copy cloud-config")
}
}
cloudConfig = uc
}

View File

@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path"
@ -45,6 +46,19 @@ func FileCopy(src, dest string) error {
return WriteFileAtomic(dest, data, 0666)
}
func HTTPDownloadToFile(url, dest string) error {
res, err := http.Get(url)
if err != nil {
return err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
return WriteFileAtomic(dest, body, 0666)
}
func WriteFileAtomic(filename string, data []byte, perm os.FileMode) error {
dir, file := path.Split(filename)
tempFile, err := ioutil.TempFile(dir, fmt.Sprintf(".%s", file))