Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. @task
  2. @roles('db')
  3. def prepare_dbstorage(
  4. additional_disks=None,
  5. mount_point_backups='/var/backups',
  6. mount_point_postgresql='/var/lib/postgresql',
  7. device_name_backups='/dev/vdb1',
  8. device_name_postgresql='/dev/vdb1',
  9. cloudless=False,
  10. ):
  11. """
  12. Format new partitions and mount, now and permanently
  13. for dbserver's backups and postgres
  14. """
  15. if additional_disks is None:
  16. additional_disks = env.buildparams.get('additional_disks_db', None)
  17. if additional_disks is None:
  18. # Single disk installation - nothing to do for db storage
  19. return None
  20.  
  21. # Stop PostgreSQL while we're working with its data directories
  22. with settings(warn_only=True):
  23. # Postgres may not be running at this point in time (e.g. cloudless)
  24. sudo('service postgresql stop')
  25.  
  26. # FIXME: do we really know which will be /dev/vdb and which /dev/vdc (async timing)
  27.  
  28. # /var/backups: exists but is empty at this stage - just mount
  29. # cloudless vm does not need a backup
  30. if not cloudless:
  31. sudo('echo -e "o\\nn\\np\\n1\\n\\n\\nw\\n" | fdisk {}'.format(device_name_backups))
  32. sudo('mkfs.ext4 {}'.format(device_name_backups))
  33. # append to root-only writable file, suppress output (can't just sudo with >>):
  34. sudo(
  35. 'echo "{} {} ext4 defaults 0 0" | sudo tee --append /etc/fstab > /dev/null'.format(
  36. device_name_backups,
  37. mount_point_backups
  38. )
  39. )
  40. sudo('mount {}'.format(mount_point_backups))
  41.  
  42. # /var/lib/postgresql: we know mount point already exists, move away
  43. sudo('mv {} {}_ORIG'.format(mount_point_postgresql, mount_point_postgresql))
  44. sudo('mkdir {}'.format(mount_point_postgresql))
  45.  
  46. # Cloudless server has disk set up differently
  47. # Does not need a new disk created
  48. # Needs fstab to use UUID of disk manually created disk (by a sysadmin)
  49. if cloudless:
  50. postgres_device_uuid = env.buildparams['postgres_device_uuid']
  51. sudo(
  52. 'echo "UUID={} {} ext4 defaults 0 2" | sudo tee --append /etc/fstab > /dev/null'.format(
  53. postgres_device_uuid,
  54. mount_point_postgresql,
  55. )
  56. )
  57. else:
  58. sudo('echo -e "o\\nn\\np\\n1\\n\\n\\nw\\n" | fdisk {}'.format(device_name_postgresql))
  59. sudo('mkfs.ext4 {}'.foramt(device_name_postgresql))
  60. # append to root-only writable file, suppress output (can't just sudo with >>):
  61. sudo(
  62. 'echo "{} {} ext4 defaults 0 0" | sudo tee --append /etc/fstab > /dev/null'.format(
  63. device_name_postgresql, mount_point_postgresql
  64. )
  65. )
  66. sudo('mount {}'.format(mount_point_postgresql))
  67. sudo('chown postgres:postgres {}'.format(mount_point_postgresql))
  68. sudo('mv {}_ORIG/* {}'.format(mount_point_postgresql, mount_point_postgresql))
  69. # also copy hidden files like .pgpass (start with . but not the parent dir ..)
  70. # move breaks with inter_device copy on cloudless vm
  71. sudo('cp -R {}_ORIG/.[!.]* {}'.format(mount_point_postgresql, mount_point_postgresql))
  72. sudo('rm -rf {}_ORIG'.format(mount_point_postgresql))
  73.  
  74. sudo('service postgresql start')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement