This module use parsers
, ymlparsers
, init_app_conf
as it’s low-level API. init_app_conf
usage is limited.
The source code you can found here. It is available as part of my AlexBerUtils s project.
You can install AlexBerUtils from PyPi:
python3 -m pip install -U alex-ber-utils
You should also install some optional dependencies, such as fabric. The easiest way is to run:
python3 -m pip install alex-ber-utils[fabric]
python3 -m pip install alex-ber-utils[yml]
**Note **also that hiyapyco should be at least 0.4.16.
See here for more details explanation on how to install.
This is simple alternative for deploying your code to production.
This code example basically do the following:
config.xml
file with profile of the remote machine that is supplied from system args. See details below.Fabric
to remote machine.export PYTHONSCRIPTHOST=your-host
export PYTHONSCRIPTKEY=C:/dev/keys/keys.pem
export PYTHONSCRIPTUSER=user
export PYTHONSCRIPTREMOTEPATH = '/root/'
import tempfile
import zipfile
from pathlib import Path
import logging
import shutil
import os as _os
from alexber.utils.mains import fixabscwd
from dotenv import load_dotenv
from alexber.utils.fabs import Connection as FabConnection
import alexber.utils.ymlparsers as ymlparsers
import alexber.utils.init_app_conf as init_app_conf
from alexber.utils.deploys import load_config, add_to_zip_copy_function, split_path
name = 'PythonScript'
NAME = name.upper()
CONNECTION_KEY = ''.join([NAME, 'KEY'])
CONNECTION_USER = ''.join([NAME, 'USER'])
CONNECTION_HOSTS = ''.join([NAME, 'HOST'])
REMOTE_PATH_KEY = ''.join([NAME, 'REMOTEPATH'])
def run(**kwargs):
host = _os.environ[CONNECTION_HOSTS]
user = _os.environ[CONNECTION_USER]
key_filename = _os.environ[CONNECTION_KEY]
remote_path = _os.environ[REMOTE_PATH_KEY]
default_conf_full_path = kwargs['full_path']
dd = kwargs['content']
root_folder, relative_part_path = split_path(default_conf_full_path, name)
with tempfile.TemporaryDirectory() as temp_dir:
local_folder = Path(temp_dir)
base_local_folder = local_folder / name
full_default_conf_local_path = base_local_folder.joinpath(*relative_part_path.parts)
zip_path = base_local_folder.with_suffix('.zip')
with zipfile.ZipFile(zip_path, mode="w") as zf:
#not real copy, add to zip archive
shutil.copytree(str(root_folder), str(base_local_folder),
ignore=shutil.ignore_patterns('__pycache__', 'venv', 'logs', '*.pyc', 'tmp*',
'.*', full_default_conf_local_path.name),
copy_function=add_to_zip_copy_function(split_dirname=name, zf=zf),
symlinks=True, ignore_dangling_symlinks=True)
# overwrite config.yml
full_default_conf_local_path.parent.mkdir(parents=True, exist_ok=True)
ymlparsers.safe_dump(dd, full_default_conf_local_path.open('w'))
_, full_default_conf_name_path = split_path(str(full_default_conf_local_path), split_dirname=name)
dest_path = Path(name) / full_default_conf_name_path
zf.write(str(full_default_conf_local_path), str(dest_path))
with FabConnection(host=host, user=user, connect_kwargs={"key_filename": key_filename}) as fab_con:
fab_con.cp(remote_path, zip_path)
with fab_con.cd(remote_path):
fab_con.run(f'sudo rm -fr {name}')
fab_con.run(f'sudo unzip -o ./{zip_path.name}')
fab_con.run(f'sudo chown -R root:root {name}')
fab_con.run(f'sudo rm -fr {zip_path.name}')
fab_con.run(f'mkdir -p /tmp/logs/{name}')
fab_con.run(f'cd {name} && sudo ln -s /tmp/logs/{name} logs')
def main(args=None):
logging.basicConfig(format='%(asctime)-15s %(levelname)s [%(name)s.%(funcName)s] %(message)s',
level=logging.INFO)
logging.captureWarnings(True)
fixabscwd()
_env_path = Path('.env')
load_dotenv(dotenv_path=_env_path)
# ymlparsers.initConfig()
# init_app_conf.initConfig()
full_path, dd = load_config(args)
run(**{"full_path":full_path, "content":dd})
if __name__ == "__main__":
main()
#python3 #libraries #parse #deployment #python