-
Notifications
You must be signed in to change notification settings - Fork 0
/
govnsible-agent
executable file
·129 lines (110 loc) · 5.34 KB
/
govnsible-agent
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
#!/usr/bin/env python
import shlex
import socket
import tarfile
from govnsiblecli.modules.base.utils.display import init_logger
logger = init_logger("root")
import argparse
import requests
import sys
import os
import subprocess
class GovnsibleAgent():
def __init__(self,govnsible_url,username,password, exec_group=None,playbook_name=None):
self.url = govnsible_url
self.username = username
self.password = password
self.hostname = socket.gethostname()
self.access_token = self.get_jwt_token()
self.exec_group = "Test"
if not self.is_server_registerd():
self.server_uuid = self.register_server()
else:
self.server_uuid = self.get_server_uuid()
def get_jwt_token(self):
request = requests.post(f"{self.url}/api/v1/user/auth",json={'username': self.username, 'password_hash': self.password})
if request.status_code != 200:
logger.error("Failed to auth")
sys.exit(255)
access_token = request.json().get('access_token')
return access_token
def register_playbook(self, playbook_name):
request = requests.post(f"{self.url}/api/v1/playbooks", json={'playbook_name': playbook_name, 'execGroupName': self.exec_group}, headers={'Authorization': 'Bearer {}'.format(self.access_token)})
if request.status_code != 200:
logger.error("Failed to register playbook")
sys.exit(255)
print(request.json())
def register_server(self):
request = requests.post(f"{self.url}/api/v1/server", json={'hostname': self.hostname, 'execGroupName': self.exec_group}, headers={'Authorization': 'Bearer {}'.format(self.access_token)})
if request.status_code != 200:
logger.error("Failed to register server")
sys.exit(255)
serverUUID = request.json().get('serverUID')
return serverUUID
def is_server_registerd(self):
request = requests.get(f"{self.url}/api/v1/server", json={'hostname': self.hostname}, headers={'Authorization': 'Bearer {}'.format(self.access_token)})
if request.status_code not in [200,404]:
logger.error("Error")
sys.exit(255)
if request.status_code == 404:
return False
if request.json().get('message') == "OK":
return True
return False
def get_server_uuid(self):
request = requests.get(f"{self.url}/api/v1/server", json={'hostname': self.hostname}, headers={'Authorization': 'Bearer {}'.format(self.access_token)})
if request.status_code not in [200,404]:
logger.error("Error")
sys.exit(255)
if request.status_code == 404:
return None
return request.json().get('serverUID')
def register_exec_group(self):
request = requests.post(f"{self.url}/api/v1/execgroup", json={'groupname': self.exec_group},
headers={'Authorization': 'Bearer {}'.format(self.access_token)})
if request.status_code != 200:
logger.error("Error")
sys.exit(255)
return True
def get_avail_playbooks(self):
request = requests.get(f"{self.url}/api/v1/playbooks", json={'groupname': self.exec_group},
headers={'Authorization': 'Bearer {}'.format(self.access_token)})
if request.status_code not in [401,200]:
logger.error("Error")
sys.exit(255)
if request.status_code == 401:
logger.info("No playbooks to play")
return False
return request.json().get('playbook_name')
def sync_playbooks(self,playbook_name):
request = requests.get(f"{self.url}/api/v1/playbook_download{playbook_name}",headers={'Authorization': 'Bearer {}'.format(self.access_token)})
if request.status_code not in [404,200]:
logger.error("Error")
sys.exit(255)
open(f"/opt/data/agnt_playbooks/{playbook_name}.tar.gz", "wb").write(request.content)
my_tar = tarfile.open(f"/opt/data/agnt_playbooks/{playbook_name}.tar.gz")
os.mkdir(f"/opt/data/agnt_playbooks/{playbook_name}")
my_tar.extractall(f"/opt/data/agnt_playbooks/{playbook_name}")
return f"/opt/data/agnt_playbooks/{playbook_name}.tar.gz"
def run_playbook(self,playbook_path):
shell_command = f"govnsible {playbook_path}"
command = shlex.split(shell_command)
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
return (result.stdout,result.stderr)
def main():
parser = argparse.ArgumentParser(description='Playbook executor')
parser.add_argument('--master-url', type=str, help='master_url')
parser.add_argument('--master-password', type=str, help='master_password')
parser.add_argument('--master-username', type=str, help='master_password')
args = parser.parse_args()
print(args)
print(args.master_username, args.master_password, args.master_url)
agent_handler = GovnsibleAgent(args.master_url, args.master_username, args.master_password)
print(agent_handler.access_token)
playbook_name = agent_handler.get_avail_playbooks()
playbook_path = agent_handler.sync_playbooks(playbook_name)
stdout,stderr = agent_handler.run_playbook(playbook_path)
logger.info(stdout)
logger.error(stderr)
if __name__ == "__main__":
main()