From b0b809f781b78ff19c6800d679b6d27e03b5f70b Mon Sep 17 00:00:00 2001 From: Jean-BaPtiste Besselat Date: Thu, 21 Nov 2019 09:41:17 +0100 Subject: [PATCH] [lsw] add netplan if config --- .../com/linkfluence/inventory/leaseweb.clj | 27 ++++++++++++++++--- src/clj/com/linkfluence/inventory/net.clj | 7 +++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/clj/com/linkfluence/inventory/leaseweb.clj b/src/clj/com/linkfluence/inventory/leaseweb.clj index 8634a17..8ef146b 100644 --- a/src/clj/com/linkfluence/inventory/leaseweb.clj +++ b/src/clj/com/linkfluence/inventory/leaseweb.clj @@ -13,7 +13,8 @@ [chime :refer [chime-at]] [clj-time.core :as t] [clj-time.periodic :refer [periodic-seq]] - [clojure.spec.alpha :as spec]) + [clojure.spec.alpha :as spec] + [clj-yaml.core :as yaml]) (:import [java.io File] [java.util.concurrent LinkedBlockingQueue])) @@ -60,14 +61,16 @@ (defn load-inventory! [] ;;default use s3 + (when-not @test (when-let [inventory (store/load-map (assoc (:store @lsw-conf) :key (str (:key (:store @lsw-conf)) "-inventory")))] - (reset! lsw-inventory inventory))) + (reset! lsw-inventory inventory)))) (defn load-pscheme! [] ;;default use s3 + (when-not @test (when-let [pscheme (store/load-map (assoc (:store @lsw-conf) :key (str (:key (:store @lsw-conf)) "-pscheme")))] - (reset! lsw-pscheme pscheme))) + (reset! lsw-pscheme pscheme)))) (defn save-pscheme "Save on both local file and s3" @@ -241,6 +244,23 @@ (str "ifconfig " ifname " up") "sleep 20"]) +(defn- configure-netplan-if + [ifname server-id n] + (let [nfile (str "/etc/netplan/02-" ifname ".yaml") + netplan-conf {:network { + :version 2 + :ethernets { + (keyword ifname) { + :dhcp4 "no" + :addresses [ + (str + (get-available-address server-id) + (net/get-cidr (:network n) (:netmask n))) + ]}}}}] + [(str "echo \"" (yaml/generate-string netplan-conf)"\" > " nfile) + (str "netplan try") + "sleep 20"])) + (defn- configure-red-hat-if [ifname server-id n] [(str "echo \"DEVICE=" ifname "\" >> /etc/sysconfig/network-scripts/ifcfg-" ifname) @@ -260,6 +280,7 @@ (condp = (get @lsw-conf :base-distrib :debian) :debian (configure-debian-if ifname server-id n) :red-hat (configure-red-hat-if ifname server-id n) + :netplan (configure-netplan-if ifname server-id n) [])) (defn- choose-if diff --git a/src/clj/com/linkfluence/inventory/net.clj b/src/clj/com/linkfluence/inventory/net.clj index d24eaa6..f305c98 100644 --- a/src/clj/com/linkfluence/inventory/net.clj +++ b/src/clj/com/linkfluence/inventory/net.clj @@ -21,6 +21,13 @@ ([network netmask address] (.isInRange (get-subnet-info network netmask) address))) +(defn get-cidr + [network netmask] + (str "/" + (last (.split + (.getCidrSignature (get-subnet-info network netmask)) + "/")))) + (defn get-broadcast-addr ([cidr-address] (.getBroadcastAddress (get-subnet-info cidr-address)))