network.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package network
  2. import (
  3. "errors"
  4. "fmt"
  5. "io/ioutil"
  6. "net"
  7. "net/http"
  8. "strings"
  9. "time"
  10. yaml "github.com/cloudfoundry-incubator/candiedyaml"
  11. log "github.com/Sirupsen/logrus"
  12. "github.com/rancher/os/config"
  13. )
  14. var (
  15. ErrNoNetwork = errors.New("Networking not available to load resource")
  16. ErrNotFound = errors.New("Failed to find resource")
  17. )
  18. func GetServices(urls []string) ([]string, error) {
  19. return getServices(urls, "services")
  20. }
  21. func GetConsoles(urls []string) ([]string, error) {
  22. return getServices(urls, "consoles")
  23. }
  24. func getServices(urls []string, key string) ([]string, error) {
  25. result := []string{}
  26. for _, url := range urls {
  27. indexUrl := fmt.Sprintf("%s/index.yml", url)
  28. content, err := LoadResource(indexUrl, true)
  29. if err != nil {
  30. log.Errorf("Failed to load %s: %v", indexUrl, err)
  31. continue
  32. }
  33. services := make(map[string][]string)
  34. err = yaml.Unmarshal(content, &services)
  35. if err != nil {
  36. log.Errorf("Failed to unmarshal %s: %v", indexUrl, err)
  37. continue
  38. }
  39. if list, ok := services[key]; ok {
  40. result = append(result, list...)
  41. }
  42. }
  43. return result, nil
  44. }
  45. func loadFromNetwork(location string) ([]byte, error) {
  46. bytes := cacheLookup(location)
  47. if bytes != nil {
  48. return bytes, nil
  49. }
  50. var err error
  51. for i := 0; i < 300; i++ {
  52. net.UpdateDnsConf()
  53. var resp *http.Response
  54. resp, err = http.Get(location)
  55. if err == nil {
  56. defer resp.Body.Close()
  57. if resp.StatusCode != http.StatusOK {
  58. return nil, fmt.Errorf("non-200 http response: %d", resp.StatusCode)
  59. }
  60. bytes, err := ioutil.ReadAll(resp.Body)
  61. if err != nil {
  62. return nil, err
  63. }
  64. cacheAdd(location, bytes)
  65. return bytes, nil
  66. }
  67. time.Sleep(100 * time.Millisecond)
  68. }
  69. return nil, err
  70. }
  71. func LoadResource(location string, network bool) ([]byte, error) {
  72. if strings.HasPrefix(location, "http:/") || strings.HasPrefix(location, "https:/") {
  73. if !network {
  74. return nil, ErrNoNetwork
  75. }
  76. return loadFromNetwork(location)
  77. } else if strings.HasPrefix(location, "/") {
  78. return ioutil.ReadFile(location)
  79. }
  80. return nil, ErrNotFound
  81. }
  82. func serviceUrl(url, name string) string {
  83. return fmt.Sprintf("%s/%s/%s.yml", url, name[0:1], name)
  84. }
  85. func LoadServiceResource(name string, useNetwork bool, cfg *config.CloudConfig) ([]byte, error) {
  86. bytes, err := LoadResource(name, useNetwork)
  87. if err == nil {
  88. log.Debugf("Loaded %s from %s", name, name)
  89. return bytes, nil
  90. }
  91. if err == ErrNoNetwork || !useNetwork {
  92. return nil, ErrNoNetwork
  93. }
  94. urls := cfg.Rancher.Repositories.ToArray()
  95. for _, url := range urls {
  96. serviceUrl := serviceUrl(url, name)
  97. bytes, err = LoadResource(serviceUrl, useNetwork)
  98. if err == nil {
  99. log.Debugf("Loaded %s from %s", name, serviceUrl)
  100. return bytes, nil
  101. }
  102. }
  103. return nil, err
  104. }