install.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env python3
  2. # encoding: utf-8
  3. import platform
  4. import os
  5. import time
  6. from lib import k3s
  7. from . import ocboot
  8. from . import cmd
  9. from . import logger
  10. from . import kernel_utils
  11. logger = logger.new(__file__)
  12. EE_INSTALLED_STR = """Initialized successfully!
  13. Check Web Page: https://%s
  14. To start using the command line tools, you need to `source ~/.bashrc` profile or relogin.
  15. """
  16. CE_INSTALLED_STR = """Initialized successfully!
  17. Web page: https://%s
  18. User: %s
  19. Password: %s
  20. """
  21. REBOOT_MSG = """
  22. ┌───────────────────────────────────────────────────────────────────────────────┐
  23. │ │
  24. │ The system is about to reboot in 30 secs to use new kernel. │
  25. │ You may abort the reboot by press Ctrl + C, and manually reboot later. │
  26. │ │
  27. └───────────────────────────────────────────────────────────────────────────────┘
  28. """
  29. REBOOT_TIMEOUT = 30
  30. def add_command(subparsers):
  31. parser = subparsers.add_parser("install", help="install onecloud cluster")
  32. parser.add_argument('config', help="config yaml file")
  33. parser.set_defaults(func=do_install)
  34. def do_install(args):
  35. try:
  36. return start(args.config)
  37. except (ocboot.ConfigNotFoundException) as e:
  38. logger.error("%s" % e)
  39. except Exception as e:
  40. raise e
  41. def need_reboot(ip, inside):
  42. if os.environ.get('NO_REBOOT', ''):
  43. print("NO_REBOOT flag passed, ignore rebooting")
  44. return False
  45. if not ip:
  46. return False
  47. if (inside is False) and (not kernel_utils.is_local_ip(ip)):
  48. return False
  49. if kernel_utils.is_local_ip(ip) and inside is True:
  50. return False
  51. if kernel_utils.is_yunion_kernel():
  52. return False
  53. return True
  54. def try_reboot_primary(ip):
  55. if not need_reboot(ip, inside=False):
  56. return
  57. print(REBOOT_MSG)
  58. for i in range(REBOOT_TIMEOUT):
  59. time.sleep(1)
  60. print("."),
  61. os.system('reboot')
  62. def start(config_file, extra_vars=None):
  63. config = ocboot.load_config(config_file)
  64. k3s.init_airgap_assets(k3s.GET_AIRGAP_DIR(), k3s.VERSION_V1_28_5_K3S_1)
  65. inventory_f = config.generate_inventory_file()
  66. ip = None
  67. try:
  68. ip = config.primary_master_config.node.node_ip
  69. except AttributeError:
  70. pass
  71. vars = config.ansible_global_vars()
  72. # 240722 wanyaoqi: no_reboot has been removed before this change
  73. # vars['no_reboot'] = 'false' if need_reboot(ip, inside=True) else 'true'
  74. vars['is_controller_node'] = 'true' if config.is_controller_node() else 'false'
  75. # 合并额外的变量(如 ai 相关变量)
  76. if extra_vars:
  77. vars.update(extra_vars)
  78. print("vars: ", vars)
  79. return_code = cmd.run_ansible_playbook(
  80. inventory_f,
  81. './onecloud/install-cluster.yml',
  82. vars=vars,
  83. )
  84. if return_code is not None and return_code != 0:
  85. return return_code
  86. login_info = config.get_login_info()
  87. if login_info is None:
  88. return 0
  89. if config.is_using_ee():
  90. print(EE_INSTALLED_STR % (login_info[0]))
  91. else:
  92. print(CE_INSTALLED_STR % (login_info[0],
  93. login_info[1],
  94. login_info[2]))
  95. try_reboot_primary(ip)
  96. return 0