Skip to content
Snippets Groups Projects
setdocker.py 929 B
Newer Older
root's avatar
root committed
#!/usr/bin/python3
import json
import argparse
import os, sys
import copy

parser = argparse.ArgumentParser()
parser.add_argument("--proxy", type = str, default = "http://192.168.32.92:7890")
args = parser.parse_args()

proxy_json = {
        "proxies": {
            "http-proxy": args.proxy,
            "https-proxy": args.proxy
            }
        }

file_path = "/etc/docker/daemon.json"
# file_path = "test.json"
if not os.path.exists(file_path):
    # print(json.dumps(proxy_json, indent = 2))
    with open(file_path, 'w') as f:
        f.write(json.dumps(proxy_json, indent = 2))
root's avatar
root committed
    exit(0)
root's avatar
root committed
else:
    with open(file_path, 'r') as f:
        conf_json = json.load(f)
    if not conf_json.get("proxies"):
        conf_json.update(proxy_json)
root's avatar
root committed
        # print(json.dumps(conf_json))
        with open(file_path, 'w') as f:
            f.write(json.dumps(conf_json, indent = 2))
        exit(0)
    else:
        exit(1)