Added deploy command
This commit is contained in:
		
							
								
								
									
										86
									
								
								src/modules/admin/command/deploy_command.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										86
									
								
								src/modules/admin/command/deploy_command.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,86 @@ | ||||
| import os | ||||
| import shutil | ||||
| import zipfile | ||||
| from io import BytesIO | ||||
|  | ||||
| import requests | ||||
| from cpl_core.configuration import ConfigurationABC | ||||
| from cpl_core.logging import LoggerABC | ||||
| from cpl_discord.command import DiscordCommandABC | ||||
| from cpl_discord.service import DiscordBotServiceABC | ||||
| from cpl_translation import TranslatePipe | ||||
| from discord.ext import commands | ||||
| from discord.ext.commands import Context | ||||
|  | ||||
| from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC | ||||
| from bot_core.abc.message_service_abc import MessageServiceABC | ||||
| from bot_core.configuration.bot_settings import BotSettings | ||||
| from modules.permission.abc.permission_service_abc import PermissionServiceABC | ||||
|  | ||||
|  | ||||
| class DeployCommand(DiscordCommandABC): | ||||
|  | ||||
|     def __init__( | ||||
|             self, | ||||
|             logger: LoggerABC, | ||||
|             config: ConfigurationABC, | ||||
|             message_service: MessageServiceABC, | ||||
|             bot: DiscordBotServiceABC, | ||||
|             client_utils: ClientUtilsServiceABC, | ||||
|             translate: TranslatePipe, | ||||
|             bot_settings: BotSettings | ||||
|     ): | ||||
|         DiscordCommandABC.__init__(self) | ||||
|  | ||||
|         self._logger = logger | ||||
|         self._config = config | ||||
|         self._message_service = message_service | ||||
|         self._bot = bot | ||||
|         self._client_utils = client_utils | ||||
|         self._t = translate | ||||
|         self._bot_settings = bot_settings | ||||
|  | ||||
|         self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') | ||||
|  | ||||
|     @commands.command() | ||||
|     @commands.dm_only() | ||||
|     async def deploy(self, ctx: Context, old_version: str): | ||||
|         self._logger.debug(__name__, f'Received command deploy {ctx}') | ||||
|  | ||||
|         if ctx.author.id not in self._bot_settings.technicians: | ||||
|             await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) | ||||
|             self._logger.trace(__name__, f'Finished deploy command') | ||||
|             return | ||||
|  | ||||
|         if len(ctx.message.attachments) > 1: | ||||
|             raise IndexError(self._t.transform('common.errors.too_many_arguments')) | ||||
|  | ||||
|         if len(ctx.message.attachments) < 1: | ||||
|             raise IndexError(self._t.transform('common.errors.missing_required_argument')) | ||||
|  | ||||
|         try: | ||||
|             attachment_url = ctx.message.attachments[0].url | ||||
|             file_request = requests.get(attachment_url) | ||||
|         except Exception as e: | ||||
|             self._logger.error(__name__, f'An error occurred downloading the zip file', e) | ||||
|             await self._message_service.send_ctx_msg(ctx, self._t.transform('common.command_error')) | ||||
|             return | ||||
|  | ||||
|         try: | ||||
|             self._logger.debug(__name__, f'Deploying files to {self._bot_settings.deploy_file_path}/latest') | ||||
|             deploy_path = f'{self._bot_settings.deploy_file_path}/latest' | ||||
|             deploy_old_path = f'{self._bot_settings.deploy_file_path}/{old_version}' | ||||
|  | ||||
|             if os.path.exists(deploy_path): | ||||
|                 self._logger.debug(__name__, f'Moving files of {self._bot_settings.deploy_file_path}/latest to {self._bot_settings.deploy_file_path}/{old_version}') | ||||
|                 shutil.move(deploy_path, deploy_old_path) | ||||
|             os.makedirs(deploy_path) | ||||
|  | ||||
|             file = zipfile.ZipFile(BytesIO(file_request.content)) | ||||
|             file.extractall(deploy_path) | ||||
|         except Exception as e: | ||||
|             self._logger.error(__name__, f'An error occurred extracting the zip file', e) | ||||
|             await self._message_service.send_ctx_msg(ctx, self._t.transform('common.command_error')) | ||||
|             return | ||||
|  | ||||
|         self._logger.trace(__name__, f'Finished deploy command') | ||||
| @@ -1,4 +1,5 @@ | ||||
| import datetime | ||||
| import traceback | ||||
| import uuid | ||||
|  | ||||
| from cpl_core.time import TimeFormatSettings | ||||
|   | ||||
		Reference in New Issue
	
	Block a user