CLI supporting kolla-ansible deployment project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cli_setup.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2018 OpenStack Foundation
  4. # All Rights Reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  7. # not use this file except in compliance with the License. You may obtain
  8. # a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. # License for the specific language governing permissions and limitations
  16. # under the License.
  17. import os
  18. import sys
  19. from kolla_cli.common import utils
  20. kolla_ansible_source_base = '../kolla-ansible'
  21. kolla_ansible_home_target = utils.get_kolla_ansible_home()
  22. kolla_ansible_etc_target = utils.get_kolla_etc()
  23. ansible = 'ansible'
  24. kolla = 'kolla'
  25. kolla_cli = 'kolla-cli'
  26. tools = 'tools'
  27. def setup_ansible_etc():
  28. # if the kolla-cli directory for the inventory doesn't exist
  29. # already then make it. this will also create the directory the
  30. # globals and password file goes into
  31. cli_etc_dir = os.path.join(kolla_ansible_etc_target,
  32. kolla_cli, ansible)
  33. if not os.path.exists(cli_etc_dir):
  34. make_cli_etc_dir_cmd = ('mkdir -p %s' % cli_etc_dir)
  35. _command_exec(make_cli_etc_dir_cmd)
  36. # Create the inventory file (if it doesn't already exist).
  37. # (The script will exit here if the user doesn't have sufficient privs.)
  38. inventory_file_path = os.path.join(cli_etc_dir, 'inventory.json')
  39. touch_inventory_file_cmd = ('touch %s' % inventory_file_path)
  40. _command_exec(touch_inventory_file_cmd)
  41. # copy over all kolla ansible etc files
  42. kolla_ansible_etc_source = os.path.join(kolla_ansible_source_base,
  43. 'etc', kolla)
  44. copy_kolla_etc_files_cmd = ('cp -a %s/* %s' % (kolla_ansible_etc_source,
  45. kolla_ansible_etc_target))
  46. _command_exec(copy_kolla_etc_files_cmd)
  47. # add ssh keys for cli
  48. key_path = os.path.join(os.getenv('HOME'), '.ssh', 'id_rsa')
  49. if not os.path.exists(key_path):
  50. # generate new ssh keys
  51. keygen_cmd = 'ssh-keygen -t rsa -N \'\' -f %s' % key_path
  52. _command_exec(keygen_cmd)
  53. # copy the public key to where kolla-cli expects it
  54. pub_key_path = os.path.join(os.getenv('HOME'), '.ssh', 'id_rsa.pub')
  55. cli_etc_path = os.path.join(kolla_ansible_etc_target, kolla_cli)
  56. copy_cmd = 'cp -p %s %s/' % (pub_key_path, cli_etc_path)
  57. _command_exec(copy_cmd)
  58. def setup_ansible_home():
  59. # make cli home ansible directory
  60. cli_ansible_dir = os.path.join(kolla_ansible_home_target,
  61. kolla_cli, ansible)
  62. if not os.path.exists(cli_ansible_dir):
  63. make_cli_ansible_dir_cmd = ('mkdir -p %s' % cli_ansible_dir)
  64. _command_exec(make_cli_ansible_dir_cmd)
  65. # make cli home tools directory
  66. cli_tools_dir = os.path.join(kolla_ansible_home_target,
  67. kolla_cli, tools)
  68. if not os.path.exists(cli_tools_dir):
  69. make_cli_tools_dir_cmd = ('mkdir -p %s' % cli_tools_dir)
  70. _command_exec(make_cli_tools_dir_cmd)
  71. # move cli tools files to tools directory
  72. copy_cli_tools_files_cmd = ('cp -a %s %s' % ('./tools/*', cli_tools_dir))
  73. _command_exec(copy_cli_tools_files_cmd)
  74. # create cli ansible lock file
  75. lock_file_path = os.path.join(kolla_ansible_home_target,
  76. kolla_cli, 'ansible.lock')
  77. if not os.path.exists(lock_file_path):
  78. touch_ansible_lock_file_cmd = ('touch %s' % lock_file_path)
  79. _command_exec(touch_ansible_lock_file_cmd)
  80. # copy over all kolla ansible home files
  81. kolla_ansible_home_source = os.path.join(kolla_ansible_source_base,
  82. ansible)
  83. copy_kolla_home_files_cmd = ('cp -a %s %s' % (kolla_ansible_home_source,
  84. kolla_ansible_home_target))
  85. _command_exec(copy_kolla_home_files_cmd)
  86. # create the host_vars directory if it doesn't exist already
  87. host_vars_path = os.path.join(kolla_ansible_home_target,
  88. ansible, 'host_vars')
  89. if not os.path.exists(host_vars_path):
  90. make_kolla_host_vars_dir_cmd = ('mkdir %s' % host_vars_path)
  91. _command_exec(make_kolla_host_vars_dir_cmd)
  92. # make link from etc globals to home globals
  93. target_etc_path = os.path.join(kolla_ansible_etc_target, 'globals.yml')
  94. target_home_link = os.path.join(kolla_ansible_home_target,
  95. ansible, 'group_vars', '__GLOBAL__')
  96. if not os.path.exists(target_home_link):
  97. link_globals_file_cmd = ('ln -s %s %s' % (target_etc_path,
  98. target_home_link))
  99. _command_exec(link_globals_file_cmd)
  100. def _command_exec(command):
  101. print('running - %s' % command)
  102. error, _ = utils.run_cmd(command)
  103. if error:
  104. print('error - %s' % error)
  105. sys.exit(1)
  106. def main():
  107. """make sure kolla-ansible and cli files are in the right places"""
  108. if len(sys.argv) >= 2:
  109. global kolla_ansible_source_base
  110. kolla_ansible_source_base = sys.argv[1]
  111. setup_ansible_etc()
  112. setup_ansible_home()
  113. if __name__ == '__main__':
  114. main()