bk node and refactor
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
[gdrive]
|
||||
type = drive
|
||||
client_id = {{ hostvars['console']['rclone']['google_drive']['oidc']['id'] }}
|
||||
client_secret = {{ hostvars['console']['rclone']['google_drive']['oidc']['secret'] }}
|
||||
scope = drive
|
||||
token = {{
|
||||
{
|
||||
"access_token":"expired",
|
||||
"token_type":"Bearer",
|
||||
"refresh_token":hostvars['console']['rclone']['google_drive']['refresh_token'],
|
||||
"expiry":"1970-01-01T00:00:00Z",
|
||||
} | to_json
|
||||
}}
|
||||
@@ -0,0 +1,28 @@
|
||||
[Unit]
|
||||
Description=Daily Kopia rclone backup service
|
||||
Wants=network-online.target
|
||||
After=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
|
||||
# logging
|
||||
StandardOutput=journal
|
||||
StandardError=journal
|
||||
|
||||
Environment="RCLONE_CONFIG=/etc/secrets/{{ node['uid'] }}/rclone/rclone.config"
|
||||
|
||||
ExecStart=/usr/bin/rclone copy %h/data/kopia/ gdrive:kopia \
|
||||
--transfers=4 \
|
||||
--checkers=4 \
|
||||
--tpslimit=8 \
|
||||
--tpslimit-burst=4 \
|
||||
--drive-chunk-size=64M \
|
||||
--retries=20 \
|
||||
--low-level-retries=10 \
|
||||
--retries-sleep=30s \
|
||||
--timeout=3m \
|
||||
--contimeout=30s \
|
||||
--drive-stop-on-upload-limit \
|
||||
--stats=30s \
|
||||
--log-level=INFO
|
||||
@@ -0,0 +1,10 @@
|
||||
[Unit]
|
||||
Description=Daily Kopia rclone backup timer
|
||||
|
||||
[Timer]
|
||||
OnCalendar=*-*-* 09:00:00
|
||||
Persistent=true
|
||||
RandomizedDelaySec=300
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
@@ -6,7 +6,7 @@
|
||||
//// Metric ouput
|
||||
prometheus.remote_write "prometheus" {
|
||||
endpoint {
|
||||
url = "https://{{ services['prometheus']['domain'] }}.{{ domain['internal'] }}:{{ services['prometheus']['ports']['https'] }}/api/v1/write"
|
||||
url = "https://{{ services['prometheus']['domain']['internal'] }}.{{ domain['internal'] }}:{{ services['prometheus']['ports']['https'] }}/api/v1/write"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,8 +71,8 @@ prometheus.scrape "system" {
|
||||
////// For Crowdsec metrics
|
||||
prometheus.scrape "crowdsec" {
|
||||
targets = [
|
||||
{ "__address__" = "{{ services['crowdsec']['domain'] }}.{{ domain['internal'] }}:6060", "job" = "crowdsec" },
|
||||
{ "__address__" = "{{ services['crowdsec']['domain'] }}.{{ domain['internal'] }}:60601", "job" = "crowdsec-bouncer" },
|
||||
{ "__address__" = "{{ services['crowdsec']['domain']['internal'] }}.{{ domain['internal'] }}:6060", "job" = "crowdsec" },
|
||||
{ "__address__" = "{{ services['crowdsec']['domain']['internal'] }}.{{ domain['internal'] }}:60601", "job" = "crowdsec-bouncer" },
|
||||
]
|
||||
honor_labels = true
|
||||
forward_to = [prometheus.relabel.default_label.receiver]
|
||||
@@ -83,7 +83,7 @@ prometheus.scrape "crowdsec" {
|
||||
////// For postgresql metrics
|
||||
prometheus.exporter.postgres "postgresql" {
|
||||
data_source_names = [
|
||||
"postgres://alloy@{{ services['postgresql']['domain'] }}.{{ domain['internal'] }}:{{ services['postgresql']['ports']['tcp'] }}/postgres?sslmode=verify-full",
|
||||
"postgres://alloy@{{ services['postgresql']['domain']['internal'] }}.{{ domain['internal'] }}:{{ services['postgresql']['ports']['tcp'] }}/postgres?sslmode=verify-full",
|
||||
]
|
||||
}
|
||||
prometheus.scrape "postgresql" {
|
||||
@@ -99,7 +99,7 @@ prometheus.scrape "x509" {
|
||||
}
|
||||
{% endif %}
|
||||
|
||||
{% if node['name'] in ['infra', 'auth', 'app'] %}
|
||||
{% if node['name'] in ["infra", "auth", "app"] %}
|
||||
////// For Input Caddy metrics
|
||||
prometheus.scrape "caddy" {
|
||||
targets = [
|
||||
@@ -114,7 +114,7 @@ prometheus.scrape "caddy" {
|
||||
//// Logs output
|
||||
loki.write "loki" {
|
||||
endpoint {
|
||||
url = "https://{{ services['loki']['domain'] }}.{{ domain['internal'] }}:{{ services['loki']['ports']['https'] }}/loki/api/v1/push"
|
||||
url = "https://{{ services['loki']['domain']['internal'] }}.{{ domain['internal'] }}:{{ services['loki']['ports']['https'] }}/loki/api/v1/push"
|
||||
tenant_id = "{{ domain['internal'] }}"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ log_compression: true
|
||||
log_max_size: 100
|
||||
log_max_backups: 3
|
||||
log_max_age: 30
|
||||
api_url: "https://{{ services['crowdsec']['domain'] }}.{{ domain['internal'] }}:{{ services['crowdsec']['ports']['https'] }}"
|
||||
api_url: "https://{{ services['crowdsec']['domain']['internal'] }}.{{ domain['internal'] }}:{{ services['crowdsec']['ports']['https'] }}"
|
||||
api_key: "{{ hostvars['console']['crowdsec']['bouncer']['fw'] }}"
|
||||
insecure_skip_verify: false
|
||||
disable_ipv6: false
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
url: https://{{ services['crowdsec']['domain'] }}.{{ domain['internal'] }}:{{ services['crowdsec']['ports']['https'] }}
|
||||
url: https://{{ services['crowdsec']['domain']['internal'] }}.{{ domain['internal'] }}:{{ services['crowdsec']['ports']['https'] }}
|
||||
login: {{ node['name'] }}
|
||||
password: {{ hostvars['console']['crowdsec']['machine'][node['name']] }}
|
||||
|
||||
@@ -28,8 +28,10 @@ BindPaths=/etc/secrets/{{ kopia_uid }}
|
||||
BindPaths=/var/cache/kopia
|
||||
EnvironmentFile=/etc/secrets/{{ kopia_uid }}/kopia.env
|
||||
|
||||
ExecStartPre=/usr/bin/nc -zv {{ services['kopia']['domain']['internal'] }}.{{ domain['internal'] }} 443
|
||||
|
||||
ExecStartPre=/usr/bin/kopia repository connect server \
|
||||
--url=https://{{ services['kopia']['domain'] }}.{{ domain['internal'] }}:{{ services['kopia']['ports']['https'] }} \
|
||||
--url=https://{{ services['kopia']['domain']['internal'] }}.{{ domain['internal'] }}:443 \
|
||||
--override-username={{ node['name'] }} \
|
||||
--override-hostname={{ node['name'] }}.{{ domain['internal'] }}
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
[Unit]
|
||||
Description=Kopia repository server
|
||||
Wants=network-online.target
|
||||
After=network-online.target
|
||||
|
||||
[Service]
|
||||
# logging
|
||||
StandardOutput=journal
|
||||
StandardError=journal
|
||||
|
||||
EnvironmentFile=/etc/secrets/{{ node['uid'] }}/kopia.env
|
||||
|
||||
ExecStartPre=/usr/bin/kopia repository connect filesystem \
|
||||
--path={{ node['home_path'] }}/data/kopia/repository
|
||||
|
||||
ExecStart=/usr/bin/kopia server start \
|
||||
--no-ui \
|
||||
--tls-cert-file=/etc/kopia/ssl/kopia.crt \
|
||||
--tls-key-file=/etc/kopia/ssl/kopia.key \
|
||||
--address=0.0.0.0:{{ services['kopia']['ports']['https'] }} \
|
||||
--log-level=info
|
||||
|
||||
[Install]
|
||||
WantedBy=default.target
|
||||
@@ -1,4 +1,8 @@
|
||||
{% if node['name'] == "bk" %}
|
||||
KOPIA_PASSWORD={{ hostvars['console']['kopia']['repository'] }}
|
||||
{% elif node['name'] in ["infra", "app"] %}
|
||||
KOPIA_PASSWORD={{ hostvars['console']['kopia']['users'][node['name']] }}
|
||||
{% endif %}
|
||||
KOPIA_CONFIG_PATH=/etc/kopia/repository.config
|
||||
KOPIA_CACHE_DIRECTORY=/var/cache/kopia
|
||||
KOPIA_LOG_DIR=/var/cache/kopia/logs
|
||||
|
||||
@@ -9,5 +9,4 @@ $TTL 86400
|
||||
)
|
||||
IN NS bind.ilnmors.internal.
|
||||
1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 IN PTR fw.ilnmors.internal.
|
||||
1.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0 IN PTR nas.ilnmors.internal.
|
||||
0.2.0.0.0.0.0.0.0.0.0.0.0.0.0.0 IN PTR console.ilnmors.internal.
|
||||
|
||||
@@ -9,5 +9,6 @@ $TTL 86400
|
||||
)
|
||||
IN NS bind.ilnmors.internal.
|
||||
1 IN PTR fw.ilnmors.internal.
|
||||
11 IN PTR nas.ilnmors.internal.
|
||||
2 IN PTR switch.ilnmors.internal.
|
||||
20 IN PTR console.ilnmors.internal.
|
||||
101 IN PTR printer.ilnmors.internal.
|
||||
|
||||
@@ -11,6 +11,7 @@ $TTL 86400
|
||||
1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 IN PTR fw.ilnmors.internal.
|
||||
2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 IN PTR blocky.ilnmors.internal.
|
||||
3.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 IN PTR bind.ilnmors.internal.
|
||||
9.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 IN PTR bk.ilnmors.internal.
|
||||
0.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0 IN PTR vmm.ilnmors.internal.
|
||||
1.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0 IN PTR infra.ilnmors.internal.
|
||||
2.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0 IN PTR auth.ilnmors.internal.
|
||||
|
||||
@@ -11,6 +11,7 @@ $TTL 86400
|
||||
1 IN PTR fw.ilnmors.internal.
|
||||
2 IN PTR blocky.ilnmors.internal.
|
||||
3 IN PTR bind.ilnmors.internal.
|
||||
9 IN PTR bk.ilnmors.internal.
|
||||
10 IN PTR vmm.ilnmors.internal.
|
||||
11 IN PTR infra.ilnmors.internal.
|
||||
12 IN PTR auth.ilnmors.internal.
|
||||
|
||||
@@ -8,5 +8,7 @@ $TTL 86400
|
||||
86400 ; minimum (1 day)
|
||||
)
|
||||
IN NS bind.ilnmors.internal.
|
||||
@ IN A 192.168.10.12
|
||||
@ IN AAAA fd00:10::12
|
||||
* IN A 192.168.10.12
|
||||
* IN AAAA fd00:10::12
|
||||
|
||||
@@ -14,6 +14,8 @@ fw IN A 192.168.10.1
|
||||
fw IN AAAA fd00:10::1
|
||||
blocky IN A 192.168.10.2
|
||||
blocky IN AAAA fd00:10::2
|
||||
bk IN A 192.168.10.9
|
||||
bk IN AAAA fd00:10::9
|
||||
vmm IN A 192.168.10.10
|
||||
vmm IN AAAA fd00:10::10
|
||||
infra IN A 192.168.10.11
|
||||
@@ -23,13 +25,12 @@ auth IN AAAA fd00:10::12
|
||||
app IN A 192.168.10.13
|
||||
app IN AAAA fd00:10::13
|
||||
switch IN A 192.168.1.2
|
||||
nas IN A 192.168.1.11
|
||||
nas IN AAAA fd00:1::11
|
||||
console IN A 192.168.1.20
|
||||
console IN AAAA fd00:1::20
|
||||
printer IN A 192.168.1.101
|
||||
ntp IN CNAME fw.ilnmors.internal.
|
||||
crowdsec IN CNAME fw.ilnmors.internal.
|
||||
kopia IN CNAME bk.ilnmors.internal.
|
||||
ca IN CNAME infra.ilnmors.internal.
|
||||
postgresql IN CNAME infra.ilnmors.internal.
|
||||
ldap IN CNAME infra.ilnmors.internal.
|
||||
|
||||
@@ -5,14 +5,14 @@ connectIPVersion: dual
|
||||
|
||||
ports:
|
||||
dns:
|
||||
- "{{ hostvars['fw']['network4']['blocky']['server'] }}:53"
|
||||
- "[{{ hostvars['fw']['network6']['blocky']['server'] }}]:53"
|
||||
- "{{ hostvars['fw']['network4']['blocky']['server'] }}:{{ services['blocky']['ports']['dns'] }}"
|
||||
- "[{{ hostvars['fw']['network6']['blocky']['server'] }}]:{{ services['blocky']['ports']['dns'] }}"
|
||||
tls:
|
||||
- "{{ hostvars['fw']['network4']['blocky']['server'] }}:853"
|
||||
- "[{{ hostvars['fw']['network6']['blocky']['server'] }}]:853"
|
||||
- "{{ hostvars['fw']['network4']['blocky']['server'] }}:{{ services['blocky']['ports']['dot'] }}"
|
||||
- "[{{ hostvars['fw']['network6']['blocky']['server'] }}]:{{ services['blocky']['ports']['dot'] }}"
|
||||
https:
|
||||
- "{{ hostvars['fw']['network4']['blocky']['server'] }}:443"
|
||||
- "[{{ hostvars['fw']['network6']['blocky']['server'] }}]:443"
|
||||
- "{{ hostvars['fw']['network4']['blocky']['server'] }}:{{ services['blocky']['ports']['https'] }}"
|
||||
- "[{{ hostvars['fw']['network6']['blocky']['server'] }}]:{{ services['blocky']['ports']['https'] }}"
|
||||
|
||||
log:
|
||||
level: info
|
||||
@@ -31,13 +31,13 @@ upstreams:
|
||||
conditional:
|
||||
fallbackUpstream: false
|
||||
mapping:
|
||||
ilnmors.internal: "{{ hostvars['fw']['network4']['bind']['server'] }}, {{ hostvars['fw']['network6']['bind']['server'] }}"
|
||||
ilnmors.com: "{{ hostvars['fw']['network4']['bind']['server'] }}, {{ hostvars['fw']['network6']['bind']['server'] }}"
|
||||
{{ domain['internal'] }}: "{{ hostvars['fw']['network4']['bind']['server'] }}, {{ hostvars['fw']['network6']['bind']['server'] }}"
|
||||
{{ domain['public'] }}: "{{ hostvars['fw']['network4']['bind']['server'] }}, {{ hostvars['fw']['network6']['bind']['server'] }}"
|
||||
1.168.192.in-addr.arpa: "{{ hostvars['fw']['network4']['bind']['server'] }}, {{ hostvars['fw']['network6']['bind']['server'] }}"
|
||||
10.168.192.in-addr.arpa: "{{ hostvars['fw']['network4']['bind']['server'] }}, {{ hostvars['fw']['network6']['bind']['server'] }}"
|
||||
0.0.0.0.0.0.0.0.1.0.0.0.0.0.d.f.ip6.arpa: "{{ hostvars['fw']['network4']['bind']['server'] }}, {{ hostvars['fw']['network6']['bind']['server'] }}"
|
||||
0.0.0.0.0.0.0.0.0.1.0.0.0.0.d.f.ip6.arpa: "{{ hostvars['fw']['network4']['bind']['server'] }}, {{ hostvars['fw']['network6']['bind']['server'] }}"
|
||||
vpn.ilnmors.com: "tcp-tls:1.1.1.1:853, tcp-tls:1.0.0.1:853, tcp-tls:[2606:4700:4700::1111]:853, tcp-tls:[2606:4700:4700::1001]:853"
|
||||
vpn.{{ domain['public'] }}: "tcp-tls:1.1.1.1:853, tcp-tls:1.0.0.1:853, tcp-tls:[2606:4700:4700::1111]:853, tcp-tls:[2606:4700:4700::1001]:853"
|
||||
|
||||
blocking:
|
||||
blockType: nxDomain
|
||||
|
||||
@@ -17,25 +17,27 @@ CURRENT_IP=""
|
||||
|
||||
# usage() function
|
||||
usage() {
|
||||
echo "Usage: $0 -d \"domain\" [-t \"ttl\"] [-p] [-r]"
|
||||
echo "-d <domain>: Specify the domain to update"
|
||||
echo "-t <ttl>: Specify the TTL(Time to live)"
|
||||
echo "-p: Specify the cloudflare proxy to use"
|
||||
echo "-r: Delete the DNS record"
|
||||
exit 1
|
||||
echo "Usage: $0 -d \"domain\" [-t \"ttl\"] [-p] [-r]"
|
||||
echo "-d <domain>: Specify the domain to update"
|
||||
echo "-t <ttl>: Specify the TTL(Time to live)"
|
||||
echo "-p: Specify the cloudflare proxy to use"
|
||||
echo "-r: Delete the DNS record"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Log function
|
||||
log() {
|
||||
local timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
local timestamp
|
||||
local level="$1"
|
||||
local msg="$2"
|
||||
echo "time=\"$timestamp\" level=\"$level\" msg=\"$msg\" source=\"ddns.sh\"">&2
|
||||
|
||||
timestamp="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
|
||||
echo "time=\"$timestamp\" level=\"$level\" msg=\"$msg\" source=\"ddns.sh\"" >&2
|
||||
}
|
||||
|
||||
# getopts to get arguments
|
||||
while getopts "d:t:pr" opt; do
|
||||
case $opt in
|
||||
case "$opt" in
|
||||
d)
|
||||
DOMAIN="$OPTARG"
|
||||
;;
|
||||
@@ -60,26 +62,26 @@ while getopts "d:t:pr" opt; do
|
||||
done
|
||||
|
||||
# Get option and move to parameters - This has no functional thing, because it only use arguments with parameters
|
||||
shift $((OPTIND - 1))
|
||||
shift "$((OPTIND - 1))"
|
||||
|
||||
# Check necessary options
|
||||
if [ -z "$DOMAIN" ]; then
|
||||
if [[ -z "$DOMAIN" ]]; then
|
||||
log "error" "-d option is required"
|
||||
usage
|
||||
fi
|
||||
|
||||
if ! [[ "$TTL" =~ ^[0-9]+$ ]] || [ "$TTL" -le 0 ]; then
|
||||
if ! [[ "$TTL" =~ ^[0-9]+$ ]] || [[ "$TTL" -le 0 ]]; then
|
||||
log "error" "-t option (ttl) requires a number above 0."
|
||||
usage
|
||||
fi
|
||||
|
||||
# Check necessary environment variables (Injected by systemd or shell)
|
||||
if [ -z "$ZONE_ID" ]; then
|
||||
if [[ -z "$ZONE_ID" ]]; then
|
||||
log "error" "ZONE_ID is required via environment variable."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -z "$API_KEY" ]; then
|
||||
if [[ -z "$API_KEY" ]]; then
|
||||
log "error" "API_KEY is required via environment variable."
|
||||
exit 1
|
||||
fi
|
||||
@@ -89,6 +91,7 @@ if ! command -v curl >/dev/null; then
|
||||
log "error" "curl is required"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! command -v jq >/dev/null; then
|
||||
log "error" "jq is required"
|
||||
exit 1
|
||||
@@ -100,32 +103,61 @@ CONTENT_TYPE="Content-Type: application/json"
|
||||
AUTHORIZATION="Authorization: Bearer $API_KEY"
|
||||
|
||||
# Current IP
|
||||
CURRENT_IP=$( ip address show dev wan | grep 'inet ' | awk '{print $2}' | cut -d'/' -f1 )
|
||||
CURRENT_IP="$(ip address show dev wan | grep 'inet ' | awk '{print $2}' | cut -d'/' -f1)"
|
||||
|
||||
# Get current IP from external server when IP is private IP
|
||||
if [[ -z "$CURRENT_IP" || "$CURRENT_IP" =~ ^(10\.|172\.(1[6-9]|2[0-9]|3[0-1])\.|192\.168\.|127\.) ]]; then
|
||||
log "info" "IP from interface is private or empty. Fetching public IP..."
|
||||
CURRENT_IP=$(curl -sf "https://ifconfig.me") ||\
|
||||
CURRENT_IP=$(curl -sf "https://ifconfig.kr") ||\
|
||||
CURRENT_IP=$(curl -sf "https://api.ipify.org")
|
||||
CURRENT_IP="$(curl -sf "https://ifconfig.me")" || \
|
||||
CURRENT_IP="$(curl -sf "https://ifconfig.kr")" || \
|
||||
CURRENT_IP="$(curl -sf "https://api.ipify.org")"
|
||||
fi
|
||||
if [ "$CURRENT_IP" == "" ]; then
|
||||
|
||||
if [[ -z "$CURRENT_IP" ]]; then
|
||||
log "error" "Can't get an IP"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# DNS functions
|
||||
|
||||
# get_dns_record() function
|
||||
get_dns_record()
|
||||
{
|
||||
# build_dns_payload() function
|
||||
build_dns_payload() {
|
||||
local type="$1"
|
||||
local name="$2"
|
||||
local ttl="$3"
|
||||
local comment="$4"
|
||||
local content="$5"
|
||||
|
||||
local response="$(
|
||||
curl -s "$URL?type=$type&name=$name"\
|
||||
-H "$CONTENT_TYPE"\
|
||||
-H "$AUTHORIZATION")"
|
||||
if [ "$(echo "$response" | jq -r '.success')" == "false" ]; then
|
||||
jq -n \
|
||||
--arg name "$name" \
|
||||
--argjson ttl "$ttl" \
|
||||
--arg type "$type" \
|
||||
--arg comment "$comment" \
|
||||
--arg content "$content" \
|
||||
--argjson proxied "$PROXIED" \
|
||||
'{
|
||||
name: $name,
|
||||
ttl: $ttl,
|
||||
type: $type,
|
||||
comment: $comment,
|
||||
content: $content,
|
||||
proxied: $proxied
|
||||
}'
|
||||
}
|
||||
|
||||
# get_dns_record() function
|
||||
get_dns_record() {
|
||||
local type="$1"
|
||||
local name="$2"
|
||||
local response
|
||||
|
||||
response="$(
|
||||
curl -s "$URL?type=$type&name=$name" \
|
||||
-H "$CONTENT_TYPE" \
|
||||
-H "$AUTHORIZATION"
|
||||
)"
|
||||
|
||||
if [[ "$(jq -r '.success' <<< "$response")" == "false" ]]; then
|
||||
log "error" "Can't get dns record by $response"
|
||||
exit 1
|
||||
else
|
||||
@@ -135,27 +167,26 @@ get_dns_record()
|
||||
}
|
||||
|
||||
# create_dns_record() function
|
||||
create_dns_record()
|
||||
{
|
||||
create_dns_record() {
|
||||
local type="$1"
|
||||
local name="$2"
|
||||
local ttl="$3"
|
||||
local comment="$4"
|
||||
local content="$5"
|
||||
local response="$(
|
||||
curl -s "$URL"\
|
||||
-X POST\
|
||||
-H "$CONTENT_TYPE"\
|
||||
-H "$AUTHORIZATION"\
|
||||
-d "{
|
||||
\"name\": \"$name\",
|
||||
\"ttl\": $ttl,
|
||||
\"type\": \"$type\",
|
||||
\"comment\": \"$comment\",
|
||||
\"content\": \"$content\",
|
||||
\"proxied\": $PROXIED
|
||||
}")"
|
||||
if [ "$(echo "$response" | jq -r '.success')" == "false" ]; then
|
||||
local payload
|
||||
local response
|
||||
|
||||
payload="$(build_dns_payload "$type" "$name" "$ttl" "$comment" "$content")"
|
||||
|
||||
response="$(
|
||||
curl -s "$URL" \
|
||||
-X POST \
|
||||
-H "$CONTENT_TYPE" \
|
||||
-H "$AUTHORIZATION" \
|
||||
-d "$payload"
|
||||
)"
|
||||
|
||||
if [[ "$(jq -r '.success' <<< "$response")" == "false" ]]; then
|
||||
log "error" "Can't create dns record by $response"
|
||||
exit 1
|
||||
else
|
||||
@@ -165,49 +196,49 @@ create_dns_record()
|
||||
}
|
||||
|
||||
# update_dns_record() function
|
||||
update_dns_record()
|
||||
{
|
||||
update_dns_record() {
|
||||
local type="$1"
|
||||
local name="$2"
|
||||
local ttl="$3"
|
||||
local comment="$4"
|
||||
local content="$5"
|
||||
local id="$6"
|
||||
local response=$(
|
||||
curl -s "$URL/$id"\
|
||||
-X PUT\
|
||||
-H "$CONTENT_TYPE"\
|
||||
-H "$AUTHORIZATION"\
|
||||
-d "{
|
||||
\"name\": \"$name\",
|
||||
\"ttl\": $ttl,
|
||||
\"type\": \"$type\",
|
||||
\"comment\": \"$comment\",
|
||||
\"content\": \"$content\",
|
||||
\"proxied\": $PROXIED
|
||||
}")
|
||||
if [ "$(echo "$response" | jq -r '.success')" == "false" ]; then
|
||||
local payload
|
||||
local response
|
||||
|
||||
payload="$(build_dns_payload "$type" "$name" "$ttl" "$comment" "$content")"
|
||||
|
||||
response="$(
|
||||
curl -s "$URL/$id" \
|
||||
-X PUT \
|
||||
-H "$CONTENT_TYPE" \
|
||||
-H "$AUTHORIZATION" \
|
||||
-d "$payload"
|
||||
)"
|
||||
|
||||
if [[ "$(jq -r '.success' <<< "$response")" == "false" ]]; then
|
||||
log "error" "Can't update dns record by $response"
|
||||
exit 1
|
||||
else
|
||||
#return
|
||||
# return
|
||||
echo "$response"
|
||||
fi
|
||||
}
|
||||
|
||||
# delete_dns_record() function
|
||||
delete_dns_record()
|
||||
{
|
||||
delete_dns_record() {
|
||||
local type="$1"
|
||||
local id="$2"
|
||||
local response
|
||||
|
||||
local response=$(
|
||||
curl -s "$URL/$id"\
|
||||
-X DELETE\
|
||||
-H "$CONTENT_TYPE"\
|
||||
response="$(
|
||||
curl -s "$URL/$id" \
|
||||
-X DELETE \
|
||||
-H "$CONTENT_TYPE" \
|
||||
-H "$AUTHORIZATION"
|
||||
)
|
||||
if [ "$(echo "$response" | jq -r '.success')" == "false" ]; then
|
||||
)"
|
||||
|
||||
if [[ "$(jq -r '.success' <<< "$response")" == "false" ]]; then
|
||||
log "error" "Can't delete dns record by $response"
|
||||
exit 1
|
||||
else
|
||||
@@ -217,44 +248,50 @@ delete_dns_record()
|
||||
}
|
||||
|
||||
# Get DNS A, and CNAME record
|
||||
A_DNS_RECORD=$(get_dns_record "A" "$DOMAIN")
|
||||
S_DNS_RECORD=$(get_dns_record "CNAME" "*.$DOMAIN")
|
||||
W_DNS_RECORD=$(get_dns_record "CNAME" "www.$DOMAIN")
|
||||
A_DNS_RECORD="$(get_dns_record "A" "$DOMAIN")"
|
||||
S_DNS_RECORD="$(get_dns_record "CNAME" "*.$DOMAIN")"
|
||||
W_DNS_RECORD="$(get_dns_record "CNAME" "www.$DOMAIN")"
|
||||
|
||||
# Delete DNS record with Delete flag
|
||||
if [ "$DELETE_FLAG" == "true" ]; then
|
||||
if [[ "$DELETE_FLAG" == "true" ]]; then
|
||||
FLAG="false"
|
||||
if [ "$(echo $A_DNS_RECORD | jq -r '.result | length')" -eq 1 ]; then
|
||||
A_DNS_ID="$(echo $A_DNS_RECORD | jq -r '.result[0].id')"
|
||||
|
||||
if [[ "$(jq -r '.result | length' <<< "$A_DNS_RECORD")" -eq 1 ]]; then
|
||||
A_DNS_ID="$(jq -r '.result[0].id' <<< "$A_DNS_RECORD")"
|
||||
delete_dns_record "A" "$A_DNS_ID"
|
||||
log "info" "root DNS record is deleted"
|
||||
FLAG="true"
|
||||
fi
|
||||
if [ "$(echo $S_DNS_RECORD | jq -r '.result | length')" -eq 1 ]; then
|
||||
S_DNS_ID="$(echo $S_DNS_RECORD | jq -r '.result[0].id')"
|
||||
|
||||
if [[ "$(jq -r '.result | length' <<< "$S_DNS_RECORD")" -eq 1 ]]; then
|
||||
S_DNS_ID="$(jq -r '.result[0].id' <<< "$S_DNS_RECORD")"
|
||||
delete_dns_record "CNAME" "$S_DNS_ID"
|
||||
log "info" "sub DNS record is deleted"
|
||||
FLAG="true"
|
||||
fi
|
||||
if [ "$(echo $W_DNS_RECORD | jq -r '.result | length')" -eq 1 ]; then
|
||||
W_DNS_ID="$(echo $W_DNS_RECORD | jq -r '.result[0].id')"
|
||||
|
||||
if [[ "$(jq -r '.result | length' <<< "$W_DNS_RECORD")" -eq 1 ]]; then
|
||||
W_DNS_ID="$(jq -r '.result[0].id' <<< "$W_DNS_RECORD")"
|
||||
delete_dns_record "CNAME" "$W_DNS_ID"
|
||||
log "info" "www DNS record is deleted"
|
||||
FLAG="true"
|
||||
fi
|
||||
if [ "$FLAG" == "false" ]; then
|
||||
|
||||
if [[ "$FLAG" == "false" ]]; then
|
||||
log "info" "Nothing is Deleted. There are no DNS records"
|
||||
fi
|
||||
|
||||
exit
|
||||
fi
|
||||
|
||||
# Create or update DNS A record
|
||||
if [ "$(echo $A_DNS_RECORD | jq -r '.result | length')" -eq 1 ]; then # root DNS record exist
|
||||
A_DNS_ID="$(echo $A_DNS_RECORD | jq -r '.result[0].id')"
|
||||
A_DNS_CONTENT="$(echo $A_DNS_RECORD | jq -r '.result[0].content')"
|
||||
A_DNS_TTL="$(echo $A_DNS_RECORD | jq -r '.result[0].ttl')"
|
||||
A_DNS_PROXIED="$(echo $A_DNS_RECORD | jq -r '.result[0].proxied')"
|
||||
if [ "$A_DNS_CONTENT" != $CURRENT_IP -o "$A_DNS_TTL" != "$TTL" -o "$A_DNS_PROXIED" != "$PROXIED" ]; then
|
||||
if [[ "$(jq -r '.result | length' <<< "$A_DNS_RECORD")" -eq 1 ]]; then # root DNS record exist
|
||||
A_DNS_ID="$(jq -r '.result[0].id' <<< "$A_DNS_RECORD")"
|
||||
A_DNS_CONTENT="$(jq -r '.result[0].content' <<< "$A_DNS_RECORD")"
|
||||
A_DNS_TTL="$(jq -r '.result[0].ttl' <<< "$A_DNS_RECORD")"
|
||||
A_DNS_PROXIED="$(jq -r '.result[0].proxied' <<< "$A_DNS_RECORD")"
|
||||
|
||||
if [[ "$A_DNS_CONTENT" != "$CURRENT_IP" || "$A_DNS_TTL" != "$TTL" || "$A_DNS_PROXIED" != "$PROXIED" ]]; then
|
||||
update_dns_record "A" "$DOMAIN" "$TTL" "$(date "+%Y-%m-%d %H:%M:%S"): root domain from ddns.sh" "$CURRENT_IP" "$A_DNS_ID"
|
||||
log "info" "Root DNS record is successfully changed Domain: $DOMAIN IP: $A_DNS_CONTENT to $CURRENT_IP TTL: $A_DNS_TTL to $TTL proxied: $A_DNS_PROXIED to $PROXIED"
|
||||
else
|
||||
@@ -266,12 +303,13 @@ else # root DNS record does not exist
|
||||
fi
|
||||
|
||||
# Create or update DNS CNAME records
|
||||
if [ "$(echo $S_DNS_RECORD | jq -r '.result | length')" -eq 1 ]; then # sub DNS record exist
|
||||
S_DNS_ID="$(echo $S_DNS_RECORD | jq -r '.result[0].id')"
|
||||
S_DNS_CONTENT="$(echo $S_DNS_RECORD | jq -r '.result[0].content')"
|
||||
S_DNS_TTL="$(echo $S_DNS_RECORD | jq -r '.result[0].ttl')"
|
||||
S_DNS_PROXIED="$(echo $S_DNS_RECORD | jq -r '.result[0].proxied')"
|
||||
if [ "$S_DNS_CONTENT" != "$DOMAIN" -o "$S_DNS_TTL" != "$C_TTL" -o "$S_DNS_PROXIED" != "$PROXIED" ]; then
|
||||
if [[ "$(jq -r '.result | length' <<< "$S_DNS_RECORD")" -eq 1 ]]; then # sub DNS record exist
|
||||
S_DNS_ID="$(jq -r '.result[0].id' <<< "$S_DNS_RECORD")"
|
||||
S_DNS_CONTENT="$(jq -r '.result[0].content' <<< "$S_DNS_RECORD")"
|
||||
S_DNS_TTL="$(jq -r '.result[0].ttl' <<< "$S_DNS_RECORD")"
|
||||
S_DNS_PROXIED="$(jq -r '.result[0].proxied' <<< "$S_DNS_RECORD")"
|
||||
|
||||
if [[ "$S_DNS_CONTENT" != "$DOMAIN" || "$S_DNS_TTL" != "$C_TTL" || "$S_DNS_PROXIED" != "$PROXIED" ]]; then
|
||||
update_dns_record "CNAME" "*.$DOMAIN" "$C_TTL" "$(date "+%Y-%m-%d %H:%M:%S"): sub domain from ddns.sh" "$DOMAIN" "$S_DNS_ID"
|
||||
log "info" "Sub DNS record is successfully changed Domain: $S_DNS_CONTENT to *.$DOMAIN CNAME: $DOMAIN TTL: $S_DNS_TTL to $C_TTL proxied: $S_DNS_PROXIED to $PROXIED"
|
||||
else
|
||||
@@ -282,12 +320,13 @@ else # sub DNS record does not exist
|
||||
log "info" "Sub DNS record is successfully created Domain: *.$DOMAIN CNAME: $DOMAIN TTL: $C_TTL proxied: $PROXIED"
|
||||
fi
|
||||
|
||||
if [ "$(echo $W_DNS_RECORD | jq -r '.result | length')" -eq 1 ]; then # www DNS record exist
|
||||
W_DNS_ID="$(echo $W_DNS_RECORD | jq -r '.result[0].id')"
|
||||
W_DNS_CONTENT="$(echo $W_DNS_RECORD | jq -r '.result[0].content')"
|
||||
W_DNS_TTL="$(echo $W_DNS_RECORD | jq -r '.result[0].ttl')"
|
||||
W_DNS_PROXIED="$(echo $W_DNS_RECORD | jq -r '.result[0].proxied')"
|
||||
if [ "$W_DNS_CONTENT" != "$DOMAIN" -o "$W_DNS_TTL" != "$C_TTL" -o "$W_DNS_PROXIED" != "$PROXIED" ]; then
|
||||
if [[ "$(jq -r '.result | length' <<< "$W_DNS_RECORD")" -eq 1 ]]; then # www DNS record exist
|
||||
W_DNS_ID="$(jq -r '.result[0].id' <<< "$W_DNS_RECORD")"
|
||||
W_DNS_CONTENT="$(jq -r '.result[0].content' <<< "$W_DNS_RECORD")"
|
||||
W_DNS_TTL="$(jq -r '.result[0].ttl' <<< "$W_DNS_RECORD")"
|
||||
W_DNS_PROXIED="$(jq -r '.result[0].proxied' <<< "$W_DNS_RECORD")"
|
||||
|
||||
if [[ "$W_DNS_CONTENT" != "$DOMAIN" || "$W_DNS_TTL" != "$C_TTL" || "$W_DNS_PROXIED" != "$PROXIED" ]]; then
|
||||
update_dns_record "CNAME" "www.$DOMAIN" "$C_TTL" "$(date "+%Y-%m-%d %H:%M:%S"): www domain from ddns.sh" "$DOMAIN" "$W_DNS_ID"
|
||||
log "info" "www DNS record is successfully changed Domain: $W_DNS_CONTENT to www.$DOMAIN CNAME: $DOMAIN TTL: $W_DNS_TTL to $C_TTL proxied: $W_DNS_PROXIED to $PROXIED"
|
||||
else
|
||||
|
||||
@@ -28,15 +28,10 @@
|
||||
"ip-address": "{{ hostvars['fw']['network4']['switch']['client'] }}",
|
||||
"hostname": "switch"
|
||||
},
|
||||
{
|
||||
"hw-address": "90:09:d0:65:a9:db",
|
||||
"ip-address": "{{ hostvars['fw']['network4']['nas']['client'] }}",
|
||||
"hostname": "nas"
|
||||
},
|
||||
{
|
||||
"hw-address": "d8:e2:df:ff:1b:d5",
|
||||
"ip-address": "{{ hostvars['fw']['network4']['console']['client'] }}",
|
||||
"hostname": "surface"
|
||||
"hostname": "console"
|
||||
},
|
||||
{
|
||||
"hw-address": "38:ca:84:94:5e:06",
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
[Unit]
|
||||
Description=BTRFS backup auto scrub
|
||||
ConditionPathIsMountPoint={{ hardware['disk']['backup']['fs']['mount_point'] }}
|
||||
RequiresMountsFor={{ hardware['disk']['backup']['fs']['mount_point'] }}
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/usr/bin/btrfs scrub start -Bd {{ hardware['disk']['backup']['fs']['mount_point'] }}
|
||||
Nice=19
|
||||
IOSchedulingClass=idle
|
||||
@@ -0,0 +1,10 @@
|
||||
[Unit]
|
||||
Description=BTRFS data auto scrub
|
||||
ConditionPathIsMountPoint={{ hardware['disk']['data']['fs']['mount_point'] }}
|
||||
RequiresMountsFor={{ hardware['disk']['data']['fs']['mount_point'] }}
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/usr/bin/btrfs scrub start -Bd {{ hardware['disk']['data']['fs']['mount_point'] }}
|
||||
Nice=19
|
||||
IOSchedulingClass=idle
|
||||
@@ -0,0 +1,10 @@
|
||||
[Unit]
|
||||
Description=Monthly BTRFS auto scrub
|
||||
|
||||
[Timer]
|
||||
OnCalendar=*-*-01 04:00:00
|
||||
Persistent=true
|
||||
RandomizedDelaySec=300
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
@@ -1,10 +0,0 @@
|
||||
[Unit]
|
||||
Description=BTRFS auto scrub
|
||||
ConditionPathIsMountPoint={{ hardware['btrfs']['mount_point'] }}
|
||||
RequiresMountsFor={{ hardware['btrfs']['mount_point'] }}
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/usr/bin/btrfs scrub start -Bd {{ hardware['btrfs']['mount_point'] }}
|
||||
Nice=19
|
||||
IOSchedulingClass=idle
|
||||
+4
-2
@@ -1,7 +1,9 @@
|
||||
[Unit]
|
||||
Description=app vm
|
||||
After=network-online.target libvirtd.service fw.service infra.service auth.service
|
||||
Wants=fw.service infra.service auth.service
|
||||
After=network-online.target libvirtd.service fw.service bk.service infra.service auth.service
|
||||
Wants=fw.service bk.service infra.service auth.service
|
||||
ConditionPathIsMountPoint={{ hardware['disk']['data']['fs']['mount_point'] }}
|
||||
RequiresMountsFor={{ hardware['disk']['data']['fs']['mount_point'] }}
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
@@ -0,0 +1,25 @@
|
||||
[Unit]
|
||||
Description=bk vm
|
||||
After=network-online.target libvirtd.service fw.service
|
||||
Wants=fw.service
|
||||
ConditionPathIsMountPoint={{ hardware['disk']['backup']['fs']['mount_point'] }}
|
||||
RequiresMountsFor={{ hardware['disk']['backup']['fs']['mount_point'] }}
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
RemainAfterExit=yes
|
||||
TimeoutStopSec=360
|
||||
|
||||
ExecStart=/usr/bin/virsh -c qemu:///system start bk
|
||||
|
||||
ExecStartPost=/bin/sleep 30
|
||||
|
||||
ExecStop=/bin/bash -c '\
|
||||
/usr/bin/virsh -c qemu:///system shutdown bk; \
|
||||
while /usr/bin/virsh -c qemu:///system list --state-running --name | grep -q "bk"; do \
|
||||
echo "Waiting for bk to shutdown..."; \
|
||||
sleep 2; \
|
||||
done'
|
||||
|
||||
[Install]
|
||||
WantedBy=default.target
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
[Unit]
|
||||
Description=infra vm
|
||||
After=network-online.target libvirtd.service fw.service
|
||||
Wants=fw.service
|
||||
After=network-online.target libvirtd.service fw.service bk.service
|
||||
Wants=fw.service bk.service
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
@@ -0,0 +1,8 @@
|
||||
<!-- Storage pool define -->
|
||||
<!-- backup-pool.xml -->
|
||||
<pool type='dir'>
|
||||
<name>backup-pool</name>
|
||||
<target>
|
||||
<path>/var/lib/libvirt/disk/backup</path>
|
||||
</target>
|
||||
</pool>
|
||||
@@ -3,6 +3,6 @@
|
||||
<pool type='dir'>
|
||||
<name>data-pool</name>
|
||||
<target>
|
||||
<path>/var/lib/libvirt/data</path>
|
||||
<path>/var/lib/libvirt/disk/data</path>
|
||||
</target>
|
||||
</pool>
|
||||
|
||||
@@ -70,10 +70,14 @@
|
||||
</source>
|
||||
<address type='pci' domain='{{ device['domain'] }}' bus='{{ device['bus'] }}' slot='{{ device['slot'] }}' function='{{ device['function'] }}'/>
|
||||
</hostdev>
|
||||
{% endif %}
|
||||
{% if hostvars[target_vm]['vm']['data_storage'] is defined %}
|
||||
{% set pool = hostvars[target_vm]['vm']['data_storage']['pool'] %}
|
||||
{% set disk = hostvars['vmm']['hardware']['disk'][pool]['fs'] %}
|
||||
<disk type='file' device='disk'>
|
||||
<driver name='qemu' type='raw' />
|
||||
<source file='{{ hardware['btrfs']['mount_point'] }}/{{ target_vm }}.raw' />
|
||||
<target dev='vdb' bus='virtio' />
|
||||
<source file='{{ disk['mount_point'] }}/{{ hostvars[target_vm]['vm']['name'] }}.raw' />
|
||||
<target dev='{{ hostvars[target_vm]['vm']['data_storage']['name'] }}' bus='virtio' />
|
||||
</disk>
|
||||
{% endif %}
|
||||
<tpm model='tpm-crb'>
|
||||
|
||||
Reference in New Issue
Block a user