1-
21import yaml
32import os
4- from git import Repo
5- from git import RemoteProgress
6- from tqdm import tqdm
3+ import subprocess
4+ import shutil
75
8- dockerComposeDir = f 'docker-compose'
6+ dockerComposeDir = 'docker-compose'
97nginxConfDir = f'{ dockerComposeDir } /nginx'
108phpConfDir = f'{ dockerComposeDir } /php-fpm'
119mysqlDir = f'{ dockerComposeDir } /mysql'
1210
13- appName = input ("Informe o nome da aplicação (app): " ).replace (" " ,"-" ).lower () or 'app'
11+ appName = input ("Informe o nome da aplicação (app): " ).replace (" " ,"-" ).lower () or 'app'
1412phpversion = input ("Informe a imagem docker do php-fpm que deseja usar: " )
15- gitRepoUrl = input ("Informe a url do repositorio git : " ) or ''
13+ gitRepoUrl = input ("Informe a url do repositorio git (opcional) : " ) or ''
1614
17- dockerComposeFile = f' { appName } / docker-compose.yml'
18- envFile = f' { appName } / .env'
15+ dockerComposeFile = ' docker-compose.yml'
16+ envFile = ' .env'
1917
2018data = {
2119 'version' : '3.7' ,
2220 'services' : {
23- 'app' :
24- {
25- 'container_name' : f'{ appName } -app-dev' ,
26- 'environment' : [
27- 'COMPOSER_MEMORY_LIMIT=-1'
28- ],
29- 'image' : f'{ phpversion } ' ,
30- 'networks' : [
31- f'{ appName } Network'
32- ],
33- 'restart' : 'unless-stopped' ,
34- 'volumes' : [
35- './:/var/www/app' ,
36- './docker-compose/php-fpm/custom.ini:/usr/local/etc/php/conf.d/custom.ini' ,
37- ],
38- 'working_dir' : '/var/www/app'
39- },
40- 'db' :
41- {
42- 'command' : '--default-authentication-plugin=mysql_native_password' ,
43- 'container_name' : f'{ appName } -dev-db' ,
44- 'environment' : {
45- 'MYSQL_DATABASE' : f'{ appName } ' ,
46- 'MYSQL_PASSWORD' : f'{ appName } ' ,
47- 'MYSQL_ROOT_PASSWORD' : f'{ appName } ' ,
48- 'MYSQL_USER' : f'{ appName } ' ,
49- 'SERVICE_NAME' : 'mysql' ,
50- 'SERVICE_TAGS' : 'dev'
51- },
52- 'image' : 'mysql:5.7' ,
53- 'networks' : [f'{ appName } Network' ],
54- 'ports' : ['33306:3306' ],
55- 'restart' : 'unless-stopped' ,
56- 'tty' : True ,
57- 'volumes' : [
58- './docker-compose/mysql:/docker-entrypoint-initdb.d' ,
59- f'{ appName } MysqlData:/var/lib/mysql'
60- ]
61- },
62- 'nginx' :
63- {
64- 'container_name' : f'{ appName } -dev-nginx' ,
65- 'image' : 'nginx:alpine' ,
66- 'networks' : [f'{ appName } Network' ],
67- 'ports' : ['8000:80' ],
68- 'restart' : 'unless-stopped' ,
69- 'volumes' : [
70- './:/var/www/app' ,
71- './docker-compose/nginx:/etc/nginx/conf.d/'
72- ],
73- 'working_dir' : '/var/www/app'
74- }
21+ 'app' : {
22+ 'container_name' : f'{ appName } -app-dev' ,
23+ 'environment' : [
24+ 'COMPOSER_MEMORY_LIMIT=-1'
25+ ],
26+ 'image' : f'{ phpversion } ' ,
27+ 'networks' : [
28+ f'{ appName } Network'
29+ ],
30+ 'restart' : 'unless-stopped' ,
31+ 'volumes' : [
32+ './:/var/www/app' ,
33+ './docker-compose/php-fpm/custom.ini:/usr/local/etc/php/conf.d/custom.ini' ,
34+ ],
35+ 'working_dir' : '/var/www/app'
36+ },
37+ 'db' : {
38+ 'command' : '--default-authentication-plugin=mysql_native_password' ,
39+ 'container_name' : f'{ appName } -dev-db' ,
40+ 'environment' : {
41+ 'MYSQL_DATABASE' : f'{ appName } ' ,
42+ 'MYSQL_PASSWORD' : f'{ appName } ' ,
43+ 'MYSQL_ROOT_PASSWORD' : f'{ appName } ' ,
44+ 'MYSQL_USER' : f'{ appName } ' ,
45+ 'SERVICE_NAME' : 'mysql' ,
46+ 'SERVICE_TAGS' : 'dev'
7547 },
76- 'networks' :{
77- f'{ appName } Network' :{
78- 'driver' :'bridge'
48+ 'image' : 'mysql:5.7' ,
49+ 'networks' : [f'{ appName } Network' ],
50+ 'ports' : ['33306:3306' ],
51+ 'restart' : 'unless-stopped' ,
52+ 'tty' : True ,
53+ 'volumes' : [
54+ './docker-compose/mysql:/docker-entrypoint-initdb.d' ,
55+ f'{ appName } MysqlData:/var/lib/mysql'
56+ ]
57+ },
58+ 'nginx' : {
59+ 'container_name' : f'{ appName } -dev-nginx' ,
60+ 'image' : 'nginx:alpine' ,
61+ 'networks' : [f'{ appName } Network' ],
62+ 'ports' : ['8000:80' ],
63+ 'restart' : 'unless-stopped' ,
64+ 'volumes' : [
65+ './:/var/www/app' ,
66+ './docker-compose/nginx:/etc/nginx/conf.d/'
67+ ],
68+ 'working_dir' : '/var/www/app'
69+ }
70+ },
71+ 'networks' : {
72+ f'{ appName } Network' : {
73+ 'driver' : 'bridge'
7974 },
80-
8175 },
8276 'volumes' : {
8377 f'{ appName } MysqlData' : {
8478 'driver' : 'local' ,
8579 'name' : f'{ appName } MysqlData'
8680 }
87- }}
88-
89- ## Source: https://localcoder.org/python-progress-bar-for-git-clone
90-
91- class CloneProgress (RemoteProgress ):
92- def __init__ (self ):
93- super ().__init__ ()
94- self .pbar = tqdm ()
95-
96- def update (self , op_code , cur_count , max_count = None , message = '' ):
97- self .pbar .total = max_count
98- self .pbar .n = cur_count
99- self .pbar .refresh ()
100-
81+ }
82+ }
10183
102- def generateFileWithPath (path ,content ,lines = False ,isYaml = False ):
103- os .makedirs (os .path .dirname (path ),exist_ok = True )
84+ def generateFileWithPath (path , content , lines = False , isYaml = False ):
85+ dir_path = os .path .dirname (path )
86+ if dir_path :
87+ os .makedirs (dir_path , exist_ok = True )
10488 with open (path , 'w' ) as f :
105- if not ( isYaml ) :
89+ if not isYaml :
10690 if lines :
10791 f .writelines (content )
10892 else :
10993 f .write (content )
11094 else :
11195 yaml .dump (content , f , default_flow_style = False , sort_keys = False )
11296
97+ def clone_repository (url , path ):
98+ print (f'Clonando repositório { url } para { path } \n ' )
99+ try :
100+ process = subprocess .Popen (['git' , 'clone' , url , path ],
101+ stdout = subprocess .PIPE ,
102+ stderr = subprocess .PIPE ,
103+ universal_newlines = True )
104+
105+ for line in process .stdout :
106+ print (line , end = '' )
107+
108+ for line in process .stderr :
109+ print (line , end = '' )
110+
111+ process .wait ()
112+
113+ if process .returncode != 0 :
114+ print (f'\n Erro ao clonar repositório. Código de saída: { process .returncode } ' )
115+ return False
116+ else :
117+ print (f'\n Repositório clonado com sucesso.' )
118+ return True
119+ except Exception as e :
120+ print (f'\n Erro ao clonar repositório: { e } ' )
121+ return False
113122
114- print ( 'Gerando arquivo docker.compose.yml \n ' )
115-
116- generateFileWithPath ( dockerComposeFile , data , False , True )
123+ # Criar a pasta do app
124+ os . makedirs ( appName , exist_ok = True )
125+ os . chdir ( appName )
117126
118- # print('Gerando arquivo .env do ambiente docker\n')
119- # generateFileWithPath(envFile,[
120- # "DB_HOST=db\n",
121- # "DB_PORT=3306\n",
122- # "DB_EXTERNAL_PORT=33306\n",
123- # f"DB_DATABASE={appName}\n",
124- # f"DB_USERNAME={appName}\n",
125- # f'DB_PASSWORD={appName}\n',
126- # ],True)
127+ print ('Gerando arquivo docker-compose.yml\n ' )
128+ generateFileWithPath (dockerComposeFile , data , False , True )
127129
128130print ('Criando app.conf do nginx\n ' )
129-
130- generateFileWithPath (f'{ appName } /{ nginxConfDir } /app.conf' ,"""server{
131+ generateFileWithPath (f'{ nginxConfDir } /app.conf' , """server {
131132 listen 80;
132133 client_max_body_size 0;
133134 index index.php index.html;
@@ -151,22 +152,33 @@ def generateFileWithPath(path,content,lines=False,isYaml=False):
151152 }
152153}""" )
153154
154-
155155print ('Criando config do php\n ' )
156-
157- generateFileWithPath (f'{ appName } /{ phpConfDir } /custom.ini' ,"""memory_limit = 4096M
156+ generateFileWithPath (f'{ phpConfDir } /custom.ini' , """memory_limit = 4096M
158157upload_max_filesize = 500m
159158max_execution_time = 5600
160159post_max_size = 500M
161160""" )
162161
163- generateFileWithPath (f'{ appName } / { mysqlDir } /.gitignore' ,"""*.sql""" )
162+ generateFileWithPath (f'{ mysqlDir } /.gitignore' , """*.sql""" )
164163
164+ # Clonar o repositório Git, se fornecido
165165if gitRepoUrl :
166- print (f'Clonando repositório { gitRepoUrl } \n ' )
167- Repo .clone_from (gitRepoUrl , f"{ appName } " ,progress = CloneProgress ())
168-
169- print (f'\n Seu ambiente PHP foi criado com sucesso. Acesse a pasta { appName } \n ' )
170-
171-
172- ##os.system(f"git clone {gitRepoUrl} {appName}")
166+ print ("Clonando repositório Git..." )
167+ temp_dir = 'temp_git_clone'
168+ if clone_repository (gitRepoUrl , temp_dir ):
169+ # Mover conteúdo do repositório para a raiz do projeto
170+ for item in os .listdir (temp_dir ):
171+ s = os .path .join (temp_dir , item )
172+ d = os .path .join ('.' , item )
173+ if os .path .isdir (s ):
174+ shutil .move (s , d )
175+ else :
176+ shutil .copy2 (s , d )
177+ shutil .rmtree (temp_dir )
178+ print ("Conteúdo do repositório Git movido para a pasta do projeto." )
179+ else :
180+ print ("Falha ao clonar o repositório. A estrutura Docker foi criada sem o conteúdo do Git." )
181+ else :
182+ print ("Nenhum repositório Git fornecido. Estrutura Docker criada sem conteúdo do Git." )
183+
184+ print (f'\n Seu ambiente PHP foi criado com sucesso na pasta { appName } . A estrutura Docker LAMP está pronta para uso.\n ' )
0 commit comments