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.

zap_disk.py 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright 2018 Canonical Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import os
  17. import sys
  18. sys.path.append('lib')
  19. sys.path.append('hooks')
  20. import charmhelpers.core.hookenv as hookenv
  21. from charmhelpers.contrib.storage.linux.utils import (
  22. is_block_device,
  23. is_device_mounted,
  24. zap_disk,
  25. )
  26. from charmhelpers.core.unitdata import kv
  27. from ceph.utils import is_active_bluestore_device
  28. from ceph.utils import is_mapped_luks_device
  29. def get_devices():
  30. """Parse 'devices' action parameter, returns list."""
  31. devices = []
  32. for path in hookenv.action_get('devices').split(' '):
  33. path = path.strip()
  34. if not os.path.isabs(path):
  35. hookenv.action_fail('{}: Not absolute path.'.format(path))
  36. raise
  37. devices.append(path)
  38. return devices
  39. def zap():
  40. if not hookenv.action_get('i-really-mean-it'):
  41. hookenv.action_fail('i-really-mean-it is a required parameter')
  42. return
  43. failed_devices = []
  44. not_block_devices = []
  45. devices = get_devices()
  46. for device in devices:
  47. if not is_block_device(device):
  48. not_block_devices.append(device)
  49. if (is_device_mounted(device) or
  50. is_active_bluestore_device(device) or
  51. is_mapped_luks_device(device)):
  52. failed_devices.append(device)
  53. if failed_devices or not_block_devices:
  54. message = ""
  55. if failed_devices:
  56. message = "{} devices are mounted: {}".format(
  57. len(failed_devices),
  58. ", ".join(failed_devices))
  59. if not_block_devices:
  60. if message is not '':
  61. message += "\n\n"
  62. message += "{} devices are not block devices: {}".format(
  63. len(not_block_devices),
  64. ", ".join(not_block_devices))
  65. hookenv.action_fail(message)
  66. return
  67. db = kv()
  68. used_devices = db.get('osd-devices', [])
  69. for device in devices:
  70. zap_disk(device)
  71. if device in used_devices:
  72. used_devices.remove(device)
  73. db.set('osd-devices', used_devices)
  74. db.flush()
  75. hookenv.action_set({
  76. 'message': "{} disk(s) have been zapped, to use them as OSDs, run: \n"
  77. "juju run-action {} add-disk osd-devices=\"{}\"".format(
  78. len(devices),
  79. hookenv.local_unit(),
  80. " ".join(devices))
  81. })
  82. if __name__ == "__main__":
  83. zap()