|
2 | 2 | import sys
|
3 | 3 | import json
|
4 | 4 | import jinja2
|
| 5 | +import shutil |
5 | 6 | import logging
|
| 7 | +import requests |
6 | 8 |
|
7 | 9 | from collections import defaultdict
|
8 | 10 | from os.path import dirname, join, abspath, split, splitext, exists, basename
|
@@ -1525,6 +1527,87 @@ def export_directives(self):
|
1525 | 1527 | # Flush params json to stdout
|
1526 | 1528 | sys.stdout.write(json.dumps(directives_json))
|
1527 | 1529 |
|
| 1530 | + def fetch_docker_tags(self): |
| 1531 | + """ |
| 1532 | + Export all dockerhub tags associated with each component given by |
| 1533 | + the -t flag. |
| 1534 | + """ |
| 1535 | + |
| 1536 | + # fetches terminal width and subtracts 1 because we always add a |
| 1537 | + # new line character |
| 1538 | + terminal_width = shutil.get_terminal_size().columns - 1 |
| 1539 | + |
| 1540 | + # first header |
| 1541 | + center_string = " Selected container tags " |
| 1542 | + |
| 1543 | + # starts a list with the headers |
| 1544 | + tags_list = [ |
| 1545 | + [ |
| 1546 | + "=" * int(terminal_width / 4), |
| 1547 | + "{0}{1}{0}".format( |
| 1548 | + "=" * int(((terminal_width/2 - len(center_string)) / 2)), |
| 1549 | + center_string) |
| 1550 | + , |
| 1551 | + "{}\n".format("=" * int(terminal_width / 4)) |
| 1552 | + ], |
| 1553 | + ["component", "container", "tags"], |
| 1554 | + [ |
| 1555 | + "=" * int(terminal_width / 4), |
| 1556 | + "=" * int(terminal_width / 2), |
| 1557 | + "=" * int(terminal_width / 4) |
| 1558 | + ] |
| 1559 | + ] |
| 1560 | + |
| 1561 | + # Skip first init process and iterate through the others |
| 1562 | + for p in self.processes[1:]: |
| 1563 | + template = p.template |
| 1564 | + # fetch repo name from directives of the template. Since some |
| 1565 | + # components like integrity_coverage doesn't have a directives with |
| 1566 | + # container, thus if no directive there the script will skip this |
| 1567 | + # template |
| 1568 | + try: |
| 1569 | + repo = p.directives[template]["container"] |
| 1570 | + except KeyError: |
| 1571 | + continue |
| 1572 | + # make the request to docker hub |
| 1573 | + r = requests.get( |
| 1574 | + "https://hub.docker.com/v2/repositories/{}/tags/".format( |
| 1575 | + repo |
| 1576 | + )) |
| 1577 | + # checks the status code of the request, if it is 200 then parses |
| 1578 | + # docker hub entry, otherwise retrieve no tags but alerts the user |
| 1579 | + if r.status_code != 404: |
| 1580 | + # parse response content to dict and fetch results key |
| 1581 | + r_content = json.loads(r.content)["results"] |
| 1582 | + for version in r_content: |
| 1583 | + tags_list.append([template, repo, version["name"]]) |
| 1584 | + else: |
| 1585 | + tags_list.append([template, repo, "No DockerHub tags"]) |
| 1586 | + |
| 1587 | + # iterate through each entry in tags_list and print the list of tags |
| 1588 | + # for each component. Each entry (excluding the headers) contains |
| 1589 | + # 3 elements (component name, container and tag version) |
| 1590 | + for x, entry in enumerate(tags_list): |
| 1591 | + # adds different color to the header in the first list and |
| 1592 | + # if row is pair add one color and if is even add another (different |
| 1593 | + # background) |
| 1594 | + color = "blue_bold" if x < 3 else \ |
| 1595 | + ("white" if x % 2 != 0 else "0;37;40m") |
| 1596 | + # generates a small list with the terminal width for each column, |
| 1597 | + # this will be given to string formatting as the 3, 4 and 5 element |
| 1598 | + final_width = [ |
| 1599 | + int(terminal_width/4), |
| 1600 | + int(terminal_width/2), |
| 1601 | + int(terminal_width/4) |
| 1602 | + ] |
| 1603 | + # writes the string to the stdout |
| 1604 | + sys.stdout.write( |
| 1605 | + colored_print("\n{0: <{3}} {1: ^{4}} {2: >{5}}".format( |
| 1606 | + *entry, *final_width), color) |
| 1607 | + ) |
| 1608 | + # assures that the entire line gets the same color |
| 1609 | + sys.stdout.write("\n") |
| 1610 | + |
1528 | 1611 | def build(self):
|
1529 | 1612 | """Main pipeline builder
|
1530 | 1613 |
|
|
0 commit comments