cmd.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env python3
  2. # encoding: utf-8
  3. import subprocess
  4. import os
  5. from lib import utils
  6. from lib.utils import init_local_user_path
  7. def init_ansible_playbook_path():
  8. if '/usr/local/bin' not in os.environ.get('PATH', '').split(':'):
  9. os.environ['PATH'] = '/usr/local/bin:' + os.environ.get('PATH', '')
  10. def get_ansible_config_path():
  11. return os.path.join(os.getcwd(), 'onecloud/ansible.cfg')
  12. def run_cmd(cmds, env=None, no_strip=False, realtime_output=False):
  13. shell_cmd = cmds
  14. if isinstance(cmds, list):
  15. shell_cmd = ' '.join(cmds)
  16. # logging.debug('run cmd `%s` with env %s' % (shell_cmd, env))
  17. print('run cmd: `%s`' % shell_cmd)
  18. if env is None:
  19. env = os.environ.copy()
  20. proc = subprocess.Popen(
  21. shell_cmd,
  22. shell=True,
  23. universal_newlines=True,
  24. stdout=subprocess.PIPE,
  25. stderr=subprocess.STDOUT,
  26. env=env,
  27. )
  28. output = ''
  29. while True:
  30. line = proc.stdout.readline()
  31. if not line:
  32. break
  33. if not no_strip:
  34. line = line.rstrip()
  35. if realtime_output:
  36. print(line, end='')
  37. else:
  38. print(line)
  39. output += line
  40. proc.wait()
  41. if proc.returncode != 0:
  42. print(output)
  43. raise Exception('cmd `%s` return %s' % (shell_cmd, proc.returncode))
  44. return output
  45. def _run_cmd(cmds):
  46. shell_cmd = ' '.join(cmds)
  47. print(shell_cmd)
  48. os.environ['ANSIBLE_FORCE_COLOR'] = '1'
  49. config_file = get_ansible_config_path()
  50. if not os.path.exists(config_file):
  51. raise Exception("Not found file %s" % config_file)
  52. os.environ['ANSIBLE_CONFIG'] = get_ansible_config_path()
  53. init_ansible_playbook_path()
  54. proc = subprocess.Popen(
  55. shell_cmd,
  56. shell=True,
  57. universal_newlines=True,
  58. stdout=subprocess.PIPE,
  59. stderr=subprocess.STDOUT,)
  60. while True:
  61. line = proc.stdout.readline()
  62. if not line:
  63. break
  64. print(line.rstrip())
  65. proc.wait()
  66. return proc.returncode
  67. def run_ansible_playbook(hosts_f, playbook_f, debug_level=0, vars=None):
  68. """
  69. debug level support example:
  70. ANSIBLE_VERBOSITY=4 /opt/yunionboot/run.py /opt/yunion/upgrade/config.yml
  71. """
  72. init_local_user_path()
  73. debug_flag = ''
  74. if debug_level == 0:
  75. debug_level = int(os.environ.get('ANSIBLE_VERBOSITY', 0))
  76. if debug_level > 0:
  77. if debug_level > 0:
  78. debug_flag = '-' + 'v' * debug_level
  79. cmd = ["ansible-playbook"]
  80. if vars:
  81. vars_f = "./oc_vars.yml"
  82. with open(vars_f, 'w') as f:
  83. f.write(utils.to_yaml(vars))
  84. cmd.extend(["-e", "@%s" % vars_f])
  85. cmd.extend(["-i", hosts_f, playbook_f])
  86. if len(debug_flag) > 0:
  87. cmd.append(debug_flag)
  88. skip_tags = os.environ.get('SKIP_TAGS', "")
  89. if len(skip_tags) > 0:
  90. cmd.extend(["--skip-tags", f"'{skip_tags}'"])
  91. return _run_cmd(cmd)
  92. def run_bash_cmd(cmd):
  93. import os
  94. os.system(cmd)
  95. def ensure_pv():
  96. if not os.path.isfile('/usr/bin/pv'):
  97. run_bash_cmd('yum install -y pv >/dev/null')