Skip to content

Commit

Permalink
Merge pull request rancher#2104 from SvenDowideit/http-proxy-fix
Browse files Browse the repository at this point in the history
Refactor a little so 'ros os list' also uses the configured proxy info
(cherry picked from commit f7327d7)
  • Loading branch information
SvenDowideit committed Sep 18, 2017
1 parent 521bf0f commit 2d03041
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 16 deletions.
17 changes: 17 additions & 0 deletions cmd/control/console_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,23 @@ func consoleInitFunc() error {
log.Error(err)
}

// write out a profile.d file for the proxy settings.
// maybe write these on the host and bindmount into everywhere?
proxyLines := []string{}
for _, k := range []string{"http_proxy", "HTTP_PROXY", "https_proxy", "HTTPS_PROXY", "no_proxy", "NO_PROXY"} {
if v, ok := cfg.Rancher.Environment[k]; ok {
proxyLines = append(proxyLines, fmt.Sprintf("export %s=%s", k, v))
}
}

if len(proxyLines) > 0 {
proxyString := strings.Join(proxyLines, "\n")
proxyString = fmt.Sprintf("#!/bin/sh\n%s\n", proxyString)
if err := ioutil.WriteFile("/etc/profile.d/proxy.sh", []byte(proxyString), 0755); err != nil {
log.Error(err)
}
}

cmd = exec.Command("bash", "-c", `echo $(/sbin/ifconfig | grep -B1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' |awk -F: '{ print $1 ": " $3}') >> /etc/issue`)
if err := cmd.Run(); err != nil {
log.Error(err)
Expand Down
10 changes: 2 additions & 8 deletions cmd/control/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package control
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"runtime"
Expand All @@ -22,6 +21,7 @@ import (
"github.com/rancher/os/compose"
"github.com/rancher/os/config"
"github.com/rancher/os/docker"
"github.com/rancher/os/util/network"
)

type Images struct {
Expand Down Expand Up @@ -83,7 +83,6 @@ func osSubcommands() []cli.Command {
}
}

// TODO: this and the getLatestImage should probably move to utils/network and be suitably cached.
func getImages() (*Images, error) {
upgradeURL, err := getUpgradeURL()
if err != nil {
Expand All @@ -108,12 +107,7 @@ func getImages() (*Images, error) {
u.RawQuery = q.Encode()
upgradeURL = u.String()

resp, err := http.Get(upgradeURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
body, err = network.LoadFromNetwork(upgradeURL)
if err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions config/cloudinit/datasource/url/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/rancher/os/config/cloudinit/datasource"
"github.com/rancher/os/config/cloudinit/pkg"
"github.com/rancher/os/util/network"
)

type RemoteFile struct {
Expand All @@ -31,6 +32,7 @@ func NewDatasource(url string) *RemoteFile {
}

func (f *RemoteFile) IsAvailable() bool {
network.SetProxyEnvironmentVariables()
client := pkg.NewHTTPClient()
_, f.lastError = client.Get(f.url)
return (f.lastError == nil)
Expand Down
7 changes: 4 additions & 3 deletions init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,10 @@ func RunInit() error {
return c, dfs.PrepareFs(&mountConfig)
}},
config.CfgFuncData{"load modules2", loadModules},
config.CfgFuncData{"set proxy env", func(c *config.CloudConfig) (*config.CloudConfig, error) {
network.SetProxyEnvironmentVariables(c)
return c, nil
config.CfgFuncData{"set proxy env", func(cfg *config.CloudConfig) (*config.CloudConfig, error) {
network.SetProxyEnvironmentVariables()

return cfg, nil
}},
config.CfgFuncData{"init SELinux", initializeSelinux},
config.CfgFuncData{"setupSharedRoot", setupSharedRoot},
Expand Down
4 changes: 4 additions & 0 deletions os-config.tpl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ rancher:
io.rancher.os.after: cloud-init-execute
io.docker.compose.rebuild: always
io.rancher.os.console: default
environment:
- HTTP_PROXY
- HTTPS_PROXY
- NO_PROXY
net: host
uts: host
pid: host
Expand Down
25 changes: 20 additions & 5 deletions util/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func getServices(urls []string, key string) ([]string, error) {
return result, nil
}

func SetProxyEnvironmentVariables(cfg *config.CloudConfig) {
func SetProxyEnvironmentVariables() {
cfg := config.LoadConfig()
if cfg.Rancher.Network.HTTPProxy != "" {
err := os.Setenv("HTTP_PROXY", cfg.Rancher.Network.HTTPProxy)
if err != nil {
Expand All @@ -76,16 +77,30 @@ func SetProxyEnvironmentVariables(cfg *config.CloudConfig) {
log.Errorf("Unable to set NO_PROXY: %s", err)
}
}
if cfg.Rancher.Network.HTTPProxy != "" {
config.Set("rancher.environment.http_proxy", cfg.Rancher.Network.HTTPProxy)
config.Set("rancher.environment.HTTP_PROXY", cfg.Rancher.Network.HTTPProxy)
}
if cfg.Rancher.Network.HTTPSProxy != "" {
config.Set("rancher.environment.https_proxy", cfg.Rancher.Network.HTTPSProxy)
config.Set("rancher.environment.HTTPS_PROXY", cfg.Rancher.Network.HTTPSProxy)
}
if cfg.Rancher.Network.NoProxy != "" {
config.Set("rancher.environment.no_proxy", cfg.Rancher.Network.NoProxy)
config.Set("rancher.environment.NO_PROXY", cfg.Rancher.Network.NoProxy)
}
}

func loadFromNetwork(location string) ([]byte, error) {
func LoadFromNetworkWithCache(location string) ([]byte, error) {
bytes := cacheLookup(location)
if bytes != nil {
return bytes, nil
}
return LoadFromNetwork(location)
}

cfg := config.LoadConfig()
SetProxyEnvironmentVariables(cfg)
func LoadFromNetwork(location string) ([]byte, error) {
SetProxyEnvironmentVariables()

resp, err := http.Get(location)
if err == nil {
Expand All @@ -111,7 +126,7 @@ func LoadResource(location string, network bool) ([]byte, error) {
if !network {
return nil, ErrNoNetwork
}
return loadFromNetwork(location)
return LoadFromNetworkWithCache(location)
} else if strings.HasPrefix(location, "/") {
return ioutil.ReadFile(location)
}
Expand Down

0 comments on commit 2d03041

Please sign in to comment.