forked from hyphacoop/cosmos-ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_control.py
executable file
·71 lines (60 loc) · 2.31 KB
/
node_control.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
#!/usr/bin/env python3
"""
Ansible-backed node management operations:
./node-control.py [-i <inventory file>] [-t target] <operation>
-i is optional, it defaults to inventory.yml
The target option is the server IP or domain
Operations:
restart: restarts the chain service
stop: stops the chain service
start: starts the chain service
reset: resets the chain database
reboot: reboots the host
"""
import sys
import argparse
import os
parser = argparse.ArgumentParser(description='Single-command node management.')
parser.add_argument('-i', metavar='inventory', help='inventory file (default: inventory.yml)',
required=False,
default='inventory.yml')
parser.add_argument('-t', metavar='target', help='target server',
required=False,
default='')
parser.add_argument('operation', help='the operation to perform')
args = parser.parse_args()
operation = args.operation
inventory = args.i
target = args.t
if operation == "restart":
print(os.popen("ansible-playbook node.yml -i " +
inventory + " -e 'target=" + target + " reboot=false' --tags 'chain_restart'").read())
sys.exit(0)
if operation == "stop":
print(os.popen("ansible-playbook node.yml -i " +
inventory + " -e 'target=" + target + "' --tags 'chain_stop'").read())
sys.exit(0)
if operation == "start":
print(os.popen("ansible-playbook node.yml -i " +
inventory + " -e 'target=" + target + " reboot=false' --tags 'chain_start'").read())
sys.exit(0)
if operation == "reset":
answer = input(
"This will reset chain database on all nodes in inventory. "
"Are you sure you want to continue (yes/no)? ")
if answer.lower() in ["yes"]:
print(os.popen("ansible-playbook node.yml -i " +
inventory +
" --extra-vars 'node_unsafe_reset=true target=" + target + "' "
" --tags 'chain_stop,chain_reset,chain_start'").read())
sys.exit(0)
else:
print("Aborting...")
sys.exit(2)
if operation == "reboot":
print(os.popen("ansible-playbook node.yml -i " + inventory +
" --extra-vars 'reboot=true target=" + target + "' --tags 'reboot'").read())
sys.exit(0)
else:
print("Invalid operation.")
sys.exit(1)