-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxmox__inc.jsons.basic_vm_actions.to.jsons.sh
More file actions
executable file
·341 lines (269 loc) · 12.3 KB
/
proxmox__inc.jsons.basic_vm_actions.to.jsons.sh
File metadata and controls
executable file
·341 lines (269 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/bin/bash
#
# PR-25
#
show_example() {
echo " echo 'px-testing' | $(basename "$0") network_list_interfaces_node"
}
if [ "$1" = '-h' ] ||
[ "$1" = '--help' ]; then
echo
echo NAME
echo
echo " $(basename "$0") - LIB / INCLUDE script providing basic generic helper "
echo
echo OPTIONS
echo
echo " $(basename "$0") [-h|--help] "
echo " STDIN :: [JSON] | $(basename "$0") [--json] - force output as json *default "
echo " STDIN :: [JSON] | $(basename "$0") [--text] - force output as text"
echo ""
echo EXAMPLE
echo
echo "$(show_example)"
echo
echo
exit 1
fi
#### #### #### #### #### #### #### #### #### #### #### #### #### #### #### ####
EXTRA_VAR=""
DEBUG=true
# DEBUG=false
inject_yaml_var() {
local KEY="$1"
local VALUE="$2"
local INDENTATION=" " # 8 spaces
VALUE="${VALUE//\"/\\\"}" # escaping.
#
# FORMAT YAML AND CONTENT TO EXTRA_VAR
#
# EXTRA_VAR+="${IDENTATION}${KEY}: \"${VALUE}\"\n"
EXTRA_VAR+=$(printf '\n%s%s: "%s"\n' "$INDENTATION" "$KEY" "$VALUE")
}
assign_if_not_empty() {
local KEY_NAME="$1"
local JSON_LINE="$2"
local JQ_EXPR="$3"
local NEW_VALUE
NEW_VALUE=$(
printf "%s\n" "$JSON_LINE" |
jq -r "$JQ_EXPR // empty"
)
# vm_id=$(echo "$line" | jq -r '.vm_id // empty')
if [ -n "$NEW_VALUE" ]; then
eval "$KEY_NAME=\"\$NEW_VALUE\""
inject_yaml_var "$KEY_NAME" "$NEW_VALUE"
fi
}
#### #### #### #### #### #### #### #### #### #### #### #### #### #### #### ####
set -euo pipefail
ROLE_NAME="range42-ansible_roles-proxmox_controller"
DEFAULT_OPEN_VAULT_PW_FILE_PATH="${RANGE42_VAULT_PASSWORD_FILE:-/tmp/vault/vault_pass.txt}"
# CURRENT_ANSIBLE_CONFIG="./ansible_no_skipped_json.cfg"
CURRENT_ANSIBLE_CONFIG="$RANGE42_ANSIBLE_ROLES__DEVKITS_DIR/ansible_no_skipped_json.cfg"
ARG_ACTION="${1:-}"
# ARG_NODE_NAME="${2:-}"
#### #### #### #### #### #### #### #### #### #### #### #### #### #### #### ####
proxmox__inc.warmup_checks.sh
proxmox__inc.basic_vm_actions_warmup_checks.to.sh "$ARG_ACTION"
#### #### #### #### #### #### #### #### #### #### #### #### #### #### #### ####
#
# open vault - look for ansible-agent
#
if [[ -f $DEFAULT_OPEN_VAULT_PW_FILE_PATH ]]; then
ANSIBLE_VAULT_ARG=(--vault-password-file "$DEFAULT_OPEN_VAULT_PW_FILE_PATH")
else
OPEN_VAULT_PW_FILE_PATH=$(devkit_ansible.open_vault.to.file.sh)
ANSIBLE_VAULT_ARG=(--vault-password-file "$OPEN_VAULT_PW_FILE_PATH")
fi
#### #### #### #### #### #### #### #### #### #### #### #### #### #### #### ####
#
# inline playbook execution
#
#### #### #### #### #### #### #### #### #### #### #### #### #### #### #### ####
ANSIBLE_CONFIG="$CURRENT_ANSIBLE_CONFIG"
INVENTORY="$RANGE42_ANSIBLE_ROLES__INVENTORY_DIR/inventory_default.yml"
VAULT_ARGS=("${ANSIBLE_VAULT_ARG[@]}")
PLAYBOOK_VARS_FILE="$RANGE42_ANSIBLE_ROLES__DEVKITS_DIR/secrets/default_vault.yml"
IFS=$'\n'
# IS STDIN ?
if [ ! -t 0 ]; then
# devkit_utils.text.echo_trace.to.text.to.stderr.sh "IT IS STDIN "
STDIN_DATA=$(cat -)
IFS=$'\n'
for line in $STDIN_DATA; do
if printf "%s\n" "$line" | jq -e 'type == "object"' >/dev/null 2>&1; then
# devkit_utils.text.echo_trace.to.text.to.stderr.sh ":: JSON_LINE DETECTED :: GET DATA FROM STDIN "
# devkit_utils.text.echo_trace.to.text.to.stderr.sh ":: $line"
assign_if_not_empty "action" "$line" ".action"
assign_if_not_empty "vm_id" "$line" ".vm_id"
assign_if_not_empty "proxmox_node" "$line" ".proxmox_node"
assign_if_not_empty "storage_name" "$line" ".storage_name"
assign_if_not_empty "vm_name" "$line" ".vm_name"
assign_if_not_empty "vm_snapshot_name" "$line" ".vm_snapshot_name"
assign_if_not_empty "vm_snapshot_description" "$line" ".vm_snapshot_description"
assign_if_not_empty "lxc_name" "$line" ".lxc_name"
assign_if_not_empty "lxc_snapshot_name" "$line" ".lxc_snapshot_name"
assign_if_not_empty "lxc_snapshot_description" "$line" ".lxc_snapshot_description"
# assign_if_not_empty "vm_id""$line" ".vm_id"
# assign_if_not_empty "vm_name""$line" ".vm_name"
assign_if_not_empty "vm_cpu" "$line" ".vm_cpu"
assign_if_not_empty "vm_cores" "$line" ".vm_cores"
assign_if_not_empty "vm_sockets" "$line" ".vm_sockets"
assign_if_not_empty "vm_memory" "$line" ".vm_memory"
assign_if_not_empty "vm_disk_size" "$line" ".vm_disk_size"
assign_if_not_empty "vm_iso_file" "$line" ".vm_iso_file"
#####################
assign_if_not_empty "lxc_bridge" "$line" ".lxc_bridge"
assign_if_not_empty "lxc_cores" "$line" ".lxc_cores"
assign_if_not_empty "lxc_disk_size" "$line" ".vm_lxc_disk_sizecpu"
assign_if_not_empty "lxc_dns_primary" "$line" ".lxc_dns_primary"
assign_if_not_empty "lxc_dns_secondary" "$line" ".lxc_dns_secondary"
assign_if_not_empty "lxc_gateway" "$line" ".lxc_gateway"
assign_if_not_empty "lxc_ip" "$line" ".lxc_ip"
assign_if_not_empty "lxc_memory" "$line" ".lxc_memory"
assign_if_not_empty "lxc_net_name" "$line" ".lxc_net_name"
assign_if_not_empty "lxc_password" "$line" ".lxc_password"
assign_if_not_empty "lxc_ssh_pubkeys" "$line" ".lxc_ssh_pubkeys"
assign_if_not_empty "lxc_template" "$line" ".lxc_template"
assign_if_not_empty "lxc_disk_size" "$line" ".lxc_disk_size"
assign_if_not_empty "proxmox_storage" "$line" ".proxmox_storage"
# lxc|vm clone
assign_if_not_empty "vm_new_id" "$line" ".vm_new_id"
assign_if_not_empty "lxc_description" "$line" ".lxc_description"
assign_if_not_empty "vm_description" "$line" ".vm_description"
# storage_download_iso
assign_if_not_empty "iso_file_content_type" "$line" ".iso_file_content_type"
assign_if_not_empty "iso_file_name" "$line" ".iso_file_name"
assign_if_not_empty "iso_url" "$line" ".iso_url"
# storage cloud init
assign_if_not_empty "cloudinit_image_path" "$line" ".cloudinit_image_path"
# bonus // extra
assign_if_not_empty "proxmox_cluster_color_mapping" "$line" ".proxmox_cluster_color_mapping"
assign_if_not_empty "vm_tag_name" "$line" ".vm_tag_name"
assign_if_not_empty "lxc_tag_name" "$line" ".lxc_tag_name"
# fw
assign_if_not_empty "vm_fw_action" "$line" ".vm_fw_action"
assign_if_not_empty "vm_fw_type" "$line" ".vm_fw_type"
assign_if_not_empty "vm_fw_iface" "$line" ".vm_fw_iface"
assign_if_not_empty "vm_fw_source" "$line" ".vm_fw_source"
assign_if_not_empty "vm_fw_dest" "$line" ".vm_fw_dest"
assign_if_not_empty "vm_fw_proto" "$line" ".vm_fw_proto"
assign_if_not_empty "vm_fw_dport" "$line" ".vm_fw_dport"
assign_if_not_empty "vm_fw_sport" "$line" ".vm_fw_sport"
assign_if_not_empty "vm_fw_enable" "$line" ".vm_fw_enable"
assign_if_not_empty "vm_fw_comment" "$line" ".vm_fw_comment"
assign_if_not_empty "vm_fw_pos" "$line" ".vm_fw_pos"
assign_if_not_empty "vm_fw_log" "$line" ".vm_fw_log"
# fw - alias
assign_if_not_empty "vm_fw_alias_cidr" "$line" ".vm_fw_alias_cidr"
assign_if_not_empty "vm_fw_alias_name" "$line" ".vm_fw_alias_name"
assign_if_not_empty "vm_fw_alias_comment" "$line" ".vm_fw_alias_comment"
# net iface - vm
assign_if_not_empty "iface_model" "$line" ".iface_model"
assign_if_not_empty "iface_bridge" "$line" ".iface_bridge"
assign_if_not_empty "vm_vmnet_id" "$line" ".vm_vmnet_id"
# net iface - node
assign_if_not_empty "iface_name" "$line" ".iface_name"
assign_if_not_empty "iface_type" "$line" ".iface_type"
assign_if_not_empty "iface_autostart" "$line" ".iface_autostart"
assign_if_not_empty "iface_mtu" "$line" ".iface_mtu"
assign_if_not_empty "iface_vlan_id" "$line" ".iface_vlan_id"
assign_if_not_empty "iface_vlan_raw_device" "$line" ".iface_vlan_raw_device"
assign_if_not_empty "ip_address" "$line" ".ip_address"
assign_if_not_empty "ip_cidr" "$line" ".ip_cidr"
assign_if_not_empty "ip_gateway" "$line" ".ip_gateway"
assign_if_not_empty "ip_netmask" "$line" ".ip_netmask"
assign_if_not_empty "ip_comments" "$line" ".ip_comments"
assign_if_not_empty "ipv6_address" "$line" ".ipv6_address"
assign_if_not_empty "ipv6_cidr" "$line" ".ipv6_cidr"
assign_if_not_empty "ipv6_comments" "$line" ".ipv6_comments"
assign_if_not_empty "ipv6_gateway" "$line" ".ipv6_gateway"
assign_if_not_empty "ipv6_netmask" "$line" ".ipv6_netmask"
assign_if_not_empty "bridge_ports" "$line" ".bridge_ports"
assign_if_not_empty "bridge_vids" "$line" ".bridge_vids"
assign_if_not_empty "bridge_vlan_aware" "$line" ".bridge_vlan_aware"
assign_if_not_empty "bond_primary" "$line" ".bond_primary"
assign_if_not_empty "bond_mode" "$line" ".bond_mode"
assign_if_not_empty "bond_xmit_hash_policy" "$line" ".bond_xmit_hash_policy"
assign_if_not_empty "ovs_bonds" "$line" ".ovs_bonds"
assign_if_not_empty "ovs_bridge" "$line" ".ovs_bridge"
assign_if_not_empty "ovs_options" "$line" ".ovs_options"
assign_if_not_empty "ovs_ports" "$line" ".ovs_ports"
assign_if_not_empty "ovs_tag" "$line" ".ovs_tag"
assign_if_not_empty "iface_slaves" "$line" ".iface_slaves"
# cloud init
assign_if_not_empty "vm_ci_user" "$line" ".vm_ci_user"
assign_if_not_empty "vm_ci_password" "$line" ".vm_ci_password"
assign_if_not_empty "vm_ci_ssh_key" "$line" ".vm_ci_ssh_key"
assign_if_not_empty "vm_ci_dns_ips" "$line" ".vm_ci_dns_ips"
assign_if_not_empty "vm_ci_dns_domain" "$line" ".vm_ci_dns_domain"
assign_if_not_empty "vm_ci_ip" "$line" ".vm_ci_ip"
assign_if_not_empty "vm_ci_netmask" "$line" ".vm_ci_netmask"
assign_if_not_empty "vm_ci_ip_gw" "$line" ".vm_ci_ip_gw"
#
assign_if_not_empty "cloudinit_image_full_path" "$line" ".cloudinit_image_full_path"
assign_if_not_empty "dest_proxmox_storage" "$line" ".dest_proxmox_storage"
# ARG_ACTION=$(printf "%s\n" "$line" | jq -r ".action")
PROXMOX_NODE=$(printf "%s\n" "$line" | jq -r ".proxmox_node")
#### DEBUG purpose.
# NEW_CURRENT_JSON_LINE=$(printf '%s\n' "$line" | jq -c --arg jq_action_v "$ARG_ACTION" ' . + { ("action"): $jq_action_v } ')
# devkit_utils.text.echo_trace.to.text.to.stderr.sh "===> $NEW_CURRENT_JSON_LINE"
####
#### DEBUG BLOCK
####
if [ "$DEBUG" = true ]; then
cat <<EOF >/tmp/debug
(
ANSIBLE_CONFIG="$ANSIBLE_CONFIG" \
ansible-playbook -i "$INVENTORY" "${VAULT_ARGS[@]}" /dev/stdin <<PLAYBOOK
- hosts: $PROXMOX_NODE
gather_facts: false
vars_files:
- "$PLAYBOOK_VARS_FILE"
tasks:
- name: RUN $ROLE_NAME WITH VARS
include_role:
name: $ROLE_NAME
vars:
proxmox_vm_action: "$ARG_ACTION"
$EXTRA_VAR
PLAYBOOK
)
EOF
# devkit_utils.text.echo_trace.to.text.to.stderr.sh ":: cat /tmp/debug to see inline playbook "
fi
####
#### DEBUG BLOCK
####
(
ANSIBLE_CONFIG="$ANSIBLE_CONFIG" \
ansible-playbook -i "$INVENTORY" "${VAULT_ARGS[@]}" /dev/stdin <<EOF
- hosts: $PROXMOX_NODE
gather_facts: false
vars_files:
- "$PLAYBOOK_VARS_FILE"
tasks:
- name: RUN $ROLE_NAME WITH VARS
include_role:
name: $ROLE_NAME
vars:
proxmox_vm_action: "$ARG_ACTION"
${EXTRA_VAR}
EOF
) | jq -c --arg action "$ARG_ACTION" '
.plays[].tasks[]
| .hosts[]
| select(type=="object" and has($action))
| .[$action]
'
else
devkit_utils.text.echo_error.to.text.to.stderr.sh ":: TEXT DETECTED :: GET DATA FROM STDIN"
fi
done
else
# NO STDIN DATA - USE VAULT (default) VALUE
#
devkit_utils.text.echo_error.to.text.to.stderr.sh "NO STDIN VALUE"
exit 1
fi