1
0
mirror of https://github.com/rancher/os.git synced 2025-07-12 14:18:01 +00:00

Merge pull request #2104 from SvenDowideit/http-proxy-fix

Refactor a little so 'ros os list' also uses the configured proxy info
This commit is contained in:
Sven Dowideit 2017-09-18 15:43:38 +10:00 committed by GitHub
commit f7327d764f
7 changed files with 54 additions and 16 deletions

View File

@ -73,7 +73,9 @@ func autologinAction(c *cli.Context) error {
// until I make time to read their source, lets just give us a way to get work done // until I make time to read their source, lets just give us a way to get work done
loginBin = "bash" loginBin = "bash"
args = append(args, "--login") args = append(args, "--login")
os.Setenv("PROMPT_COMMAND", `echo "[`+fmt.Sprintf("Recovery console %s@%s:${PWD}", user, cfg.Hostname)+`]"`) if mode == "recovery" {
os.Setenv("PROMPT_COMMAND", `echo "[`+fmt.Sprintf("Recovery console %s@%s:${PWD}", user, cfg.Hostname)+`]"`)
}
} else { } else {
loginBin = "login" loginBin = "login"
args = append(args, "-f", user) args = append(args, "-f", user)
@ -91,7 +93,6 @@ func autologinAction(c *cli.Context) error {
//return syscall.Exec(loginBinPath, args, os.Environ()) //return syscall.Exec(loginBinPath, args, os.Environ())
cmd = exec.Command(loginBinPath, args...) cmd = exec.Command(loginBinPath, args...)
cmd.Env = os.Environ() cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "SVEN", "MORE")
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout

View File

@ -113,6 +113,23 @@ func consoleInitFunc() error {
log.Error(err) 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`) 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 { if err := cmd.Run(); err != nil {
log.Error(err) log.Error(err)

View File

@ -3,7 +3,6 @@ package control
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http"
"net/url" "net/url"
"os" "os"
"runtime" "runtime"
@ -22,6 +21,8 @@ import (
"github.com/rancher/os/compose" "github.com/rancher/os/compose"
"github.com/rancher/os/config" "github.com/rancher/os/config"
"github.com/rancher/os/docker" "github.com/rancher/os/docker"
"github.com/rancher/os/util"
"github.com/rancher/os/util/network"
) )
type Images struct { type Images struct {
@ -83,7 +84,6 @@ func osSubcommands() []cli.Command {
} }
} }
// TODO: this and the getLatestImage should probably move to utils/network and be suitably cached.
func getImages() (*Images, error) { func getImages() (*Images, error) {
upgradeURL, err := getUpgradeURL() upgradeURL, err := getUpgradeURL()
if err != nil { if err != nil {
@ -105,15 +105,13 @@ func getImages() (*Images, error) {
q := u.Query() q := u.Query()
q.Set("current", config.Version) q.Set("current", config.Version)
if hypervisor := util.GetHypervisor(); hypervisor == "" {
q.Set("hypervisor", hypervisor)
}
u.RawQuery = q.Encode() u.RawQuery = q.Encode()
upgradeURL = u.String() upgradeURL = u.String()
resp, err := http.Get(upgradeURL) body, err = network.LoadFromNetwork(upgradeURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -19,6 +19,7 @@ import (
"github.com/rancher/os/config/cloudinit/datasource" "github.com/rancher/os/config/cloudinit/datasource"
"github.com/rancher/os/config/cloudinit/pkg" "github.com/rancher/os/config/cloudinit/pkg"
"github.com/rancher/os/util/network"
) )
type RemoteFile struct { type RemoteFile struct {
@ -31,6 +32,7 @@ func NewDatasource(url string) *RemoteFile {
} }
func (f *RemoteFile) IsAvailable() bool { func (f *RemoteFile) IsAvailable() bool {
network.SetProxyEnvironmentVariables()
client := pkg.NewHTTPClient() client := pkg.NewHTTPClient()
_, f.lastError = client.Get(f.url) _, f.lastError = client.Get(f.url)
return (f.lastError == nil) return (f.lastError == nil)

View File

@ -451,7 +451,8 @@ func RunInit() error {
}}, }},
config.CfgFuncData{"load modules2", loadModules}, config.CfgFuncData{"load modules2", loadModules},
config.CfgFuncData{"set proxy env", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { config.CfgFuncData{"set proxy env", func(cfg *config.CloudConfig) (*config.CloudConfig, error) {
network.SetProxyEnvironmentVariables(cfg) network.SetProxyEnvironmentVariables()
return cfg, nil return cfg, nil
}}, }},
config.CfgFuncData{"init SELinux", initializeSelinux}, config.CfgFuncData{"init SELinux", initializeSelinux},

View File

@ -147,6 +147,10 @@ rancher:
io.rancher.os.after: cloud-init-execute io.rancher.os.after: cloud-init-execute
io.docker.compose.rebuild: always io.docker.compose.rebuild: always
io.rancher.os.console: default io.rancher.os.console: default
environment:
- HTTP_PROXY
- HTTPS_PROXY
- NO_PROXY
net: host net: host
uts: host uts: host
pid: host pid: host

View File

@ -57,7 +57,8 @@ func getServices(urls []string, key string) ([]string, error) {
return result, nil return result, nil
} }
func SetProxyEnvironmentVariables(cfg *config.CloudConfig) { func SetProxyEnvironmentVariables() {
cfg := config.LoadConfig()
if cfg.Rancher.Network.HTTPProxy != "" { if cfg.Rancher.Network.HTTPProxy != "" {
err := os.Setenv("HTTP_PROXY", cfg.Rancher.Network.HTTPProxy) err := os.Setenv("HTTP_PROXY", cfg.Rancher.Network.HTTPProxy)
if err != nil { if err != nil {
@ -76,16 +77,30 @@ func SetProxyEnvironmentVariables(cfg *config.CloudConfig) {
log.Errorf("Unable to set NO_PROXY: %s", err) 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) bytes := cacheLookup(location)
if bytes != nil { if bytes != nil {
return bytes, nil return bytes, nil
} }
return LoadFromNetwork(location)
}
cfg := config.LoadConfig() func LoadFromNetwork(location string) ([]byte, error) {
SetProxyEnvironmentVariables(cfg) SetProxyEnvironmentVariables()
var err error var err error
@ -116,7 +131,7 @@ func LoadResource(location string, network bool) ([]byte, error) {
if !network { if !network {
return nil, ErrNoNetwork return nil, ErrNoNetwork
} }
return loadFromNetwork(location) return LoadFromNetworkWithCache(location)
} else if strings.HasPrefix(location, "/") { } else if strings.HasPrefix(location, "/") {
return ioutil.ReadFile(location) return ioutil.ReadFile(location)
} }