22import sys
33import subprocess as sp
44
5+
56class Command (BaseCommand ):
67 help = 'Builds the Docker image of the project.'
78
89 @staticmethod
9- def print_fail (message , end = '\n ' ):
10+ def print_fail (message , end = '\n ' ):
1011 sys .stderr .write ('\x1b [1;31m' + message .strip () + '\x1b [0m' + end )
1112
1213 def add_arguments (self , parser ):
@@ -44,14 +45,14 @@ def add_arguments(self, parser):
4445
4546 def container_tag_exists (self , tag ):
4647 tags = str (sp .check_output (["docker ps -a --format '{{.Names}}'" ], shell = True )
47- .decode ('utf-8' )).split ("\n " )
48+ .decode ('utf-8' )).split ("\n " )
4849 return tag in tags
49-
50+
5051 def container_port_occupied (self ):
5152 ports = str (sp .check_output (["docker ps -a --format '{{.Ports}}'" ], shell = True )
52- .decode ('utf-8' )).split ("\n " )
53+ .decode ('utf-8' )).split ("\n " )
5354 return "0.0.0.0:8000->8000/tcp" in ports
54-
55+
5556 def export_container_env (self , tag , env_file ):
5657 # TODO: find a better way to do this
5758 command = f"docker exec -it { tag } "
@@ -60,9 +61,9 @@ def export_container_env(self, tag, env_file):
6061 env_var = env_file .readline ()
6162 while env_var :
6263 sp .Popen ([command + f"export { env_var } " ], shell = True ).wait ()
63- except :
64- self .print_fail ("Unable to open environment file" )
65-
64+ except Exception as e :
65+ self .print_fail ("Unable to open environment file: \n " + e )
66+
6667 def _run_image (self , tag ):
6768 """
6869 Runs the built docker image
@@ -75,8 +76,8 @@ def _run_image(self, tag):
7576 if self .container_port_occupied ():
7677 self .print_fail ("Port 8000>tcp already in use. Unable to run image." )
7778 else :
78- sp .Popen (["docker run --name " + tag + "_exec -tid -p 8000:8000 " + tag ], shell = True ). wait ()
79-
79+ sp .Popen (["docker run --name " + tag + "_exec -tid -p 8000:8000 " + tag ],
80+ shell = True ). wait ()
8081
8182 def _build_image (self , tag , file_path ):
8283 """
@@ -85,14 +86,13 @@ def _build_image(self, tag, file_path):
8586 # TODO: Add monitoring and exception handling
8687 sp .Popen (["docker build" + " -t " + tag + " . -f " + file_path ], shell = True ).wait ()
8788
88-
8989 def _push_image (self , tag ):
9090 """
9191 Push image to docker repository
9292 """
9393 # TODO: Add support for third party docker repositories and login exceptions
9494 info = str (sp .check_output (["docker info" ], shell = True )
95- .decode ('utf-8' )).split ("\n " )
95+ .decode ('utf-8' )).split ("\n " )
9696 info = list (filter (lambda a : "Username" in a , info ))
9797 try :
9898 username = info [0 ].split (":" )[1 ][1 :]
@@ -104,10 +104,9 @@ def _push_image(self, tag):
104104 else :
105105 self .print_fail ("Not logged into docker.io. Please login and try again." )
106106 return
107- except :
107+ except IndexError as e :
108108 self .print_fail ("Unable to detect username. Pushing regardless..." )
109109 sp .Popen ([f'docker push docker.io/{ tag } ' ], shell = True ).wait ()
110-
111110
112111 def handle (self , * args , ** kwargs ):
113112 prod = kwargs .get ('prod' , False )
@@ -129,4 +128,3 @@ def handle(self, *args, **kwargs):
129128 print (f'\n { str (step )} : Pushing { tag } to docker repository...' )
130129 self ._push_image (tag )
131130 step += 1
132-
0 commit comments