Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
| 3 | Copyright 2018-present Open Networking Foundation |
| 4 | |
| 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations under the License. |
| 16 | """ |
| 17 | |
| 18 | from subprocess import call |
| 19 | import tempfile |
| 20 | import hashlib |
| 21 | import requests, os |
| 22 | import xml.etree.ElementTree, shutil |
Ray Milkey | f77ea41 | 2018-08-10 17:37:28 -0700 | [diff] [blame] | 23 | import time |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 24 | |
| 25 | SONATYPE_USER=os.environ.get("SONATYPE_USER") |
| 26 | SONATYPE_PASSWORD=os.environ.get("SONATYPE_PASSWORD") |
| 27 | SONATYPE_PROFILE=os.environ.get("SONATYPE_PROFILE") |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 28 | |
| 29 | CREATE_REPO_REQUEST_TEMPLATE = '''\ |
| 30 | <promoteRequest> |
| 31 | <data> |
| 32 | <description>%(description)</description> |
| 33 | </data> |
| 34 | </promoteRequest> |
| 35 | ''' |
| 36 | |
| 37 | CLOSE_REPO_REQUEST_TEMPLATE = '''\ |
| 38 | <promoteRequest> |
| 39 | <data> |
| 40 | <description>%(description)</description> |
| 41 | <stagedRepositoryId>%(repo_id)</stagedRepositoryId> |
| 42 | </data> |
| 43 | </promoteRequest> |
| 44 | ''' |
| 45 | |
Ray Milkey | f77ea41 | 2018-08-10 17:37:28 -0700 | [diff] [blame] | 46 | CLOSE_RETRY_ATTEMPTS = 12 * 2 |
| 47 | |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 48 | def hashlib_compute(hash, input_file, output_file): |
| 49 | with open(input_file, 'rb') as f: |
| 50 | for block in iter(lambda: f.read(100000), b''): |
| 51 | hash.update(block) |
| 52 | md5_string = hash.hexdigest() |
| 53 | output = open(output_file, "w") |
| 54 | output.write(md5_string + "\n") |
| 55 | f.close() |
| 56 | output.close() |
| 57 | |
| 58 | |
Ray Milkey | 7f46b1f | 2018-07-24 19:01:58 -0700 | [diff] [blame] | 59 | def generate_metadata_files(input_file, dest): |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 60 | # create a temporary directory to hold the metadata files |
| 61 | global tempdir |
Ray Milkey | 7f46b1f | 2018-07-24 19:01:58 -0700 | [diff] [blame] | 62 | base_metadata_filename = tempdir + "/" + os.path.basename(dest) |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 63 | |
| 64 | files = [] |
| 65 | |
| 66 | # generate the signature file |
| 67 | signature_filename = base_metadata_filename + ".asc" |
| 68 | call(["gpg", "--armor", "--detach-sig", "--output", signature_filename, input_file]) |
| 69 | files.append(signature_filename) |
| 70 | |
| 71 | # generate the md5 checksum file |
| 72 | md5_filename = base_metadata_filename + ".md5" |
| 73 | md5 = hashlib.md5() |
| 74 | hashlib_compute(md5, input_file, md5_filename) |
| 75 | files.append(md5_filename) |
| 76 | |
| 77 | # generate the SHA checksum file |
| 78 | sha1_filename = base_metadata_filename + ".sha1" |
| 79 | sha1 = hashlib.sha1() |
| 80 | hashlib_compute(sha1, input_file, sha1_filename) |
| 81 | files.append(sha1_filename) |
| 82 | |
Ray Milkey | 7f46b1f | 2018-07-24 19:01:58 -0700 | [diff] [blame] | 83 | # generate the base artifact |
| 84 | base_artifact_filename = base_metadata_filename |
| 85 | shutil.copyfile(input_file, base_artifact_filename) |
| 86 | files.append(base_artifact_filename) |
| 87 | |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 88 | return files |
| 89 | |
| 90 | |
| 91 | def create_staging_repo(description): |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 92 | if destination_repo_url is None: |
| 93 | return None |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 94 | create_request = CREATE_REPO_REQUEST_TEMPLATE.replace("%(description)", description) |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 95 | url = "https://" + destination_repo_url + "/service/local/staging/profiles" + "/" + SONATYPE_PROFILE + "/start" |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 96 | headers = {'Content-Type': 'application/xml'} |
| 97 | r = requests.post(url, create_request, headers=headers, auth=(SONATYPE_USER, SONATYPE_PASSWORD)) |
| 98 | root = xml.etree.ElementTree.fromstring(r.text) |
| 99 | repo_id = root.find("data").find("stagedRepositoryId").text |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 100 | return repo_id |
| 101 | |
| 102 | |
| 103 | def close_staging_repo(description, repo_id): |
Ray Milkey | 245d119 | 2018-08-14 17:49:33 -0700 | [diff] [blame] | 104 | if destination_repo_url is None: |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 105 | return |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 106 | close_request = CLOSE_REPO_REQUEST_TEMPLATE.replace("%(description)", description).replace("%(repo_id)", repo_id) |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 107 | url = "https://" + destination_repo_url + "/service/local/staging/profiles" + "/" + SONATYPE_PROFILE + "/finish" |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 108 | headers = {'Content-Type': 'application/xml'} |
| 109 | r = requests.post(url, close_request, headers=headers, auth=(SONATYPE_USER, SONATYPE_PASSWORD)) |
| 110 | |
| 111 | |
Ray Milkey | f77ea41 | 2018-08-10 17:37:28 -0700 | [diff] [blame] | 112 | def wait_for_staging_repo(description, repo_id): |
Ray Milkey | cb8ea06 | 2018-08-14 19:31:47 -0700 | [diff] [blame] | 113 | if destination_repo_url is None: |
Ray Milkey | f77ea41 | 2018-08-10 17:37:28 -0700 | [diff] [blame] | 114 | return |
Ray Milkey | cb8ea06 | 2018-08-14 19:31:47 -0700 | [diff] [blame] | 115 | base_url = "https://" + destination_repo_url + "/service/local/staging/profiles" + "/" + SONATYPE_PROFILE |
Ray Milkey | f77ea41 | 2018-08-10 17:37:28 -0700 | [diff] [blame] | 116 | close_request = CLOSE_REPO_REQUEST_TEMPLATE.replace("%(description)", description).replace("%(repo_id)", repo_id) |
| 117 | url = base_url + "/finish" |
| 118 | headers = {'Content-Type': 'application/xml'} |
| 119 | repo_query_url = "https://oss.sonatype.org/service/local/staging/repository/" + repo_id |
| 120 | |
| 121 | attempt = 1 |
| 122 | print ("waiting for repo to close...") |
| 123 | while True: |
| 124 | r = requests.get(repo_query_url, close_request, headers=headers, auth=(SONATYPE_USER, SONATYPE_PASSWORD)) |
| 125 | root = xml.etree.ElementTree.fromstring(r.text) |
| 126 | transitioning = root.find("transitioning").text |
| 127 | if transitioning != "true": |
| 128 | break |
| 129 | if attempt == CLOSE_RETRY_ATTEMPTS: |
| 130 | print ("Unable to close repo") |
| 131 | sys.exit(1) |
| 132 | attempt = attempt + 1 |
| 133 | time.sleep(5) |
| 134 | print ("Repo closed successfully") |
| 135 | |
| 136 | |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 137 | def stage_file(file, repo_id, dest): |
Ray Milkey | 7f46b1f | 2018-07-24 19:01:58 -0700 | [diff] [blame] | 138 | filename_in_repo = os.path.dirname(dest) + "/" + os.path.basename(file) |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 139 | if destination_repo_url is not None: |
| 140 | # deploy to Nexus repo |
| 141 | upload_base = "https://" + destination_repo_url + "/service/local/staging/deployByRepositoryId" |
Ray Milkey | 7f46b1f | 2018-07-24 19:01:58 -0700 | [diff] [blame] | 142 | url = upload_base + "/" + repo_id + "/" + filename_in_repo |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 143 | headers = {'Content-Type': 'application/xml'} |
| 144 | with open(file, 'rb') as f: |
Ray Milkey | 7f46b1f | 2018-07-24 19:01:58 -0700 | [diff] [blame] | 145 | r = requests.post(url, data=f.read(), headers=headers, auth=(SONATYPE_USER, SONATYPE_PASSWORD)) |
| 146 | if r.status_code != 201: |
| 147 | print (r.status_code) |
| 148 | print (r.text) |
| 149 | sys.exit(1) |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 150 | else: |
| 151 | # deploy to local repo |
Ray Milkey | 7f46b1f | 2018-07-24 19:01:58 -0700 | [diff] [blame] | 152 | file_in_local_repo = os.path.expanduser(local_maven_repo + "/" + filename_in_repo) |
| 153 | dir_in_local_repo = os.path.dirname(file_in_local_repo) |
| 154 | if not os.path.isdir(dir_in_local_repo): |
| 155 | os.makedirs(dir_in_local_repo) |
| 156 | shutil.copyfile(src, file_in_local_repo) |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 157 | |
| 158 | |
| 159 | def stage_files(files, dest): |
| 160 | for file in files: |
| 161 | stage_file(file=file, repo_id=repo_id, dest=dest) |
| 162 | |
| 163 | |
| 164 | def upload_file(src, dest): |
Ray Milkey | 7f46b1f | 2018-07-24 19:01:58 -0700 | [diff] [blame] | 165 | print ("publishing: " + dest.replace("org/onosproject", "")) |
| 166 | files = generate_metadata_files(src, dest) |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 167 | stage_files(files, dest) |
| 168 | |
| 169 | |
| 170 | if __name__ == '__main__': |
| 171 | import sys |
| 172 | |
| 173 | if len(sys.argv) < 2: |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 174 | print 'USAGE: upload-maven-artifacts catalog-file-name [nexus root url]' |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 175 | sys.exit(1) |
| 176 | |
| 177 | input_list_file = sys.argv[1] |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 178 | |
| 179 | local_maven_repo = None |
| 180 | destination_repo_url = None |
| 181 | |
| 182 | if len(sys.argv) == 3: |
| 183 | destination_repo_url = sys.argv[2] |
| 184 | else: |
| 185 | local_maven_repo = os.environ.get("MAVEN_REPO") |
| 186 | if local_maven_repo is None: |
| 187 | local_maven_repo = "~/.m2/repository" |
| 188 | |
| 189 | if destination_repo_url is not None: |
| 190 | if SONATYPE_USER is None: |
| 191 | print "Environment variable SONATYPE_USER must be set" |
| 192 | sys.exit(1) |
| 193 | |
| 194 | if SONATYPE_PASSWORD is None: |
| 195 | print "Environment variable SONATYPE_PASSWORD must be set" |
| 196 | sys.exit(1) |
| 197 | |
| 198 | if SONATYPE_PROFILE is None: |
| 199 | print "Environment variable SONATYPE_PROFILE must be set" |
| 200 | sys.exit(1) |
| 201 | |
| 202 | print ("Uploading to remote repo: " + destination_repo_url) |
| 203 | else: |
| 204 | print ("Installing in local repo: " + local_maven_repo) |
| 205 | |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 206 | list_file = open(input_list_file, "r") |
| 207 | lines = list_file.readlines() |
| 208 | list_file.close() |
| 209 | |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 210 | tempdir = tempfile.mkdtemp(prefix="upload-maven-artifacts-") |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 211 | description = "test repo" |
| 212 | repo_id = create_staging_repo(description) |
| 213 | for line in lines: |
| 214 | s = line.split() |
| 215 | src = s[0] |
| 216 | dest = s[1] |
| 217 | upload_file(src, dest) |
| 218 | close_staging_repo(repo_id=repo_id, description=description) |
Ray Milkey | f77ea41 | 2018-08-10 17:37:28 -0700 | [diff] [blame] | 219 | wait_for_staging_repo(repo_id=repo_id, description=description) |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 220 | shutil.rmtree(tempdir) |