1
0
mirror of https://github.com/rancher/os.git synced 2025-09-02 07:15:41 +00:00

Cache remote repos and service definitions

This commit is contained in:
Josh Curl
2016-05-25 09:40:28 -07:00
parent 50153d77c5
commit 69fe4bb619
8 changed files with 201 additions and 105 deletions

View File

@@ -2,11 +2,8 @@ package util
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
@@ -17,11 +14,6 @@ import (
"reflect"
)
var (
ErrNoNetwork = errors.New("Networking not available to load resource")
ErrNotFound = errors.New("Failed to find resource")
)
type AnyMap map[interface{}]interface{}
func Contains(values []string, value string) bool {
@@ -256,32 +248,6 @@ func ToStrings(data []interface{}) []string {
return result
}
func GetServices(urls []string) ([]string, error) {
result := []string{}
for _, url := range urls {
indexUrl := fmt.Sprintf("%s/index.yml", url)
content, err := LoadResource(indexUrl, true, []string{})
if err != nil {
log.Errorf("Failed to load %s: %v", indexUrl, err)
continue
}
services := make(map[string][]string)
err = yaml.Unmarshal(content, &services)
if err != nil {
log.Errorf("Failed to unmarshal %s: %v", indexUrl, err)
continue
}
if list, ok := services["services"]; ok {
result = append(result, list...)
}
}
return result, nil
}
func DirLs(dir string) ([]interface{}, error) {
result := []interface{}{}
files, err := ioutil.ReadDir(dir)
@@ -294,49 +260,6 @@ func DirLs(dir string) ([]interface{}, error) {
return result, nil
}
func retryHttp(f func() (*http.Response, error), times int) (resp *http.Response, err error) {
for i := 0; i < times; i++ {
if resp, err = f(); err == nil {
return
}
log.Warnf("Error making HTTP request: %s. Retrying", err)
}
return
}
func LoadResource(location string, network bool, urls []string) ([]byte, error) {
var bytes []byte
err := ErrNotFound
if strings.HasPrefix(location, "http:/") || strings.HasPrefix(location, "https:/") {
if !network {
return nil, ErrNoNetwork
}
resp, err := retryHttp(func() (*http.Response, error) { return http.Get(location) }, 8)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("non-200 http response: %d", resp.StatusCode)
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
} else if strings.HasPrefix(location, "/") {
return ioutil.ReadFile(location)
} else if len(location) > 0 {
for _, url := range urls {
ymlUrl := fmt.Sprintf("%s/%s/%s.yml", url, location[0:1], location)
bytes, err = LoadResource(ymlUrl, network, []string{})
if err == nil {
log.Debugf("Loaded %s from %s", location, ymlUrl)
return bytes, nil
}
}
}
return nil, err
}
func Map2KVPairs(m map[string]string) []string {
r := make([]string, 0, len(m))
for k, v := range m {