From 2d5ad8d45c884478fb28dbb82da61de6817d2f68 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Sun, 2 Jul 2023 21:32:49 +0200 Subject: [PATCH] updated apt state --- states/apt/defaults.yaml | 4 ++- states/apt/init.sls | 18 +++++++++---- states/apt/map.jinja | 10 +++++-- states/apt/osarchmap.yaml | 21 +++++++++++++++ states/apt/templates/apt.go | 54 +++++++++++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 states/apt/osarchmap.yaml create mode 100644 states/apt/templates/apt.go diff --git a/states/apt/defaults.yaml b/states/apt/defaults.yaml index 26cf1b0..19aa01c 100644 --- a/states/apt/defaults.yaml +++ b/states/apt/defaults.yaml @@ -10,7 +10,9 @@ apt: - APT::Periodic::Download-Upgradeable-Packages "1"; - APT::Periodic::AutocleanInterval "0"; 10proxy: - - Acquire::http::Proxy-Auto-Detect "/usr/local/bin/apt-proxy-detect.py"; + - Acquire::http::Proxy-Auto-Detect "/usr/local/bin/apt-proxy-detect"; - Acquire::https::Proxy "DIRECT"; 99unauth: - APT::Get::AllowUnauthenticated "true"; + fetch_url: https://paulbsd-pub.s3.fr-par.scw.cloud/apt + arch: amd64 diff --git a/states/apt/init.sls b/states/apt/init.sls index e3a3527..ded4978 100644 --- a/states/apt/init.sls +++ b/states/apt/init.sls @@ -6,19 +6,27 @@ apt-deps: - avahi-daemon - avahi-utils -apt-proxy-detect: +apt-proxy-detect-py: file.managed: - name: /usr/local/bin/apt-proxy-detect.py - source: salt://apt/templates/apt.py.j2 - template: jinja - mode: "0755" -{% for itemkey, itemvalue in apt.config.items() -%} -apt-proxy-{{ itemkey }}: +apt-proxy-detect-go: file.managed: - - name: /etc/apt/apt.conf.d/{{ itemkey }} + - name: /usr/local/bin/apt-proxy-detect + - source: {{ apt.fetch_url }}/apt_{{ apt.arch }} + - template: jinja + - skip_verify: true + - mode: "0755" + +{% for key, value in apt.config.items() -%} +apt-proxy-{{ key }}: + file.managed: + - name: /etc/apt/apt.conf.d/{{ key }} - user: root - group: root - mode: "0644" - - contents: {{ itemvalue }} + - contents: {{ value }} {% endfor -%} diff --git a/states/apt/map.jinja b/states/apt/map.jinja index e98f8ad..b8d357e 100644 --- a/states/apt/map.jinja +++ b/states/apt/map.jinja @@ -1,5 +1,11 @@ {%- import_yaml "apt/defaults.yaml" as default_settings %} -{%- set defaults = salt['grains.filter_by'](default_settings, default='apt') %} +{%- import_yaml "apt/osarchmap.yaml" as osarchmap -%} -{%- set apt = salt['pillar.get']('apt', default=defaults, merge=True) %} +{%- set defaults = salt['grains.filter_by'](default_settings, + default='apt', + merge=salt['grains.filter_by'](osarchmap, grain='osarch') + ) +-%} + +{%- set apt = salt['pillar.get']('apt', default=defaults, merge=True) -%} diff --git a/states/apt/osarchmap.yaml b/states/apt/osarchmap.yaml new file mode 100644 index 0000000..4bd82f8 --- /dev/null +++ b/states/apt/osarchmap.yaml @@ -0,0 +1,21 @@ +--- +amd64: + arch: "amd64" + +x86_64: + arch: "amd64" + +386: + arch: "386" + +arm64: + arch: "arm64" + +armv6l: + arch: "arm" + +armv7l: + arch: "arm" + +armhf: + arch: "arm" diff --git a/states/apt/templates/apt.go b/states/apt/templates/apt.go new file mode 100644 index 0000000..7b49172 --- /dev/null +++ b/states/apt/templates/apt.go @@ -0,0 +1,54 @@ +package main + +import ( + "context" + "fmt" + "net" + "time" + + "github.com/pion/mdns" + "golang.org/x/net/ipv4" +) + +type Proxy struct { + SCHEME string + HOSTNAME string + PORT int +} + +func (p *Proxy) Format() string { + return fmt.Sprintf(TMPL, p.SCHEME, p.HOSTNAME, p.PORT) +} + +var DEFAULT_PORT = 8000 +var DEFAULT_PROXY = Proxy{SCHEME: "http", HOSTNAME: "apt.paulbsd.com", PORT: 8001} + +const TMPL = "%s://%s:%d" + +func main() { + addr, err := net.ResolveUDPAddr("udp", mdns.DefaultAddress) + if err != nil { + panic(err) + } + + l, err := net.ListenUDP("udp4", addr) + if err != nil { + panic(err) + } + + server, err := mdns.Server(ipv4.NewPacketConn(l), &mdns.Config{}) + if err != nil { + panic(err) + } + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) + defer cancel() + _, src, err := server.Query(ctx, "apt.local") + if err != nil { + var res = DEFAULT_PROXY + fmt.Println(res.Format()) + return + } + + var res = Proxy{SCHEME: "http", HOSTNAME: src.String(), PORT: DEFAULT_PORT} + fmt.Println(res.Format()) +}