publish.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import pwd
  2. import subprocess
  3. import time
  4. from pathlib import Path
  5. import zipfile
  6. import os
  7. import oss2
  8. import shutil
  9. def path_is_parent(parent_path, child_path):
  10. parent_path = os.path.abspath(parent_path)
  11. child_path = os.path.abspath(child_path)
  12. return os.path.commonpath([parent_path]) == os.path.commonpath([parent_path, child_path])
  13. def is_ignored(target_path, ignores):
  14. for ignore in ignores:
  15. if os.path.relpath(target_path, ignore) == '.' or path_is_parent(ignore, target_path):
  16. return True
  17. return False
  18. def zip_file(workspace, eve_app):
  19. os.chdir('%s/%s/src' % (workspace, eve_app))
  20. if os.path.isfile('README.md'):
  21. shutil.copy('README.md', 'code')
  22. elif os.path.isfile('readme.md'):
  23. shutil.copy('readme.md', 'code')
  24. elif os.path.isfile('Readme.md'):
  25. shutil.copy('Readme.md', 'code')
  26. os.chdir('%s/%s/src/code' % (workspace, eve_app))
  27. ignore_list = ['./.git', './.github',
  28. './.idea', './.DS_Store', './.vscode']
  29. with zipfile.ZipFile('code.zip', mode="w") as f:
  30. for dirpath, dirnames, filenames in os.walk('./'):
  31. if dirpath != './' and is_ignored(dirpath, ignore_list):
  32. continue
  33. for filename in filenames:
  34. absoult_file_path = os.path.join(dirpath, filename)
  35. if not is_ignored(absoult_file_path, ignore_list) and "code.zip" not in filename:
  36. f.write(os.path.join(dirpath, filename))
  37. return 'code.zip'
  38. def zip_golang_binary(workspace, eve_app):
  39. os.chdir('%s/%s/src' % (workspace, eve_app))
  40. if os.path.isfile('README.md'):
  41. shutil.copy('README.md', 'code/target')
  42. elif os.path.isfile('readme.md'):
  43. shutil.copy('readme.md', 'code/target')
  44. elif os.path.isfile('Readme.md'):
  45. shutil.copy('Readme.md', 'code/target')
  46. os.chdir('%s/%s/src/code/target' % (workspace, eve_app))
  47. ignore_list = ['./.git', './.github',
  48. './.idea', './.DS_Store', './.vscode']
  49. with zipfile.ZipFile('code.zip', mode="w") as f:
  50. for dirpath, dirnames, filenames in os.walk('./'):
  51. if dirpath != './' and is_ignored(dirpath, ignore_list):
  52. continue
  53. for filename in filenames:
  54. absoult_file_path = os.path.join(dirpath, filename)
  55. if not is_ignored(absoult_file_path, ignore_list) and "code.zip" not in filename:
  56. f.write(os.path.join(dirpath, filename))
  57. return 'code.zip'
  58. def upload_oss(code_name, zip_file):
  59. auth = oss2.Auth(os.environ.get('AccessKeyId'),
  60. os.environ.get('AccessKeySecret'))
  61. bucket = oss2.Bucket(auth, os.environ.get(
  62. 'ArtifactEndpoint'), os.environ.get('ArtifactBucket'))
  63. with open(zip_file, 'rb') as fileobj:
  64. object_name = '%s/code.zip' % (code_name)
  65. bucket.put_object(object_name, fileobj)
  66. workspace = os.getcwd()
  67. with open('update.list') as f:
  68. publish_list = [eve_app.strip() for eve_app in f.readlines()]
  69. failed_registry = []
  70. failed_oss = []
  71. for eve_app in publish_list:
  72. times = 1
  73. os.chdir(workspace)
  74. try:
  75. while times <= 3:
  76. print("----------------------: ", eve_app)
  77. # publish app to registry
  78. publish_script = 'https://serverless-registry.oss-cn-hangzhou.aliyuncs.com/publish-file/python3/hub-publish.py'
  79. command = 'cd %s && wget %s && python hub-publish.py' % (
  80. eve_app, publish_script)
  81. child = subprocess.Popen(
  82. command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, )
  83. stdout, stderr = child.communicate()
  84. if child.returncode == 0:
  85. print("stdout:", stdout.decode("utf-8"))
  86. break
  87. else:
  88. print("stdout:", stdout.decode("utf-8"))
  89. print("stderr:", stderr.decode("utf-8"))
  90. time.sleep(3)
  91. if times == 3:
  92. raise ChildProcessError(stderr)
  93. times = times + 1
  94. except Exception as e:
  95. print('Failed to publish registry, app %s, err: %s' % (eve_app, e))
  96. failed_registry.append(eve_app)
  97. # publish code.zip to oss
  98. os.chdir(workspace)
  99. try:
  100. makefile = Path('%s/src/Makefile' % (eve_app))
  101. if makefile.is_file():
  102. print("----------------------Makefile: ", makefile)
  103. command = 'cd %s/src && make release' % (eve_app)
  104. print(command)
  105. child = subprocess.Popen(
  106. command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, )
  107. stdout, stderr = child.communicate()
  108. print("stdout:", stdout.decode("utf-8"))
  109. if child.returncode != 0:
  110. print("stderr:", stderr.decode("utf-8"))
  111. raise ChildProcessError(stderr)
  112. jarPath = '%s/%s/src/code/target/code.jar' % (workspace, eve_app)
  113. golangBinaryPath = '%s/%s/src/code/target/main' % (workspace, eve_app)
  114. if os.path.isfile(jarPath):
  115. code_zip = jarPath
  116. elif os.path.isfile(golangBinaryPath):
  117. code_zip = zip_golang_binary(workspace, eve_app)
  118. else:
  119. code_zip = zip_file(workspace, eve_app)
  120. upload_oss(eve_app, code_zip)
  121. except Exception as e:
  122. print('Failed to publish oss, app %s, err: %s' % (eve_app, e))
  123. failed_oss.append(eve_app)
  124. print('Failed registry list: ', failed_registry)
  125. print('Failed oss list: ', failed_oss)