forked from norcams/himlarcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.py
executable file
·109 lines (97 loc) · 3.58 KB
/
node.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
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
#!/usr/bin/env python
import sys
import time
import pprint
from himlarcli import utils as himutils
from himlarcli.foremanclient import ForemanClient
from himlarcli.parser import Parser
from himlarcli.sensu import Sensu
#from himlarcli.printer import Printer
himutils.is_virtual_env()
# Load parser config from config/parser/*
parser = Parser()
options = parser.parse_args()
client = ForemanClient(options.config, options.debug)
region = client.get_config('openstack', 'region')
logger = client.get_logger()
sensu = Sensu(options.config, debug=options.debug)
# Load node config
node_config = himutils.load_config('config/nodes/%s.yaml' % region)
if not node_config:
node_config = himutils.load_config('config/nodes/default.yaml')
nodes = node_config['nodes']
def action_show():
node_name = '%s-%s' % (region, options.node)
node = client.get_host(node_name)
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(node)
def action_list():
count = dict()
print("These nodes can be intalled:")
for name, node in sorted(nodes.items()):
if 'compute_resource' in node:
print("node: %s (%s)" % (name, node['compute_resource']))
if not node['compute_resource'] in count:
count[node['compute_resource']] = 0
count[node['compute_resource']] += 1
else:
print("node: %s (%s)" % (name, node['mac']))
print("Stats:")
print(count)
def action_install():
node_name = '%s-%s' % (region, options.node)
if options.node in nodes:
client.create_node(name=node_name,
node_data=nodes[options.node],
region=region)
else:
sys.stderr.write("Node %s not found in config/nodes/%s.yaml\n" %
(options.node, region))
sys.exit(1)
def action_delete():
node_name = '%s-%s' % (region, options.node)
if not options.assume_yes:
if not himutils.confirm_action('Are you sure you want to delete %s?' % node_name):
return
client.delete_node(node_name)
sensu.delete_client(node_name)
def action_rebuild():
node_name = '%s-%s' % (region, options.node)
if options.sensu_expire:
sensu.silence_host(node_name, options.sensu_expire)
else:
sensu.delete_client(node_name)
client.set_host_build(node_name)
def action_reinstall():
node_name = '%s-%s' % (region, options.node)
if options.node in nodes:
if not options.assume_yes:
if not himutils.confirm_action('Are you sure you want to reinstall %s?' % node_name):
return
client.delete_node(node_name)
if options.sensu_expire:
sensu.silence_host(node_name, options.sensu_expire)
else:
sensu.delete_client(node_name)
client.create_node(name=node_name,
node_data=nodes[options.node],
region=region)
else:
sys.stderr.write("Node %s not found in config/nodes/%s.yaml\n" %
(options.node, region))
sys.exit(1)
def action_full():
for name, node_data in sorted(nodes.items()):
node_name = '%s-%s' % (region, name)
client.create_node(name=node_name,
node_data=node_data,
region=region)
time.sleep(10)
# Run local function with the same name as the action
sensu.set_dry_run(options.dry_run)
client.set_dry_run(options.dry_run)
action = locals().get('action_' + options.action)
if not action:
logger.error("Function action_%s not implemented" % options.action)
sys.exit(1)
action()