-
Notifications
You must be signed in to change notification settings - Fork 0
/
installmagento
executable file
·266 lines (225 loc) · 10.9 KB
/
installmagento
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/python3
import os, json, argparse
MAGENTO_CONFIG_SET = 'bin/magento config:set --lock-env'
MAGENTO_MODULE_DISABLE = 'bin/magento module:disable'
MAGENTO_CACHE_FLUSH = 'bin/magento cache:flush'
MAGENTO_SETUP_UPGRADE = 'bin/magento setup:upgrade'
MAGENTO_SETUP_DI_COMPILE = 'bin/magento setup:di:compile'
MAGENTO_STATIC_CONTENT_DEPLOY = 'bin/magento -f setup:static-content:deploy'
MAGENTO_INDEXER_REINDEX = 'bin/magento indexer:reindex'
MAGENTO_CRON_INSTALL = 'bin/magento cron:install'
MAGENTO_SAMPLEDATA_DEPLOY = 'bin/magento sampledata:deploy'
MAGENTO_FULL_COMMANDS = f'{MAGENTO_SETUP_UPGRADE}; \
{MAGENTO_SETUP_DI_COMPILE}; \
{MAGENTO_CACHE_FLUSH}; \
{MAGENTO_STATIC_CONTENT_DEPLOY}'
CONTAINER_FPM = 'FPM'
CONTAINER_NGINX = 'NGINX'
NGROK_DOWNLOAD_FILE = "ngrok-v3-stable-linux-amd64.tgz"
NGROK_DOWNLOAD_URL = f"https://bin.equinox.io/c/bNyj1mQVY4c/{NGROK_DOWNLOAD_FILE}"
def exec(commands, type=CONTAINER_FPM):
container = get_container(type=type)
for command in commands:
print(f'---------------\n> {command}\n---------------')
os.system(f"docker exec {container} /bin/bash -c '{command}'")
def get_container(type=CONTAINER_FPM):
for line in os.popen("docker ps --format '{{.Names}}' ").read().splitlines():
if type == CONTAINER_FPM and "php-fpm" in line and domain in line:
container = line
if type == CONTAINER_NGINX and "nginx" in line and domain in line:
container = line
return container
def config_set(configs):
commands = []
for config in configs:
commands += [f"{MAGENTO_CONFIG_SET} {config}"]
commands += [MAGENTO_CACHE_FLUSH]
exec(commands)
def warden_setup():
os.system(f"warden env-init {domain} magento2")
modify_env()
os.system(f"warden sign-certificate {domain}.test")
os.system("warden env up")
def load_versions():
#Loads magentoversions.json
with open("magentoversions.json", "r") as f:
versions = json.load(f)["version"]
return versions
def download_magento():
magento_repo_creds = config['composer']["magento_repo"]
commands = [
f"composer config http-basic.repo.magento.com {magento_repo_creds['user']} {magento_repo_creds['pass']} --global",
f"composer create-project --no-install --repository-url=https://repo.magento.com/ magento/project-{magento_edition}-edition={magento_version} magento2/",
"mv magento2/* .",
"composer config --no-plugins allow-plugins.laminas/laminas-dependency-plugin true",
"composer config --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer true",
"composer config --no-plugins allow-plugins.magento/composer-root-update-plugin true",
"composer config --no-plugins allow-plugins.magento/magento-composer-installer true",
"composer config --no-plugins allow-plugins.magento/composer-dependency-version-audit-plugin true",
"composer config --no-plugins allow-plugins.magento/inventory-composer-installer true",
"composer install -n",
"chmod +x bin/magento"
]
exec(commands)
def magento_installer(sampledata):
os.system("pwd")
os.system(f"mkdir {domain}")
os.chdir(domain)
warden_setup()
download_magento()
backend = config.get("admin")
installer_command = f"bin/magento setup:install \
--db-host=db --db-name=magento --db-user=magento --db-password=magento \
--admin-firstname={backend['firstname']} --admin-lastname={backend['lastname']} --admin-email={backend['email']} \
--admin-user={backend['user']} --admin-password={backend['pass']} --language=en_US --currency=GBP \
--timezone=America/Chicago --use-rewrites=1 --backend-frontname={backend['frontname']} --session-save=db \
--key=00000000000000000000000000000000"
if "elasticsearch" in versions[magento_version]:
installer_command = installer_command + " --search-engine=elasticsearch7 --elasticsearch-host=elasticsearch --elasticsearch-port=9200"
commands = [installer_command]
magento_url="https://${TRAEFIK_SUBDOMAIN}.${TRAEFIK_DOMAIN}/"
commands += [
f"{MAGENTO_CONFIG_SET} web/unsecure/base_url {magento_url}",
f"{MAGENTO_CONFIG_SET} web/secure/base_url {magento_url}",
f"{MAGENTO_CONFIG_SET} web/secure/use_in_frontend 1",
f"{MAGENTO_CONFIG_SET} web/secure/use_in_adminhtml 1",
f"{MAGENTO_CONFIG_SET} web/seo/use_rewrites 1",
MAGENTO_INDEXER_REINDEX,
MAGENTO_FULL_COMMANDS,
MAGENTO_CRON_INSTALL
]
if "twofactor" in versions[magento_version]:
commands += [
f"{MAGENTO_MODULE_DISABLE} Magento_AdminAdobeImsTwoFactorAuth",
f"{MAGENTO_MODULE_DISABLE} Magento_TwoFactorAuth",
]
if sampledata:
commands += [MAGENTO_SAMPLEDATA_DEPLOY]
commands += [MAGENTO_FULL_COMMANDS]
exec(commands)
if magento_version[:3 != "2.1"]:
configs = [
"admin/security/admin_account_sharing 1",
"admin/security/use_form_key 0",
"admin/security/session_lifetime 31536000",
"admin/security/password_is_forced 0"
]
config_set(configs)
def set_baseurl(url):
commands = []
if magento_version[:3 == "2.1"]:
query = f"mysql -h db -umagento -pmagento -b magento -e \
\"INSERT INTO core_config_data(path, value) \
VALUES (\\\"web/unsecure/base_url\\\", \\\"{url}\\\"),(\\\"web/secure/base_url\\\", \\\"{url}\\\") \
ON DUPLICATE KEY UPDATE value = \\\"{url}\\\";\""
commands += [query]
else:
commands += [
f"{MAGENTO_CONFIG_SET} web/unsecure/base_url {url}",
f"{MAGENTO_CONFIG_SET} web/secure/base_url {url}"
]
commands += [MAGENTO_CACHE_FLUSH]
exec(commands)
print(url)
def get_baseurl():
os.system(f"docker exec {domain}_php-fpm_1 /bin/bash -c 'bin/magento config:show web/unsecure/base_url'")
def ngrok_remove():
command = [f"cd; killall ngrok; rm tunnels {NGROK_DOWNLOAD_FILE}"]
exec(command, type=CONTAINER_NGINX)
set_baseurl(f"https://app.{domain}.test/")
def ngrok_installer(ngrok_token):
commands = [
f'cd; killall ngrok; rm tunnels {NGROK_DOWNLOAD_FILE}',
f'cd; wget {NGROK_DOWNLOAD_URL}',
f'cd; tar xvzf {NGROK_DOWNLOAD_FILE}',
f'cd; ./ngrok config add-authtoken {ngrok_token}',
'cd; ./ngrok http 80 > /dev/null &',
'cd; wget http://localhost:4040/api/tunnels'
]
exec(commands, type=CONTAINER_NGINX)
container = get_container(type=CONTAINER_NGINX)
try:
ngrok_json = json.load(os.popen(f"docker exec {container} /bin/bash -c 'cd; cat tunnels'"))
ngrok_url = f"{ngrok_json['tunnels'][0]['public_url']}/"
if ngrok_url:
set_baseurl(ngrok_url)
except:
print("ngrok setup failed")
def modify_env():
print(f"version = {versions[magento_version]}")
os.system(f"cat .env | grep -v -E 'COMPOSER_VERSION|PHP_VERSION|MARIADB_VERSION|MYSQL_DISTRIBUTION_VERSION|WARDEN_REDIS|WARDEN_VARNISH|PHP_XDEBUG_3|WARDEN_RABBITMQ' > env")
os.system('mv env .env')
#change to php version
os.system(f"echo 'PHP_VERSION={versions[magento_version]['php']}' >> .env")
#disable opensearch
os.system(f"{sed_i} 's/WARDEN_OPENSEARCH=1/WARDEN_OPENSEARCH=0/g' .env")
#change the composer version
os.system(f"echo 'COMPOSER_VERSION={versions[magento_version]['composer']}' >> .env")
#change the mariadb version
os.system(f"echo 'MARIADB_VERSION={versions[magento_version]['mariadb']}' >> .env")
os.system(f"echo 'MYSQL_DISTRIBUTION_VERSION={versions[magento_version]['mariadb']}' >> .env")
#disable rabit
os.system(f"echo 'WARDEN_RABBITMQ=0' >> .env")
#disable redis
os.system(f"echo 'WARDEN_REDIS=0' >> .env")
#disable PHP_XDEBUG_3
if (versions[magento_version]['php'][1] != 8):
os.system(f"echo 'PHP_XDEBUG_3=0' >> .env")
else:
os.system(f"echo 'PHP_XDEBUG_3=1' >> .env")
#disable varnish
os.system(f"{sed_i} 's/WARDEN_VARNISH=1/WARDEN_VARNISH=0/g' .env")
if "elasticsearch" in versions[magento_version]:
os.system(f"cat .env | grep -v -E 'ELASTICSEARCH_VERSION|WARDEN_ELASTICSEARCH' > env")
os.system('mv env .env')
os.system(f"echo 'WARDEN_ELASTICSEARCH=1' >> .env")
os.system(f"echo 'ELASTICSEARCH_VERSION={versions[magento_version]['elasticsearch']}' >> .env")
else:
os.system(f"{sed_i} 's/WARDEN_ELASTICSEARCH=1/WARDEN_ELASTICSEARCH=0/g' .env")
def load_config_file():
with open("config.json", "r") as f:
global config
config = json.load(f)
def main():
parser = argparse.ArgumentParser(description = "Warden Magento Installer")
parser.add_argument("-H", "--Help", help = "Example: Help argument", required = False, default = "")
parser.add_argument("-d", "--domain", help = "Example: -d 244test", required = False, default = "")
parser.add_argument("-v", "--magentoversion", help = "Example: -v 2.4.4", required = False, default = "")
parser.add_argument("-n", "--ngrok", help = "Example: -n auth_token", required = False, default = "")
parser.add_argument("--m1", help = "Magento 1", default=False, action='store_true')
parser.add_argument("-r","--removengrok", default=False, action='store_true')
parser.add_argument("-s","--sampledata", default=False, action='store_true')
parser.add_argument("-e","--enterprise", default=False, action='store_true')
global magento_edition
global magento_version
global domain
argument = parser.parse_args()
ngrok = argument.ngrok
sampledata = argument.sampledata
removengrok = argument.removengrok
m1 = argument.m1
magento_version = argument.magentoversion
magento_edition = "enterprise" if argument.enterprise else "community"
domain = argument.domain
load_config_file()
if (not(domain and ngrok) and not(domain and magento_version) and not(domain and removengrok)):
return
if (domain and magento_version):
if magento_version not in versions:
print('Version not supported')
return
magento_installer(sampledata)
if (domain and ngrok):
if not f"\n{domain}" in os.popen("docker ps --format '{{.Names}}' ").read():
print ('Domain not found in running containers')
return
ngrok_installer(ngrok)
if (domain and removengrok):
if not f"\n{domain}" in os.popen("docker ps --format '{{.Names}}' ").read():
print ('Domain not found in running containers')
return
ngrok_remove()
versions = load_versions()
sed_i = "sed -i" if os.popen('uname -s').read() == "Linux\n" else "sed -i ''"
if __name__ == "__main__":
main()