diff --git a/states/apt/apt.py.j2 b/states/apt/apt.py.j2 new file mode 100755 index 0000000..febece1 --- /dev/null +++ b/states/apt/apt.py.j2 @@ -0,0 +1,24 @@ +{%- from "apt/map.jinja" import apt with context -%} +#!/usr/bin/env python3 + +import subprocess +import re + +SCHEME = "{{ apt.proxy.scheme }}" +HOSTNAME = "{{ apt.proxy.hostname }}" +PORT = "{{ apt.proxy.port }}" + +def main(): + regex = re.compile(f".*{HOSTNAME}.*") + cmd = subprocess.Popen(f"avahi-resolve-host-name {HOSTNAME}", + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + result = cmd.stdout.read().decode().splitlines() + if len(result) > 0: + if regex.match(result[0]): + return f"{SCHEME}://{HOSTNAME}:{PORT}" + return "DIRECT" + +if __name__ == "__main__": + print(main()) diff --git a/states/apt/defaults.yaml b/states/apt/defaults.yaml index c645290..ddefc93 100644 --- a/states/apt/defaults.yaml +++ b/states/apt/defaults.yaml @@ -1,6 +1,17 @@ --- apt: - config: {} + proxy: + scheme: http + hostname: apt.local + port: 8000 + config: + 10proxy: + content: + - Acquire::http::ProxyAutoDetect "/usr/local/bin/apt-proxy-detect.py"; + - Acquire::https::Proxy "DIRECT"; + 99unauth: + content: + - APT::Get::AllowUnauthenticated "true"; # apt: # config: diff --git a/states/apt/init.sls b/states/apt/init.sls index 68902b3..cbd9709 100644 --- a/states/apt/init.sls +++ b/states/apt/init.sls @@ -1,9 +1,22 @@ --- {%- from "apt/map.jinja" import apt with context %} +apt-deps: + pkg.installed: + - pkgs: + - avahi-daemon + - avahi-utils + +apt-proxy-detect: + file.managed: + - name: /usr/local/bin/apt-proxy-detect.py + - source: salt://apt/apt.py.j2 + - template: jinja + - mode: "0755" + {% for itemkey, itemvalue in apt.config.items() -%} apt-proxy-{{ itemkey }}: file.managed: - - name: /etc/apt/apt.conf.d/{{ k }} + - name: /etc/apt/apt.conf.d/{{ itemkey }} - user: root - group: root - mode: "0644"