Added docker support
This commit is contained in:
@@ -1,106 +0,0 @@
|
||||
import os
|
||||
import shutil
|
||||
import zipfile
|
||||
from io import BytesIO
|
||||
|
||||
import requests
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.environment import ApplicationEnvironmentABC
|
||||
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
|
||||
|
||||
|
||||
class DeployCommand(DiscordCommandABC):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
logger: LoggerABC,
|
||||
config: ConfigurationABC,
|
||||
message_service: MessageServiceABC,
|
||||
bot: DiscordBotServiceABC,
|
||||
client_utils: ClientUtilsServiceABC,
|
||||
translate: TranslatePipe,
|
||||
bot_settings: BotSettings,
|
||||
env: ApplicationEnvironmentABC
|
||||
):
|
||||
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._env = env
|
||||
|
||||
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
|
||||
|
||||
blacklist = ['venv', 'start', 'latest']
|
||||
|
||||
if old_version in blacklist:
|
||||
raise Exception(self._t.transform('common.errors.bad_argument'))
|
||||
|
||||
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'), without_tracking=True)
|
||||
return
|
||||
|
||||
try:
|
||||
shutil.copy(f'{deploy_old_path}/bot/config/appsettings.{self._env.host_name}.json', f'{deploy_path}/bot/config/appsettings.{self._env.host_name}.json')
|
||||
except Exception as e:
|
||||
self._logger.error(__name__, f'An error occurred copying config file', e)
|
||||
await self._message_service.send_ctx_msg(ctx, self._t.transform('common.command_error'), without_tracking=True)
|
||||
return
|
||||
|
||||
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.admin.deploy_message'), without_tracking=True)
|
||||
|
||||
self._config.add_configuration('IS_RESTART', 'true')
|
||||
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.admin.restart_message'), without_tracking=True)
|
||||
await self._bot.stop_async()
|
||||
|
||||
self._logger.trace(__name__, f'Finished deploy command')
|
Reference in New Issue
Block a user