forked from RunOnFlux/FluxVault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault_agent.py
executable file
·76 lines (69 loc) · 2.47 KB
/
vault_agent.py
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
#!/usr/bin/python3
'''This module is a single file that supports the loading of secrets into a Flux Node'''
import json
import sys
import os
import requests
from fluxvault import FluxAgent
VAULT_NAME = os.getenv('VAULT_NAME') # EDIT ME
VAULT_PORT = os.getenv('VAULT_PORT') # EDIT ME
APP_NAME = os.getenv('VAULT_APP') # EDIT ME
FILE_DIR = os.getenv('VAULT_FILE_DIR') # EDIT ME
VERBOSE = True
if VAULT_PORT is None:
VAULT_PORT = 39898
else:
VAULT_PORT = int(VAULT_PORT)
if VAULT_NAME is None:
VAULT_NAME = 'localhost'
if FILE_DIR is None:
FILE_DIR = './files/'
if APP_NAME is None:
APP_NAME = 'VaultDemo'
class MyFluxAgent(FluxAgent):
'''User class to allow easy configuration, see EDIT ME above'''
def __init__(self) -> None:
super().__init__()
self.vault_name = VAULT_NAME
self.vault_port = VAULT_PORT
self.file_dir = FILE_DIR
self.verbose = VERBOSE
def node_vault():
'''Vault runs this to poll every Flux node running their app'''
url = "https://api.runonflux.io/apps/location/" + APP_NAME
req = requests.get(url, timeout=30)
# Get the list of nodes where our app is deplolyed
if req.status_code == 200:
values = json.loads(req.text)
if values["status"] == "success":
# json looks good and status correct, iterate through node list
nodes = values["data"]
for node in nodes:
agent = MyFluxAgent() # Each connection to a node get a fresh agent
ipadr = node['ip'].split(':')[0]
if VERBOSE:
print(node['name'], ipadr)
agent.node_vault_ip(ipadr)
if VERBOSE:
print(node['name'], ipadr, agent.result)
else:
print("Error", req.text)
else:
print("Error", url, "Status", req.status_code)
if __name__ == "__main__":
if len(sys.argv) == 1:
node_vault()
sys.exit(0)
if sys.argv[1].lower() == "--ip":
if len(sys.argv) > 2:
ipaddr = sys.argv[2]
one_node = MyFluxAgent()
one_node.node_vault_ip(ipaddr)
print(ipaddr, one_node.result)
sys.exit(0)
else:
print("Missing Node IP Address: --ip ipaddress")
print("Incorrect arguments:")
print("With no arguments all nodes running ", APP_NAME, " will be polled")
print("If you specify '--ip ipaddress' then that ipaddress will be polled")
sys.exit(1)