network.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package network
  2. import (
  3. "errors"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "os"
  8. "strings"
  9. "time"
  10. yaml "github.com/cloudfoundry-incubator/candiedyaml"
  11. "github.com/rancher/os/config"
  12. "github.com/rancher/os/log"
  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 GetEngines(urls []string) ([]string, error) {
  25. return getServices(urls, "engines")
  26. }
  27. func getServices(urls []string, key string) ([]string, error) {
  28. result := []string{}
  29. for _, url := range urls {
  30. indexURL := fmt.Sprintf("%s/index.yml", url)
  31. content, err := LoadResource(indexURL, true)
  32. if err != nil {
  33. log.Errorf("Failed to load %s: %v", indexURL, err)
  34. continue
  35. }
  36. services := make(map[string][]string)
  37. err = yaml.Unmarshal(content, &services)
  38. if err != nil {
  39. log.Errorf("Failed to unmarshal %s: %v", indexURL, err)
  40. continue
  41. }
  42. if list, ok := services[key]; ok {
  43. result = append(result, list...)
  44. }
  45. }
  46. return result, nil
  47. }
  48. func SetProxyEnvironmentVariables(cfg *config.CloudConfig) {
  49. if cfg.Rancher.Network.HTTPProxy != "" {
  50. err := os.Setenv("HTTP_PROXY", cfg.Rancher.Network.HTTPProxy)
  51. if err != nil {
  52. log.Errorf("Unable to set HTTP_PROXY: %s", err)
  53. }
  54. }
  55. if cfg.Rancher.Network.HTTPSProxy != "" {
  56. err := os.Setenv("HTTPS_PROXY", cfg.Rancher.Network.HTTPSProxy)
  57. if err != nil {
  58. log.Errorf("Unable to set HTTPS_PROXY: %s", err)
  59. }
  60. }
  61. if cfg.Rancher.Network.NoProxy != "" {
  62. err := os.Setenv("NO_PROXY", cfg.Rancher.Network.NoProxy)
  63. if err != nil {
  64. log.Errorf("Unable to set NO_PROXY: %s", err)
  65. }
  66. }
  67. }
  68. func loadFromNetwork(location string) ([]byte, error) {
  69. bytes := cacheLookup(location)
  70. if bytes != nil {
  71. return bytes, nil
  72. }
  73. cfg := config.LoadConfig()
  74. SetProxyEnvironmentVariables(cfg)
  75. var err error
  76. for i := 0; i < 300; i++ {
  77. updateDNSCache()
  78. var resp *http.Response
  79. resp, err = http.Get(location)
  80. if err == nil {
  81. defer resp.Body.Close()
  82. if resp.StatusCode != http.StatusOK {
  83. return nil, fmt.Errorf("non-200 http response: %d", resp.StatusCode)
  84. }
  85. bytes, err := ioutil.ReadAll(resp.Body)
  86. if err != nil {
  87. return nil, err
  88. }
  89. cacheAdd(location, bytes)
  90. return bytes, nil
  91. }
  92. time.Sleep(100 * time.Millisecond)
  93. }
  94. return nil, err
  95. }
  96. func LoadResource(location string, network bool) ([]byte, error) {
  97. if strings.HasPrefix(location, "http:/") || strings.HasPrefix(location, "https:/") {
  98. if !network {
  99. return nil, ErrNoNetwork
  100. }
  101. return loadFromNetwork(location)
  102. } else if strings.HasPrefix(location, "/") {
  103. return ioutil.ReadFile(location)
  104. }
  105. return nil, ErrNotFound
  106. }
  107. func serviceURL(url, name string) string {
  108. return fmt.Sprintf("%s/%s/%s.yml", url, name[0:1], name)
  109. }
  110. func LoadServiceResource(name string, useNetwork bool, cfg *config.CloudConfig) ([]byte, error) {
  111. bytes, err := LoadResource(name, useNetwork)
  112. if err == nil {
  113. log.Debugf("Loaded %s from %s", name, name)
  114. return bytes, nil
  115. }
  116. if err == ErrNoNetwork || !useNetwork {
  117. return nil, ErrNoNetwork
  118. }
  119. urls := cfg.Rancher.Repositories.ToArray()
  120. for _, url := range urls {
  121. serviceURL := serviceURL(url, name)
  122. bytes, err = LoadResource(serviceURL, useNetwork)
  123. if err == nil {
  124. log.Debugf("Loaded %s from %s", name, serviceURL)
  125. return bytes, nil
  126. }
  127. }
  128. return nil, err
  129. }