from .services.base_service import Base_service
import sys
import os
from py_module.whiptail import Whiptail
import py_module.yaml as yaml
import py_module.docker as docker
import py_module.jinja2 as jinja
import shutil
import subprocess
import string
import uuid

from .docker_installer import Docker_installer
from .services import *


class DS_Update(Base_service):
    def preload(self):
#        with Docker_installer(logger_level = self.logger_level) as run:
#            pass
        self.menu = (
            ('ds-rabbitmq','Р7-ДС: Служба базы данных','ON'),
            ('ds-postgres','Р7-ДС: MQTT-служба очередей','ON'),
            ('ds-redis','Р7-ДС: Сервер кэширования','ON'),
            ('ds-docserver','Р7-ДС: Служба документ-сервера','ON'),
            ('ds-frontend','Р7-ДС: Веб-интерфейс','ON'),
        );
        self.logger.info('Инициализирую основные переменные')

        if not self.DETALIZATION:
            self.DETALIZATION = 1
        self.INSTALL_DUMP_PATH='./images'
        self.INSTALL_CONF_PATH='./configs'
        self.INSTALL_NEW_PATH=['/opt/r7-office-docker']
        self.INSTALL_NEW_PATH_DS_DB=['/opt/r7-office-docker/data/db/ds_database']
        self.BACK_MENU=[False]

### ENVIRONMENT GERENAL
        self.DETALIZATION = 1
        self.CS_VERSION=['2025.3.12.15877']
#        self.DS_VERSION=['latest']
        self.DS_VERSION=['2025.3.1.923']
        self.MAIL_VERSION=['1.0.35']
        self.PREFIX_CS=['cs']
        self.PREFIX_DS=['ds']
        self.PREFIX_MAIL=['mail']
        self.PREFIX_TEAM=['team']

        self.GENERAL_NODE_LOGS_STORAGE=['./support_logs']

        self.CS_NODE_FRONTEND_CERT_CRT_PATH=['./configs/ssl']
        self.CS_NODE_FRONTEND_CERT_CRT_FILE=['fullchain.crt']
        self.CS_NODE_FRONTEND_CERT_KEY_PATH=['./configs/ssl']
        self.CS_NODE_FRONTEND_CERT_KEY_FILE=['privkey.key']

        self.CS_NODE_PSQL_STORAGE=['./data/db/cs_database']
        self.CS_NODE_FILE_STORAGE=['./data/cs_storage/root']
        self.CS_NODE_INDEX_STORAGE=['./data/search_index']

        self.DS_NODE_PSQL_STORAGE=['./data/db/ds_database','./data/db/ds_database']
        self.DS_NODE_FILE_STORAGE=['./data/ds_storage','./data/ds_storage']
        self.DS_NODE_LICENSE_FILE=['./license/license.lic']

        self.MAIL_NODE_PSQL_STORAGE=['./data/db/mail_database']
        self.MAIL_NODE_MAILDIR_STORAGE=['./data/mail_storage/maildir']
        self.MAIL_NODE_DKIM_STORAGE=['./data/mail_storage/dkim_keys']

#env for published port of services on node
        self.CS_NODE_API_PORT_PUBLISHED=['38033']
        self.CS_NODE_RABBIT_PORT_PUBLISHED=['5672']
        self.CS_NODE_PSQL_PORT_PUBLISHED=['5432']
        self.CS_NODE_DRAW_PORT_PUBLISHED=['3005']
        self.CS_NODE_FILESTORAGE_PORT_HTTP_PUBLISHED=['11580']
        self.CS_NODE_FILESTORAGE_PORT_TCP_PUBLISHED=['11581']
        self.CS_NODE_SEARCH_PORT_PUBLISHED=['2664']
        self.CS_NODE_SSO_PORT_PUBLISHED=['38034']
        self.CS_NODE_REGISTRY_PORT_PUBLISHED=['7777']

        self.DS_NODE_DOCSERVER_PORT_PUBLISHED=['8000']
        self.DS_NODE_RABBIT_PORT_PUBLISHED=['5673']
        self.DS_NODE_PSQL_PORT_PUBLISHED=['5433']
        self.DS_NODE_REDIS_PORT_PUBLISHED=['6379']

        self.MAIL_NODE_API_PORT_PUBLISHED=['8891']
        self.MAIL_NODE_PSQL_PORT_PUBLISHED=['5434']
        self.MAIL_NODE_DKIM_PORT_PUBLISHED=['8891']
        self.MAIL_NODE_MTA_PORT_SMTP_PUBLISHED=['25']
        self.MAIL_NODE_MTA_PORT_SMTPS_PUBLISHED=['465']
        self.MAIL_NODE_MTA_PORT_SMTPS_TLS_PUBLISHED=['587']
        self.MAIL_NODE_MDA_PORT_POP3_PUBLISHED=['110']
        self.MAIL_NODE_MDA_PORT_POP3S_PUBLISHED=['995']
        self.MAIL_NODE_MDA_PORT_IMAP_PUBLISHED=['143']
        self.MAIL_NODE_MDA_PORT_IMAPS_PUBLISHED=['993']

### ENVIRONMENT CS
#env for frontend
        self.CS_FRONTEND_IMAGE_NAME=['downloads.r7-office.ru:9011/r7office/cs/cddisk-frontend']
        self.CS_FRONTEND_IMAGE_VERSION=[self.CS_VERSION[0]]
        self.CS_FRONTEND_CONTAINER_NAME=['cs-frontend']

        self.CS_FRONTEND_HOST=['cs-frontend']
        self.CS_FRONTEND_SCHEME=['https']
        self.CS_FRONTEND_SCHEME_REDIRECT=[True]
        self.CS_FRONTEND_DOMAIN=['domain.ru']
        self.CS_FRONTEND_HTTP_PORT=['80']
        self.CS_FRONTEND_HTTPS_PORT=['443']
        self.CS_FRONTEND_PREFIX_CDDISK=['disk']
        self.CS_FRONTEND_PREFIX_PAYMENTS=['payments']
        self.CS_FRONTEND_PREFIX_PAGES=['pages']
        self.CS_FRONTEND_PREFIX_FORMS=['forms']
        self.CS_FRONTEND_PREFIX_DRAW=['draw']
        self.CS_FRONTEND_PREFIX_ADMIN=['admin']
        self.CS_FRONTEND_PREFIX_CDMAIL=['mail']
        self.CS_FRONTEND_PREFIX_CONTACTS=['contacts']
        self.CS_FRONTEND_PREFIX_CALENDAR=['calendar']
        self.CS_FRONTEND_PREFIX_PROJECTS=['projects']
        self.CS_FRONTEND_STORAGE=['/app']
        self.CS_FRONTEND_CERT_PATH=['/etc/nginx/ssl']
        self.CS_FRONTEND_CERT_CRT_FILE=['fullchain.crt']
        self.CS_FRONTEND_CERT_KEY_FILE=['privkey.key']
#env for rabbit
        self.CS_RABBIT_IMAGE_NAME=['downloads.r7-office.ru:9011/r7office/cs/cddisk-rabbitmq']
        self.CS_RABBIT_IMAGE_VERSION=[self.CS_VERSION[0]]
        self.CS_RABBIT_CONTAINER_NAME=['cs-rabbitmq']

        self.CS_RABBIT_HOST=['cs-rabbit']
        self.CS_RABBIT_USER=['guest']
        self.CS_RABBIT_PWD=['guest']
        self.CS_RABBIT_PORT=['5672']
        self.CS_RABBIT_CLIENT_TIMEOUT=['10']
#env for psql
        self.CS_PSQL_IMAGE_NAME=['downloads.r7-office.ru:9011/r7office/cs/cddisk-postgres']
        self.CS_PSQL_IMAGE_VERSION=[self.CS_VERSION[0]]
        self.CS_PSQL_CONTAINER_NAME=['cs-psql']

        self.CS_TEAM_CLIENT_ID=['']
        self.CS_TEAM_CLIENT_SECRET=['']
        self.CS_PAGES_CLIENT_ID=['']
        self.CS_PAGES_CLIENT_SECRET=['']
        self.CS_FORMS_CLIENT_ID=['empty']
        self.CS_FORMS_CLIENT_SECRET=['empty']

        self.CS_PSQL_HOST=['cs-psql']
        self.CS_PSQL_DB=['postgres']
        self.CS_PSQL_USER=['postgres']
        self.CS_PSQL_PWD=['pg_pass']
        self.CS_PSQL_PORT=['5432']
        self.CS_PSQL_DATA=['/var/lib/postgresql/data']
        self.CS_PSQL_CORE_MACHINE_KEY=['empty']
#env for draw
        self.CS_DRAW_IMAGE_NAME=['downloads.r7-office.ru:9011/r7office/cs/cddisk-draw']
        self.CS_DRAW_IMAGE_VERSION=[self.CS_VERSION[0]]
        self.CS_DRAW_CONTAINER_NAME=['cs-draw']

        self.CS_DRAW_HOST=['cs-draw']
        self.CS_DRAW_PORT=['3005']
#env for api
        self.CS_API_IMAGE_NAME=['downloads.r7-office.ru:9011/r7office/cs/cddisk-api']
        self.CS_API_IMAGE_VERSION=[self.CS_VERSION[0]]
        self.CS_API_CONTAINER_NAME=['cs-api']

        self.CS_API_HOST=['cs-api']
        self.CS_API_PORT=['38033']
        self.CS_API_LOGLEVEL=['Trace']
        self.CS_API_INTERFACE=['+']
        self.CS_API_ALLOWHOSTS=['*']
        self.CS_API_HTTP_TIMEOUT=['00:10:00']
        self.CS_API_CERT_OAUTH_FILE=['domain.pfx']
        self.CS_API_CERT_PATH=['/app/ssl']
        self.CS_API_CERT_PATHKEY=['/app/ssl']
        self.CS_API_STORAGE_TMP=['/app/tmp_storage']
        self.CS_API_ROOTNODE=['False']
#env for api and processing
        self.CS_STORAGE_S3_REGION=['region']
        self.CS_STORAGE_S3_ENDPOINT=['http://localhost/s3']
        self.CS_STORAGE_S3_BUCKET_NAME=['ds-storage']
        self.CS_STORAGE_S3_ACCESS_KEY_ID=['AKID2']
        self.CS_STORAGE_S3_SECRET_ACCESS_KEY=['SECRET']
#env for processing
        self.CS_PROCESSING_IMAGE_NAME=['downloads.r7-office.ru:9011/r7office/cs/cddisk-processing']
        self.CS_PROCESSING_IMAGE_VERSION=[self.CS_VERSION[0]]
        self.CS_PROCESSING_CONTAINER_NAME=['cs-processing']

        self.CS_PROCESSING_HOST=['cs-processing']
        self.CS_PROCESSING_LOGLEVEL=['Trace']
        self.CS_PROCESSING_STORAGE_TMP=['/app/tmp_storage']
        self.CS_PROCESSING_ROOTNODE=['False']
        self.CS_PROCESSING_PUSH_CERT_PATH=['None']
        self.CS_PROCESSING_GCM_AUTHKEY=['None']
        self.CS_PROCESSING_GCM_URL=['None']
        self.CS_PROCESSING_VOIP_CERT=['None']
        self.CS_PROCESSING_VOIP_CERT_PWD=['None']
        self.CS_PROCESSING_SANDBOX_CERT=['None']
        self.CS_PROCESSING_SANDBOX_CERT_PWD=['None']
        self.CS_PROCESSING_PUSH_PRODUCTION_ONLY=['None']
#env for filestorage
        self.CS_FILESTORAGE_IMAGE_NAME=['downloads.r7-office.ru:9011/r7office/cs/cddisk-filestorage']
        self.CS_FILESTORAGE_IMAGE_VERSION=[self.CS_VERSION[0]]
        self.CS_FILESTORAGE_CONTAINER_NAME=['cs-filestorage']

        self.CS_FILESTORAGE_HOST=['cs-filestorage']
        self.CS_FILESTORAGE_PORT_HTTP=['11580']
        self.CS_FILESTORAGE_PORT_TCP=['11581']
        self.CS_FILESTORAGE_HTTP_INTERFACE=['0.0.0.0']
        self.CS_FILESTORAGE_ALLOWHOSTS=['*']
        self.CS_FILESTORAGE_TCP_INTERFACE=['0.0.0.0']
        self.CS_FILESTORAGE_ROOTNODE=['False']
        self.CS_FILESTORAGE_LOGLEVEL=['Trace']
        self.CS_FILESTORAGE_STORAGE=['/var/filestorage']
#env for search
        self.CS_SEARCH_IMAGE_NAME=['downloads.r7-office.ru:9011/r7office/cs/cddisk-searchapi']
        self.CS_SEARCH_IMAGE_VERSION=[self.CS_VERSION[0]]
        self.CS_SEARCH_CONTAINER_NAME=['cs-search']

        self.CS_SEARCH_HOST=['cs-search']
        self.CS_SEARCH_PORT=['2664']
        self.CS_SEARCH_INTERFACE=['+']
        self.CS_SEARCH_ALLOWHOSTS=['*']
        self.CS_SEARCH_INDEXFOLDER=['/app/data']
        self.CS_SEARCH_LOGLEVEL=['Trace']
        self.CS_SEARCH_CLEARMEMORYINTERVAL=['00:01:00']
        self.CS_SEARCH_CLOSEINDEXINTERVAL=['0.23:30:00']
        self.CS_SEARCH_INDEXLIVEINTERVAL=['60']
        self.CS_SEARCH_NEVERCLOSEINDEX=['True']
        self.CS_SEARCH_USELOCALTERMREPOSITORY=['True']
        self.CS_SEARCH_PERCENTMEMORYUSAGE=['80']
        self.CS_SEARCH_SHARDCOUNT=['16']
        self.CS_SEARCH_AVAILABLEPHYSICALMEMORY=['8000']
#env for registry
        self.CS_REGISTRY_IMAGE_NAME=['downloads.r7-office.ru:9011/r7office/cs/cddisk-registry']
        self.CS_REGISTRY_IMAGE_VERSION=[self.CS_VERSION[0]]
        self.CS_REGISTRY_CONTAINER_NAME=['cs-registry']

        self.CS_REGISTRY_HOST=['cs-registry']
        self.CS_REGISTRY_PORT=['7777']
        self.CS_REGISTRY_INTERFACE=['0.0.0.0']
        self.CS_REGISTRY_ALLOWHOSTS=['*']
        self.CS_REGISTRY_LOGLEVEL=['Trace']
#env for sso
        self.CS_SSO_IMAGE_NAME=['downloads.r7-office.ru:9011/r7office/cs/cddisk-ssoapi']
        self.CS_SSO_IMAGE_VERSION=[self.CS_VERSION[0]]
        self.CS_SSO_CONTAINER_NAME=['cs-ssoapi']

        self.CS_SSO_HOST=['cs-sso']
        self.CS_SSO_PORT=['38034']
        self.CS_SSO_INTERFACE=['+']
        self.CS_SSO_ALLOWHOSTS=['*']
        self.CS_SSO_LOGLEVEL=['Trace']
        self.CS_LDAP_ENDPOINT=['']
        self.CS_LDAP_USER=['']
        self.CS_LDAP_PWD=['']
        self.CS_LDAP_LOGINPOSTFIX=['']
        self.CS_LDAP_TYPE=['ActiveDirectory']
        self.CS_SSO_IDP_METADATA=['empty']
        self.CS_SSO_SAML_SIGNATUREALGORITM=['http://www.w3.org/2001/04/xmldsig-more#rsa-sha256']
        self.CS_SSO_ISSUER=['empty']
        self.CS_SSO_SIGN_CERT=['domain.pfx']
        self.CS_SSO_SIGN_CERT_PWD=['empty']
        self.CS_SSO_CERT_VALID_MODE=['None']
        self.CS_SSO_REVOCATION_MODE=['NoCheck']


### ENVIRONMENT DS
#env for frontend
        self.DS_FRONTEND_IMAGE_NAME=['downloads.r7-office.ru:9011/r7office/ds/ds-frontend']
#        self.DS_FRONTEND_IMAGE_NAME=['ds-frontend']
        self.DS_FRONTEND_IMAGE_VERSION=[self.DS_VERSION[0]]
        self.DS_FRONTEND_CONTAINER_NAME=['ds-frontend']

        self.DS_FRONTEND_HOST=['ds-frontend','ds-frontend']
        self.DS_FRONTEND_PREFIX_DOCSERVER=['doc']
        self.DS_FRONTEND_SCHEME=self.CS_FRONTEND_SCHEME
        self.DS_FRONTEND_SCHEME_REDIRECT=self.CS_FRONTEND_SCHEME_REDIRECT
        self.DS_FRONTEND_DOMAIN=self.CS_FRONTEND_DOMAIN
        self.DS_FRONTEND_HTTP_PORT=['80']
        self.DS_FRONTEND_HTTPS_PORT=['443']
#env for docserver
        self.DS_DOCSERVER_IMAGE_NAME=['downloads.r7-office.ru:9011/r7office/ds/ds-app']
#        self.DS_DOCSERVER_IMAGE_NAME=['ds-app']
        self.DS_DOCSERVER_IMAGE_VERSION=[self.DS_VERSION[0]]
        self.DS_DOCSERVER_CONTAINER_NAME=['ds-docserver']

        self.DS_DOCSERVER_HOST=['ds-docserver']
        self.DS_DOCSERVER_PORT=['8000']
        self.DS_DOCSERVER_JWT_KEY=['empty']
        self.DS_DOCSERVER_STORAGE_KEY=self.DS_DOCSERVER_JWT_KEY
#env for docserver over s3
        self.DS_STORAGE_S3_REGION=['region']
        self.DS_STORAGE_S3_ENDPOINT=['http://localhost/s3']
        self.DS_STORAGE_S3_BUCKET_NAME=['ds-storage']
        self.DS_STORAGE_S3_ACCESS_KEY_ID=['AKID2']
        self.DS_STORAGE_S3_SECRET_ACCESS_KEY=['SECRET']
#env for rabbit
        self.DS_RABBIT_IMAGE_NAME=['downloads.r7-office.ru:9011/r7office/ds/ds-rabbitmq']
#        self.DS_RABBIT_IMAGE_NAME=['ds-rabbitmq']
        self.DS_RABBIT_IMAGE_VERSION=[self.DS_VERSION[0]]
        self.DS_RABBIT_CONTAINER_NAME=['ds-rabbitmq']

        self.DS_RABBIT_HOST=['ds-rabbit','ds-rabbit']
        self.DS_RABBIT_USER=['guest','guest']
        self.DS_RABBIT_PWD=['guest','guest']
        self.DS_RABBIT_PORT=['5672','5672']
#env for psql
        self.DS_PSQL_IMAGE_NAME=['downloads.r7-office.ru:9011/r7office/ds/ds-postgres']
#        self.DS_PSQL_IMAGE_NAME=['ds-postgres']
        self.DS_PSQL_IMAGE_VERSION=[self.DS_VERSION[0]]
        self.DS_PSQL_CONTAINER_NAME=['ds-postgres']

        self.DS_PSQL_HOST=['ds-psql','ds-psql']
        self.DS_PSQL_DB=['postgres','postgres']
        self.DS_PSQL_USER=['postgres','postgres']
        self.DS_PSQL_PWD=['pg_pass','pg_pass']
        self.DS_PSQL_PORT=['5432','5432']
#env for redis
        self.DS_REDIS_IMAGE_NAME=['downloads.r7-office.ru:9011/r7office/ds/ds-redis']
        self.DS_REDIS_IMAGE_VERSION=[self.DS_VERSION[0]]
        self.DS_REDIS_CONTAINER_NAME=['ds-redis']

        self.DS_REDIS_HOST=['ds-redis']
        self.DS_REDIS_PORT=['6379']

### ENVIRONMENT TEAM
#env for rabbit
        self.TEAM_FQDN=['team.domain.ru']
        self.TEAM_RABBIT_HOST=['team-rabbit']
        self.TEAM_RABBIT_USER=['team']
        self.TEAM_RABBIT_PWD=['team']
        self.TEAM_RABBIT_PORT=['5672']
        self.TEAM_RABBIT_PUBLISH_PORT=['5672']
        self.TEAM_HTTPS_PUBLISH_PORT=['443']
        self.TEAM_HTTP_PUBLISH_PORT=['80']
        self.dockercli = docker.from_env();
        self.CS_TEAM_CLIENT_ID=['']
        self.CS_TEAM_CLIENT_SECRET=['']
        self.CS_PAGES_CLIENT_ID=['']
        self.CS_PAGES_CLIENT_SECRET=['']
        self.CS_FORMS_CLIENT_ID=['']
        self.CS_FORMS_CLIENT_SECRET=['']


    def postload(self, **kwarg):
        self.CS_PAGES_CLIENT_ID[0]=str(uuid.uuid4())
        self.CS_PAGES_CLIENT_SECRET[0]=str(uuid.uuid4())
        self.CS_FORMS_CLIENT_ID[0]=str(uuid.uuid4())
        self.CS_FORMS_CLIENT_SECRET[0]=str(uuid.uuid4())

        self.get_requests(**kwarg);
        if self.BACK_MENU[0]==True:
            return
        self.load_images(['ds-app.tar.gz','ds-frontend.tar.gz','ds-postgres.tar.gz','ds-rabbitmq.tar.gz','ds-redis.tar.gz']);
        self.build_compose();
        self.build_configs();
        self.deploy();
        return self;

    def load_images(self, images: list = []):
        outputs=[];
        self.logger.info(f'Начинаю импорт дампа образов: {images}')
        for image in images:
            self.logger.debug(f'Открываю файл дампа: {self.INSTALL_DUMP_PATH}/{image}')
            if os.path.isfile(f'{self.INSTALL_DUMP_PATH}/{image}'):
                with open(f'{self.INSTALL_DUMP_PATH}/{image}', 'rb') as file:
                    print(f'\nЗагружаю dump образов:\n{sys.path[0]}/images/{image}\n');
                    self.logger.info(f'Загружаю dump образов: {self.INSTALL_DUMP_PATH}/{image}')
                    try:
                        outputs = outputs + self.dockercli.images.load(file);
                    except:
                        self.logger.error(f'Произошла ошибка при загрузке docker-образов из dump-файла: {image}')
                        return self.msgbox(f'Произошла ошибка при загрузке docker-образов из dump-файла {image}.');
            else:
                self.logger.error(f'Файл dump образов отсутствует: {self.INSTALL_DUMP_PATH}/{image}')


    def build_compose(self):
        self.logger.info(f'Открываю compose-файл template.yaml на запись')
        try:        
            with open(f'{self.INSTALL_NEW_PATH[-1]}/template.yaml', 'r') as file:
                compose_data = yaml.safe_load(file)
        except FileNotFoundError:
            self.msgbox(f'в каталоге {self.INSTALL_NEW_PATH[-1]} отсутствует файл template.yaml')
            return
        except PermissionError:
            self.msgbox(f'в каталоге {self.INSTALL_NEW_PATH[-1]} отсутствует доступ к файлу template.yaml')
            return            
        if 'ds-rabbitmq' in compose_data['services']:
            compose_data['services']['ds-rabbitmq']['image'] = f'{self.DS_RABBIT_IMAGE_NAME[-1]}:{self.DS_RABBIT_IMAGE_VERSION[-1]}'
        else:
            print(f"Сервис ds-rabbitmq не найден в template.yaml")
        if 'ds-postgres' in compose_data['services']:
            compose_data['services']['ds-postgres']['image'] = f'{self.DS_PSQL_IMAGE_NAME[-1]}:{self.DS_PSQL_IMAGE_VERSION[-1]}'
        else:
            print(f"Сервис ds-postgres не найден в template.yaml")
        if 'ds-docserver' in compose_data['services']:
            compose_data['services']['ds-docserver']['image'] = f'{self.DS_DOCSERVER_IMAGE_NAME[-1]}:{self.DS_DOCSERVER_IMAGE_VERSION[-1]}'
            compose_data['services']['ds-docserver']['environment']['S3_STORAGE_FOLDER_NAME'] = 'files'
            compose_data['services']['ds-docserver']['environment']['S3_FORCE_PATH_STYLE'] = 'true'
            compose_data['services']['ds-docserver']['environment']['S3_CACHE_FOLDER_NAME'] = 'data'
            compose_data['services']['ds-docserver']['environment']['S3_URL_EXPIRES'] = '604800'
            compose_data['services']['ds-docserver']['environment']['S3_SSL_ENABLED'] = 'false'
            compose_data['services']['ds-docserver']['environment']['S3_NAME'] = 'storage-fs'
        else:
            print(f"Сервис ds-docserver не найден в template.yaml")
        if 'ds-redis' in compose_data['services']:
            compose_data['services']['ds-redis']['image'] = f'{self.DS_REDIS_IMAGE_NAME[-1]}:{self.DS_REDIS_IMAGE_VERSION[-1]}'
        else:
            print(f"Сервис ds-redis не найден в template.yaml")
        if 'ds-frontend' in compose_data['services']:
            compose_data['services']['ds-frontend']['image'] = f'{self.DS_FRONTEND_IMAGE_NAME[-1]}:{self.DS_FRONTEND_IMAGE_VERSION[-1]}'
        else:
            print(f"Сервис ds-frontend не найден в template.yaml")

        # Сохраняем изменения обратно в файл

        with open(f'{self.INSTALL_NEW_PATH[-1]}/template.yaml', 'w') as file:
            yaml.dump(compose_data, file, default_flow_style=False)

        print(f"Переменная окружения обновлена ")
        self.logger.info(f'Выгружаю compose-файл template.yaml')

    def build_configs(self):
        self.logger.info(f'Произвожу поиск файлов-шаблонов конфигураций')

    def deploy(self):
        print (f'\n\nКопирую файлы.\n\n############\n#### ВНИМАНИЕ: НЕ ОСТАНАВЛИВАЙТЕ ПРОЦЕСС\n############\n\n');
        print (f'\n\nКопирую /py_module\n\n');
        shutil.copytree('./py_module',f'{self.INSTALL_NEW_PATH[-1]}/py_module', dirs_exist_ok=True)
        print (f'\n\nКопирую /config-examples\n\n');
        shutil.copytree('./config-examples',f'{self.INSTALL_NEW_PATH[-1]}/config-examples', dirs_exist_ok=True)
        print (f'\n\nКопирую /images\n\n');
        shutil.copytree('./images',f'{self.INSTALL_NEW_PATH[-1]}/images', dirs_exist_ok=True)
#        shutil.copy2('./clear-data.sh',f'{self.INSTALL_NEW_PATH[-1]}')

        project_name = os.path.basename(os.path.normpath(self.INSTALL_NEW_PATH[-1]))

        command = [f'{self.INSTALL_NEW_PATH[-1]}/runtime/docker-plugins/docker-compose', '-f', f'{self.INSTALL_NEW_PATH[-1]}/template.yaml', 'down']
        return_code, stdout, stderr = self.subprocess(command, fd_stderr=sys.stdout)
        if return_code != 0:
            self.logger.error(f'При попытке запуска команды ({command}) произошли ошибки. Не удалось остановить сервисы {self.INSTALL_NEW_PATH[-1]}/template.yaml')
            for err in stderr: self.logger.error(err) if 'error' in err.lower() else self.logger.info(err)
            self.msgbox(f'При запуске сервисов произошли ошибки. Не удалось остановить сервисы {self.INSTALL_NEW_PATH[-1]}/template.yaml. Обратитесь в службу технической поддержки Р7-офис.')
        else:
            for err in stderr: self.logger.debug(err)
            self.logger.info('Docker Compose Plugin выполнена остановка сервисов без ошибок.')

        command = [f'{self.INSTALL_NEW_PATH[-1]}/runtime/docker/docker', 'volume', 'rm', f'{project_name}_ds-www']
        return_code, stdout, stderr = self.subprocess(command, fd_stderr=sys.stdout)
        if return_code != 0:
            self.logger.error(f'При попытке запуска команды ({command}) произошли ошибки, не удалось удалить volume {project_name}_ds-www')
            for err in stderr: self.logger.error(err) if 'error' in err.lower() else self.logger.info(err)
            self.msgbox(f'Не удалось удалить volume ds-www. Обратитесь в службу технической поддержки Р7-офис.')
        else:
            for err in stderr: self.logger.debug(err)
            self.logger.info('Docker Compose Plugin завершился без ошибок. volume {project_name}_ds-www удален')

        shutil.rmtree(self.INSTALL_NEW_PATH_DS_DB[-1])
        command = [f'{self.INSTALL_NEW_PATH[-1]}/runtime/docker-plugins/docker-compose', '-f', f'{self.INSTALL_NEW_PATH[-1]}/template.yaml', 'up', '--force-recreate', '-d', '-V']
        return_code, stdout, stderr = self.subprocess(command, fd_stderr=sys.stdout)
        if return_code != 0:
            self.logger.error(f'При попытке запуска команды ({command}) произошли ошибки. Не удалось запустить сервисы {self.INSTALL_NEW_PATH[-1]}/template.yaml')
            for err in stderr: self.logger.error(err) if 'error' in err.lower() else self.logger.info(err)
            self.msgbox(f'При запуске сервисов произошли ошибки. Не удалось запустить сервисы {self.INSTALL_NEW_PATH[-1]}/template.yaml. Обратитесь в службу технической поддержки Р7-офис.')
        else:
            for err in stderr: self.logger.debug(err)
            self.logger.info('Docker Compose Plugin завершился без ошибок.')
            self.msgbox(f'Сервисы продуктов Р7-офис обновлены.');
    
    def get_requests(self, prefix_env: str = "CS", dialog: list = None, external:bool = False, **kwarg):
        dialogs = dialog if isinstance(dialog, list) else [
            {
                "env": f'INSTALL_NEW_PATH',
                "enable": None,
                'msg': (
                    f'Р7 Сервер документов:\n\nУкажите путь, где установлена версия 622',
                    f'Путь до директории обязателен к заполнению',
                    f'Указанный путь не найден'
                ),
                "func_parse": self.parse_path_dir(),
                "func_check": self.check_extended_dir(),
                "func_visibility": self.visibility_detalization(),
                "detalization": 1
            },
            {
                "env": f'INSTALL_NEW_PATH_DS_DB',
                "enable": True,
                "msg": (
                    f'Р7 Сервер документов:\n\nУкажите путь, где установлена база данных сервера документов версии 622',
                    f'Путь до директории обязателен к заполнению',
                    f'Указанный путь не найден'
                ),
                "func_parse": self.parse_path_dir(),
                "func_check": self.check_extended_dir(),
                "func_visibility": self.visibility_detalization(),
                "detalization": 1
            },
        ];
        self.get_dialogs(dialogs)

