diff mbox series

[v7,52/54] docker: docker.py adding age check command

Message ID 20180615194705.28019-53-alex.bennee@linaro.org
State New
Headers show
Series fix building of tests/tcg - last chance to review! | expand

Commit Message

Alex Bennée June 15, 2018, 7:47 p.m. UTC
This is useful for querying if an image is too old.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>

---
 tests/docker/docker.py | 49 ++++++++++++++++++++++++++++++++----------
 1 file changed, 38 insertions(+), 11 deletions(-)

-- 
2.17.1

Comments

Philippe Mathieu-Daudé June 18, 2018, 3:07 p.m. UTC | #1
On 06/15/2018 04:47 PM, Alex Bennée wrote:
> This is useful for querying if an image is too old.

> 

> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>


Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>


> ---

>  tests/docker/docker.py | 49 ++++++++++++++++++++++++++++++++----------

>  1 file changed, 38 insertions(+), 11 deletions(-)

> 

> diff --git a/tests/docker/docker.py b/tests/docker/docker.py

> index 3666197a16..ad0c47921a 100755

> --- a/tests/docker/docker.py

> +++ b/tests/docker/docker.py

> @@ -29,6 +29,7 @@ from tarfile import TarFile, TarInfo

>  from StringIO import StringIO

>  from shutil import copy, rmtree

>  from pwd import getpwuid

> +from datetime import datetime,timedelta

>  

>  

>  FILTERED_ENV_NAMES = ['ftp_proxy', 'http_proxy', 'https_proxy']

> @@ -184,6 +185,10 @@ class Docker(object):

>          labels = json.loads(resp)[0]["Config"].get("Labels", {})

>          return labels.get("com.qemu.dockerfile-checksum", "")

>  

> +    def get_image_creation_time(self, tag):

> +        resp = self._output(["inspect", tag])

> +        return json.loads(resp)[0]["Created"]

> +

>      def build_image(self, tag, docker_dir, dockerfile,

>                      quiet=True, user=False, argv=None, extra_files_cksum=[]):

>          if argv == None:

> @@ -450,21 +455,43 @@ class CheckCommand(SubCommand):

>      def args(self, parser):

>          parser.add_argument("tag",

>                              help="Image Tag")

> -        parser.add_argument("dockerfile",

> -                            help="Dockerfile name")

> +        parser.add_argument("dockerfile", default=None,

> +                            help="Dockerfile name", nargs='?')

> +        parser.add_argument("--checktype", choices=["checksum", "age"],

> +                            default="checksum", help="check type")

> +        parser.add_argument("--olderthan", default=60, type=int,

> +                            help="number of minutes")

>  

>      def run(self, args, argv):

> -        dockerfile = open(args.dockerfile, "rb").read()

>          tag = args.tag

> -

>          dkr = Docker()

> -        if dkr.image_matches_dockerfile(tag, dockerfile):

> -            if not args.quiet:

> -                print("Image is up to date.")

> -            return 0

> -        else:

> -            print("Image needs updating")

> -            return 1

> +

> +        if args.checktype == "checksum":

> +            if not args.dockerfile:

> +                print("Need a dockerfile for tag:%s" % (tag))

> +                return 1

> +

> +            dockerfile = open(args.dockerfile, "rb").read()

> +

> +            if dkr.image_matches_dockerfile(tag, dockerfile):

> +                if not args.quiet:

> +                    print("Image is up to date")

> +                return 0

> +            else:

> +                print("Image needs updating")

> +                return 1

> +        elif args.checktype == "age":

> +            timestr = dkr.get_image_creation_time(tag).split(".")[0]

> +            created = datetime.strptime(timestr, "%Y-%m-%dT%H:%M:%S")

> +            past = datetime.now() - timedelta(minutes=args.olderthan)

> +            if created < past:

> +                print ("Image created @ %s more than %d minutes old" %

> +                       (timestr, args.olderthan))

> +                return 1

> +            else:

> +                if not args.quiet:

> +                    print ("Image less than %d minutes old" % (args.olderthan))

> +                return 0

>  

>  

>  def main():

>
diff mbox series

Patch

diff --git a/tests/docker/docker.py b/tests/docker/docker.py
index 3666197a16..ad0c47921a 100755
--- a/tests/docker/docker.py
+++ b/tests/docker/docker.py
@@ -29,6 +29,7 @@  from tarfile import TarFile, TarInfo
 from StringIO import StringIO
 from shutil import copy, rmtree
 from pwd import getpwuid
+from datetime import datetime,timedelta
 
 
 FILTERED_ENV_NAMES = ['ftp_proxy', 'http_proxy', 'https_proxy']
@@ -184,6 +185,10 @@  class Docker(object):
         labels = json.loads(resp)[0]["Config"].get("Labels", {})
         return labels.get("com.qemu.dockerfile-checksum", "")
 
+    def get_image_creation_time(self, tag):
+        resp = self._output(["inspect", tag])
+        return json.loads(resp)[0]["Created"]
+
     def build_image(self, tag, docker_dir, dockerfile,
                     quiet=True, user=False, argv=None, extra_files_cksum=[]):
         if argv == None:
@@ -450,21 +455,43 @@  class CheckCommand(SubCommand):
     def args(self, parser):
         parser.add_argument("tag",
                             help="Image Tag")
-        parser.add_argument("dockerfile",
-                            help="Dockerfile name")
+        parser.add_argument("dockerfile", default=None,
+                            help="Dockerfile name", nargs='?')
+        parser.add_argument("--checktype", choices=["checksum", "age"],
+                            default="checksum", help="check type")
+        parser.add_argument("--olderthan", default=60, type=int,
+                            help="number of minutes")
 
     def run(self, args, argv):
-        dockerfile = open(args.dockerfile, "rb").read()
         tag = args.tag
-
         dkr = Docker()
-        if dkr.image_matches_dockerfile(tag, dockerfile):
-            if not args.quiet:
-                print("Image is up to date.")
-            return 0
-        else:
-            print("Image needs updating")
-            return 1
+
+        if args.checktype == "checksum":
+            if not args.dockerfile:
+                print("Need a dockerfile for tag:%s" % (tag))
+                return 1
+
+            dockerfile = open(args.dockerfile, "rb").read()
+
+            if dkr.image_matches_dockerfile(tag, dockerfile):
+                if not args.quiet:
+                    print("Image is up to date")
+                return 0
+            else:
+                print("Image needs updating")
+                return 1
+        elif args.checktype == "age":
+            timestr = dkr.get_image_creation_time(tag).split(".")[0]
+            created = datetime.strptime(timestr, "%Y-%m-%dT%H:%M:%S")
+            past = datetime.now() - timedelta(minutes=args.olderthan)
+            if created < past:
+                print ("Image created @ %s more than %d minutes old" %
+                       (timestr, args.olderthan))
+                return 1
+            else:
+                if not args.quiet:
+                    print ("Image less than %d minutes old" % (args.olderthan))
+                return 0
 
 
 def main():