Juju Charm - Ceph OSD
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

host.py 37KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. # Copyright 2014-2015 Canonical Limited.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Tools for working with the host system"""
  15. # Copyright 2012 Canonical Ltd.
  16. #
  17. # Authors:
  18. # Nick Moffitt <nick.moffitt@canonical.com>
  19. # Matthew Wedgwood <matthew.wedgwood@canonical.com>
  20. import os
  21. import re
  22. import pwd
  23. import glob
  24. import grp
  25. import random
  26. import string
  27. import subprocess
  28. import hashlib
  29. import functools
  30. import itertools
  31. import six
  32. from contextlib import contextmanager
  33. from collections import OrderedDict
  34. from .hookenv import log, DEBUG, local_unit
  35. from .fstab import Fstab
  36. from charmhelpers.osplatform import get_platform
  37. __platform__ = get_platform()
  38. if __platform__ == "ubuntu":
  39. from charmhelpers.core.host_factory.ubuntu import (
  40. service_available,
  41. add_new_group,
  42. lsb_release,
  43. cmp_pkgrevno,
  44. CompareHostReleases,
  45. ) # flake8: noqa -- ignore F401 for this import
  46. elif __platform__ == "centos":
  47. from charmhelpers.core.host_factory.centos import (
  48. service_available,
  49. add_new_group,
  50. lsb_release,
  51. cmp_pkgrevno,
  52. CompareHostReleases,
  53. ) # flake8: noqa -- ignore F401 for this import
  54. UPDATEDB_PATH = '/etc/updatedb.conf'
  55. def service_start(service_name, **kwargs):
  56. """Start a system service.
  57. The specified service name is managed via the system level init system.
  58. Some init systems (e.g. upstart) require that additional arguments be
  59. provided in order to directly control service instances whereas other init
  60. systems allow for addressing instances of a service directly by name (e.g.
  61. systemd).
  62. The kwargs allow for the additional parameters to be passed to underlying
  63. init systems for those systems which require/allow for them. For example,
  64. the ceph-osd upstart script requires the id parameter to be passed along
  65. in order to identify which running daemon should be reloaded. The follow-
  66. ing example stops the ceph-osd service for instance id=4:
  67. service_stop('ceph-osd', id=4)
  68. :param service_name: the name of the service to stop
  69. :param **kwargs: additional parameters to pass to the init system when
  70. managing services. These will be passed as key=value
  71. parameters to the init system's commandline. kwargs
  72. are ignored for systemd enabled systems.
  73. """
  74. return service('start', service_name, **kwargs)
  75. def service_stop(service_name, **kwargs):
  76. """Stop a system service.
  77. The specified service name is managed via the system level init system.
  78. Some init systems (e.g. upstart) require that additional arguments be
  79. provided in order to directly control service instances whereas other init
  80. systems allow for addressing instances of a service directly by name (e.g.
  81. systemd).
  82. The kwargs allow for the additional parameters to be passed to underlying
  83. init systems for those systems which require/allow for them. For example,
  84. the ceph-osd upstart script requires the id parameter to be passed along
  85. in order to identify which running daemon should be reloaded. The follow-
  86. ing example stops the ceph-osd service for instance id=4:
  87. service_stop('ceph-osd', id=4)
  88. :param service_name: the name of the service to stop
  89. :param **kwargs: additional parameters to pass to the init system when
  90. managing services. These will be passed as key=value
  91. parameters to the init system's commandline. kwargs
  92. are ignored for systemd enabled systems.
  93. """
  94. return service('stop', service_name, **kwargs)
  95. def service_restart(service_name, **kwargs):
  96. """Restart a system service.
  97. The specified service name is managed via the system level init system.
  98. Some init systems (e.g. upstart) require that additional arguments be
  99. provided in order to directly control service instances whereas other init
  100. systems allow for addressing instances of a service directly by name (e.g.
  101. systemd).
  102. The kwargs allow for the additional parameters to be passed to underlying
  103. init systems for those systems which require/allow for them. For example,
  104. the ceph-osd upstart script requires the id parameter to be passed along
  105. in order to identify which running daemon should be restarted. The follow-
  106. ing example restarts the ceph-osd service for instance id=4:
  107. service_restart('ceph-osd', id=4)
  108. :param service_name: the name of the service to restart
  109. :param **kwargs: additional parameters to pass to the init system when
  110. managing services. These will be passed as key=value
  111. parameters to the init system's commandline. kwargs
  112. are ignored for init systems not allowing additional
  113. parameters via the commandline (systemd).
  114. """
  115. return service('restart', service_name)
  116. def service_reload(service_name, restart_on_failure=False, **kwargs):
  117. """Reload a system service, optionally falling back to restart if
  118. reload fails.
  119. The specified service name is managed via the system level init system.
  120. Some init systems (e.g. upstart) require that additional arguments be
  121. provided in order to directly control service instances whereas other init
  122. systems allow for addressing instances of a service directly by name (e.g.
  123. systemd).
  124. The kwargs allow for the additional parameters to be passed to underlying
  125. init systems for those systems which require/allow for them. For example,
  126. the ceph-osd upstart script requires the id parameter to be passed along
  127. in order to identify which running daemon should be reloaded. The follow-
  128. ing example restarts the ceph-osd service for instance id=4:
  129. service_reload('ceph-osd', id=4)
  130. :param service_name: the name of the service to reload
  131. :param restart_on_failure: boolean indicating whether to fallback to a
  132. restart if the reload fails.
  133. :param **kwargs: additional parameters to pass to the init system when
  134. managing services. These will be passed as key=value
  135. parameters to the init system's commandline. kwargs
  136. are ignored for init systems not allowing additional
  137. parameters via the commandline (systemd).
  138. """
  139. service_result = service('reload', service_name, **kwargs)
  140. if not service_result and restart_on_failure:
  141. service_result = service('restart', service_name, **kwargs)
  142. return service_result
  143. def service_pause(service_name, init_dir="/etc/init", initd_dir="/etc/init.d",
  144. **kwargs):
  145. """Pause a system service.
  146. Stop it, and prevent it from starting again at boot.
  147. :param service_name: the name of the service to pause
  148. :param init_dir: path to the upstart init directory
  149. :param initd_dir: path to the sysv init directory
  150. :param **kwargs: additional parameters to pass to the init system when
  151. managing services. These will be passed as key=value
  152. parameters to the init system's commandline. kwargs
  153. are ignored for init systems which do not support
  154. key=value arguments via the commandline.
  155. """
  156. stopped = True
  157. if service_running(service_name, **kwargs):
  158. stopped = service_stop(service_name, **kwargs)
  159. upstart_file = os.path.join(init_dir, "{}.conf".format(service_name))
  160. sysv_file = os.path.join(initd_dir, service_name)
  161. if init_is_systemd():
  162. service('disable', service_name)
  163. service('mask', service_name)
  164. elif os.path.exists(upstart_file):
  165. override_path = os.path.join(
  166. init_dir, '{}.override'.format(service_name))
  167. with open(override_path, 'w') as fh:
  168. fh.write("manual\n")
  169. elif os.path.exists(sysv_file):
  170. subprocess.check_call(["update-rc.d", service_name, "disable"])
  171. else:
  172. raise ValueError(
  173. "Unable to detect {0} as SystemD, Upstart {1} or"
  174. " SysV {2}".format(
  175. service_name, upstart_file, sysv_file))
  176. return stopped
  177. def service_resume(service_name, init_dir="/etc/init",
  178. initd_dir="/etc/init.d", **kwargs):
  179. """Resume a system service.
  180. Reenable starting again at boot. Start the service.
  181. :param service_name: the name of the service to resume
  182. :param init_dir: the path to the init dir
  183. :param initd dir: the path to the initd dir
  184. :param **kwargs: additional parameters to pass to the init system when
  185. managing services. These will be passed as key=value
  186. parameters to the init system's commandline. kwargs
  187. are ignored for systemd enabled systems.
  188. """
  189. upstart_file = os.path.join(init_dir, "{}.conf".format(service_name))
  190. sysv_file = os.path.join(initd_dir, service_name)
  191. if init_is_systemd():
  192. service('unmask', service_name)
  193. service('enable', service_name)
  194. elif os.path.exists(upstart_file):
  195. override_path = os.path.join(
  196. init_dir, '{}.override'.format(service_name))
  197. if os.path.exists(override_path):
  198. os.unlink(override_path)
  199. elif os.path.exists(sysv_file):
  200. subprocess.check_call(["update-rc.d", service_name, "enable"])
  201. else:
  202. raise ValueError(
  203. "Unable to detect {0} as SystemD, Upstart {1} or"
  204. " SysV {2}".format(
  205. service_name, upstart_file, sysv_file))
  206. started = service_running(service_name, **kwargs)
  207. if not started:
  208. started = service_start(service_name, **kwargs)
  209. return started
  210. def service(action, service_name, **kwargs):
  211. """Control a system service.
  212. :param action: the action to take on the service
  213. :param service_name: the name of the service to perform th action on
  214. :param **kwargs: additional params to be passed to the service command in
  215. the form of key=value.
  216. """
  217. if init_is_systemd():
  218. cmd = ['systemctl', action, service_name]
  219. else:
  220. cmd = ['service', service_name, action]
  221. for key, value in six.iteritems(kwargs):
  222. parameter = '%s=%s' % (key, value)
  223. cmd.append(parameter)
  224. return subprocess.call(cmd) == 0
  225. _UPSTART_CONF = "/etc/init/{}.conf"
  226. _INIT_D_CONF = "/etc/init.d/{}"
  227. def service_running(service_name, **kwargs):
  228. """Determine whether a system service is running.
  229. :param service_name: the name of the service
  230. :param **kwargs: additional args to pass to the service command. This is
  231. used to pass additional key=value arguments to the
  232. service command line for managing specific instance
  233. units (e.g. service ceph-osd status id=2). The kwargs
  234. are ignored in systemd services.
  235. """
  236. if init_is_systemd():
  237. return service('is-active', service_name)
  238. else:
  239. if os.path.exists(_UPSTART_CONF.format(service_name)):
  240. try:
  241. cmd = ['status', service_name]
  242. for key, value in six.iteritems(kwargs):
  243. parameter = '%s=%s' % (key, value)
  244. cmd.append(parameter)
  245. output = subprocess.check_output(cmd,
  246. stderr=subprocess.STDOUT).decode('UTF-8')
  247. except subprocess.CalledProcessError:
  248. return False
  249. else:
  250. # This works for upstart scripts where the 'service' command
  251. # returns a consistent string to represent running
  252. # 'start/running'
  253. if ("start/running" in output or
  254. "is running" in output or
  255. "up and running" in output):
  256. return True
  257. elif os.path.exists(_INIT_D_CONF.format(service_name)):
  258. # Check System V scripts init script return codes
  259. return service('status', service_name)
  260. return False
  261. SYSTEMD_SYSTEM = '/run/systemd/system'
  262. def init_is_systemd():
  263. """Return True if the host system uses systemd, False otherwise."""
  264. if lsb_release()['DISTRIB_CODENAME'] == 'trusty':
  265. return False
  266. return os.path.isdir(SYSTEMD_SYSTEM)
  267. def adduser(username, password=None, shell='/bin/bash',
  268. system_user=False, primary_group=None,
  269. secondary_groups=None, uid=None, home_dir=None):
  270. """Add a user to the system.
  271. Will log but otherwise succeed if the user already exists.
  272. :param str username: Username to create
  273. :param str password: Password for user; if ``None``, create a system user
  274. :param str shell: The default shell for the user
  275. :param bool system_user: Whether to create a login or system user
  276. :param str primary_group: Primary group for user; defaults to username
  277. :param list secondary_groups: Optional list of additional groups
  278. :param int uid: UID for user being created
  279. :param str home_dir: Home directory for user
  280. :returns: The password database entry struct, as returned by `pwd.getpwnam`
  281. """
  282. try:
  283. user_info = pwd.getpwnam(username)
  284. log('user {0} already exists!'.format(username))
  285. if uid:
  286. user_info = pwd.getpwuid(int(uid))
  287. log('user with uid {0} already exists!'.format(uid))
  288. except KeyError:
  289. log('creating user {0}'.format(username))
  290. cmd = ['useradd']
  291. if uid:
  292. cmd.extend(['--uid', str(uid)])
  293. if home_dir:
  294. cmd.extend(['--home', str(home_dir)])
  295. if system_user or password is None:
  296. cmd.append('--system')
  297. else:
  298. cmd.extend([
  299. '--create-home',
  300. '--shell', shell,
  301. '--password', password,
  302. ])
  303. if not primary_group:
  304. try:
  305. grp.getgrnam(username)
  306. primary_group = username # avoid "group exists" error
  307. except KeyError:
  308. pass
  309. if primary_group:
  310. cmd.extend(['-g', primary_group])
  311. if secondary_groups:
  312. cmd.extend(['-G', ','.join(secondary_groups)])
  313. cmd.append(username)
  314. subprocess.check_call(cmd)
  315. user_info = pwd.getpwnam(username)
  316. return user_info
  317. def user_exists(username):
  318. """Check if a user exists"""
  319. try:
  320. pwd.getpwnam(username)
  321. user_exists = True
  322. except KeyError:
  323. user_exists = False
  324. return user_exists
  325. def uid_exists(uid):
  326. """Check if a uid exists"""
  327. try:
  328. pwd.getpwuid(uid)
  329. uid_exists = True
  330. except KeyError:
  331. uid_exists = False
  332. return uid_exists
  333. def group_exists(groupname):
  334. """Check if a group exists"""
  335. try:
  336. grp.getgrnam(groupname)
  337. group_exists = True
  338. except KeyError:
  339. group_exists = False
  340. return group_exists
  341. def gid_exists(gid):
  342. """Check if a gid exists"""
  343. try:
  344. grp.getgrgid(gid)
  345. gid_exists = True
  346. except KeyError:
  347. gid_exists = False
  348. return gid_exists
  349. def add_group(group_name, system_group=False, gid=None):
  350. """Add a group to the system
  351. Will log but otherwise succeed if the group already exists.
  352. :param str group_name: group to create
  353. :param bool system_group: Create system group
  354. :param int gid: GID for user being created
  355. :returns: The password database entry struct, as returned by `grp.getgrnam`
  356. """
  357. try:
  358. group_info = grp.getgrnam(group_name)
  359. log('group {0} already exists!'.format(group_name))
  360. if gid:
  361. group_info = grp.getgrgid(gid)
  362. log('group with gid {0} already exists!'.format(gid))
  363. except KeyError:
  364. log('creating group {0}'.format(group_name))
  365. add_new_group(group_name, system_group, gid)
  366. group_info = grp.getgrnam(group_name)
  367. return group_info
  368. def add_user_to_group(username, group):
  369. """Add a user to a group"""
  370. cmd = ['gpasswd', '-a', username, group]
  371. log("Adding user {} to group {}".format(username, group))
  372. subprocess.check_call(cmd)
  373. def chage(username, lastday=None, expiredate=None, inactive=None,
  374. mindays=None, maxdays=None, root=None, warndays=None):
  375. """Change user password expiry information
  376. :param str username: User to update
  377. :param str lastday: Set when password was changed in YYYY-MM-DD format
  378. :param str expiredate: Set when user's account will no longer be
  379. accessible in YYYY-MM-DD format.
  380. -1 will remove an account expiration date.
  381. :param str inactive: Set the number of days of inactivity after a password
  382. has expired before the account is locked.
  383. -1 will remove an account's inactivity.
  384. :param str mindays: Set the minimum number of days between password
  385. changes to MIN_DAYS.
  386. 0 indicates the password can be changed anytime.
  387. :param str maxdays: Set the maximum number of days during which a
  388. password is valid.
  389. -1 as MAX_DAYS will remove checking maxdays
  390. :param str root: Apply changes in the CHROOT_DIR directory
  391. :param str warndays: Set the number of days of warning before a password
  392. change is required
  393. :raises subprocess.CalledProcessError: if call to chage fails
  394. """
  395. cmd = ['chage']
  396. if root:
  397. cmd.extend(['--root', root])
  398. if lastday:
  399. cmd.extend(['--lastday', lastday])
  400. if expiredate:
  401. cmd.extend(['--expiredate', expiredate])
  402. if inactive:
  403. cmd.extend(['--inactive', inactive])
  404. if mindays:
  405. cmd.extend(['--mindays', mindays])
  406. if maxdays:
  407. cmd.extend(['--maxdays', maxdays])
  408. if warndays:
  409. cmd.extend(['--warndays', warndays])
  410. cmd.append(username)
  411. subprocess.check_call(cmd)
  412. remove_password_expiry = functools.partial(chage, expiredate='-1', inactive='-1', mindays='0', maxdays='-1')
  413. def rsync(from_path, to_path, flags='-r', options=None, timeout=None):
  414. """Replicate the contents of a path"""
  415. options = options or ['--delete', '--executability']
  416. cmd = ['/usr/bin/rsync', flags]
  417. if timeout:
  418. cmd = ['timeout', str(timeout)] + cmd
  419. cmd.extend(options)
  420. cmd.append(from_path)
  421. cmd.append(to_path)
  422. log(" ".join(cmd))
  423. return subprocess.check_output(cmd, stderr=subprocess.STDOUT).decode('UTF-8').strip()
  424. def symlink(source, destination):
  425. """Create a symbolic link"""
  426. log("Symlinking {} as {}".format(source, destination))
  427. cmd = [
  428. 'ln',
  429. '-sf',
  430. source,
  431. destination,
  432. ]
  433. subprocess.check_call(cmd)
  434. def mkdir(path, owner='root', group='root', perms=0o555, force=False):
  435. """Create a directory"""
  436. log("Making dir {} {}:{} {:o}".format(path, owner, group,
  437. perms))
  438. uid = pwd.getpwnam(owner).pw_uid
  439. gid = grp.getgrnam(group).gr_gid
  440. realpath = os.path.abspath(path)
  441. path_exists = os.path.exists(realpath)
  442. if path_exists and force:
  443. if not os.path.isdir(realpath):
  444. log("Removing non-directory file {} prior to mkdir()".format(path))
  445. os.unlink(realpath)
  446. os.makedirs(realpath, perms)
  447. elif not path_exists:
  448. os.makedirs(realpath, perms)
  449. os.chown(realpath, uid, gid)
  450. os.chmod(realpath, perms)
  451. def write_file(path, content, owner='root', group='root', perms=0o444):
  452. """Create or overwrite a file with the contents of a byte string."""
  453. uid = pwd.getpwnam(owner).pw_uid
  454. gid = grp.getgrnam(group).gr_gid
  455. # lets see if we can grab the file and compare the context, to avoid doing
  456. # a write.
  457. existing_content = None
  458. existing_uid, existing_gid = None, None
  459. try:
  460. with open(path, 'rb') as target:
  461. existing_content = target.read()
  462. stat = os.stat(path)
  463. existing_uid, existing_gid = stat.st_uid, stat.st_gid
  464. except:
  465. pass
  466. if content != existing_content:
  467. log("Writing file {} {}:{} {:o}".format(path, owner, group, perms),
  468. level=DEBUG)
  469. with open(path, 'wb') as target:
  470. os.fchown(target.fileno(), uid, gid)
  471. os.fchmod(target.fileno(), perms)
  472. if six.PY3 and isinstance(content, six.string_types):
  473. content = content.encode('UTF-8')
  474. target.write(content)
  475. return
  476. # the contents were the same, but we might still need to change the
  477. # ownership.
  478. if existing_uid != uid:
  479. log("Changing uid on already existing content: {} -> {}"
  480. .format(existing_uid, uid), level=DEBUG)
  481. os.chown(path, uid, -1)
  482. if existing_gid != gid:
  483. log("Changing gid on already existing content: {} -> {}"
  484. .format(existing_gid, gid), level=DEBUG)
  485. os.chown(path, -1, gid)
  486. def fstab_remove(mp):
  487. """Remove the given mountpoint entry from /etc/fstab"""
  488. return Fstab.remove_by_mountpoint(mp)
  489. def fstab_add(dev, mp, fs, options=None):
  490. """Adds the given device entry to the /etc/fstab file"""
  491. return Fstab.add(dev, mp, fs, options=options)
  492. def mount(device, mountpoint, options=None, persist=False, filesystem="ext3"):
  493. """Mount a filesystem at a particular mountpoint"""
  494. cmd_args = ['mount']
  495. if options is not None:
  496. cmd_args.extend(['-o', options])
  497. cmd_args.extend([device, mountpoint])
  498. try:
  499. subprocess.check_output(cmd_args)
  500. except subprocess.CalledProcessError as e:
  501. log('Error mounting {} at {}\n{}'.format(device, mountpoint, e.output))
  502. return False
  503. if persist:
  504. return fstab_add(device, mountpoint, filesystem, options=options)
  505. return True
  506. def umount(mountpoint, persist=False):
  507. """Unmount a filesystem"""
  508. cmd_args = ['umount', mountpoint]
  509. try:
  510. subprocess.check_output(cmd_args)
  511. except subprocess.CalledProcessError as e:
  512. log('Error unmounting {}\n{}'.format(mountpoint, e.output))
  513. return False
  514. if persist:
  515. return fstab_remove(mountpoint)
  516. return True
  517. def mounts():
  518. """Get a list of all mounted volumes as [[mountpoint,device],[...]]"""
  519. with open('/proc/mounts') as f:
  520. # [['/mount/point','/dev/path'],[...]]
  521. system_mounts = [m[1::-1] for m in [l.strip().split()
  522. for l in f.readlines()]]
  523. return system_mounts
  524. def fstab_mount(mountpoint):
  525. """Mount filesystem using fstab"""
  526. cmd_args = ['mount', mountpoint]
  527. try:
  528. subprocess.check_output(cmd_args)
  529. except subprocess.CalledProcessError as e:
  530. log('Error unmounting {}\n{}'.format(mountpoint, e.output))
  531. return False
  532. return True
  533. def file_hash(path, hash_type='md5'):
  534. """Generate a hash checksum of the contents of 'path' or None if not found.
  535. :param str hash_type: Any hash alrgorithm supported by :mod:`hashlib`,
  536. such as md5, sha1, sha256, sha512, etc.
  537. """
  538. if os.path.exists(path):
  539. h = getattr(hashlib, hash_type)()
  540. with open(path, 'rb') as source:
  541. h.update(source.read())
  542. return h.hexdigest()
  543. else:
  544. return None
  545. def path_hash(path):
  546. """Generate a hash checksum of all files matching 'path'. Standard
  547. wildcards like '*' and '?' are supported, see documentation for the 'glob'
  548. module for more information.
  549. :return: dict: A { filename: hash } dictionary for all matched files.
  550. Empty if none found.
  551. """
  552. return {
  553. filename: file_hash(filename)
  554. for filename in glob.iglob(path)
  555. }
  556. def check_hash(path, checksum, hash_type='md5'):
  557. """Validate a file using a cryptographic checksum.
  558. :param str checksum: Value of the checksum used to validate the file.
  559. :param str hash_type: Hash algorithm used to generate `checksum`.
  560. Can be any hash alrgorithm supported by :mod:`hashlib`,
  561. such as md5, sha1, sha256, sha512, etc.
  562. :raises ChecksumError: If the file fails the checksum
  563. """
  564. actual_checksum = file_hash(path, hash_type)
  565. if checksum != actual_checksum:
  566. raise ChecksumError("'%s' != '%s'" % (checksum, actual_checksum))
  567. class ChecksumError(ValueError):
  568. """A class derived from Value error to indicate the checksum failed."""
  569. pass
  570. def restart_on_change(restart_map, stopstart=False, restart_functions=None):
  571. """Restart services based on configuration files changing
  572. This function is used a decorator, for example::
  573. @restart_on_change({
  574. '/etc/ceph/ceph.conf': [ 'cinder-api', 'cinder-volume' ]
  575. '/etc/apache/sites-enabled/*': [ 'apache2' ]
  576. })
  577. def config_changed():
  578. pass # your code here
  579. In this example, the cinder-api and cinder-volume services
  580. would be restarted if /etc/ceph/ceph.conf is changed by the
  581. ceph_client_changed function. The apache2 service would be
  582. restarted if any file matching the pattern got changed, created
  583. or removed. Standard wildcards are supported, see documentation
  584. for the 'glob' module for more information.
  585. @param restart_map: {path_file_name: [service_name, ...]
  586. @param stopstart: DEFAULT false; whether to stop, start OR restart
  587. @param restart_functions: nonstandard functions to use to restart services
  588. {svc: func, ...}
  589. @returns result from decorated function
  590. """
  591. def wrap(f):
  592. @functools.wraps(f)
  593. def wrapped_f(*args, **kwargs):
  594. return restart_on_change_helper(
  595. (lambda: f(*args, **kwargs)), restart_map, stopstart,
  596. restart_functions)
  597. return wrapped_f
  598. return wrap
  599. def restart_on_change_helper(lambda_f, restart_map, stopstart=False,
  600. restart_functions=None):
  601. """Helper function to perform the restart_on_change function.
  602. This is provided for decorators to restart services if files described
  603. in the restart_map have changed after an invocation of lambda_f().
  604. @param lambda_f: function to call.
  605. @param restart_map: {file: [service, ...]}
  606. @param stopstart: whether to stop, start or restart a service
  607. @param restart_functions: nonstandard functions to use to restart services
  608. {svc: func, ...}
  609. @returns result of lambda_f()
  610. """
  611. if restart_functions is None:
  612. restart_functions = {}
  613. checksums = {path: path_hash(path) for path in restart_map}
  614. r = lambda_f()
  615. # create a list of lists of the services to restart
  616. restarts = [restart_map[path]
  617. for path in restart_map
  618. if path_hash(path) != checksums[path]]
  619. # create a flat list of ordered services without duplicates from lists
  620. services_list = list(OrderedDict.fromkeys(itertools.chain(*restarts)))
  621. if services_list:
  622. actions = ('stop', 'start') if stopstart else ('restart',)
  623. for service_name in services_list:
  624. if service_name in restart_functions:
  625. restart_functions[service_name](service_name)
  626. else:
  627. for action in actions:
  628. service(action, service_name)
  629. return r
  630. def pwgen(length=None):
  631. """Generate a random pasword."""
  632. if length is None:
  633. # A random length is ok to use a weak PRNG
  634. length = random.choice(range(35, 45))
  635. alphanumeric_chars = [
  636. l for l in (string.ascii_letters + string.digits)
  637. if l not in 'l0QD1vAEIOUaeiou']
  638. # Use a crypto-friendly PRNG (e.g. /dev/urandom) for making the
  639. # actual password
  640. random_generator = random.SystemRandom()
  641. random_chars = [
  642. random_generator.choice(alphanumeric_chars) for _ in range(length)]
  643. return(''.join(random_chars))
  644. def is_phy_iface(interface):
  645. """Returns True if interface is not virtual, otherwise False."""
  646. if interface:
  647. sys_net = '/sys/class/net'
  648. if os.path.isdir(sys_net):
  649. for iface in glob.glob(os.path.join(sys_net, '*')):
  650. if '/virtual/' in os.path.realpath(iface):
  651. continue
  652. if interface == os.path.basename(iface):
  653. return True
  654. return False
  655. def get_bond_master(interface):
  656. """Returns bond master if interface is bond slave otherwise None.
  657. NOTE: the provided interface is expected to be physical
  658. """
  659. if interface:
  660. iface_path = '/sys/class/net/%s' % (interface)
  661. if os.path.exists(iface_path):
  662. if '/virtual/' in os.path.realpath(iface_path):
  663. return None
  664. master = os.path.join(iface_path, 'master')
  665. if os.path.exists(master):
  666. master = os.path.realpath(master)
  667. # make sure it is a bond master
  668. if os.path.exists(os.path.join(master, 'bonding')):
  669. return os.path.basename(master)
  670. return None
  671. def list_nics(nic_type=None):
  672. """Return a list of nics of given type(s)"""
  673. if isinstance(nic_type, six.string_types):
  674. int_types = [nic_type]
  675. else:
  676. int_types = nic_type
  677. interfaces = []
  678. if nic_type:
  679. for int_type in int_types:
  680. cmd = ['ip', 'addr', 'show', 'label', int_type + '*']
  681. ip_output = subprocess.check_output(cmd).decode('UTF-8')
  682. ip_output = ip_output.split('\n')
  683. ip_output = (line for line in ip_output if line)
  684. for line in ip_output:
  685. if line.split()[1].startswith(int_type):
  686. matched = re.search('.*: (' + int_type +
  687. r'[0-9]+\.[0-9]+)@.*', line)
  688. if matched:
  689. iface = matched.groups()[0]
  690. else:
  691. iface = line.split()[1].replace(":", "")
  692. if iface not in interfaces:
  693. interfaces.append(iface)
  694. else:
  695. cmd = ['ip', 'a']
  696. ip_output = subprocess.check_output(cmd).decode('UTF-8').split('\n')
  697. ip_output = (line.strip() for line in ip_output if line)
  698. key = re.compile('^[0-9]+:\s+(.+):')
  699. for line in ip_output:
  700. matched = re.search(key, line)
  701. if matched:
  702. iface = matched.group(1)
  703. iface = iface.partition("@")[0]
  704. if iface not in interfaces:
  705. interfaces.append(iface)
  706. return interfaces
  707. def set_nic_mtu(nic, mtu):
  708. """Set the Maximum Transmission Unit (MTU) on a network interface."""
  709. cmd = ['ip', 'link', 'set', nic, 'mtu', mtu]
  710. subprocess.check_call(cmd)
  711. def get_nic_mtu(nic):
  712. """Return the Maximum Transmission Unit (MTU) for a network interface."""
  713. cmd = ['ip', 'addr', 'show', nic]
  714. ip_output = subprocess.check_output(cmd).decode('UTF-8').split('\n')
  715. mtu = ""
  716. for line in ip_output:
  717. words = line.split()
  718. if 'mtu' in words:
  719. mtu = words[words.index("mtu") + 1]
  720. return mtu
  721. def get_nic_hwaddr(nic):
  722. """Return the Media Access Control (MAC) for a network interface."""
  723. cmd = ['ip', '-o', '-0', 'addr', 'show', nic]
  724. ip_output = subprocess.check_output(cmd).decode('UTF-8')
  725. hwaddr = ""
  726. words = ip_output.split()
  727. if 'link/ether' in words:
  728. hwaddr = words[words.index('link/ether') + 1]
  729. return hwaddr
  730. @contextmanager
  731. def chdir(directory):
  732. """Change the current working directory to a different directory for a code
  733. block and return the previous directory after the block exits. Useful to
  734. run commands from a specificed directory.
  735. :param str directory: The directory path to change to for this context.
  736. """
  737. cur = os.getcwd()
  738. try:
  739. yield os.chdir(directory)
  740. finally:
  741. os.chdir(cur)
  742. def chownr(path, owner, group, follow_links=True, chowntopdir=False):
  743. """Recursively change user and group ownership of files and directories
  744. in given path. Doesn't chown path itself by default, only its children.
  745. :param str path: The string path to start changing ownership.
  746. :param str owner: The owner string to use when looking up the uid.
  747. :param str group: The group string to use when looking up the gid.
  748. :param bool follow_links: Also follow and chown links if True
  749. :param bool chowntopdir: Also chown path itself if True
  750. """
  751. uid = pwd.getpwnam(owner).pw_uid
  752. gid = grp.getgrnam(group).gr_gid
  753. if follow_links:
  754. chown = os.chown
  755. else:
  756. chown = os.lchown
  757. if chowntopdir:
  758. broken_symlink = os.path.lexists(path) and not os.path.exists(path)
  759. if not broken_symlink:
  760. chown(path, uid, gid)
  761. for root, dirs, files in os.walk(path, followlinks=follow_links):
  762. for name in dirs + files:
  763. full = os.path.join(root, name)
  764. broken_symlink = os.path.lexists(full) and not os.path.exists(full)
  765. if not broken_symlink:
  766. chown(full, uid, gid)
  767. def lchownr(path, owner, group):
  768. """Recursively change user and group ownership of files and directories
  769. in a given path, not following symbolic links. See the documentation for
  770. 'os.lchown' for more information.
  771. :param str path: The string path to start changing ownership.
  772. :param str owner: The owner string to use when looking up the uid.
  773. :param str group: The group string to use when looking up the gid.
  774. """
  775. chownr(path, owner, group, follow_links=False)
  776. def owner(path):
  777. """Returns a tuple containing the username & groupname owning the path.
  778. :param str path: the string path to retrieve the ownership
  779. :return tuple(str, str): A (username, groupname) tuple containing the
  780. name of the user and group owning the path.
  781. :raises OSError: if the specified path does not exist
  782. """
  783. stat = os.stat(path)
  784. username = pwd.getpwuid(stat.st_uid)[0]
  785. groupname = grp.getgrgid(stat.st_gid)[0]
  786. return username, groupname
  787. def get_total_ram():
  788. """The total amount of system RAM in bytes.
  789. This is what is reported by the OS, and may be overcommitted when
  790. there are multiple containers hosted on the same machine.
  791. """
  792. with open('/proc/meminfo', 'r') as f:
  793. for line in f.readlines():
  794. if line:
  795. key, value, unit = line.split()
  796. if key == 'MemTotal:':
  797. assert unit == 'kB', 'Unknown unit'
  798. return int(value) * 1024 # Classic, not KiB.
  799. raise NotImplementedError()
  800. UPSTART_CONTAINER_TYPE = '/run/container_type'
  801. def is_container():
  802. """Determine whether unit is running in a container
  803. @return: boolean indicating if unit is in a container
  804. """
  805. if init_is_systemd():
  806. # Detect using systemd-detect-virt
  807. return subprocess.call(['systemd-detect-virt',
  808. '--container']) == 0
  809. else:
  810. # Detect using upstart container file marker
  811. return os.path.exists(UPSTART_CONTAINER_TYPE)
  812. def add_to_updatedb_prunepath(path, updatedb_path=UPDATEDB_PATH):
  813. with open(updatedb_path, 'r+') as f_id:
  814. updatedb_text = f_id.read()
  815. output = updatedb(updatedb_text, path)
  816. f_id.seek(0)
  817. f_id.write(output)
  818. f_id.truncate()
  819. def updatedb(updatedb_text, new_path):
  820. lines = [line for line in updatedb_text.split("\n")]
  821. for i, line in enumerate(lines):
  822. if line.startswith("PRUNEPATHS="):
  823. paths_line = line.split("=")[1].replace('"', '')
  824. paths = paths_line.split(" ")
  825. if new_path not in paths:
  826. paths.append(new_path)
  827. lines[i] = 'PRUNEPATHS="{}"'.format(' '.join(paths))
  828. output = "\n".join(lines)
  829. return output
  830. def modulo_distribution(modulo=3, wait=30, non_zero_wait=False):
  831. """ Modulo distribution
  832. This helper uses the unit number, a modulo value and a constant wait time
  833. to produce a calculated wait time distribution. This is useful in large
  834. scale deployments to distribute load during an expensive operation such as
  835. service restarts.
  836. If you have 1000 nodes that need to restart 100 at a time 1 minute at a
  837. time:
  838. time.wait(modulo_distribution(modulo=100, wait=60))
  839. restart()
  840. If you need restarts to happen serially set modulo to the exact number of
  841. nodes and set a high constant wait time:
  842. time.wait(modulo_distribution(modulo=10, wait=120))
  843. restart()
  844. @param modulo: int The modulo number creates the group distribution
  845. @param wait: int The constant time wait value
  846. @param non_zero_wait: boolean Override unit % modulo == 0,
  847. return modulo * wait. Used to avoid collisions with
  848. leader nodes which are often given priority.
  849. @return: int Calculated time to wait for unit operation
  850. """
  851. unit_number = int(local_unit().split('/')[1])
  852. calculated_wait_time = (unit_number % modulo) * wait
  853. if non_zero_wait and calculated_wait_time == 0:
  854. return modulo * wait
  855. else:
  856. return calculated_wait_time