Use nomad-python client for setting nomad vars
This commit is contained in:
parent
b75f8fce7b
commit
486df58bac
@ -1,91 +1,28 @@
|
||||
#! /usr/bin/env python3
|
||||
from collections import defaultdict
|
||||
from os import getenv
|
||||
|
||||
import requests
|
||||
import yaml
|
||||
|
||||
|
||||
NOMAD_ADDR = getenv("NOMAD_ADDR", "http://127.0.0.1:4646")
|
||||
NOMAD_TOKEN = getenv("NOMAD_TOKEN")
|
||||
|
||||
|
||||
def nomad_req(method: str, path: str, json: dict|None = None) -> requests.Response:
|
||||
headers = {}
|
||||
if NOMAD_TOKEN:
|
||||
headers["X-Nomad-Token"] = NOMAD_TOKEN
|
||||
|
||||
result = requests.request(
|
||||
method,
|
||||
f"{NOMAD_ADDR}/v1/{path}",
|
||||
headers=headers,
|
||||
json=json,
|
||||
)
|
||||
|
||||
print(result.text)
|
||||
result.raise_for_status()
|
||||
|
||||
return result
|
||||
|
||||
|
||||
|
||||
def write_var(path: str, items: dict[str, str | float | int]) -> requests.Response:
|
||||
return nomad_req("PUT", f"var/{path}",
|
||||
json={
|
||||
"Path": path,
|
||||
"Items": {k: str(v) for k, v in items.items()},
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def delete_var(path: str) -> requests.Response:
|
||||
return nomad_req("DELETE", f"var/{path}")
|
||||
|
||||
|
||||
def write_consul():
|
||||
with open("./ansible_playbooks/vars/consul_values.yml") as f:
|
||||
vars = yaml.load(f, yaml.CLoader)["consul_values"]
|
||||
prefix = "insecure/"
|
||||
|
||||
key_values = defaultdict(list)
|
||||
for path, value in vars.items():
|
||||
path, _, item = path.rpartition("/")
|
||||
key_values[path].append((item, value))
|
||||
|
||||
for path, items in key_values.items():
|
||||
print("path", path, "items", items)
|
||||
response = write_var(prefix + path, dict(items))
|
||||
print(response)
|
||||
|
||||
|
||||
def write_vault():
|
||||
with open("./ansible_playbooks/vars/vault_hashi_vault_values.yml") as f:
|
||||
vars = yaml.load(f, yaml.CLoader)["hashi_vault_values"]
|
||||
prefix = "secrets/"
|
||||
|
||||
for path, items in vars.items():
|
||||
print("path", path, "items", items)
|
||||
response = write_var(prefix + path, items)
|
||||
print(response)
|
||||
from nomad import Nomad
|
||||
|
||||
|
||||
def write_nomad():
|
||||
nomad = Nomad()
|
||||
with open("./ansible_playbooks/vars/nomad_vars.yml") as f:
|
||||
vars = yaml.load(f, yaml.CLoader)
|
||||
|
||||
for path, items in vars.items():
|
||||
print("path", path, "items", items)
|
||||
response = None
|
||||
if items == "DELETE":
|
||||
response = delete_var(path)
|
||||
else:
|
||||
response = write_var(path, items)
|
||||
# ignore special path there for terraform
|
||||
if path == "nomad/oidc":
|
||||
continue
|
||||
|
||||
try:
|
||||
response.raise_for_status()
|
||||
except:
|
||||
print(response.text)
|
||||
raise
|
||||
print("path", path, "items", items)
|
||||
if items == "DELETE":
|
||||
print(nomad.variable.delete_variable(path))
|
||||
else:
|
||||
print(
|
||||
nomad.variable.create_variable(
|
||||
path,
|
||||
{"Path": path, "Items": {k: str(v) for k, v in items.items()}},
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
|
Loading…
Reference in New Issue
Block a user