common.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. import os
  9. def replace_tag(tag_name, spec, new_tag_content):
  10. print("Fixing tag %s in %s" % (tag_name, spec))
  11. ifp = open(spec, "r")
  12. config = ifp.readlines()
  13. ifp.close()
  14. source_line = -1
  15. for line in config:
  16. if str(line).count(tag_name + ":") > 0:
  17. source_line = config.index(line)
  18. print("Found line: %s in item %d" % (line, source_line))
  19. break
  20. if source_line >= 0:
  21. print("Replacing line %s with %s in spec file" %(config[source_line], new_tag_content))
  22. config[source_line] = "%s: %s\n" % (tag_name, new_tag_content)
  23. config_str = ''.join(config)
  24. ofp = open(spec, 'w')
  25. ofp.write(config_str)
  26. ofp.close()
  27. def run_command(container, command):
  28. print("Running command: %s" % command)
  29. command_result = container.attach_wait(lxc.attach_run_command, command)
  30. if command_result != 0:
  31. raise Exception("Command failed with exit code %d" % command_result)
  32. def run_command_in_host(cmd):
  33. print("Issue command in host: %s" % str(cmd))
  34. proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  35. o, e = proc.communicate()
  36. print('Output: ' + o.decode('ascii'))
  37. print('Error: ' + e.decode('ascii'))
  38. print('code: ' + str(proc.returncode))
  39. def install_common_dependendencies(container):
  40. if str(os.environ["REPO_TOOL"]).count("zypper") == 1:
  41. run_command(container, [os.environ["REPO_TOOL"], "clean", "-a"])
  42. run_command(container, [os.environ["REPO_TOOL"], "--no-gpg-checks", "update", "-y"])
  43. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "json-glib-devel"])
  44. elif str(os.environ["REPO_TOOL"]).count("yum") == 1:
  45. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "json-c-devel"])
  46. run_command(container, [os.environ["REPO_TOOL"], "clean", "all"])
  47. run_command(container, [os.environ["REPO_TOOL"], "update", "-y"])
  48. if os.environ["BUILD_STRING"].count("el/7") == 1 and os.environ["BUILD_ARCH"].count("i386") == 1:
  49. print ("Skipping epel-release install for %s-%s" % (os.environ["BUILD_STRING"], os.environ["BUILD_ARCH"]))
  50. else:
  51. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "epel-release"])
  52. else:
  53. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "json-c-devel"])
  54. run_command(container, [os.environ["REPO_TOOL"], "update", "-y"])
  55. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "sudo"])
  56. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "wget"])
  57. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "bash"])
  58. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "freeipmi-devel"])
  59. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "cups-devel"])
  60. def prepare_version_source(dest_archive, pkg_friendly_version, tag=None):
  61. print(".0 Preparing local implementation tarball for version %s" % pkg_friendly_version)
  62. tar_file = os.environ['LXC_CONTAINER_ROOT'] + dest_archive
  63. if tag is not None:
  64. print(".1 Checking out tag %s" % tag)
  65. run_command_in_host(['git', 'fetch', '--all'])
  66. # TODO: Keep in mind that tricky 'v' there, needs to be removed once we clear our versioning scheme
  67. run_command_in_host(['git', 'checkout', 'v%s' % pkg_friendly_version])
  68. print(".2 Tagging the code with version: %s" % pkg_friendly_version)
  69. run_command_in_host(['git', 'tag', '-a', pkg_friendly_version, '-m', 'Tagging while packaging on %s' % os.environ["CONTAINER_NAME"]])
  70. print(".3 Run autoreconf -ivf")
  71. run_command_in_host(['autoreconf', '-ivf'])
  72. print(".4 Run configure")
  73. run_command_in_host(['./configure', '--with-math', '--with-zlib', '--with-user=netdata'])
  74. print(".5 Run make dist")
  75. run_command_in_host(['make', 'dist'])
  76. print(".6 Copy generated tarbal to desired path")
  77. if os.path.exists('netdata-%s.tar.gz' % pkg_friendly_version):
  78. run_command_in_host(['sudo', 'cp', 'netdata-%s.tar.gz' % pkg_friendly_version, tar_file])
  79. print(".7 Fixing permissions on tarball")
  80. run_command_in_host(['sudo', 'chmod', '777', tar_file])
  81. else:
  82. print("I could not find (%s) on the disk, stopping the build. Kindly check the logs and try again" % 'netdata-%s.tar.gz' % pkg_friendly_version)
  83. sys.exit(1)