1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
import os import platform import shutil import toml import requests import hashlib import zipfile
def getFileMd5(filePath): with open(filePath,'rb') as f: md5obj = hashlib.md5() md5obj.update(f.read()) hash = md5obj.hexdigest()
return hash
def unzip_single(src, dest, password): if password: password = password.encode() zf = zipfile.ZipFile(src) zf.extractall(path=dest, pwd=password) zf.close()
def unzip_all(src, dest, password=None): if not os.path.isdir(src): unzip_single(src, dest, password) else: it = os.scandir(src) for entry in it: if entry.is_file() and os.path.splitext(entry.name)[1]=='.zip' : unzip_single(entry.path, dest, password)
def verifyRes(targetDir,remoteRes): returnValue = False if not os.path.exists(targetDir): os.makedirs(targetDir)
srcFile = os.path.join(targetDir,"{0}-{1}.zip".format(remoteRes["name"],remoteRes["md5"])) hasFile = False if os.path.exists(srcFile): hashMd5 = getFileMd5(srcFile) print("cached,Md5",hashMd5) if hashMd5 == remoteRes["md5"] or hashMd5 == remoteRes["md5"].lower(): hasFile = True
if not hasFile : httpResponse = requests.get(remoteRes["url"]) hashMd5 = None if httpResponse.status_code == requests.codes.ok: with open(srcFile,"wb") as f: f.write(httpResponse.content) hashMd5 = getFileMd5(srcFile) print("new,Md5",hashMd5) if hashMd5 == remoteRes["md5"] or hashMd5 == remoteRes["md5"].lower(): hasFile = True if hasFile and zipfile.is_zipfile(srcFile) : unzip_all(srcFile,targetDir) returnValue = True print("解压成功",srcFile) return returnValue
if __name__ == '__main__':
dir = os.path.abspath(os.path.dirname(__file__))
source = None osName = platform.system() if osName == "Windows": source = os.path.join(dir,"gitlab-runner-config.toml") elif osName == "Linux": source = "/etc/gitlab-runner/config.toml"
if source is None: print("Source config is none") exit(1) elif not os.access(source, os.F_OK): print("Source config not exist") exit(1) elif not os.access(source, os.R_OK): print("Source config can't read") exit(1) elif not os.access(source, os.W_OK): print("Source config can't write") exit(1)
target = os.path.join(dir,"config.toml")
try: shutil.copyfile(source, target) except IOError as e: print("Unable to copy file. %s" % e) exit(1) except: print("Unexpected error:", sys.exc_info()) exit(1)
targetRunnerDefaultName = "android-runner-book"
targetRunnerName = input("Enter target runner name ({0}): ".format(targetRunnerDefaultName)) if len(targetRunnerName) == 0 : targetRunnerName = targetRunnerDefaultName
if os.path.exists(target) : hasChanged = False configs = toml.load(target)
hostGradleHomeDir = None hostAndroidSdkDir = None if osName == "Windows": hostGradleHomeDir = os.path.join(dir,"gradle-home") hostAndroidSdkDir = os.path.join(dir,"android-sdk") elif osName == "Linux": hostGradleHomeDir = "/opt/gitlab-runner/gradle-home" hostAndroidSdkDir = "/opt/gitlab-runner/android-sdk"
remoteGradleRes = {"url":"https://192.168.2.25:456/gradle-5.6.4-6.1.1.zip","md5":"A8CDFB46BE21A98EBFFAC9174276CF47","name":"xGradle-5.6.4-6.1.1"} remoteAndroidSdkRes = {"url":"https://192.168.2.25:456/sdk-29.0.3.zip","md5":"BFA11A9E09C5B7525EFDD9627B2604FE","name":"xAndroidSdk"} hasLocalGradleRes = False hasLocalAndroidSdkRes = False
for runnerConfig in configs["runners"]: if runnerConfig["name"] == targetRunnerName: hasChanged = True runnerConfig["builds_dir"] = "/opt/gitlab-runner/builds" runnerConfig["cache_dir"] = "/opt/gitlab-runner/cache" runnerConfig["custom_build_dir"]["enabled"] = False
if "cache" in runnerConfig : del runnerConfig["cache"]
if runnerConfig["executor"] == "docker" and "docker" in runnerConfig: dockerConfig = runnerConfig["docker"] runnerDockerVolumes = None if "volumes" not in dockerConfig : runnerDockerVolumes = [] else: runnerDockerVolumes = dockerConfig["volumes"] volumeOfGradle = "{0}:/android/gradle:rw".format(hostGradleHomeDir) if volumeOfGradle not in runnerDockerVolumes: runnerDockerVolumes.append(volumeOfGradle) hasLocalGradleRes = False else: hasLocalGradleRes = True
volumeOfAndroidSdk = "{0}:/android/sdk:rw".format(hostAndroidSdkDir) if volumeOfAndroidSdk not in runnerDockerVolumes: runnerDockerVolumes.append(volumeOfAndroidSdk) hasLocalAndroidSdkRes = False else: hasLocalAndroidSdkRes = True
if not hasLocalGradleRes: hasLocalGradleRes=verifyRes(hostGradleHomeDir,remoteGradleRes) if not hasLocalAndroidSdkRes: hasLocalAndroidSdkRes=verifyRes(hostAndroidSdkDir,remoteAndroidSdkRes)
if hasChanged : with open(target, 'w') as f: r = toml.dump(configs, f) print("配置已更新到:",target)
|