summaryrefslogtreecommitdiff
path: root/daemons/lvmdbusd/manager.py
blob: 9c41f0fc354740ba42dbed9f45d3eff8f8623a0f (plain)
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
# Copyright (C) 2015-2016 Red Hat, Inc. All rights reserved.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions
# of the GNU General Public License v.2.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from .automatedproperties import AutomatedProperties

from . import utils
from .cfg import MANAGER_INTERFACE
import dbus
from . import cfg
from . import cmdhandler
from .request import RequestEntry
from . import udevwatch


# noinspection PyPep8Naming
class Manager(AutomatedProperties):
	_Version_meta = ("s", MANAGER_INTERFACE)

	def __init__(self, object_path):
		super(Manager, self).__init__(object_path)
		self.set_interface(MANAGER_INTERFACE)

	@property
	def Version(self):
		return dbus.String('1.1.0')

	@staticmethod
	def handle_execute(rc, out, err):
		if rc == 0:
			cfg.load()
		else:
			# Need to work on error handling, need consistent
			raise dbus.exceptions.DBusException(
				MANAGER_INTERFACE,
				'Exit code %s, stderr = %s' % (str(rc), err))

	@staticmethod
	def _pv_create(device, create_options):

		# Check to see if we are already trying to create a PV for an existing
		# PV
		pv = cfg.om.get_object_path_by_uuid_lvm_id(device, device)
		if pv:
			raise dbus.exceptions.DBusException(
				MANAGER_INTERFACE, "PV %s Already exists!" % device)

		rc, out, err = cmdhandler.pv_create(create_options, [device])
		Manager.handle_execute(rc, out, err)
		return cfg.om.get_object_path_by_lvm_id(device)

	@dbus.service.method(
		dbus_interface=MANAGER_INTERFACE,
		in_signature='sia{sv}',
		out_signature='(oo)',
		async_callbacks=('cb', 'cbe'))
	def PvCreate(self, device, tmo, create_options, cb, cbe):
		utils.validate_device_path(MANAGER_INTERFACE, device)
		r = RequestEntry(
			tmo, Manager._pv_create,
			(device, create_options), cb, cbe)
		cfg.worker_q.put(r)

	@staticmethod
	def _create_vg(name, pv_object_paths, create_options):
		pv_devices = []

		for p in pv_object_paths:
			pv = cfg.om.get_object_by_path(p)
			if pv:
				pv_devices.append(pv.Name)
			else:
				raise dbus.exceptions.DBusException(
					MANAGER_INTERFACE, 'object path = %s not found' % p)

		rc, out, err = cmdhandler.vg_create(create_options, pv_devices, name)
		Manager.handle_execute(rc, out, err)
		return cfg.om.get_object_path_by_lvm_id(name)

	@dbus.service.method(
		dbus_interface=MANAGER_INTERFACE,
		in_signature='saoia{sv}',
		out_signature='(oo)',
		async_callbacks=('cb', 'cbe'))
	def VgCreate(self, name, pv_object_paths, tmo, create_options, cb, cbe):
		utils.validate_vg_name(MANAGER_INTERFACE, name)
		r = RequestEntry(
			tmo, Manager._create_vg,
			(name, pv_object_paths, create_options,),
			cb, cbe)
		cfg.worker_q.put(r)

	@staticmethod
	def _refresh():
		utils.log_debug('Manager.Refresh - entry')

		# This is a diagnostic and should not be run in normal operation, so
		# lets remove the log entries for refresh as it's implied.

		# Run an internal diagnostic on the object manager look up tables
		lc = cfg.om.validate_lookups()

		rc = cfg.load(log=False)

		if rc != 0:
			utils.log_debug('Manager.Refresh - exit %d %d' % (rc, lc),
							'bg_black', 'fg_light_red')
		else:
			utils.log_debug('Manager.Refresh - exit %d %d' % (rc, lc))
		return rc + lc

	@dbus.service.method(
		dbus_interface=MANAGER_INTERFACE,
		out_signature='t',
		async_callbacks=('cb', 'cbe'))
	def Refresh(self, cb, cbe):
		"""
		Take all the objects we know about and go out and grab the latest
		more of a test method at the moment to make sure we are handling object
		paths correctly.

		:param cb   Callback for result
		:param cbe  Callback for errors

		Returns the number of changes, object add/remove/properties changed
		"""
		r = RequestEntry(-1, Manager._refresh, (), cb, cbe, False)
		cfg.worker_q.put(r)

	@dbus.service.method(
		dbus_interface=MANAGER_INTERFACE)
	def FlightRecorderDump(self):
		"""
		Dump the flight recorder to syslog
		"""
		cfg.debug.dump()
		cfg.flightrecorder.dump()

	@staticmethod
	def _lookup_by_lvm_id(key):
		p = cfg.om.get_object_path_by_uuid_lvm_id(key, key)
		if not p:
			p = '/'
		utils.log_debug('LookUpByLvmId: key = %s, result = %s' % (key, p))
		return p

	@dbus.service.method(
		dbus_interface=MANAGER_INTERFACE,
		in_signature='s',
		out_signature='o',
		async_callbacks=('cb', 'cbe'))
	def LookUpByLvmId(self, key, cb, cbe):
		"""
		Given a lvm id in one of the forms:

		/dev/sda
		some_vg
		some_vg/some_lv
		Oe1rPX-Pf0W-15E5-n41N-ZmtF-jXS0-Osg8fn

		return the object path in O(1) time.

		:param key: The lookup value
		:param cb:	dbus python call back parameter, not client visible
		:param cbe:	dbus python error call back parameter, not client visible
		:return: Return the object path.  If object not found you will get '/'
		"""
		r = RequestEntry(-1, Manager._lookup_by_lvm_id, (key,), cb, cbe, False)
		cfg.worker_q.put(r)

	@staticmethod
	def _use_lvm_shell(yes_no):
		return dbus.Boolean(cmdhandler.set_execution(yes_no))

	@dbus.service.method(
		dbus_interface=MANAGER_INTERFACE,
		in_signature='b', out_signature='b',
		async_callbacks=('cb', 'cbe'))
	def UseLvmShell(self, yes_no, cb, cbe):
		"""
		Allow the client to enable/disable lvm shell, used for testing
		:param yes_no:
		:param cb:	dbus python call back parameter, not client visible
		:param cbe:	dbus python error call back parameter, not client visible
		:return: Boolean
		"""
		r = RequestEntry(-1, Manager._use_lvm_shell, (yes_no,), cb, cbe, False)
		cfg.worker_q.put(r)

	@staticmethod
	def _external_event(command):
		utils.log_debug("Processing _external_event= %s" % command,
							'bg_black', 'fg_orange')
		cfg.got_external_event = True
		cfg.load()

	@dbus.service.method(
		dbus_interface=MANAGER_INTERFACE,
		in_signature='s', out_signature='i')
	def ExternalEvent(self, command):
		utils.log_debug("ExternalEvent %s" % command)
		# If a user didn't explicitly specify udev, we will turn it off now.
		if not cfg.args.use_udev:
			if udevwatch.remove():
				utils.log_msg("ExternalEvent received, disabling "
								"udev monitoring")
				# We are dependent on external events now to stay current!
		r = RequestEntry(
			-1, Manager._external_event, (command,), None, None, False)
		cfg.worker_q.put(r)
		return dbus.Int32(0)

	@staticmethod
	def _pv_scan(activate, cache, device_path, major_minor, scan_options):

		rc, out, err = cmdhandler.pv_scan(
			activate, cache, device_path,
			major_minor, scan_options)

		Manager.handle_execute(rc, out, err)
		return '/'

	@dbus.service.method(
		dbus_interface=MANAGER_INTERFACE,
		in_signature='bbasa(ii)ia{sv}',
		out_signature='o',
		async_callbacks=('cb', 'cbe'))
	def PvScan(self, activate, cache, device_paths, major_minors,
			tmo, scan_options, cb, cbe):
		"""
		Scan all supported LVM block devices in the system for physical volumes
		NOTE: major_minors & device_paths only usable when cache == True
		:param activate: If True, activate any newly found LVs
		:param cache:    If True, update lvmetad
		:param device_paths: Array of device paths or empty
		:param major_minors: Array of structures (major,minor)
		:param tmo: Timeout for operation
		:param scan_options:  Additional options to pvscan
		:param cb: Not visible in API (used for async. callback)
		:param cbe: Not visible in API (used for async. error callback)
		:return: '/' if operation done, else job path
		"""
		for d in device_paths:
			utils.validate_device_path(MANAGER_INTERFACE, d)

		r = RequestEntry(
			tmo, Manager._pv_scan,
			(activate, cache, device_paths, major_minors,
			scan_options), cb, cbe, False)
		cfg.worker_q.put(r)

	@property
	def lvm_id(self):
		"""
		Intended to be overridden by classes that inherit
		"""
		return str(id(self))

	@property
	def Uuid(self):
		"""
		Intended to be overridden by classes that inherit
		"""
		import uuid
		return uuid.uuid1()