|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +''' |
| 4 | +utils.py: general http functions (utils) for som api |
| 5 | +
|
| 6 | +''' |
| 7 | + |
| 8 | +from singularity.logman import bot |
| 9 | +from singularity.hub.auth import get_headers |
| 10 | + |
| 11 | +import requests |
| 12 | +import os |
| 13 | +import sys |
| 14 | + |
| 15 | + |
| 16 | +def api_get(url,headers=None,token=None,data=None, return_json=True): |
| 17 | + '''api_get will use requests to get a particular url |
| 18 | + :param url: the url to send file to |
| 19 | + :param headers: a dictionary with headers for the request |
| 20 | + :param putdata: additional data to add to the request |
| 21 | + :param return_json: return json if successful |
| 22 | + ''' |
| 23 | + bot.logger.debug("GET %s",url) |
| 24 | + |
| 25 | + if headers == None: |
| 26 | + headers = get_headers(token=token) |
| 27 | + if data == None: |
| 28 | + response = requests.get(url, |
| 29 | + headers=headers) |
| 30 | + else: |
| 31 | + response = requests.get(url, |
| 32 | + headers=headers, |
| 33 | + json=data) |
| 34 | + |
| 35 | + if response.status_code == 200 and return_json: |
| 36 | + return response.json() |
| 37 | + |
| 38 | + return response |
| 39 | + |
| 40 | + |
| 41 | +def api_put(url,headers=None,token=None,data=None, return_json=True): |
| 42 | + '''api_put will send a read file (spec) to Singularity Hub with a particular set of headers |
| 43 | + :param url: the url to send file to |
| 44 | + :param headers: the headers to get |
| 45 | + :param headers: a dictionary with headers for the request |
| 46 | + :param data: additional data to add to the request |
| 47 | + :param return_json: return json if successful |
| 48 | + ''' |
| 49 | + bot.logger.debug("PUT %s",url) |
| 50 | + |
| 51 | + if headers == None: |
| 52 | + headers = get_headers(token=token) |
| 53 | + if data == None: |
| 54 | + response = requests.put(url, |
| 55 | + headers=headers) |
| 56 | + else: |
| 57 | + response = requests.put(url, |
| 58 | + headers=headers, |
| 59 | + json=data) |
| 60 | + |
| 61 | + if response.status_code == 200 and return_json: |
| 62 | + return response.json() |
| 63 | + |
| 64 | + return response |
| 65 | + |
| 66 | + |
| 67 | +def api_post(url,headers=None,data=None,token=None,return_json=True): |
| 68 | + '''api_get will use requests to get a particular url |
| 69 | + :param url: the url to send file to |
| 70 | + :param headers: a dictionary with headers for the request |
| 71 | + :param data: additional data to add to the request |
| 72 | + :param return_json: return json if successful |
| 73 | + ''' |
| 74 | + bot.logger.debug("POST %s",url) |
| 75 | + |
| 76 | + if headers == None: |
| 77 | + headers = get_headers(token=token) |
| 78 | + if data == None: |
| 79 | + response = requests.post(url, |
| 80 | + headers=headers) |
| 81 | + else: |
| 82 | + response = requests.post(url, |
| 83 | + headers=headers, |
| 84 | + json=data) |
| 85 | + |
| 86 | + if response.status_code == 200 and return_json: |
| 87 | + return response.json() |
| 88 | + |
| 89 | + return response |
| 90 | + |
| 91 | + |
| 92 | +###################################################################### |
| 93 | +# OS/IO and Formatting Functions |
| 94 | +###################################################################### |
| 95 | + |
| 96 | + |
| 97 | +def is_number(container_name): |
| 98 | + '''is_number determines if the user is providing a singularity hub |
| 99 | + number (meaning the id of an image to download) vs a full name) |
| 100 | + ''' |
| 101 | + if isinstance(container_name,dict): |
| 102 | + return False |
| 103 | + try: |
| 104 | + float(container_name) |
| 105 | + return True |
| 106 | + except ValueError: |
| 107 | + return False |
| 108 | + |
| 109 | + |
| 110 | +def parse_container_name(image): |
| 111 | + '''parse_container_name will return a json structure with a repo name, tag, user. |
| 112 | + ''' |
| 113 | + container_name = image |
| 114 | + if not is_number(image): |
| 115 | + image = image.replace(' ','') |
| 116 | + |
| 117 | + # If the user provided a number (unique id for an image), return it |
| 118 | + if is_number(image) == True: |
| 119 | + logger.info("Numeric image ID %s found.", image) |
| 120 | + return int(image) |
| 121 | + |
| 122 | + image = image.split('/') |
| 123 | + |
| 124 | + # If there are two parts, we have username with repo (and maybe tag) |
| 125 | + if len(image) >= 2: |
| 126 | + user = image[0] |
| 127 | + image = image[1] |
| 128 | + |
| 129 | + # Otherwise, we trigger error (not supported just usernames yet) |
| 130 | + else: |
| 131 | + bot.logger.error('You must specify a repo name and username, %s is not valid',container_name) |
| 132 | + sys.exit(1) |
| 133 | + |
| 134 | + # Now split the name by : in case there is a tag |
| 135 | + image = image.split(':') |
| 136 | + if len(image) == 2: |
| 137 | + repo_name = image[0] |
| 138 | + repo_tag = image[1] |
| 139 | + |
| 140 | + # Otherwise, assume latest of an image |
| 141 | + else: |
| 142 | + repo_name = image[0] |
| 143 | + repo_tag = "latest" |
| 144 | + |
| 145 | + bot.logger.info("User: %s", user) |
| 146 | + bot.logger.info("Repo Name: %s", repo_name) |
| 147 | + bot.logger.info("Repo Tag: %s", repo_tag) |
| 148 | + |
| 149 | + parsed = {'repo_name':repo_name, |
| 150 | + 'repo_tag':repo_tag, |
| 151 | + 'user':user } |
| 152 | + |
| 153 | + return parsed |
0 commit comments