-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathproxy.py
More file actions
180 lines (135 loc) · 5.72 KB
/
Copy pathproxy.py
File metadata and controls
180 lines (135 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from pathlib import Path
import os
import json
from subprocess import run
def is_proxy_set():
"""
Check if proxy environment variables are set in the system.
Returns True if any proxy variables are set, False otherwise.
"""
proxy_vars = [
'http_proxy',
'https_proxy',
'no_proxy'
]
for var in proxy_vars:
if os.environ.get(var) or os.environ.get(var.upper()):
return True
return False
def get_proxy_var():
"""
Get all proxy environment variables.
Returns a dictionary with proxy variable names and their values.
"""
proxy_vars = [
'http_proxy',
'https_proxy',
'no_proxy'
]
proxy_dict = {}
for var in proxy_vars:
lowercase_value = os.environ.get(var)
uppercase_value = os.environ.get(var.upper())
if lowercase_value:
proxy_dict[var.upper()] = lowercase_value
if uppercase_value:
proxy_dict[var.upper()] = uppercase_value
return proxy_dict
def configure_containerd_proxy(debug=False):
"""
Configure containerd daemon to use proxy settings detected form the system.
"""
proxy_settings = get_proxy_var()
proxy_dir = Path("/etc/systemd/system/containerd.service.d")
proxy_conf = proxy_dir / "proxy.conf"
run(f"sudo mkdir -p {proxy_dir}", shell=True, check=True)
config_content = """[Service]
Environment="HTTP_PROXY={HTTP_PROXY}"
Environment="HTTPS_PROXY={HTTPS_PROXY}"
Environment="NO_PROXY={NO_PROXY}"
""".format(**proxy_settings)
run(f"sudo tee {proxy_conf} > /dev/null", shell=True, input=config_content.encode(), check=True)
run("sudo systemctl daemon-reload", shell=True, check=True)
def configure_kubelet_proxy(debug=False):
"""
Configure kubelet daemon to use proxy settings detected form the system.
"""
proxy_settings = get_proxy_var()
proxy_dir = Path("/etc/systemd/system/kubelet.service.d")
proxy_conf = proxy_dir / "proxy.conf"
run(f"sudo mkdir -p {proxy_dir}", shell=True, check=True)
config_content = """[Service]
Environment="HTTP_PROXY={HTTP_PROXY}"
Environment="HTTPS_PROXY={HTTPS_PROXY}"
Environment="NO_PROXY={NO_PROXY}"
""".format(**proxy_settings)
run(f"sudo tee {proxy_conf} > /dev/null", shell=True, input=config_content.encode(), check=True)
run("sudo systemctl daemon-reload", shell=True, check=True)
def configure_docker_proxy(debug=False):
"""
Configure Docker to use proxy settings detected from the system.
Sets up proxies in:
1. Systemd drop-in file
2. Docker daemon.json
3. User ~/.docker/config.json
"""
proxy_settings = get_proxy_var()
# 1. Configure systemd drop-in file
proxy_dir = Path("/etc/systemd/system/docker.service.d")
proxy_conf = proxy_dir / "proxy.conf"
run(f"sudo mkdir -p {proxy_dir}", shell=True, check=True)
systemd_content = """[Service]
Environment="HTTP_PROXY={HTTP_PROXY}"
Environment="HTTPS_PROXY={HTTPS_PROXY}"
Environment="NO_PROXY={NO_PROXY}"
""".format(**proxy_settings)
run(f"sudo tee {proxy_conf} > /dev/null", shell=True, input=systemd_content.encode(), check=True)
# 2. Configure docker daemon.json (preserving existing settings)
daemon_json_path = Path("/etc/docker/daemon.json")
daemon_config = {}
if daemon_json_path.exists():
with open(daemon_json_path, 'r') as f:
daemon_config = json.load(f)
daemon_proxy_map = {
"http-proxy": "HTTP_PROXY",
"https-proxy": "HTTPS_PROXY",
"no-proxy": "NO_PROXY"
}
if 'proxies' in daemon_config:
# Check for conflicts with existing settings
for daemon_key, env_key in daemon_proxy_map.items():
if daemon_key in daemon_config['proxies'] and daemon_config['proxies'][daemon_key] != proxy_settings[env_key]:
raise ValueError(f"Existing proxy setting {daemon_key} in daemon.json differs from environment")
else:
# Add new proxy settings
daemon_config['proxies'] = {k: proxy_settings[v] for k, v in daemon_proxy_map.items()}
run(f"sudo mkdir -p {daemon_json_path.parent}", shell=True, check=True)
run(f"sudo tee {daemon_json_path} > /dev/null", shell=True,
input=json.dumps(daemon_config, indent=2).encode(), check=True)
# 3. Configure user docker config.json - preserve existing settings
user_config_dir = Path(os.path.expanduser("~/.docker"))
user_config_path = user_config_dir / "config.json"
user_config = {}
if user_config_path.exists():
with open(user_config_path, 'r') as f:
user_config = json.load(f)
user_proxy_map = {
"httpProxy": "HTTP_PROXY",
"httpsProxy": "HTTPS_PROXY",
"noProxy": "NO_PROXY"
}
if 'proxies' not in user_config:
user_config['proxies'] = {}
if 'default' in user_config['proxies']:
# Check for conflicts with existing settings
for config_key, env_key in user_proxy_map.items():
if config_key in user_config['proxies']['default'] and user_config['proxies']['default'][config_key] != proxy_settings[env_key]:
raise ValueError(f"Existing proxy setting {config_key} in config.json differs from environment")
else:
# Add new proxy settings
user_config['proxies']['default'] = {k: proxy_settings[v] for k, v in user_proxy_map.items()}
run(f"mkdir -p {user_config_dir}", shell=True, check=True)
run(f"tee {user_config_path} > /dev/null", shell=True,
input=json.dumps(user_config, indent=2).encode(), check=True)
run("sudo systemctl daemon-reload", shell=True, check=True)
run("sudo systemctl restart docker", shell=True, check=True)