Last active
May 20, 2021 17:15
-
-
Save hn-support/d7a6fdd89bd78ebd7a03982605743616 to your computer and use it in GitHub Desktop.
Copy and adjust your base_urls for Magento 2 staging environment on hypernode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Set the base-urls for your Magento 2 staging environment by copying and adjusting the base-urls from your production site. | |
To use, download the file and make it executable. Then run: | |
./change_magento2_staging_baseurls.py | |
After use, check your base-urls by issuing: | |
n98-magerun2 sys:store:config:base-url:list | |
This script requires n98-magerun2. | |
""" | |
from __future__ import print_function | |
import json | |
import subprocess | |
import os | |
import sys | |
def stagify(url, url_type='secure'): | |
while url.endswith('/'): | |
url = url.rstrip('/') | |
if url.startswith('https://'): | |
return '{}:8443/'.format(url) | |
return '{}:8888/'.format(url) | |
def set_base_url(data, store_type): | |
scope_id = data['Scope-ID'] | |
scope = data['Scope'] | |
path = data['Path'] | |
value = stagify(data['Value'], url_type=store_type) | |
command = '/usr/local/bin/n98-magerun2 --root-dir=/data/web/magento2_staging config:set --scope="{}" --scope-id="{}" "{}" "{}"'.format(scope, scope_id, path, value) | |
subprocess.check_output(command, shell=True) | |
def main(): | |
if not os.path.isfile('/data/web/magento2/app/etc/env.php') or not os.path.isfile('/data/web/magento2_staging/app/etc/env.php'): | |
print("No magento 2 installation found in /data/web/magento2 or in /data/web/magento2_staging", file=sys.stderr) | |
sys.exit(1) | |
for store_type, store_front in ('unsecure', 'web/unsecure/base_url'), ('secure', 'web/secure/base_url'): | |
command = '/usr/local/bin/n98-magerun2 --root-dir=/data/web/magento2 config:get "{}" --format=json'.format(store_front) | |
magerun_json = subprocess.check_output(command, shell=True) | |
try: | |
magerun_dict = json.loads(magerun_json) | |
except ValueError: | |
break | |
for value in magerun_dict.values(): | |
set_base_url(data=value, store_type=store_type) | |
subprocess.check_output('/usr/local/bin/n98-magerun2 --root-dir=/data/web/magento2_staging cache:flush', shell=True) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment