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

311 lines
10 KiB
Python
Raw 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
from datetime import datetime
2020-04-05 20:21:05 +02:00
2020-04-06 17:43:54 +02:00
host_name = '.1cfresh.dev'
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-05 20:21:05 +02:00
configurations = {}
2020-04-09 18:31:00 +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/'
local_work_dir = helper.replace_sep(helper.this_path + '/workdir/')
2020-04-09 18:31:00 +02:00
class colors:
GREEN = '\033[92m'
WHITE = '\033[97m'
def print_description(function_to_decorate):
def wrapper(*args, **kwargs):
if 'desc' in kwargs:
desc = kwargs['desc']
else:
desc = ''
print(function_to_decorate.__doc__, desc, '...', end='\r')
function_to_decorate(*args, **kwargs)
print(function_to_decorate.__doc__,
desc, '...', '{}done'.format(colors.GREEN), colors.WHITE)
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-09 18:31:00 +02:00
if action != '':
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-09 18:31:00 +02:00
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-09 18:31:00 +02:00
if action != '':
print(action, 'is fihish.', 'Duration:{}'.format(
end_time) if measure_duration else '')
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-05 20:21:05 +02:00
# r=root, d=directories, files = files
2020-04-06 17:43:54 +02:00
for r, d, files in os.walk(helper.replace_sep(local_work_dir + 'mnt')):
2020-04-05 20:21:05 +02:00
for file in files:
conf_key = file.split('.')[0].split('_')[0]
2020-04-09 18:31:00 +02:00
configurations[conf_key] = '.'.join(file.split(
'.')[0].split('_')).replace(conf_key + '.', '')
2020-04-05 20:21:05 +02:00
2020-04-08 20:56:50 +02:00
def prepare_new_ib(key, post_data=''):
2020-04-06 17:43:54 +02:00
2020-04-09 18:31:00 +02:00
call(' '.join(helper.create_ib_command(host_name, key, configurations[key])),
remote=False,
action='Creating ' + key,
measure_duration=True)
2020-04-05 20:21:05 +02:00
2020-04-09 18:31:00 +02:00
call(' '.join(helper.install_control_ext_command(host_name, key)),
remote=False,
action='Installing control extension',
measure_duration=True)
2020-04-06 17:43:54 +02:00
ext_name = helper.replace_sep(local_work_dir + 'mnt/' + key + '.cfe')
if os.path.isfile(ext_name):
2020-04-09 18:31:00 +02:00
call(' '.join(helper.install_ext_command(host_name, key)),
remote=False,
action='Installing extension',
measure_duration=True)
if key == 'sm':
call(' '.join(helper.install_sm_ext_command(host_name, key)),
remote=False,
action='Installing gate control extension',
measure_duration=True)
call(' '.join(helper.disable_safe_mode(host_name, key)),
remote=False,
action='Disabling safe mode for extensions',
measure_duration=True)
2020-04-08 20:56:50 +02:00
str_post = '-d @{}'.format(post_data) if post_data != '' else ''
2020-04-09 18:31:00 +02:00
call('docker exec web.{} curl {} -X POST http://localhost/int/{}/hs/api.1cfresh/init'.format(host_name, str_post, key),
remote=False,
action='Initialization',
measure_duration=True)
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))
call('sh -c \'cp -r /out_files/conf/nginx/* {}'.format(conf_catalog) + '\'')
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))
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-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-08 20:56:50 +02:00
call('rm -rf /out_files/workdir')
call('mkdir -p {}mnt'.format(work_dir))
call('mkdir -p {}artifacts'.format(work_dir))
call('sh -c "cp /out_files/distr/*.cf {}mnt/"'.format(work_dir))
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"""
2020-04-08 20:56:50 +02:00
call('cp /out_files/docker-compose.yml /out_files/workdir/docker-compose.yml')
call('sh -c "sed -i \'s/HOSTNAMEREPLACE/{}/\' {}/*.yml"'.format(host_name, work_dir))
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-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-08 20:56:50 +02:00
# publish a services
2020-04-09 18:31:00 +02:00
call(' '.join(helper.web_publish_command(
host_name, 'adm', False, 'zoneless', 'sm')), remote=False)
call(' '.join(helper.web_publish_command(
host_name, 'smtl', False, 'withzone')), remote=False)
call(' '.join(helper.web_publish_command(
host_name, 'sa', False, 'zoneless')), remote=False)
call(' '.join(helper.web_publish_command(
host_name, 'openid', False, 'openid', 'sm')), remote=False)
2020-04-08 20:56:50 +02:00
# publish int services
2020-04-09 18:31:00 +02:00
call(' '.join(helper.web_publish_command(
host_name, 'sm', True, 'zoneless')), remote=False)
call(' '.join(helper.web_publish_command(
host_name, 'smtl', True, 'zoneless')), remote=False)
call(' '.join(helper.web_publish_command(
host_name, 'sa', True, 'zoneless')), remote=False)
call(' '.join(helper.web_publish_command(
host_name, 'am', True, 'zoneless')), remote=False)
call(' '.join(helper.web_publish_command(host_name, 'sc', True,
'sessioncontrol', 'sm;Usr=SessionControl;Pwd=' + sup_password)), remote=False)
call(' '.join(helper.web_publish_command(host_name, 'extreg', True,
'extreg', 'sm;Usr=ExtReg;Pwd=' + sup_password)), remote=False)
2020-04-08 20:56:50 +02:00
# restart Apache
2020-04-09 18:31:00 +02:00
call('docker exec web.' + host_name +
' chown -R usr1cv8:grp1cv8 /var/www', remote=False)
2020-04-08 20:56:50 +02:00
call('docker exec web.' + host_name + ' httpd -k graceful', 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-09 18:31:00 +02:00
f = open(local_work_dir + 'hostname', 'x+')
2020-04-06 17:43:54 +02:00
f.write(part_host_name)
f.close()
else:
f = open(local_work_dir + 'hostname')
part_host_name = f.read() + host_name
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"""
call('docker exec -t db.{} sh -c \'/usr/bin/psql -U postgres -f {}'.format(host_name, '/create_db_site.psql\''),
remote=False)
@print_description
def create_db_forum():
"""Create db for forum"""
call('docker exec -t db.{} sh -c \'/usr/bin/psql -U postgres -f {}'.format(host_name, '/create_db_forum.psql\''),
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)
@print_description
def configurate_site():
"""Configurate site settings"""
call(' '.join(helper.edit_site_settings(host_name, sup_password)), remote=False)
call('curl https://{}/settings/finish_configuration'.format(host_name), remote=False)
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,
brief_desc='БТС',
full_desc='БТС',
display_order=0,
id='smtl',
possibilities='БТС',
title='Библиотека технологии сервиса'
)), 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)
global_start_time = datetime.now()
print('{}Fresh is starting{}'.format(colors.GREEN, colors.WHITE))
# destroy exist conteiners and network
call(docker_compose_str + 'down', remote=False, silent=False)
2020-04-05 23:09:47 +02:00
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-05 23:09:47 +02:00
if new_server:
2020-04-08 20:56:50 +02:00
renew_workdir()
2020-04-05 23:09:47 +02:00
get_configurations_data()
2020-04-09 18:31:00 +02:00
2020-04-06 17:43:54 +02:00
set_full_host_name(new_server)
2020-04-09 18:31:00 +02:00
if new_server:
2020-04-08 20:56:50 +02:00
renew_nginx_files()
renew_docker_compose()
renew_other_files()
2020-04-09 18:31:00 +02:00
2020-04-08 20:56:50 +02:00
# start db srv ras web gate conteiners
2020-04-09 18:31:00 +02:00
call(docker_compose_str + 'up -d db srv ras web gate', remote=False, silent=False)
2020-04-05 20:21:05 +02:00
2020-04-05 23:09:47 +02:00
if new_server:
2020-04-09 18:31:00 +02:00
create_db_site()
create_db_forum()
2020-04-08 20:56:50 +02:00
publish_sevises()
2020-04-05 23:09:47 +02:00
prepare_new_ib('smtl')
2020-04-08 20:56:50 +02:00
prepare_new_ib('sa')
prepare_new_ib('am')
2020-04-09 18:31:00 +02:00
prepare_new_ib('sm', post_data='/mnt/other-files/params.json')
2020-04-05 20:21:05 +02:00
2020-04-09 18:31:00 +02:00
# start site forum nginx conteiners
call(docker_compose_str + 'up -d site forum nginx', remote=False, silent=False)
2020-04-05 20:21:05 +02:00
2020-04-09 18:31:00 +02:00
if new_server:
delete_control_extension('smtl', 'Admin', 'smtl')
delete_control_extension('am', 'Администратор', 'am')
delete_control_extension('sm', 'Администратор', 'sm')
delete_control_extension('sa', None, 'sa')
configurate_site()
init_gate()
global_end_time = datetime.now() - global_start_time
print('{}Fresh started{}'.format(colors.GREEN, colors.WHITE), 'Duration:', global_end_time)