network.go 3.7 KB

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