-
Notifications
You must be signed in to change notification settings - Fork 0
/
hosts.py
executable file
·108 lines (84 loc) · 4.18 KB
/
hosts.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
#!/usr/bin/env python3
# Copyright 2020, Schuberg Philis B.V
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import click
import click_log
from cosmicops import CosmicOps, logging
@click.command()
@click.option('--profile', '-p', metavar='<name>', default='config',
help='Name of the CloudMonkey profile containing the credentials')
@click.option('--disable', is_flag=True, help='Disable host(s)')
@click.option('--enable', is_flag=True, help='Enable host(s)')
@click.option('--flip-resource-state', is_flag=True, help='Flip the resource state of all hosts in cluster')
@click.option('--hypervisor', '-h', type=int, help='Hypervisor number')
@click.option('--end', '-e', type=int, help='Hypervisor number to end, requires -h option as start number')
@click.option('--dry-run/--exec', is_flag=True, default=True, show_default=True, help='Enable/disable dry-run')
@click_log.simple_verbosity_option(logging.getLogger(), default="INFO", show_default=True)
@click.argument('cluster')
def main(profile, disable, enable, flip_resource_state, hypervisor, end, dry_run, cluster):
"""Enable or disable a range of hosts in a specific pod."""
click_log.basic_config()
log_to_slack = True
logging.task = 'Disable/Enable hypervisor'
logging.slack_title = 'Host'
if end and end <= hypervisor:
logging.error(f"--end cannot be equal or smaller then --hypervisor ({end}<={hypervisor})")
sys.exit(1)
if (flip_resource_state or disable or enable) and dry_run:
logging.info('Running in dry-run mode, will only show changes')
co = CosmicOps(profile=profile, dry_run=dry_run, log_to_slack=log_to_slack)
clstr = co.get_cluster(name=cluster)
if not clstr or not clstr['name'] == cluster:
logging.error(f"Could not find cluster :'{cluster}'")
sys.exit(1)
pod = cluster.split('-')[0]
if hypervisor and (disable or enable):
if not end:
end = hypervisor
logging.info(f"Going to change {pod}-hv{hypervisor:02d}")
else:
logging.info(f"Going to change {pod}-hv{hypervisor:02d} upto {pod}-hv{end:02d}")
for hv in range(hypervisor, end + 1):
hostname = f"{pod}-hv{hv:02d}"
host = co.get_host(name=hostname)
if not host:
logging.warning(f"host {hostname} not found!")
continue
if disable and host['resourcestate'] == 'Disabled':
logging.warning(f"host {hostname} already disabled! Skipping")
continue
if enable and host['resourcestate'] == 'Enabled':
logging.warning(f"host {hostname} already enabled! Skipping")
continue
if disable and not host.disable():
raise RuntimeError(f"Failed to disable host '{hostname}'")
if enable and not host.enable():
raise RuntimeError(f"Failed to enable host '{hostname}'")
logging.info(f"Done!\n")
if flip_resource_state:
logging.info(f"Going to flip the resource state in cluster: {cluster}")
for host in sorted(clstr.get_all_hosts(), key=lambda h: h['name']):
if host['resourcestate'] == 'Disabled':
if not host.enable():
raise RuntimeError(f"Failed to enable host '{host['name']}'")
elif host['resourcestate'] == 'Enabled':
if not host.disable():
raise RuntimeError(f"Failed to disable host '{host['name']}'")
logging.info(f"Done!\n")
hosts = sorted(clstr.get_all_hosts(), key=lambda h: h['name'])
for host in hosts:
logging.info(f"Host {host['name']} is {host['resourcestate']}")
if __name__ == '__main__':
main()