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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
'''
Implements the targetcli root UI.
This file is part of targetcli.
Copyright (c) 2011-2013 by Datera, Inc
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.
'''
from datetime import datetime
from glob import glob
import os
import re
import shutil
import stat
import filecmp
import gzip
from configshell_fb import ExecutionError
from rtslib_fb import RTSRoot
from rtslib_fb.utils import ignored
from .ui_backstore import complete_path, UIBackstores
from .ui_node import UINode
from .ui_target import UIFabricModule
default_target_dir = "/etc/target"
default_save_file = os.path.join(default_target_dir, "saveconfig.json")
universal_prefs_file = os.path.join(default_target_dir, "targetcli.conf")
class UIRoot(UINode):
'''
The targetcli hierarchy root node.
'''
def __init__(self, shell, as_root=False):
UINode.__init__(self, '/', shell=shell)
self.as_root = as_root
self.rtsroot = RTSRoot()
def refresh(self):
'''
Refreshes the tree of target fabric modules.
'''
self._children = set([])
# Invalidate any rtslib caches
if 'invalidate_caches' in dir(RTSRoot):
self.rtsroot.invalidate_caches()
UIBackstores(self)
# only show fabrics present in the system
for fm in self.rtsroot.fabric_modules:
if fm.wwns == None or any(fm.wwns):
UIFabricModule(fm, self)
def _compare_files(self, backupfile, savefile):
'''
Compare backfile and saveconfig file
'''
if (os.path.splitext(backupfile)[1] == '.gz'):
try:
with gzip.open(backupfile, 'rb') as fbkp:
fdata_bkp = fbkp.read()
except IOError as e:
self.shell.log.warning("Could not gzip open backupfile %s: %s"
% (backupfile, e.strerror))
else:
try:
with open(backupfile, 'rb') as fbkp:
fdata_bkp = fbkp.read()
except IOError as e:
self.shell.log.warning("Could not open backupfile %s: %s"
% (backupfile, e.strerror))
try:
with open(savefile, 'rb') as f:
fdata = f.read()
except IOError as e:
self.shell.log.warning("Could not open saveconfig file %s: %s"
% (savefile, e.strerror))
if fdata_bkp == fdata:
return True
else:
return False
def _create_dir(self, dirname):
'''
create directory with permissions 0o600 set
if directory already exists, set right perms
'''
mode = stat.S_IRUSR | stat.S_IWUSR # 0o600
if not os.path.exists(dirname):
umask = 0o777 ^ mode # Prevents always downgrading umask to 0
umask_original = os.umask(umask)
try:
os.makedirs(dirname, mode)
except OSError as exe:
raise ExecutionError("Cannot create directory [%s] %s."
% (dirname, exe.strerror))
finally:
os.umask(umask_original)
else:
if dirname == default_target_dir:
if (os.stat(dirname).st_mode & 0o777) != mode:
os.chmod(dirname, mode)
def _save_backups(self, savefile):
'''
Take backup of config-file if needed.
'''
# Only save backups if saving to default location
if savefile != default_save_file:
return
backup_dir = os.path.dirname(savefile) + "/backup/"
backup_name = "saveconfig-" + \
datetime.now().strftime("%Y%m%d-%H:%M:%S") + "-json.gz"
backupfile = backup_dir + backup_name
backup_error = None
self._create_dir(backup_dir)
# Only save backups if savefile exits
if not os.path.exists(savefile):
return
backed_files_list = sorted(glob(os.path.dirname(savefile) + \
"/backup/saveconfig-*json*"))
# Save backup if backup dir is empty, or savefile is differnt from recent backup copy
if not backed_files_list or not self._compare_files(backed_files_list[-1], savefile):
mode = stat.S_IRUSR | stat.S_IWUSR # 0o600
umask = 0o777 ^ mode # Prevents always downgrading umask to 0
umask_original = os.umask(umask)
try:
with open(savefile, 'rb') as f_in, gzip.open(backupfile, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
f_out.flush()
except IOError as ioe:
backup_error = ioe.strerror or "Unknown error"
finally:
os.umask(umask_original)
if backup_error == None:
# remove excess backups
max_backup_files = int(self.shell.prefs['max_backup_files'])
try:
with open(universal_prefs_file) as prefs:
backups = [line for line in prefs.read().splitlines() if re.match('^max_backup_files\s*=', line)]
if max_backup_files < int(backups[0].split('=')[1].strip()):
max_backup_files = int(backups[0].split('=')[1].strip())
except:
self.shell.log.debug("No universal prefs file '%s'." % universal_prefs_file)
files_to_unlink = list(reversed(backed_files_list))[max_backup_files - 1:]
for f in files_to_unlink:
with ignored(IOError):
os.unlink(f)
self.shell.log.info("Last %d configs saved in %s."
% (max_backup_files, backup_dir))
else:
self.shell.log.warning("Could not create backup file %s: %s."
% (backupfile, backup_error))
def ui_command_saveconfig(self, savefile=default_save_file):
'''
Saves the current configuration to a file so that it can be restored
on next boot.
'''
self.assert_root()
if not savefile:
savefile = default_save_file
savefile = os.path.expanduser(savefile)
save_dir = os.path.dirname(savefile)
self._create_dir(save_dir)
self._save_backups(savefile)
self.rtsroot.save_to_file(savefile)
self.shell.log.info("Configuration saved to %s" % savefile)
def ui_command_restoreconfig(self, savefile=default_save_file, clear_existing=False,
target=None, storage_object=None):
'''
Restores configuration from a file.
'''
self.assert_root()
savefile = os.path.expanduser(savefile)
if not os.path.isfile(savefile):
self.shell.log.info("Restore file %s not found" % savefile)
return
target = self.ui_eval_param(target, 'string', None)
storage_object = self.ui_eval_param(storage_object, 'string', None)
errors = self.rtsroot.restore_from_file(savefile, clear_existing,
target, storage_object)
self.refresh()
if errors:
raise ExecutionError("Configuration restored, %d recoverable errors:\n%s" % \
(len(errors), "\n".join(errors)))
self.shell.log.info("Configuration restored from %s" % savefile)
def ui_complete_saveconfig(self, parameters, text, current_param):
'''
Auto-completes the file name
'''
if current_param != 'savefile':
return []
completions = complete_path(text, stat.S_ISREG)
if len(completions) == 1 and not completions[0].endswith('/'):
completions = [completions[0] + ' ']
return completions
ui_complete_restoreconfig = ui_complete_saveconfig
def ui_command_clearconfig(self, confirm=None):
'''
Removes entire configuration of backstores and targets
'''
self.assert_root()
confirm = self.ui_eval_param(confirm, 'bool', False)
self.rtsroot.clear_existing(confirm=confirm)
self.shell.log.info("All configuration cleared")
self.refresh()
def ui_command_version(self):
'''
Displays the targetcli and support libraries versions.
'''
from targetcli import __version__ as targetcli_version
self.shell.log.info("targetcli version %s" % targetcli_version)
def ui_command_sessions(self, action="list", sid=None):
'''
Displays a detailed list of all open sessions.
PARAMETERS
==========
action
------
The action is one of:
- `list`` gives a short session list
- `detail` gives a detailed list
sid
---
You can specify an "sid" to only list this one,
with or without details.
SEE ALSO
========
status
'''
indent_step = 4
base_steps = 0
action_list = ("list", "detail")
if action not in action_list:
raise ExecutionError("action must be one of: %s" %
", ".join(action_list))
if sid is not None:
try:
int(sid)
except ValueError:
raise ExecutionError("sid must be a number, '%s' given" % sid)
def indent_print(text, steps):
console = self.shell.con
console.display(console.indent(text, indent_step * steps),
no_lf=True)
def print_session(session):
acl = session['parent_nodeacl']
indent_print("alias: %(alias)s\tsid: %(id)i type: " \
"%(type)s session-state: %(state)s" % session,
base_steps)
if action == 'detail':
if self.as_root:
if acl.authenticate_target:
auth = " (authenticated)"
else:
auth = " (NOT AUTHENTICATED)"
else:
auth = ""
indent_print("name: %s%s" % (acl.node_wwn, auth),
base_steps + 1)
for mlun in acl.mapped_luns:
plugin = mlun.tpg_lun.storage_object.plugin
name = mlun.tpg_lun.storage_object.name
if mlun.write_protect:
mode = "r"
else:
mode = "rw"
indent_print("mapped-lun: %d backstore: %s/%s mode: %s" %
(mlun.mapped_lun, plugin, name, mode),
base_steps + 1)
for connection in session['connections']:
indent_print("address: %(address)s (%(transport)s) cid: " \
"%(cid)i connection-state: %(cstate)s" % \
connection, base_steps + 1)
if sid:
printed_sessions = [x for x in self.rtsroot.sessions if x['id'] == int(sid)]
else:
printed_sessions = list(self.rtsroot.sessions)
if len(printed_sessions):
for session in printed_sessions:
print_session(session)
else:
if sid is None:
indent_print("(no open sessions)", base_steps)
else:
raise ExecutionError("no session found with sid %i" % int(sid))
|