From d92d4e4400d37ff55963f43cd3685a16cb97ca79 Mon Sep 17 00:00:00 2001
From: Dmitry <b4tm4n@mail.ru>
Date: Tue, 16 May 2023 23:43:02 +0300
Subject: [PATCH] add update_files.sh

---
 config/gen_files_yml.sh | 31 +++++++++++++++----------------
 config/update_files.sh  | 22 ++++++++++++++++++++++
 2 files changed, 37 insertions(+), 16 deletions(-)
 create mode 100755 config/update_files.sh

diff --git a/config/gen_files_yml.sh b/config/gen_files_yml.sh
index 3adb552..1e28312 100755
--- a/config/gen_files_yml.sh
+++ b/config/gen_files_yml.sh
@@ -1,14 +1,14 @@
 #!/bin/bash
 
 tab() {
-    sed -e "s/^/$(printf %$1s)/"
+	sed -e "s/^/$(printf %$1s)/"
 }
 
 not_empty() {
-    sed -e "/^$/d"
+	sed -e "/^$/d"
 }
 
-define(){ IFS='\n' read -r -d '' ${1} || true; }
+define() { IFS='\n' read -r -d '' ${1} || true; }
 
 #ls -l | tab 10
 
@@ -19,22 +19,21 @@ permissions: '0644'
 EOF
 
 print_file() {
-    CONTENT=$(cat $1 | gzip | base64)
-    echo "- path: $2"
-    cat <<< $FILE_OPTS_COMMON | not_empty | tab 2
-    echo "  content: |"
-    cat <<< $CONTENT | tab 4
-    echo "    "
+	CONTENT=$(cat $1 | gzip | base64)
+	echo "- path: $2"
+	cat <<<"$FILE_OPTS_COMMON" | not_empty | tab 2
+	echo "  content: |"
+	cat <<<"$CONTENT" | tab 4
+	echo "    "
 }
 
 gen__write_files() {
-    echo "write_files:"
-    print_file src/srv1cv83@.service /etc/systemd/system/srv1cv83@.service | tab 2
-    print_file src/srv1cv83-ras@.service /etc/systemd/system/srv1cv83-ras@.service | tab 2
-    print_file src/apache-proxy /etc/nginx/sites-available/apache-proxy | tab 2
+	echo "write_files:"
+	print_file src/srv1cv83@.service /etc/systemd/system/srv1cv83@.service | tab 2
+	print_file src/srv1cv83-ras@.service /etc/systemd/system/srv1cv83-ras@.service | tab 2
+	print_file src/apache-proxy /etc/nginx/sites-available/apache-proxy | tab 2
 }
 
-pushd config
+pushd config >/dev/null 2>&1
 gen__write_files | tab 4
-popd
-
+popd >/dev/null 2>&1
diff --git a/config/update_files.sh b/config/update_files.sh
new file mode 100755
index 0000000..4f45939
--- /dev/null
+++ b/config/update_files.sh
@@ -0,0 +1,22 @@
+#!/bin/bash
+
+temp_yml() {
+	mktemp --suffix=.yml
+}
+
+gen_files() {
+	./gen_files_yml.sh >"$1"
+}
+
+update_files_from() {
+	SRC=$1 yq -i '.autoinstall.user-data.write_files |= load(strenv(SRC)).write_files' nocloud/user-data
+}
+
+pushd config >/dev/null 2>&1
+
+TMP_YML=$(temp_yml)
+gen_files "$TMP_YML"
+update_files_from "$TMP_YML"
+rm -f "$TMP_YML"
+
+popd >/dev/null 2>&1