network.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package network
  2. import (
  3. "errors"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "os"
  8. "strings"
  9. "github.com/rancher/os/config"
  10. "github.com/rancher/os/pkg/log"
  11. yaml "github.com/cloudfoundry-incubator/candiedyaml"
  12. composeConfig "github.com/docker/libcompose/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 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() {
  49. cfg := config.LoadConfig()
  50. if cfg.Rancher.Network.HTTPProxy != "" {
  51. err := os.Setenv("HTTP_PROXY", cfg.Rancher.Network.HTTPProxy)
  52. if err != nil {
  53. log.Errorf("Unable to set HTTP_PROXY: %s", err)
  54. }
  55. }
  56. if cfg.Rancher.Network.HTTPSProxy != "" {
  57. err := os.Setenv("HTTPS_PROXY", cfg.Rancher.Network.HTTPSProxy)
  58. if err != nil {
  59. log.Errorf("Unable to set HTTPS_PROXY: %s", err)
  60. }
  61. }
  62. if cfg.Rancher.Network.NoProxy != "" {
  63. err := os.Setenv("NO_PROXY", cfg.Rancher.Network.NoProxy)
  64. if err != nil {
  65. log.Errorf("Unable to set NO_PROXY: %s", err)
  66. }
  67. }
  68. if cfg.Rancher.Network.HTTPProxy != "" {
  69. config.Set("rancher.environment.http_proxy", cfg.Rancher.Network.HTTPProxy)
  70. config.Set("rancher.environment.HTTP_PROXY", cfg.Rancher.Network.HTTPProxy)
  71. }
  72. if cfg.Rancher.Network.HTTPSProxy != "" {
  73. config.Set("rancher.environment.https_proxy", cfg.Rancher.Network.HTTPSProxy)
  74. config.Set("rancher.environment.HTTPS_PROXY", cfg.Rancher.Network.HTTPSProxy)
  75. }
  76. if cfg.Rancher.Network.NoProxy != "" {
  77. config.Set("rancher.environment.no_proxy", cfg.Rancher.Network.NoProxy)
  78. config.Set("rancher.environment.NO_PROXY", cfg.Rancher.Network.NoProxy)
  79. }
  80. }
  81. func LoadFromNetworkWithCache(location string) ([]byte, error) {
  82. bytes := cacheLookup(location)
  83. if bytes != nil {
  84. return bytes, nil
  85. }
  86. return LoadFromNetwork(location)
  87. }
  88. func LoadFromNetwork(location string) ([]byte, error) {
  89. SetProxyEnvironmentVariables()
  90. var err error
  91. var resp *http.Response
  92. log.Debugf("LoadFromNetwork(%s)", location)
  93. resp, err = http.Get(location)
  94. log.Debugf("LoadFromNetwork(%s) returned %v, %v", location, resp, err)
  95. if err == nil {
  96. defer resp.Body.Close()
  97. if resp.StatusCode != http.StatusOK {
  98. return nil, fmt.Errorf("non-200 http response: %d", resp.StatusCode)
  99. }
  100. bytes, err := ioutil.ReadAll(resp.Body)
  101. if err != nil {
  102. return nil, err
  103. }
  104. cacheAdd(location, bytes)
  105. return bytes, nil
  106. }
  107. return nil, err
  108. }
  109. func LoadResource(location string, network bool) ([]byte, error) {
  110. if strings.HasPrefix(location, "http:/") || strings.HasPrefix(location, "https:/") {
  111. if !network {
  112. return nil, ErrNoNetwork
  113. }
  114. return LoadFromNetworkWithCache(location)
  115. } else if strings.HasPrefix(location, "/") {
  116. return ioutil.ReadFile(location)
  117. }
  118. return nil, ErrNotFound
  119. }
  120. func serviceURL(url, name string) string {
  121. return fmt.Sprintf("%s/%s/%s.yml", url, name[0:1], name)
  122. }
  123. func LoadServiceResource(name string, useNetwork bool, cfg *config.CloudConfig) ([]byte, error) {
  124. bytes, err := LoadResource(name, useNetwork)
  125. if err == nil {
  126. log.Debugf("Loaded %s from %s", name, name)
  127. return bytes, nil
  128. }
  129. if err == ErrNoNetwork || !useNetwork {
  130. return nil, ErrNoNetwork
  131. }
  132. urls := cfg.Rancher.Repositories.ToArray()
  133. for _, url := range urls {
  134. serviceURL := serviceURL(url, name)
  135. bytes, err = LoadResource(serviceURL, useNetwork)
  136. if err == nil {
  137. log.Debugf("Loaded %s from %s", name, serviceURL)
  138. return bytes, nil
  139. }
  140. }
  141. return nil, err
  142. }
  143. func LoadMultiEngineResource(name string) ([]byte, error) {
  144. composeConfigs := map[string]composeConfig.ServiceConfigV1{}
  145. if _, err := os.Stat(config.MultiDockerConfFile); err == nil {
  146. multiEngineBytes, err := ioutil.ReadFile(config.MultiDockerConfFile)
  147. if err != nil {
  148. return nil, err
  149. }
  150. err = yaml.Unmarshal(multiEngineBytes, &composeConfigs)
  151. if err != nil {
  152. return nil, err
  153. }
  154. }
  155. if _, ok := composeConfigs[name]; !ok {
  156. return nil, errors.New("Failed to found " + name + " from " + config.MultiDockerConfFile + " will load from network")
  157. }
  158. foundServiceConfig := map[string]composeConfig.ServiceConfigV1{}
  159. foundServiceConfig[name] = composeConfigs[name]
  160. bytes, err := yaml.Marshal(foundServiceConfig)
  161. if err == nil {
  162. return bytes, err
  163. }
  164. return nil, err
  165. }