PageRenderTime 82ms CodeModel.GetById 36ms RepoModel.GetById 2ms app.codeStats 0ms

/silverlining/commands/setup_node.py

https://bitbucket.org/ianb/silverlining/
Python | 133 lines | 126 code | 5 blank | 2 comment | 6 complexity | 0745c5c85c363c82a0463bd987b54866 MD5 | raw file
Possible License(s): GPL-2.0
  1. """Setup a new server/node"""
  2. import os
  3. from cmdutils import CommandError
  4. from silversupport.shell import ssh, run
  5. def setup_rsync(config, source, dest, delete=False):
  6. cwd = os.path.abspath(os.path.join(__file__, '../..'))
  7. options = ['--quiet', '--executability', '-rvC']
  8. if delete:
  9. options.append('--delete')
  10. stdout, stderr, returncode = run(
  11. ['rsync'] + options +
  12. [source, 'root@%s:%s' % (config.args.node, dest)],
  13. cwd=cwd)
  14. config.logger.notify(
  15. "rsyncing %s to %s" % (source, dest))
  16. if returncode:
  17. config.logger.fatal(
  18. "An error occurred in rsync (code=%s)" % returncode)
  19. response = config.ask(
  20. "Continue?")
  21. if not response:
  22. raise CommandError(
  23. "Aborting due to failure")
  24. def command_setup_node(config):
  25. os.environ['LANG'] = 'C'
  26. node = config.args.node
  27. config.logger.notify(
  28. 'Setting up authentication on server...')
  29. for path in ['id_rsa.pub', 'id_dsa.pub']:
  30. pubkey_path = os.path.join(os.environ['HOME'], '.ssh', path)
  31. if os.path.exists(pubkey_path):
  32. key = open(pubkey_path, 'rb').read()
  33. config.logger.notify("Using key file: %s", pubkey_path)
  34. break
  35. else:
  36. config.logger.fatal("Can't locate any key file")
  37. #not sure what error code are used for here but 8 was unused
  38. return 8
  39. ssh('root', node, '''
  40. if [ -e /root/.silverlining-server-setup ] ; then
  41. exit 50
  42. fi
  43. mkdir -p /usr/local/share/silverlining/lib
  44. mkdir -p /root/.ssh
  45. cat >> /root/.ssh/authorized_keys
  46. ''',
  47. stdin=key)
  48. config.logger.notify(
  49. "Updating indexes and setting up rsync")
  50. stdout, stderr, returncode = ssh(
  51. 'root', node, '''
  52. dpkg --configure -a
  53. apt-get update -qq
  54. apt-get -y -q=2 install rsync
  55. ''')
  56. config.logger.notify(
  57. "Updating postfix configuration")
  58. stdout, stderr, returncode = ssh(
  59. 'root', node, '''
  60. echo "
  61. Name: postfix/mailname
  62. Template: postfix/mailname
  63. Value: %s
  64. Owners: postfix
  65. Flags: seen
  66. Name: postfix/main_mailer_type
  67. Template: postfix/main_mailer_type
  68. Value: Internet site
  69. Owners: postfix
  70. Flags: seen
  71. " >> /root/config.dat
  72. ''' % node)
  73. if returncode:
  74. config.logger.fatal(
  75. "An error occurred (code=%r)"
  76. % returncode)
  77. response = config.ask(
  78. "Continue?")
  79. if not response:
  80. return 3
  81. config.logger.notify(
  82. "Running apt-get install on server")
  83. lines = list(open(os.path.abspath(
  84. os.path.join(__file__, '../../server-sync-scripts/dpkg-query.txt'))))
  85. packages = ' '.join(line.strip().split()[0]
  86. for line in lines
  87. if line.strip() and not line.strip().startswith('#'))
  88. stdout, stderr, returncode = ssh(
  89. 'root', node, """DEBCONF_DB_OVERRIDE='File{/root/config.dat}' """ + \
  90. """apt-get -y -q=2 install --no-install-recommends $(cat)""",
  91. stdin=packages)
  92. if returncode:
  93. config.logger.fatal(
  94. "An error occurred (code=%r)"
  95. % returncode)
  96. response = config.ask(
  97. "Continue?")
  98. if not response:
  99. return 5
  100. setup_rsync(config, 'server-root/', '/')
  101. setup_rsync(config,
  102. os.path.abspath(os.path.join(__file__, '../../../silversupport/'))+'/',
  103. '/usr/local/share/silverlining/lib/silversupport/')
  104. setup_rsync(config,
  105. os.path.abspath(os.path.join(__file__, '../../mgr-scripts/'))+'/',
  106. '/usr/local/share/silverlining/mgr-scripts/')
  107. ssh('root', node, 'mv /var/root/* /root/')
  108. # Move over the root files, we do *not* rsync a /root dir because
  109. # that would copy the wrong permissions for the /root directorty
  110. # which results in ssh key auth no longer working
  111. setup_script = open(os.path.abspath(os.path.join(
  112. __file__, '../../server-sync-scripts/update-server-script.sh'))).read()
  113. import getpass
  114. username = getpass.getuser()
  115. setup_script = setup_script.replace('__REMOTE_USER__', username)
  116. stdout, stderr, returncode = ssh(
  117. 'root', node, setup_script)
  118. if returncode:
  119. config.logger.fatal(
  120. "An error occurred (code=%r)"
  121. % returncode)
  122. # No need to ask because it's the last task anyway
  123. return 6