PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/boto-2.5.2/boto/pyami/installers/ubuntu/installer.py

#
Python | 96 lines | 72 code | 1 blank | 23 comment | 0 complexity | 40777b8c78716980c2feaf64fd0af714 MD5 | raw file
  1. # Copyright (c) 2006,2007,2008 Mitch Garnaat http://garnaat.org/
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a
  4. # copy of this software and associated documentation files (the
  5. # "Software"), to deal in the Software without restriction, including
  6. # without limitation the rights to use, copy, modify, merge, publish, dis-
  7. # tribute, sublicense, and/or sell copies of the Software, and to permit
  8. # persons to whom the Software is furnished to do so, subject to the fol-
  9. # lowing conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included
  12. # in all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
  16. # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  17. # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. # IN THE SOFTWARE.
  21. #
  22. import boto.pyami.installers
  23. import os
  24. import os.path
  25. import stat
  26. import boto
  27. import random
  28. from pwd import getpwnam
  29. class Installer(boto.pyami.installers.Installer):
  30. """
  31. Base Installer class for Ubuntu-based AMI's
  32. """
  33. def add_cron(self, name, command, minute="*", hour="*", mday="*", month="*", wday="*", who="root", env=None):
  34. """
  35. Write a file to /etc/cron.d to schedule a command
  36. env is a dict containing environment variables you want to set in the file
  37. name will be used as the name of the file
  38. """
  39. if minute == 'random':
  40. minute = str(random.randrange(60))
  41. if hour == 'random':
  42. hour = str(random.randrange(24))
  43. fp = open('/etc/cron.d/%s' % name, "w")
  44. if env:
  45. for key, value in env.items():
  46. fp.write('%s=%s\n' % (key, value))
  47. fp.write('%s %s %s %s %s %s %s\n' % (minute, hour, mday, month, wday, who, command))
  48. fp.close()
  49. def add_init_script(self, file, name):
  50. """
  51. Add this file to the init.d directory
  52. """
  53. f_path = os.path.join("/etc/init.d", name)
  54. f = open(f_path, "w")
  55. f.write(file)
  56. f.close()
  57. os.chmod(f_path, stat.S_IREAD| stat.S_IWRITE | stat.S_IEXEC)
  58. self.run("/usr/sbin/update-rc.d %s defaults" % name)
  59. def add_env(self, key, value):
  60. """
  61. Add an environemnt variable
  62. For Ubuntu, the best place is /etc/environment. Values placed here do
  63. not need to be exported.
  64. """
  65. boto.log.info('Adding env variable: %s=%s' % (key, value))
  66. if not os.path.exists("/etc/environment.orig"):
  67. self.run('cp /etc/environment /etc/environment.orig', notify=False, exit_on_error=False)
  68. fp = open('/etc/environment', 'a')
  69. fp.write('\n%s="%s"' % (key, value))
  70. fp.close()
  71. os.environ[key] = value
  72. def stop(self, service_name):
  73. self.run('/etc/init.d/%s stop' % service_name)
  74. def start(self, service_name):
  75. self.run('/etc/init.d/%s start' % service_name)
  76. def create_user(self, user):
  77. """
  78. Create a user on the local system
  79. """
  80. self.run("useradd -m %s" % user)
  81. usr = getpwnam(user)
  82. return usr
  83. def install(self):
  84. """
  85. This is the only method you need to override
  86. """
  87. raise NotImplementedError