common.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #
  2. #
  3. # Python library with commonly used functions within the package management scope
  4. #
  5. # Author : Pavlos Emm. Katsoulakis <paul@netdata.cloud>
  6. import lxc
  7. import subprocess
  8. def replace_tag(tag_name, spec, new_tag_content):
  9. print("Fixing tag %s in %s" % (tag_name, spec))
  10. ifp = open(spec, "r")
  11. config = ifp.readlines()
  12. ifp.close()
  13. source_line = -1
  14. for line in config:
  15. if str(line).count(tag_name + ":") > 0:
  16. source_line = config.index(line)
  17. print("Found line: %s in item %d" % (line, source_line))
  18. break
  19. if source_line >= 0:
  20. print("Replacing line %s with %s in spec file" %(config[source_line], new_tag_content))
  21. config[source_line] = "%s: %s\n" % (tag_name, new_tag_content)
  22. config_str = ''.join(config)
  23. ofp = open(spec, 'w')
  24. ofp.write(config_str)
  25. ofp.close()
  26. def run_command(container, command):
  27. print("Running command: %s" % command)
  28. command_result = container.attach_wait(lxc.attach_run_command, command)
  29. if command_result != 0:
  30. raise Exception("Command failed with exit code %d" % command_result)
  31. def run_command_in_host(cmd):
  32. print("Issue command in host: %s" % str(cmd))
  33. proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  34. o, e = proc.communicate()
  35. print('Output: ' + o.decode('ascii'))
  36. print('Error: ' + e.decode('ascii'))
  37. print('code: ' + str(proc.returncode))