forked from ceph/ceph-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remotes.py
193 lines (145 loc) · 4.84 KB
/
remotes.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import errno
import socket
import os
import shutil
import tempfile
import platform
def platform_information():
""" detect platform information from remote host """
distro, release, codename = platform.linux_distribution()
if not codename and 'debian' in distro.lower(): # this could be an empty string in Debian
debian_codenames = {
'8': 'jessie',
'7': 'wheezy',
'6': 'squeeze',
}
major_version = release.split('.')[0]
codename = debian_codenames.get(major_version, '')
return (
str(distro).rstrip(),
str(release).rstrip(),
str(codename).rstrip()
)
def machine_type():
""" detect machine type """
return platform.machine()
def write_sources_list(url, codename):
"""add ceph deb repo to sources.list"""
with file('/etc/apt/sources.list.d/ceph.list', 'w') as f:
f.write('deb {url} {codename} main\n'.format(
url=url,
codename=codename,
))
def write_yum_repo(content):
"""set the contents of /etc/yum.repos.d/ceph.repo"""
write_file('/etc/yum.repos.d/ceph.repo', content)
def write_conf(cluster, conf, overwrite):
""" write cluster configuration to /etc/ceph/{cluster}.conf """
path = '/etc/ceph/{cluster}.conf'.format(cluster=cluster)
tmp_file = tempfile.NamedTemporaryFile(delete=False)
err_msg = 'config file %s exists with different content; use --overwrite-conf to overwrite' % path
if os.path.exists(path):
with file(path, 'rb') as f:
old = f.read()
if old != conf and not overwrite:
raise RuntimeError(err_msg)
tmp_file.write(conf)
tmp_file.close()
shutil.move(tmp_file.name, path)
return
if os.path.exists('/etc/ceph'):
with open(path, 'w') as f:
f.write(conf)
else:
err_msg = '/etc/ceph/ does not exist - could not write config'
raise RuntimeError(err_msg)
def write_keyring(path, key):
""" create a keyring file """
tmp_file = tempfile.NamedTemporaryFile(delete=False)
tmp_file.write(key)
tmp_file.close()
shutil.move(tmp_file.name, path)
def create_mon_path(path):
"""create the mon path if it does not exist"""
if not os.path.exists(path):
os.makedirs(path)
def create_done_path(done_path):
"""create a done file to avoid re-doing the mon deployment"""
with file(done_path, 'w'):
pass
def create_init_path(init_path):
"""create the init path if it does not exist"""
if not os.path.exists(init_path):
with file(init_path, 'w'):
pass
def append_to_file(file_path, contents):
"""append contents to file"""
with open(file_path, 'a') as f:
f.write(contents)
def path_exists(path):
return os.path.exists(path)
def makedir(path):
os.makedirs(path)
def unlink(_file):
os.unlink(_file)
def write_monitor_keyring(keyring, monitor_keyring):
"""create the monitor keyring file"""
write_file(keyring, monitor_keyring)
def write_file(path, content):
with file(path, 'w') as f:
f.write(content)
def touch_file(path):
with file(path, 'wb') as f: # noqa
pass
def get_file(path):
""" fetch remote file """
try:
with file(path, 'rb') as f:
return f.read()
except IOError:
pass
def shortname():
"""get remote short hostname"""
return socket.gethostname().split('.', 1)[0]
def which_service():
""" locating the `service` executable... """
locations = ['/sbin/service', '/usr/sbin/service']
for location in locations:
if os.path.exists(location):
return location
def make_mon_removed_dir(path, file_name):
""" move old monitor data """
try:
os.makedirs('/var/lib/ceph/mon-removed')
except OSError, e:
if e.errno != errno.EEXIST:
raise
shutil.move(path, os.path.join('/var/lib/ceph/mon-removed/', file_name))
def safe_mkdir(path):
""" create path if it doesn't exist """
try:
os.mkdir(path)
except OSError, e:
if e.errno == errno.EEXIST:
pass
else:
raise
def zeroing(dev):
""" zeroing last few blocks of device """
# this kills the crab
#
# sgdisk will wipe out the main copy of the GPT partition
# table (sorry), but it doesn't remove the backup copies, and
# subsequent commands will continue to complain and fail when
# they see those. zeroing the last few blocks of the device
# appears to do the trick.
lba_size = 4096
size = 33 * lba_size
return True
with file(dev, 'wb') as f:
f.seek(-size, os.SEEK_END)
f.write(size*'\0')
# remoto magic, needed to execute these functions remotely
if __name__ == '__channelexec__':
for item in channel: # noqa
channel.send(eval(item)) # noqa