utils.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import zipfile
  2. import os
  3. import glob
  4. import re
  5. import shutil
  6. import wget
  7. from jinja2 import Template, Environment, FileSystemLoader
  8. def extract_zip(filename, target_dir):
  9. with zipfile.ZipFile(filename, 'r') as zip_ref:
  10. zip_ref.extractall(target_dir)
  11. def get_nssm(tmpPath, version):
  12. if not os.path.isdir(tmpPath):
  13. os.mkdir(tmpPath)
  14. target_filename = '{}/nssm-{}.zip'.format(tmpPath, version)
  15. exists = os.path.isfile(target_filename)
  16. if exists:
  17. return target_filename
  18. url = 'https://nssm.cc/release/nssm-{}.zip'.format(version)
  19. print('NSSM url is {}'.format(url))
  20. filename = wget.download(url, out=target_filename, bar=wget.bar_thermometer)
  21. return filename
  22. def get_zip(version, target_filename):
  23. exists = os.path.isfile(target_filename)
  24. if exists:
  25. return target_filename
  26. url = 'https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-{}.windows-amd64.zip'.format(version)
  27. #url = 'https://dl.grafana.com/enterprise/release/grafana-enterprise-{}.windows-amd64.zip'.format(version)
  28. filename = wget.download(url, out=target_filename, bar=wget.bar_thermometer)
  29. return filename
  30. #
  31. #
  32. #
  33. def detect_version(dist_path):
  34. detectedVersion = ''
  35. detectedHash = ''
  36. isEnterprise = False
  37. print("Detecting Version...")
  38. # grafana-6.0.0-ca0bc2c5pre3.windows-amd64.zip
  39. # get files in directory matching pattern
  40. fileList = glob.glob(dist_path + '/grafana*.windows-amd64.zip')
  41. print(fileList)
  42. if len(fileList) == 0:
  43. print('Skipping detection, no matches')
  44. return
  45. firstFile = fileList[0]
  46. p1 = re.search(r'grafana-(\d\.\d\.\d)\.windows-amd64.zip$', firstFile)
  47. p2 = re.search(r'grafana-(\d\.\d\.\d)-(.*)\.windows-amd64.zip$', firstFile)
  48. if p1:
  49. detectedVersion = p1.group(1)
  50. if p2:
  51. detectedVersion = p2.group(1)
  52. detectedHash = p2.group(2)
  53. return detectedVersion, detectedHash, isEnterprise
  54. #if os.path.isdir(dist_path + 'enterprise-dist'):
  55. # # grafana-enterprise-6.0.0-29b28127pre3.windows-amd64.zip
  56. # # get files in directory matching pattern
  57. # fileList = glob.glob(dist_path + '/enterprise-dist/grafana*.windows-amd64.zip')
  58. # firstFile = fileList[0]
  59. # p1 = re.search(r'grafana-enterprise-(\d\.\d\.\d)\.windows-amd64.zip$', firstFile)
  60. # p2 = re.search(r'grafana-enterprise-(\d\.\d\.\d)-(.*)\.windows-amd64.zip$', firstFile)
  61. # if p1:
  62. # detectedVersion = p1.group(1)
  63. # isEnterprise = True
  64. # if p2:
  65. # detectedVersion = p2.group(1)
  66. # detectedHash = p2.group(2)
  67. # isEnterprise = True
  68. # return detectedVersion, detectedHash, isEnterprise
  69. def generate_product_wxs(env, config, features, scratch_file, target_dir):
  70. template = env.get_template('common/product.wxs.j2')
  71. output = template.render(config=config, features=features)
  72. fh = open(scratch_file, 'w')
  73. fh.write(output)
  74. fh.close()
  75. shutil.copy2(scratch_file, target_dir)
  76. def generate_service_wxs(env, grafana_version, scratch_file, target_dir, nssm_version='2.24'):
  77. template = env.get_template('common/grafana-service.wxs.j2')
  78. output = template.render(grafana_version=grafana_version, nssm_version=nssm_version)
  79. fh = open(scratch_file, 'w')
  80. fh.write(output)
  81. fh.close()
  82. shutil.copy2(scratch_file, target_dir)
  83. def generate_firewall_wxs(env, grafana_version, scratch_file, target_dir):
  84. os.system("ls -al templates")
  85. template = env.get_template('common/grafana-firewall.wxs.j2')
  86. output = template.render(grafana_version=grafana_version)
  87. fh = open(scratch_file, 'w')
  88. fh.write(output)
  89. fh.close()
  90. shutil.copy2(scratch_file, target_dir)
  91. def generate_oracle_environment_wxs(env, instant_client_version, scratch_file, target_dir):
  92. template = env.get_template('oracle/oracle-environment.wxs.j2')
  93. output = template.render(instant_client_version=instant_client_version)
  94. fh = open(scratch_file, 'w')
  95. fh.write(output)
  96. fh.close()
  97. shutil.copy2(scratch_file, target_dir)
  98. def copy_static_files(target_dir):
  99. for item in os.listdir('resources/images'):
  100. s = os.path.join('resources/images', item)
  101. d = os.path.join(target_dir, item)
  102. shutil.copy2(s, d)
  103. for item in os.listdir('resources/license'):
  104. s = os.path.join('resources/license', item)
  105. d = os.path.join(target_dir, item)
  106. shutil.copy2(s, d)