Guest User

Untitled

a guest
Nov 30th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. # utf-8
  2. """A fabfile to boostrap a vanilla VirtualBox master image
  3.  
  4. Generally you will create a master image with the following commands::
  5.  
  6. fab -f ./bootstrap_venv_fab.py portfwd:guest=somevm-precise64-fresh
  7. fab -f ./bootstrap_venv_fab.py -H 'localhost:2222' -u somedev -p somedev shared
  8. fab -f ./bootstrap_venv_fab.py -H 'localhost:2222' -u somedev -p somedev nopasswd
  9. fab -f ./bootstrap_venv_fab.py -H 'localhost:2222' -u somedev -p somedev resetmac
  10. fab -f ./bootstrap_venv_fab.py -H 'localhost:2222' -u somedev -p somedev -- sudo shutdown -h now
  11.  
  12. Last, export an "appliance" as an .ova file.
  13.  
  14. You may create a Fabric config ini file in your home directory to avoid the
  15. host/user/pass boilerplate.
  16.  
  17. """
  18. import textwrap
  19.  
  20. import fabric
  21. import fabric.state
  22. import fabric.version
  23. import fabric.api as _fab
  24.  
  25. SSH_HOST = "localhost"
  26. SSH_PORT = "2222"
  27. SSH_USER = "somedev"
  28. SSH_PASS = SSH_USER
  29.  
  30. # Override the above host settings with the -H flag
  31. if not _fab.env['hosts']:
  32. _fab.env['hosts'] = ['{SSH_USER}@{SSH_HOST}:{SSH_PORT}'.format(locals())]
  33. _fab.env['password'] = SSH_PASS
  34.  
  35. # Chances are we don't care about security while boostrapping
  36. _fab.env['disable_known_hosts'] = True
  37.  
  38. def portfwd(guest, custom=''):
  39. """Configure VBox port forwarding for ssh and http(s)
  40.  
  41. This allows you to easily access the services hosted on the VirtualBox
  42. Guest OS from your local Host machine.
  43.  
  44. .. cmdoption:: guest
  45.  
  46. The name of the virtual machine to set up forwarding for
  47.  
  48. .. cmdoption:: custom
  49.  
  50. Add forwarding for a custom port in the form of "desc/guest/host". For
  51. example, to access Django's builtin runserver from your host::
  52.  
  53. fab vbox_portfwd:custom="runserver/8000/8000"
  54.  
  55. .. warning::
  56.  
  57. The virtual machine must be stopped and started before forwarding
  58. becomes available.
  59.  
  60. """
  61. if custom:
  62. fwd_ports = [custom.split('/')]
  63. else:
  64. fwd_ports = (
  65. ('ssh', '22', SSH_PORT),
  66. ('http', '80', '8080'))
  67.  
  68. for name, guestport, hostport in fwd_ports:
  69. with _fab.settings(_fab.hide('warnings'), warn_only=True):
  70. _fab.local('VBoxManage modifyvm %(guest)s --natpf1 delete %(name)s' % locals())
  71.  
  72. _fab.local('VBoxManage modifyvm %(guest)s --natpf1 '
  73. '%(name)s,tcp,,%(hostport)s,,%(guestport)s' % locals())
  74.  
  75. def shared():
  76. """Build the vboxsf module for Shared Folders
  77.  
  78. Try to automatically build the vboxsf module. Before invoking this
  79. function, click on the "Devices" menu then "Install Guest Additions" to
  80. make the VBoxGuestAdditions.iso image available to be mounted.
  81.  
  82. .. note::
  83.  
  84. This cannot be completely scripted without a ton of guesswork based on
  85. platform since the VBoxGuestAdditions.iso file can be in a number of
  86. places.
  87.  
  88. """
  89. VBOXSF = '/media/cdrom/VBoxLinuxAdditions.run'
  90.  
  91. with _fab.settings(_fab.hide('warnings'), warn_only=True):
  92. _fab.sudo('mkdir -p /media/cdrom')
  93. _fab.sudo('mount /dev/cdrom /media/cdrom')
  94.  
  95. if not fabric.contrib.files.exists(VBOXSF):
  96. with _fab.settings(_fab.hide('warnings'), warn_only=True):
  97. _fab.sudo('umount /media/cdrom')
  98.  
  99. _fab.abort(textwrap.dedent("""\
  100. You must click 'Install Guest Additions' in the 'Devices'
  101. menu before running this."""))
  102.  
  103. _fab.sudo('aptitude -y install %s' % ' '.join((
  104. 'build-essential',
  105. 'module-assistant',
  106. )))
  107.  
  108. _fab.sudo('sh %(VBOXSF)s' % locals())
  109. _fab.sudo('umount /media/cdrom')
  110.  
  111. ### Add fstab entries for Shared Folders
  112. fabric.contrib.files.append(
  113. '/etc/fstab',
  114. 'sharename /path/to/share/on/guest vboxsf '\
  115. 'rw,uid={0},gid=www-data 0 0'.format(SSH_USER),
  116. use_sudo=True)
  117.  
  118. _fab.warn("Now edit fstab with the correct path to the share.")
  119.  
  120. def nopasswd():
  121. """Configure sudo to not require a password"""
  122. fabric.contrib.files.sed('/etc/sudoers',
  123. 'ALL$', 'NOPASSWD: ALL', limit='%sudo', use_sudo=True)
  124.  
  125. def resetmac():
  126. """Remove the persistent-net file that stores the eth0 mac address.
Add Comment
Please, Sign In to add comment