-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathcontainer_update.py
More file actions
82 lines (67 loc) · 2.84 KB
/
Copy pathcontainer_update.py
File metadata and controls
82 lines (67 loc) · 2.84 KB
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of INGInious. See the LICENSE and the COPYRIGHTS files for
# more information about the licensing of this file.
import os
import re
import docker
from dotenv import load_dotenv
INFO = '\033[94m'
WARNING = '\033[33m'
FAIL = '\033[91m'
ENDC = '\033[0m'
UNDERLINE = '\033[4m'
def ask_boolean(question, default):
while True:
val = ask_with_default(question, ("yes" if default else "no")).lower()
if val in ["yes", "y", "1", "true", "t"]:
return True
elif val in ["no", "n", "0", "false", "f"]:
return False
print(INFO + "Please answer 'yes' or 'no'." + ENDC)
def ask_with_default(question, default=""):
default = str(default)
answer = input(INFO + UNDERLINE + question + " [" + default + "]:" + ENDC + " ")
if answer == "":
answer = default
return answer
def main():
""" Updates all "stock" images (images beginning with 'inginious/env-') """
load_dotenv()
registry = os.environ.get('REGISTRY', "ghcr.io")
version = os.environ.get('VERSION', "latest")
# Get the local images
stock_images = []
try:
docker_connection = docker.from_env()
for image in docker_connection.images.list():
for tag in image.attrs["RepoTags"]:
if re.match(fr"^{registry}/inginious/env-(base|default):{version}", tag):
stock_images.append(tag)
except:
print(FAIL + "Cannot connect to Docker!" + ENDC)
print(FAIL + "Restart this command after making sure the command `docker info` works" + ENDC)
return
# Minimum mandatory images are available locally, redownload ?
if len(stock_images) != 0:
print(" You already have minimum mandatory images for version: " + version)
if ask_boolean("Do you want to re-download them ?", True):
for image in stock_images:
print(INFO + ("Updating %s" % image) + ENDC)
docker_connection.images.pull(image)
print(INFO + "Images update done" + ENDC)
print(INFO + "Images for version " + version + " were successfully re-downloaded !" + ENDC)
return
print("Nothing happened")
# No local image, download ?
elif len(stock_images) == 0:
print(" You don't have local images compatible for the version " + version)
if ask_boolean("Do you want to download images for version " + version + " ?", "yes"):
docker_connection.images.pull(f"{registry}/inginious/env-base:{version}")
docker_connection.images.pull(f"{registry}/inginious/env-default:{version}")
print(INFO + "Images for version " + version + " were successfully downloaded !" + ENDC)
return
print(INFO + "Nothing happened" + ENDC)
if __name__ == "__main__":
main()