forked from KBB-GmbH/yaps-ceph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyapsceph.py
executable file
·158 lines (116 loc) · 3.97 KB
/
yapsceph.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/python3
# Copyright 2017 Kulturveranstaltungen des Bundes in Berlin GmbH
#
# 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 argparse
import json
import pprint
import subprocess
import sys
# try -h for info
# intended at least to easily remove osd from cluster
# watch out - this programm always assumes --yes-i-really-mean-it
CEPH_BIN = '/usr/bin/ceph'
def _exec(args):
cmd = subprocess.Popen(args,
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result = b''.join(cmd.stdout.readlines()).decode('utf8')
if result == []:
error = cmd.stderr.readlines()
exit( "ERROR: %s" % error )
return result
def _getNodeById(lNodeList,iId):
for dNode in lNodeList:
# pprint.pprint([dNode['id'],iId])
if dNode['id'] == iId:
return dNode
return
#stype sind osd host etc
def _getNodesByType(lNodeList,sType):
retval = []
for dNode in lNodeList:
if dNode['type'] == sType:
retval.append(dNode)
return retval
def _cephGetTree():
tree_json = _exec([CEPH_BIN,'osd','tree','-f','json'])
tree = json.loads(tree_json)
return tree
argparserbase = argparse.ArgumentParser(add_help=False)
argparserbase.add_argument("objtype", choices=['osd'],help="which object-type to work on")
class NoArgs:
def args(self):
argparser = argparse.ArgumentParser(parents=[argparserbase])
argparser.add_argument("objtype", choices=['osd'],help="which object-type to work on")
return argparser
def run(self):
args = self.args().parse_args()
print( 'no args - doing nothing')
class yapsOsd:
def args(self):
argparser = argparse.ArgumentParser(parents=[argparserbase])
argparser.add_argument("method",choices=['rm','dump','create_withlog'],help="which method to use")
argparser.add_argument("--osd", type=int, help="which osd to work on")
return argparser
def run(self):
args = self.args().parse_args()
# pprint.pprint(args)
if args.method == "rm":
self.rm(args)
if args.method == "dump":
self.dump()
def dump(self):
tree_json = _exec([CEPH_BIN,'osd','tree','-f','json-pretty'])
print(tree_json)
# this is an implementation to remove osd
def rm(self,args):
if "" == str(args.osd):
sys.exit("no --osd given")
tree = _cephGetTree()
# pprint.pprint(tree['nodes'])
dOsd = _getNodeById(tree['nodes'],args.osd)
if not dOsd:
exit("osd %s not found" % args.osd )
dlHosts = _getNodesByType(tree['nodes'],'host')
# pprint.pprint(dlHosts);
if not dlHosts or len(dlHosts) == 0:
exit("no hosts at all")
dHost = None
for h in dlHosts:
if dOsd['id'] in h['children']:
dHost = h
break
if not dHost:
exit("host not found for osd id {}".format(dOsd['id']))
print( "about to remove osd {} from host {} ".format(dOsd['name'],dHost['name']) )
_exec([CEPH_BIN,'osd','out',str(dOsd['id'])])
out = _exec(['/usr/bin/ssh',dHost['name'],
'sudo systemctl stop ceph-osd@{0}.service 2>&1 && sudo umount /var/lib/ceph/osd/ceph-{0} 2>&1 && echo ok'.format(dOsd['id'])
]);
if out != "ok\n" :
exit("error on stopping {}: '{}'".format(dOsd['name'],out))
_exec([CEPH_BIN,'osd','crush','remove',dOsd['name']])
_exec([CEPH_BIN,'auth','del',dOsd['name']])
_exec([CEPH_BIN,'osd','rm',str(dOsd['id'])])
#out = _exec(['/usr/bin/ssh','kbbceph04','sudo ls -l /root 2>&1 && ls -l /root 2>&1 && echo ok'])
#exit("ok:"+out+'sdf')
# this phrase is lame, but ...
if sys.argv[1] == 'osd':
op = yapsOsd()
else:
op = NoArgs()
op.run()
#pprint.pprint(args)
exit()