Rollout main=>qa 13.09.2022
parent
5367c9929e
commit
01c972771b
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,5 @@
|
||||
---
|
||||
prometheus_lvm_hcloudvol_size: 30
|
||||
prometheus_lvm_hcloudvol_count: 2
|
||||
|
||||
prometheus_tsdb_rentention_time: '90d'
|
||||
@ -0,0 +1,80 @@
|
||||
---
|
||||
- name: "Creating some hcloud volumes for LVM purpose"
|
||||
hcloud_volume:
|
||||
api_token: "{{ hetzner_authentication_ansible }}"
|
||||
name: "{{ lvm_with_hetzner_volumes__volprefix }}-{{ inventory_hostname }}--vol{{ item }}"
|
||||
server: "{{ inventory_hostname }}"
|
||||
labels:
|
||||
stage: "{{ stage }}"
|
||||
used_for: "{{ lvm_with_hetzner_volumes__volprefix }}"
|
||||
bound_on: "{{ inventory_hostname }}"
|
||||
vol_no: "{{ item | string }}"
|
||||
size: "{{ lvm_with_hetzner_volumes__volsize }}"
|
||||
state: present
|
||||
delete_protection: yes
|
||||
loop: "{{ range(1, lvm_with_hetzner_volumes__volcount+1) | list }}"
|
||||
register: created_volume
|
||||
delegate_to: localhost
|
||||
become: false
|
||||
|
||||
- name: "Getting all hcloud volumes for {{ inventory_hostname }}"
|
||||
hcloud_volume_info:
|
||||
api_token: "{{ hetzner_authentication_ansible }}"
|
||||
label_selector: "stage={{ stage }},used_for={{ lvm_with_hetzner_volumes__volprefix }},bound_on={{ inventory_hostname }}"
|
||||
register: hcloud_volumes_found
|
||||
delegate_to: localhost
|
||||
become: false
|
||||
|
||||
- name: "Getting all hcloud volumes for {{ inventory_hostname }}"
|
||||
debug:
|
||||
msg: "{{ hcloud_volumes_found }}"
|
||||
|
||||
- name: "Setting LVM related VARs"
|
||||
set_fact:
|
||||
pvs: "{{ hcloud_volumes_found.hcloud_volume_info | json_query(jmesquery) }}"
|
||||
vg_name: "vg.{{ lvm_with_hetzner_volumes__volprefix }}"
|
||||
lv_name: "lv.{{ lvm_with_hetzner_volumes__volprefix }}"
|
||||
vars:
|
||||
jmesquery: "[*].linux_device"
|
||||
|
||||
- name: "Creating a volume group on top of all found hcloud volumes"
|
||||
community.general.lvg:
|
||||
vg: "{{ vg_name }}"
|
||||
pvs: "{{ pvs }}"
|
||||
pvresize: yes
|
||||
register: create_vg
|
||||
|
||||
- name: "Create logical volume" # noqa no-handler
|
||||
community.general.lvol:
|
||||
vg: "{{ vg_name }}"
|
||||
lv: "{{ lv_name }}"
|
||||
size: '100%PVS'
|
||||
when:
|
||||
- create_vg.changed
|
||||
|
||||
- name: "Format volume"
|
||||
filesystem:
|
||||
fstype: ext4
|
||||
dev: "/dev/{{ vg_name }}/{{ lv_name }}"
|
||||
|
||||
- name: "Resize volume" # noqa no-handler
|
||||
filesystem:
|
||||
fstype: ext4
|
||||
dev: "/dev/{{ vg_name }}/{{ lv_name }}"
|
||||
resizefs: yes
|
||||
when:
|
||||
- create_vg.changed
|
||||
|
||||
# set noqa linter 'tag' due to unknown file permissions/ownership for mount path ;
|
||||
# must be set in role etc in which this role will be called!!!
|
||||
- name: "Ensure mountpath exists without setting permission/ownership" # noqa risky-file-permissions
|
||||
file:
|
||||
path: "{{ lvm_with_hetzner_volumes__mountpath }}"
|
||||
state: directory
|
||||
|
||||
- name: "Mount created LVM volume"
|
||||
mount:
|
||||
path: "{{ lvm_with_hetzner_volumes__mountpath }}"
|
||||
src: "/dev/{{ vg_name }}/{{ lv_name }}"
|
||||
fstype: ext4
|
||||
state: mounted
|
||||
@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env python3
|
||||
import requests
|
||||
|
||||
|
||||
access_token = '{{ hetzner_authentication_ansible_vault }}'
|
||||
|
||||
node_exporter_txt_dir = '/var/lib/prometheus/node-exporter'
|
||||
metrics_file = node_exporter_txt_dir + "/hetzner_metrics.prom"
|
||||
|
||||
query= {'per_page': '1000'}
|
||||
headers = {'Authorization': 'Bearer ' + access_token}
|
||||
|
||||
try:
|
||||
response_volumes = requests.get("https://api.hetzner.cloud/v1/volumes", headers=headers, params=query)
|
||||
response_servers = requests.get("https://api.hetzner.cloud/v1/servers", headers=headers, params=query)
|
||||
except requests.exceptions.RequestException as e: # This is the correct syntax
|
||||
f = open(metrics_file, "w")
|
||||
f.write("\n")
|
||||
f.close()
|
||||
raise SystemExit(e)
|
||||
|
||||
|
||||
if response_volumes.ok and response_servers.ok:
|
||||
|
||||
|
||||
volume_json = response_volumes.json()["volumes"]
|
||||
unattached_volume_count = 0
|
||||
for x in range(len(volume_json)):
|
||||
if volume_json[x]["server"] == None:
|
||||
unattached_volume_count+=1
|
||||
|
||||
locked_servers_count = 0
|
||||
servers_json = response_servers.json()["servers"]
|
||||
for x in range(len(servers_json)):
|
||||
if servers_json[x]["locked"] == "true":
|
||||
locked_servers_count+=1
|
||||
|
||||
ratelimit_limit = response_servers.headers['ratelimit-limit']
|
||||
ratelimit_remaining = response_servers.headers['ratelimit-remaining']
|
||||
|
||||
|
||||
f = open(metrics_file, "w")
|
||||
f.write(
|
||||
"hetzner_api_ratelimit_remaining " + str(ratelimit_remaining) + "\n"
|
||||
+ "hetzner_api_ratelimit_limit " + str(ratelimit_limit) + "\n"
|
||||
+ "hetzner_api_unattached_volumes " + str(unattached_volume_count) + "\n"
|
||||
+ "hetzner_api_locked_servers " + str(locked_servers_count) + "\n"
|
||||
)
|
||||
f.close()
|
||||
else:
|
||||
f = open(metrics_file, "w")
|
||||
f.write("\n")
|
||||
f.close()
|
||||
Loading…
Reference in New Issue