1
0
mirror of https://github.com/1C-Company/docker_fresh.git synced 2024-12-04 10:24:48 +02:00
docker_fresh/start.py

485 lines
15 KiB
Python
Raw Permalink Normal View History

2020-04-05 20:21:05 +02:00
import subprocess
import os
import modules.helper as helper
2020-04-05 23:09:47 +02:00
import sys
2020-04-10 18:47:06 +02:00
import json
2020-04-19 16:46:04 +02:00
import threading
import time
2020-07-23 19:57:47 +02:00
import codecs
2020-04-05 23:09:47 +02:00
from datetime import datetime
2020-04-05 20:21:05 +02:00
host_name = '.1cfresh-dev.ru'
2020-04-05 23:09:47 +02:00
sup_password = '123Qwer'
2020-04-08 20:56:50 +02:00
new_server = False
2020-04-06 17:43:54 +02:00
global_debug = False
2020-04-10 18:47:06 +02:00
info_base_list = []
2020-04-05 20:21:05 +02:00
configurations = {}
2020-04-11 18:15:51 +02:00
docker_run_str = 'docker run --rm -v {}:/out_files alpine'.format(helper.this_path)
2020-04-05 20:21:05 +02:00
docker_compose_str = 'docker-compose -f workdir/docker-compose.yml '
work_dir = '/out_files/workdir/'
work_dir_other = work_dir + 'mnt/other-files/'
2020-04-19 14:40:25 +02:00
local_work_dir = helper.replace_sep(helper.this_path + 'workdir/')
path_to_1c = ''
2020-04-05 20:21:05 +02:00
2020-04-09 18:31:00 +02:00
class colors:
GREEN = '\033[92m'
WHITE = '\033[97m'
2020-04-18 12:32:45 +02:00
RED = '\033[91m'
2020-04-09 18:31:00 +02:00
2020-04-11 15:23:52 +02:00
class ib_prop:
a_name = 'ИмяВнешнейПубликации'
a_desc = 'ИмяФайлаШаблонаВнешненийПубликации'
int_name = 'ИмяВнутреннейПубликации'
int_desc = 'ИмяФайлаШаблонаВнутреннейПубликации'
conf_file = 'ИмяФайлаКонфигурации'
name = 'ИмяВКластере'
job = 'БлокироватьРаботуРегЗаданийПриСоздании'
adm = 'Администратор'
2020-04-09 18:31:00 +02:00
2020-04-19 16:46:04 +02:00
class ProgressThread(threading.Thread):
desc = ''
def __init__(self, desc):
self.desc = desc
super().__init__(target=self._spin)
self._stopevent = threading.Event()
def stop(self):
self._stopevent.set()
def _spin(self):
while not self._stopevent.isSet():
for t in [' ', '. ', '.. ', '...']:
print(self.desc, t, end='\r')
time.sleep(0.5)
2020-04-19 20:56:33 +02:00
class DoThread(threading.Thread):
is_good = False
def run(self):
try:
threading.Thread.run(self)
except:
self.is_good = False
pass
else:
self.is_good = True
2020-04-09 18:31:00 +02:00
def print_description(function_to_decorate):
def wrapper(*args, **kwargs):
if 'desc' in kwargs:
desc = kwargs['desc']
else:
desc = ''
2020-04-19 16:46:04 +02:00
all_desc = function_to_decorate.__doc__ + desc
2020-04-19 20:56:33 +02:00
task = DoThread(target=function_to_decorate, args=args, kwargs=kwargs)
2020-04-19 16:46:04 +02:00
task.start()
progress_thread = ProgressThread(all_desc)
progress_thread.start()
task.join()
progress_thread.stop()
2020-04-19 20:56:33 +02:00
while progress_thread.is_alive():
2020-04-19 16:46:04 +02:00
time.sleep(0.2)
2020-04-19 20:56:33 +02:00
if not task.is_good: exit(1)
2020-04-19 16:46:04 +02:00
print(all_desc, '...', '{}done'.format(colors.GREEN), colors.WHITE)
2020-04-09 18:31:00 +02:00
return wrapper
def call(command, remote=True, debug=False, action='', measure_duration=False, silent=True):
2020-04-05 20:21:05 +02:00
commands = []
2020-04-09 18:31:00 +02:00
if remote:
commands.append(docker_run_str)
2020-04-05 20:21:05 +02:00
commands.append(command)
2020-04-08 20:56:50 +02:00
2020-04-11 15:23:52 +02:00
if action != '' and (debug or global_debug):
2020-04-09 18:31:00 +02:00
print(action, end='\r')
if debug or global_debug:
print(' '.join(commands))
if not silent or global_debug:
stdout = None
stderr = None
else:
stdout = subprocess.PIPE
stderr = subprocess.PIPE
2020-04-08 20:56:50 +02:00
start_time = datetime.now()
2020-04-19 14:40:25 +02:00
result = subprocess.call(' '.join(commands), shell=True, stdout=stdout, stderr=stderr)
2020-04-08 20:56:50 +02:00
end_time = datetime.now() - start_time
2020-04-11 15:23:52 +02:00
if action != '' and (debug or global_debug):
2020-04-09 18:31:00 +02:00
print(action, 'is fihish.', 'Duration:{}'.format(
end_time) if measure_duration else '')
2020-04-19 14:40:25 +02:00
return result
2020-04-05 20:21:05 +02:00
2020-04-09 18:31:00 +02:00
@print_description
2020-04-05 20:21:05 +02:00
def get_configurations_data():
2020-04-09 18:31:00 +02:00
"""Get configuration data"""
2020-04-18 12:32:45 +02:00
is_fail = False
2020-07-23 19:57:47 +02:00
with codecs.open('other_files/params.json', 'r', 'utf-8') as json_file:
2020-04-10 18:47:06 +02:00
data = json.load(json_file)
for ib_data in data['ИнформационныеБазы']:
if not os.path.isfile('distr/{}'.format(ib_data['ИмяФайлаКонфигурации'])):
2020-04-18 12:32:45 +02:00
is_fail = True
print(colors.RED, 'File {} not found'.format(ib_data['ИмяФайлаКонфигурации']), colors.WHITE)
2020-04-10 18:47:06 +02:00
else:
info_base_list.append(ib_data)
2020-04-18 12:32:45 +02:00
if is_fail: exit(1)
2020-04-06 17:43:54 +02:00
2020-04-19 14:40:25 +02:00
def check_call_work(result, action, ib_name):
if result == 0: return
print(action, ib_name, 'is failed')
f = open(local_work_dir + 'mnt/{}_{}.out'.format(action, ib_name))
print(f.read())
f.close()
exit(1)
2020-04-11 15:23:52 +02:00
def prepare_new_ib(ib_name, int_name, conf_file_name, job_block):
2020-04-10 18:47:06 +02:00
2020-04-11 15:23:52 +02:00
job_dn_str = 'Y' if job_block else 'N'
2020-04-10 18:47:06 +02:00
2020-04-19 14:40:25 +02:00
action = 'create_ib'
result = call(' '.join(helper.create_ib_command(host_name, ib_name, conf_file_name, job_dn_str, action)),
2020-04-09 18:31:00 +02:00
remote=False,
2020-04-11 15:23:52 +02:00
action='Creating ' + ib_name,
2020-04-09 18:31:00 +02:00
measure_duration=True)
2020-04-19 14:40:25 +02:00
check_call_work(result, action, ib_name)
2020-04-05 20:21:05 +02:00
2020-04-19 14:40:25 +02:00
action = 'install_control_ext'
result = call(' '.join(helper.install_control_ext_command(host_name, ib_name, action)),
2020-04-09 18:31:00 +02:00
remote=False,
action='Installing control extension',
measure_duration=True)
2020-04-19 14:40:25 +02:00
check_call_work(result, action, ib_name)
2020-04-06 17:43:54 +02:00
2020-04-11 15:23:52 +02:00
ext_name = helper.replace_sep(local_work_dir + 'mnt/' + ib_name + '.cfe')
2020-04-06 17:43:54 +02:00
if os.path.isfile(ext_name):
2020-04-19 14:40:25 +02:00
action = 'install_ext'
result = call(' '.join(helper.install_ext_command(host_name, ib_name, action)),
2020-04-09 18:31:00 +02:00
remote=False,
action='Installing extension',
measure_duration=True)
2020-04-19 14:40:25 +02:00
check_call_work(result, action, ib_name)
2020-04-09 18:31:00 +02:00
2020-04-11 15:23:52 +02:00
if ib_name == 'sm':
2020-04-19 14:40:25 +02:00
action = 'install_sm_ext'
2020-04-11 15:23:52 +02:00
post_data = '/mnt/other-files/params.json'
2020-04-19 14:40:25 +02:00
result = call(' '.join(helper.install_sm_ext_command(host_name, ib_name, action)),
2020-04-11 15:23:52 +02:00
remote=False,
action='Installing gate control extension',
measure_duration=True)
2020-04-19 14:40:25 +02:00
check_call_work(result, action, ib_name)
2020-04-11 15:23:52 +02:00
else:
post_data = ''
2020-04-09 18:31:00 +02:00
2020-04-19 14:40:25 +02:00
action = 'disable_safe_mode'
result = call(' '.join(helper.disable_safe_mode(host_name, ib_name, action)),
2020-04-09 18:31:00 +02:00
remote=False,
action='Disabling safe mode for extensions',
measure_duration=True)
2020-04-19 14:40:25 +02:00
check_call_work(result, action, ib_name)
2020-04-09 18:31:00 +02:00
2020-04-08 20:56:50 +02:00
str_post = '-d @{}'.format(post_data) if post_data != '' else ''
2020-04-11 15:23:52 +02:00
call('docker exec web.{} curl {} -X POST http://localhost/int/{}/hs/api.1cfresh/init'.format(host_name, str_post, int_name),
2020-04-09 18:31:00 +02:00
remote=False,
action='Initialization',
measure_duration=True)
2020-07-23 19:57:47 +02:00
@print_description
def delete_volumes():
"""Delete volumes"""
call('docker volume rm workdir_1c_pg_data', remote=False)
call('docker volume rm workdir_1c_pg_socket', remote=False)
call('docker volume rm workdir_1c_server_data', remote=False)
2020-07-23 19:57:47 +02:00
2020-04-11 15:23:52 +02:00
@print_description
def prepare_bases():
"""Prepare all bases"""
sm_ib = None
for ib_data in info_base_list:
if ib_data[ib_prop.name] == 'sm':
sm_ib = ib_data
continue
prepare_new_ib(
ib_name=ib_data[ib_prop.name],
int_name=ib_data[ib_prop.int_name],
conf_file_name=ib_data[ib_prop.conf_file],
job_block=ib_data[ib_prop.job]
)
if ib_data[ib_prop.job]:
enable_job(ib_data[ib_prop.name], ib_data[ib_prop.adm])
2020-04-11 15:23:52 +02:00
# prepare sm base
prepare_new_ib(
ib_name=sm_ib[ib_prop.name],
int_name=sm_ib[ib_prop.int_name],
conf_file_name=sm_ib[ib_prop.conf_file],
job_block=sm_ib[ib_prop.job]
)
if ib_prop.job:
enable_job(ib_data[ib_prop.name], ib_data[ib_prop.adm])
2020-04-08 20:56:50 +02:00
2020-04-09 18:31:00 +02:00
@print_description
2020-04-08 20:56:50 +02:00
def renew_nginx_files():
2020-04-09 18:31:00 +02:00
"""Renew nginx files"""
2020-04-08 20:56:50 +02:00
conf_catalog = work_dir + 'artifacts/nginx/conf/'
call('mkdir -p {}'.format(conf_catalog))
2020-07-23 19:57:47 +02:00
call('sh -c "cp -r /out_files/conf/nginx/* {}"'.format(conf_catalog))
2020-04-08 20:56:50 +02:00
2020-07-23 19:57:47 +02:00
call('sh -c "sed -i \'s/hosthosthost/{}/g\' {}*.conf"'.format(host_name, conf_catalog))
call('sh -c "sed -i \'s/sitesitesite/site.{}/g\' {}*.conf"'.format(host_name, conf_catalog))
call('sh -c "sed -i \'s/webwebweb/web.{}/g\' {}*.conf"'.format(host_name, conf_catalog))
call('sh -c "sed -i \'s/gategategate/gate.{}/g\' {}*.conf"'.format(host_name, conf_catalog))
2020-04-08 20:56:50 +02:00
2020-07-23 19:57:47 +02:00
call('sh -c "sed -i \'s/sitesitesite/site.{}/g\' {}conf.d/*.conf"'.format(host_name, conf_catalog))
call('sh -c "sed -i \'s/hosthosthost/{}/g\' {}conf.d/*.conf"'.format(host_name, conf_catalog))
2020-04-08 20:56:50 +02:00
2020-04-09 18:31:00 +02:00
@print_description
2020-04-08 20:56:50 +02:00
def renew_workdir():
2020-04-09 18:31:00 +02:00
"""Renew wordir"""
2020-04-19 20:56:33 +02:00
2020-04-08 20:56:50 +02:00
call('rm -rf /out_files/workdir')
call('mkdir -p {}mnt'.format(work_dir))
2020-04-11 15:23:52 +02:00
call('mkdir -p {}artifacts/web/conf'.format(work_dir))
call('sh -c "cp /out_files/conf/web/httpd.conf {}artifacts/web/conf/httpd.conf"'.format(work_dir))
2020-04-08 20:56:50 +02:00
call('sh -c "cp /out_files/distr/*.cf {}mnt/"'.format(work_dir))
2020-04-19 01:54:38 +02:00
call('sh -c "cp /out_files/distr/*.cfe {}mnt/"'.format(work_dir))
2020-04-08 20:56:50 +02:00
2020-04-09 18:31:00 +02:00
@print_description
2020-04-08 20:56:50 +02:00
def renew_docker_compose():
2020-04-09 18:31:00 +02:00
"""Renew docker-compose file"""
call('cp /out_files/docker-compose_pattern.yml /out_files/workdir/docker-compose.yml')
2020-04-08 20:56:50 +02:00
call('sh -c "sed -i \'s/HOSTNAMEREPLACE/{}/\' {}/*.yml"'.format(host_name, work_dir))
call('sh -c "sed -i \'s/PATH_TO_1C_REPLACE/{}/\' {}/*.yml"'.format(path_to_1c.replace('/','\/'), work_dir))
2020-04-08 20:56:50 +02:00
2020-04-09 18:31:00 +02:00
@print_description
2020-04-08 20:56:50 +02:00
def renew_other_files():
2020-04-09 18:31:00 +02:00
"""Renew other-files"""
2020-04-08 20:56:50 +02:00
call('rm -rf ' + work_dir_other)
call('cp -r /out_files/other_files/ ' + work_dir_other)
call('sh -c "sed -i \'s/HOSTNAMEREPLACE/{}/\' {}vrd/*.vrd"'.format(host_name, work_dir_other))
2020-04-09 18:31:00 +02:00
call('sh -c "sed -i \'s/HOSTNAMEREPLACE/{}/\' {}params.json"'.format(host_name, work_dir_other))
2020-09-27 15:19:39 +02:00
@print_description
def create_bucket():
"""Create new bucket to 1C"""
call('mkdir /out_files/workdir/artifacts/s3/files')
2020-04-08 20:56:50 +02:00
2020-04-09 18:31:00 +02:00
@print_description
2020-04-08 20:56:50 +02:00
def publish_sevises():
2020-04-09 18:31:00 +02:00
"""Publish services"""
2020-04-11 15:23:52 +02:00
for ib_data in info_base_list:
if ib_data[ib_prop.a_name] != '':
call(' '.join(helper.web_publish_command(
host_name=host_name,
conf_name=ib_data[ib_prop.a_name],
internal=False,
descriptor=ib_data[ib_prop.a_desc],
base_name=ib_data[ib_prop.name])), remote=False)
call(' '.join(helper.web_publish_command(
host_name=host_name,
conf_name=ib_data[ib_prop.int_name],
internal=True,
descriptor=ib_data[ib_prop.int_desc],
base_name=ib_data[ib_prop.name])), remote=False)
2020-04-10 18:47:06 +02:00
# publish special services
call(' '.join(helper.web_publish_command(
host_name, 'openid', False, 'openid', 'sm')), remote=False)
2020-04-09 18:31:00 +02:00
call(' '.join(helper.web_publish_command(host_name, 'sc', True,
2020-04-10 18:47:06 +02:00
'sessioncontrol', 'sm;Usr=SessionControl;Pwd=' + sup_password)), remote=False)
2020-04-09 18:31:00 +02:00
call(' '.join(helper.web_publish_command(host_name, 'extreg', True,
2020-04-10 18:47:06 +02:00
'extreg', 'sm;Usr=ExtReg;Pwd=' + sup_password)), remote=False)
2020-04-08 20:56:50 +02:00
# restart Apache
2020-04-11 15:23:52 +02:00
call('docker exec web.{} chown -R usr1cv8:grp1cv8 /var/www'.format(host_name), remote=False)
call('docker exec web.{} httpd -k graceful'.format(host_name), remote=False)
2020-04-06 17:43:54 +02:00
2020-04-09 18:31:00 +02:00
@print_description
2020-04-06 17:43:54 +02:00
def set_full_host_name(is_new):
2020-04-09 18:31:00 +02:00
"""Set full hostname"""
2020-04-06 17:43:54 +02:00
global host_name
if is_new:
part_host_name = helper.get_host_name(sys.argv)
2020-04-11 15:23:52 +02:00
f = open('.hostname', 'w+')
2020-04-06 17:43:54 +02:00
f.write(part_host_name)
f.close()
else:
2020-04-11 15:23:52 +02:00
f = open('.hostname')
2020-04-11 18:15:51 +02:00
part_host_name = f.read()
2020-04-06 17:43:54 +02:00
f.close()
2020-04-05 20:21:05 +02:00
2020-04-06 17:43:54 +02:00
host_name = part_host_name + host_name
print('host name is', host_name)
2020-04-05 23:09:47 +02:00
2020-04-09 18:31:00 +02:00
@print_description
def create_db_site():
"""Create db for site"""
2020-07-23 19:57:47 +02:00
call('docker exec -t db.{} sh -c "/usr/bin/psql -U postgres -f {}'.format(host_name, '/create_db_site.psql"'),
2020-04-09 18:31:00 +02:00
remote=False)
@print_description
def create_db_forum():
"""Create db for forum"""
2020-07-23 19:57:47 +02:00
call('docker exec -t db.{} sh -c "/usr/bin/psql -U postgres -f {}'.format(host_name, '/create_db_forum.psql"'),
2020-04-09 18:31:00 +02:00
remote=False)
@print_description
def delete_control_extension(ib_name, user, desc):
"""Delete control extension"""
call(' '.join(helper.delete_control_extension(ib_name, host_name, user)), remote=False)
2020-04-11 15:23:52 +02:00
def delete_all_control_ext():
for ib_data in info_base_list:
delete_control_extension(
ib_name=ib_data[ib_prop.name],
user= None if ib_data[ib_prop.adm] == '' else ib_data[ib_prop.adm],
desc=ib_data[ib_prop.name]
)
2020-04-09 18:31:00 +02:00
@print_description
def configurate_site():
"""Configurate site settings"""
call(' '.join(helper.edit_site_settings(host_name, sup_password)), remote=False)
2020-04-11 16:23:26 +02:00
call('docker exec -t web.{0} curl https://{0}/settings/finish_configuration'.format(host_name), remote=False)
2020-04-09 18:31:00 +02:00
call(' '.join(helper.enable_manual_registration(host_name)), remote=False)
call(' '.join(helper.enable_openid(host_name)), remote=False)
call(' '.join(helper.add_solution(
host_name=host_name,
2020-04-19 13:18:35 +02:00
brief_desc='"БТС"',
full_desc='"БТС"',
2020-04-09 18:31:00 +02:00
display_order=0,
id='smtl',
2020-04-19 13:18:35 +02:00
possibilities='"БТС"',
title='"Библиотека технологии сервиса"'
2020-04-09 18:31:00 +02:00
)), remote=False)
@print_description
def init_gate():
"""Initialization gate"""
call('docker exec -t web.{0} curl --user Администратор: https://{0}/a/adm/hs/docker_control/update_appgate'.format(host_name),
remote=False)
2020-04-11 15:23:52 +02:00
2020-04-10 18:47:06 +02:00
@print_description
def wait_postgres():
"""Waiting for postgres"""
call('docker exec -t db.{} /wait_postgres.sh'.format(host_name), remote=False)
2020-04-11 15:23:52 +02:00
2020-04-10 18:47:06 +02:00
@print_description
def wait_site():
"""Waiting for site"""
call('docker exec -t site.{} /wait_site.sh'.format(host_name), remote=False)
def enable_job(base_name, user):
2020-04-11 15:23:52 +02:00
call('docker exec -t ras.{} deployka scheduledjobs unlock -db {} -db-user "{}"'.format(host_name, base_name, user),
2020-04-10 18:47:06 +02:00
remote=False)
2020-04-09 18:31:00 +02:00
2020-04-18 12:35:41 +02:00
@print_description
def down_containers():
"""Down all conteiners"""
call(docker_compose_str + 'down', remote=False)
@print_description
def get_path_to_1c():
"""Getting path to 1C"""
global path_to_1c
cmd = "docker run --rm fresh/core sh -c \"find / -name '1cv8c' | sed 's/1cv8c//g'\""
pipe = subprocess.PIPE
p = subprocess.Popen(cmd, shell=True, stdin=pipe, stdout=pipe, stderr=pipe, close_fds=True)
path_to_1c = p.stdout.read().decode('utf-8').strip()
print('path to 1C: ' + path_to_1c)
2020-04-11 15:23:52 +02:00
2020-04-09 18:31:00 +02:00
global_start_time = datetime.now()
2020-04-19 14:40:25 +02:00
print('{}Fresh is starting{} at {}'.format(colors.GREEN, colors.WHITE, global_start_time))
2020-04-10 18:47:06 +02:00
2020-04-18 12:35:41 +02:00
down_containers()
2020-04-08 20:56:50 +02:00
new_server = '-new' in sys.argv
global_debug = '-debug' in sys.argv
2020-04-09 18:31:00 +02:00
2020-04-06 17:43:54 +02:00
set_full_host_name(new_server)
get_path_to_1c()
helper.init(path_to_1c)
2020-04-06 17:43:54 +02:00
2020-04-09 18:31:00 +02:00
if new_server:
2020-04-11 15:23:52 +02:00
renew_workdir()
get_configurations_data()
2020-04-08 20:56:50 +02:00
renew_nginx_files()
renew_docker_compose()
renew_other_files()
2020-07-23 19:57:47 +02:00
delete_volumes()
2020-04-09 18:31:00 +02:00
2020-04-08 20:56:50 +02:00
# start db srv ras web gate conteiners
2020-09-27 15:19:39 +02:00
call(docker_compose_str + 'up -d db srv ras web gate s3', remote=False, silent=False)
2020-04-10 18:47:06 +02:00
wait_postgres()
2020-04-05 20:21:05 +02:00
2020-04-05 23:09:47 +02:00
if new_server:
2020-09-27 15:19:39 +02:00
create_bucket()
2020-04-08 20:56:50 +02:00
publish_sevises()
2020-04-11 15:23:52 +02:00
prepare_bases()
2020-04-10 18:47:06 +02:00
create_db_site()
create_db_forum()
2020-04-05 20:21:05 +02:00
2020-04-09 18:31:00 +02:00
# start site forum nginx conteiners
2020-04-10 18:47:06 +02:00
call(docker_compose_str + 'up -d nginx site', remote=False, silent=False)
wait_site()
2020-04-05 20:21:05 +02:00
2020-04-09 18:31:00 +02:00
if new_server:
2020-04-11 15:23:52 +02:00
delete_all_control_ext()
2020-04-09 18:31:00 +02:00
configurate_site()
2020-04-11 17:25:04 +02:00
init_gate()
2020-04-09 18:31:00 +02:00
global_end_time = datetime.now() - global_start_time
print('{}Fresh started{}'.format(colors.GREEN, colors.WHITE), 'Duration:', global_end_time)