2024-12-24 13:52:15 -05:00
from os import makedirs
from pathlib import Path
print ( " Docker configuration \n " + ( " - " * 20 ) )
print ( " You may need to run this with sudo " )
2024-12-31 20:07:58 -05:00
possible_services = [ " message " , " blog " ]
2024-12-24 13:52:15 -05:00
required_services = [ " common " , " auth " ]
databases = {
" auth " : " auth.sqlite3 " ,
2024-12-31 20:07:58 -05:00
" message " : " message.sqlite3 " ,
" blog " : " blog.sqlite3 "
2024-12-24 13:52:15 -05:00
}
defaults = {
" common " : { " port " : " 8888 " , " public_url " : " http://localhost:8888 " } ,
" auth " : { " port " : " 8000 " , " public_url " : " http://localhost:8000 " } ,
2024-12-31 20:07:58 -05:00
" message " : { " port " : " 8001 " , " public_url " : " http://localhost:8001 " } ,
" blog " : { " port " : " 8002 " , " public_url " : " http://localhost:8002 " }
2024-12-24 13:52:15 -05:00
}
depends = {
" auth " : " common " ,
" message " : " auth "
2024-12-31 20:07:58 -05:00
" blog " : " auth "
2024-12-24 13:52:15 -05:00
}
enabled_applications = { }
storage_location = Path ( input ( " Enter the location you want to store databases (/var/tsuite/): " ) or " /var/tsuite/ " )
2024-12-24 13:57:55 -05:00
try :
makedirs ( storage_location )
except FileExistsError :
. . .
2024-12-24 13:52:15 -05:00
docker_compose = """ version: " 3.8 "
services :
"""
def get_service_config ( name : str , config : dict ) - > tuple [ str , str ] :
return f """ { name } :
build : . / { name } { f '''
depends_on :
- { depends [ name ] } ''' if name in depends else " " }
ports :
- " {config["port"]} : {config["port"]} "
restart : always { f '''
volumes :
- { storage_location } : { storage_location } ''' if name in databases else " " } \n " " " , f " " " FROM python:3.10
WORKDIR / usr / src / app
COPY . .
RUN pip install - - no - cache - dir - - upgrade - r requirements . txt { '''
CMD [ " python " , " manage.py " , " migrate " ] ''' if name in databases else " " }
EXPOSE { config [ " port " ] }
CMD [ " python " , " manage.py " , " runserver " , " 0.0.0.0: {config["port"]} " ] \n """
common_port = 0
for service in required_services :
print ( f " t { service . capitalize ( ) } - " )
port = input ( f " What port do you want to use ( { defaults [ service ] [ ' port ' ] } )? Must be unique. " )
port = int ( port ) if port . isdigit ( ) else defaults [ service ] [ " port " ]
public_url = input ( f " Enter the public url ( { defaults [ service ] [ ' public_url ' ] } ): " ) or defaults [ service ] [ " public_url " ]
strings = get_service_config ( service , {
" port " : port ,
" public_url " : public_url
} )
docker_compose + = strings [ 0 ]
if service == " common " :
common_port = port
else :
enabled_applications [ service ] = (
f " Secret t { service . capitalize ( ) } -specific token " ,
f " http:// { service } : { port } " ,
public_url
)
with open ( f " { service } /Dockerfile " , " w " ) as f :
f . write ( strings [ 1 ] )
with open ( f " { service } /config.py " , " a " ) as f :
f . write ( f " \n DB_PATH = ' { storage_location } / { databases [ service ] } ' " if service in databases else " " )
f . write ( f " \n tCOMMON_URL_INTERNAL = ' http://common: { common_port } ' " if service != " common " else " " )
print ( " \n " + ( " - " * 20 ) + " \n " )
for service in possible_services :
print ( f " t { service . capitalize ( ) } - " )
if not ( input ( " Enable? (Y/n): " ) . lower ( ) or " y " ) . startswith ( " y " ) :
continue
port = input ( f " What port do you want to use ( { defaults [ service ] [ ' port ' ] } )? Must be unique. " )
port = int ( port ) if port . isdigit ( ) else defaults [ service ] [ " port " ]
public_url = input ( f " Enter the public url ( { defaults [ service ] [ ' public_url ' ] } ): " ) or defaults [ service ] [ " public_url " ]
strings = get_service_config ( service , {
" port " : port ,
" public_url " : public_url
} )
docker_compose + = strings [ 0 ]
enabled_applications [ service ] = (
f " Secret t { service . capitalize ( ) } -specific token " ,
f " http:// { service } : { port } " ,
public_url
)
with open ( f " { service } /Dockerfile " , " w " ) as f :
f . write ( strings [ 1 ] )
with open ( f " { service } /config.py " , " a " ) as f :
f . write ( f " \n DB_PATH = ' { storage_location } / { databases [ service ] } ' " if service in databases else " " )
f . write ( f " \n tCOMMON_URL_INTERNAL = ' http://common: { common_port } ' " if service != " common " else " " )
print ( " \n " + ( " - " * 20 ) + " \n " )
with open ( " docker-compose.yml " , " w " ) as f :
f . write ( docker_compose )
with open ( " common/config.py " , " a " ) as f :
f . write ( f " ENABLED_APPLICATIONS = { repr ( enabled_applications ) } " )
print ( " Docker configuration files generated. You may have to manually edit the config.py files in each service. Be sure to change the tokens to a non-default value. \n When running the web server, you may also need to migrate the databases manually (sudo docker exec -id <container name> python3 manage.py migrate) " )