network.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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() {
  48. cfg := config.LoadConfig()
  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. if cfg.Rancher.Network.HTTPProxy != "" {
  68. config.Set("rancher.environment.http_proxy", cfg.Rancher.Network.HTTPProxy)
  69. config.Set("rancher.environment.HTTP_PROXY", cfg.Rancher.Network.HTTPProxy)
  70. }
  71. if cfg.Rancher.Network.HTTPSProxy != "" {
  72. config.Set("rancher.environment.https_proxy", cfg.Rancher.Network.HTTPSProxy)
  73. config.Set("rancher.environment.HTTPS_PROXY", cfg.Rancher.Network.HTTPSProxy)
  74. }
  75. if cfg.Rancher.Network.NoProxy != "" {
  76. config.Set("rancher.environment.no_proxy", cfg.Rancher.Network.NoProxy)
  77. config.Set("rancher.environment.NO_PROXY", cfg.Rancher.Network.NoProxy)
  78. }
  79. }
  80. func LoadFromNetworkWithCache(location string) ([]byte, error) {
  81. bytes := cacheLookup(location)
  82. if bytes != nil {
  83. return bytes, nil
  84. }
  85. return LoadFromNetwork(location)
  86. }
  87. func LoadFromNetwork(location string) ([]byte, error) {
  88. SetProxyEnvironmentVariables()
  89. var err error
  90. var resp *http.Response
  91. log.Debugf("LoadFromNetwork(%s)", location)
  92. resp, err = http.Get(location)
  93. log.Debugf("LoadFromNetwork(%s) returned %v, %v", location, resp, err)
  94. if err == nil {
  95. defer resp.Body.Close()
  96. if resp.StatusCode != http.StatusOK {
  97. return nil, fmt.Errorf("non-200 http response: %d", resp.StatusCode)
  98. }
  99. bytes, err := ioutil.ReadAll(resp.Body)
  100. if err != nil {
  101. return nil, err
  102. }
  103. cacheAdd(location, bytes)
  104. return bytes, nil
  105. }
  106. return nil, err
  107. }
  108. func LoadResource(location string, network bool) ([]byte, error) {
  109. if strings.HasPrefix(location, "http:/") || strings.HasPrefix(location, "https:/") {
  110. if !network {
  111. return nil, ErrNoNetwork
  112. }
  113. return LoadFromNetworkWithCache(location)
  114. } else if strings.HasPrefix(location, "/") {
  115. return ioutil.ReadFile(location)
  116. }
  117. return nil, ErrNotFound
  118. }
  119. func serviceURL(url, name string) string {
  120. return fmt.Sprintf("%s/%s/%s.yml", url, name[0:1], name)
  121. }
  122. func LoadServiceResource(name string, useNetwork bool, cfg *config.CloudConfig) ([]byte, error) {
  123. bytes, err := LoadResource(name, useNetwork)
  124. if err == nil {
  125. log.Debugf("Loaded %s from %s", name, name)
  126. return bytes, nil
  127. }
  128. if err == ErrNoNetwork || !useNetwork {
  129. return nil, ErrNoNetwork
  130. }
  131. urls := cfg.Rancher.Repositories.ToArray()
  132. for _, url := range urls {
  133. serviceURL := serviceURL(url, name)
  134. bytes, err = LoadResource(serviceURL, useNetwork)
  135. if err == nil {
  136. log.Debugf("Loaded %s from %s", name, serviceURL)
  137. return bytes, nil
  138. }
  139. }
  140. return nil, err
  141. }