common.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. import sys
  10. import tempfile
  11. import shutil
  12. def fetch_version(orig_build_version):
  13. tag = None
  14. friendly_version = ""
  15. # TODO: Checksum validations
  16. if str(orig_build_version).count(".latest") == 1:
  17. version_list=str(orig_build_version).replace('v', '').split('.')
  18. minor = version_list[3] if int(version_list[2]) == 0 else (version_list[2] + version_list[3])
  19. friendly_version='.'.join(version_list[0:2]) + "." + minor
  20. else:
  21. friendly_version = orig_build_version.replace('v', '')
  22. tag = friendly_version # Go to stable tag
  23. print("Version set to %s from %s" % (friendly_version, orig_build_version))
  24. return friendly_version, tag
  25. def replace_tag(tag_name, spec, new_tag_content):
  26. print("Fixing tag %s in %s" % (tag_name, spec))
  27. ifp = open(spec, "r")
  28. config = ifp.readlines()
  29. ifp.close()
  30. source_line = -1
  31. for line in config:
  32. if str(line).count(tag_name + ":") > 0:
  33. source_line = config.index(line)
  34. print("Found line: %s in item %d" % (line, source_line))
  35. break
  36. if source_line >= 0:
  37. print("Replacing line %s with %s in spec file" %(config[source_line], new_tag_content))
  38. config[source_line] = "%s: %s\n" % (tag_name, new_tag_content)
  39. config_str = ''.join(config)
  40. ofp = open(spec, 'w')
  41. ofp.write(config_str)
  42. ofp.close()
  43. def run_command(container, command):
  44. print("Running command: %s" % command)
  45. command_result = container.attach_wait(lxc.attach_run_command, command)
  46. if command_result != 0:
  47. raise Exception("Command failed with exit code %d" % command_result)
  48. def run_command_in_host(cmd, cwd=None):
  49. print("Issue command in host: %s, cwd:%s" % (str(cmd), str(cwd)))
  50. proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd)
  51. o, e = proc.communicate()
  52. print('Output: ' + o.decode('ascii'))
  53. print('Error: ' + e.decode('ascii'))
  54. print('code: ' + str(proc.returncode))
  55. def prepare_repo(container):
  56. if str(os.environ["REPO_TOOL"]).count("zypper") == 1:
  57. run_command(container, [os.environ["REPO_TOOL"], "clean", "-a"])
  58. run_command(container, [os.environ["REPO_TOOL"], "--no-gpg-checks", "update", "-y"])
  59. elif str(os.environ["REPO_TOOL"]).count("yum") == 1:
  60. run_command(container, [os.environ["REPO_TOOL"], "clean", "all"])
  61. run_command(container, [os.environ["REPO_TOOL"], "update", "-y"])
  62. if os.environ["BUILD_STRING"].count("el/7") == 1 and os.environ["BUILD_ARCH"].count("i386") == 1:
  63. print ("Skipping epel-release install for %s-%s" % (os.environ["BUILD_STRING"], os.environ["BUILD_ARCH"]))
  64. else:
  65. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "epel-release"])
  66. elif str(os.environ["REPO_TOOL"]).count("apt-get") == 1:
  67. if str(os.environ["BUILD_STRING"]).count("debian/jessie") == 1:
  68. run_command(container, ["bash", "-c", "echo deb http://archive.debian.org/debian/ jessie-backports main contrib non-free >> /etc/apt/sources.list.d/99-archived.list"])
  69. run_command(container, [os.environ["REPO_TOOL"], "update", "-y", '-o', 'Acquire::Check-Valid-Until=false'])
  70. else:
  71. run_command(container, [os.environ["REPO_TOOL"], "update", "-y"])
  72. else:
  73. run_command(container, [os.environ["REPO_TOOL"], "update", "-y"])
  74. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "sudo"])
  75. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "wget"])
  76. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "bash"])
  77. def install_common_dependendencies(container):
  78. if str(os.environ["REPO_TOOL"]).count("zypper") == 1:
  79. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "gcc-c++"])
  80. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "json-glib-devel"])
  81. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "freeipmi-devel"])
  82. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "cups-devel"])
  83. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "snappy-devel"])
  84. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "protobuf-devel"])
  85. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "protobuf-c"])
  86. elif str(os.environ["REPO_TOOL"]).count("yum") == 1:
  87. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "gcc-c++"])
  88. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "json-c-devel"])
  89. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "freeipmi-devel"])
  90. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "cups-devel"])
  91. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "snappy-devel"])
  92. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "protobuf-devel"])
  93. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "protobuf-c-devel"])
  94. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "protobuf-compiler"])
  95. elif str(os.environ["REPO_TOOL"]).count("apt-get") == 1:
  96. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "g++"])
  97. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "libipmimonitoring-dev"])
  98. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "libjson-c-dev"])
  99. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "libcups2-dev"])
  100. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "libsnappy-dev"])
  101. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "libprotobuf-dev"])
  102. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "libprotoc-dev"])
  103. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "protobuf-compiler"])
  104. if os.environ["BUILD_STRING"].count("debian/jessie") == 1:
  105. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "snappy"])
  106. else:
  107. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "gcc-c++"])
  108. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "cups-devel"])
  109. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "freeipmi-devel"])
  110. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "json-c-devel"])
  111. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "snappy-devel"])
  112. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "protobuf-devel"])
  113. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "protobuf-c-devel"])
  114. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "protobuf-compiler"])
  115. if os.environ["BUILD_STRING"].count("el/6") <= 0:
  116. run_command(container, [os.environ["REPO_TOOL"], "install", "-y", "autogen"])
  117. def prepare_version_source(dest_archive, pkg_friendly_version, tag=None):
  118. print(".0 Preparing local implementation tarball for version %s" % pkg_friendly_version)
  119. tar_file = os.environ['LXC_CONTAINER_ROOT'] + dest_archive
  120. print(".0 Copy repo to prepare it for tarball generation")
  121. tmp_src = tempfile.mkdtemp(prefix='netdata-source-')
  122. run_command_in_host(['cp', '-r', '.', tmp_src])
  123. if tag is not None:
  124. print(".1 Checking out tag %s" % tag)
  125. run_command_in_host(['git', 'fetch', '--all'], tmp_src)
  126. # TODO: Keep in mind that tricky 'v' there, needs to be removed once we clear our versioning scheme
  127. run_command_in_host(['git', 'checkout', 'v%s' % pkg_friendly_version], tmp_src)
  128. print(".2 Tagging the code with version: %s" % pkg_friendly_version)
  129. run_command_in_host(['git', 'tag', '-a', pkg_friendly_version, '-m', 'Tagging while packaging on %s' % os.environ["CONTAINER_NAME"]], tmp_src)
  130. print(".3 Run autoreconf -ivf")
  131. run_command_in_host(['autoreconf', '-ivf'], tmp_src)
  132. print(".4 Run configure")
  133. run_command_in_host(['./configure', '--prefix=/usr', '--sysconfdir=/etc', '--localstatedir=/var', '--libdir=/usr/lib', '--libexecdir=/usr/libexec', '--with-math', '--with-zlib', '--with-user=netdata'], tmp_src)
  134. print(".5 Run make dist")
  135. run_command_in_host(['make', 'dist'], tmp_src)
  136. print(".6 Copy generated tarbal to desired path")
  137. generated_tarball = '%s/netdata-%s.tar.gz' % (tmp_src, pkg_friendly_version)
  138. if os.path.exists(generated_tarball):
  139. run_command_in_host(['sudo', 'cp', generated_tarball, tar_file])
  140. print(".7 Fixing permissions on tarball")
  141. run_command_in_host(['sudo', 'chmod', '777', tar_file])
  142. print(".8 Bring over netdata.spec, Remove temp directory");
  143. run_command_in_host(['cp', '%s/netdata.spec' % tmp_src, 'netdata.spec'])
  144. shutil.rmtree(tmp_src)
  145. else:
  146. print("I could not find (%s) on the disk, stopping the build. Kindly check the logs and try again" % generated_tarball)
  147. sys.exit(1)