-
Notifications
You must be signed in to change notification settings - Fork 74
/
purge_ceph_cluster.py
106 lines (77 loc) · 3.4 KB
/
purge_ceph_cluster.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
#!/usr/bin/env python3
# -*- code: utf-8 -*-
"""Purge Ceph Cluster.
Usage:
purge_ceph_cluster.py [--installer <IP>] [--username <USERNAME>] [--password <PASSWORD>]
Options:
-h --help Show this help message and exit.
--installer <IP> Installer node IP.
--username <USERNAME> SSH username.
--password <PASSWORD> SSH password.
Assumptions :
This will assume all the ceph nodes will have same root username password as provided for installer node
"""
import json
import paramiko
from docopt import docopt
def disable_cephadm_module(ssh_client):
"""CephAdm module is disabled to daemon deployment."""
return ssh_client.exec_command("cephadm shell -- ceph mgr module disable cephadm")
def execute_ssh_command(ssh_client, command):
stdin, stdout, stderr = ssh_client.exec_command(command)
return stdout.read().decode("utf-8")
def get_fsid(ssh_client):
fsid_command = "cephadm shell -- ceph fsid -f json"
fsid_output = execute_ssh_command(ssh_client, fsid_command)
fsid_json = json.loads(fsid_output)
return fsid_json["fsid"]
def get_node_ips(ssh_client):
node_ips_command = "cephadm shell -- ceph orch host ls -f json"
node_ips_output = execute_ssh_command(ssh_client, node_ips_command)
node_ips_json = json.loads(node_ips_output)
return [node["addr"] for node in node_ips_json]
def main():
args = docopt(__doc__)
installer_node_ip = args["--installer"]
username = args["--username"]
password = args["--password"]
ssh_installer = paramiko.SSHClient()
ssh_installer.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_installer.connect(installer_node_ip, username=username, password=password)
fsid = get_fsid(ssh_installer)
node_ips = get_node_ips(ssh_installer)
node_ips.remove(installer_node_ip)
print("Cluster Node IPs list:", node_ips)
disable_cephadm_module(ssh_installer)
for node_ip in node_ips:
ssh_node = paramiko.SSHClient()
ssh_node.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_node.connect(node_ip, username=username, password=password)
# Ensure cephadm package is installed
_cmd = "dnf install -y cephadm"
_out = execute_ssh_command(ssh_node, _cmd)
print("Command output of package install ", _out)
command = f"cephadm rm-cluster --force --zap-osds --fsid {fsid}"
output = execute_ssh_command(ssh_node, command)
print(f"Command output for {node_ip}:", output)
print("Removing the cephadm logs")
command = "rm -rf /var/log/ceph/*"
output = execute_ssh_command(ssh_node, command)
print(f"Command output for {node_ip}:", output)
print("Removing the cephadm")
command = "dnf remove -y cephadm"
output = execute_ssh_command(ssh_node, command)
print(f"Command output for {node_ip}:", output)
ssh_node.close()
command_on_installer = f"cephadm rm-cluster --force --zap-osds --fsid {fsid}"
output_on_installer = execute_ssh_command(ssh_installer, command_on_installer)
print(
f"Command output on installer node ({installer_node_ip}):", output_on_installer
)
print("Removing the cephadm on Installer Node")
command = "dnf remove -y cephadm"
output = execute_ssh_command(ssh_installer, command)
print(f"Command output for {node_ip}:", output)
ssh_installer.close()
if __name__ == "__main__":
main()