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.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. def get_devices():
  29. """Parse 'devices' action parameter, returns list."""
  30. devices = []
  31. for path in hookenv.action_get('devices').split(' '):
  32. path = path.strip()
  33. if not os.path.isabs(path):
  34. hookenv.action_fail('{}: Not absolute path.'.format(path))
  35. raise
  36. devices.append(path)
  37. return devices
  38. def zap():
  39. if not hookenv.action_get('i-really-mean-it'):
  40. hookenv.action_fail('i-really-mean-it is a required parameter')
  41. return
  42. failed_devices = []
  43. not_block_devices = []
  44. devices = get_devices()
  45. for device in devices:
  46. if not is_block_device(device):
  47. not_block_devices.append(device)
  48. if is_device_mounted(device) or is_active_bluestore_device(device):
  49. failed_devices.append(device)
  50. if failed_devices or not_block_devices:
  51. message = ""
  52. if failed_devices:
  53. message = "{} devices are mounted: {}".format(
  54. len(failed_devices),
  55. ", ".join(failed_devices))
  56. if not_block_devices:
  57. if message is not '':
  58. message += "\n\n"
  59. message += "{} devices are not block devices: {}".format(
  60. len(not_block_devices),
  61. ", ".join(not_block_devices))
  62. hookenv.action_fail(message)
  63. return
  64. db = kv()
  65. used_devices = db.get('osd-devices', [])
  66. for device in devices:
  67. zap_disk(device)
  68. if device in used_devices:
  69. used_devices.remove(device)
  70. db.set('osd-devices', used_devices)
  71. db.flush()
  72. hookenv.action_set({
  73. 'message': "{} disk(s) have been zapped, to use them as OSDs, run: \n"
  74. "juju run-action {} add-disk osd-devices=\"{}\"".format(
  75. len(devices),
  76. hookenv.local_unit(),
  77. " ".join(devices))
  78. })
  79. if __name__ == "__main__":
  80. zap()