def execute(self, args, use_default_backend=False, server02_port=None, show_output=True): os.mkdir(os.path.join(self.target_dir, 'config')) # noqa os.mkdir(os.path.join(self.target_dir, 'config', 'zdaemon')) # noqa os.mkdir(os.path.join(self.target_dir, 'logs')) # noqa repo_dir = os.path.join(self.target_dir, 'config', 'repo') # noqa os.mkdir(repo_dir) # noqa log_path = os.path.abspath(os.path.join(repo_dir, '..', '..', 'logs', 'lb-agent.log')) # noqa stats_socket = os.path.join(self.target_dir, 'haproxy-stat.sock') # noqa open(os.path.join(repo_dir, 'lb-agent.conf'), 'w').write(config_template) # noqa open(os.path.join(repo_dir, 'logging.conf'), 'w').write((common_logging_conf_contents.format(log_path=log_path))) # noqa if use_default_backend: backend = default_backend.format(server01_port=http_plain_server_port, server02_port=server02_port) else: backend = '\n# ZATO default_backend_empty' zato_config = zato_config_template.format(stats_socket=stats_socket, stats_password=uuid.uuid4().hex, default_backend=backend) # noqa open(os.path.join(repo_dir, 'zato.config'), 'w').write(zato_config) # noqa self.copy_lb_crypto(repo_dir, args) # Initial info self.store_initial_info(self.target_dir, self.COMPONENTS.LOAD_BALANCER.code) if show_output: if self.verbose: msg = "Successfully created a load-balancer's agent in {}".format(self.target_dir) self.logger.debug(msg) else: self.logger.info('OK')
def execute(self, args): config = { 'host': zato_admin_host, 'port': zato_admin_port, 'db_type': args.odb_type, 'log_config': 'logging.conf', 'DATABASE_NAME': args.odb_dbname, 'DATABASE_USER': args.odb_user, 'DATABASE_PASSWORD': args.odb_password, 'DATABASE_HOST': args.odb_host, 'DATABASE_PORT': args.odb_port, 'SITE_ID': uuid.uuid4().int, 'SECRET_KEY': uuid.uuid4().hex, 'TECH_ACCOUNT_NAME':self.tech_account_name, 'TECH_ACCOUNT_PASSWORD':self.tech_account_password, } os.mkdir(os.path.join(self.target_dir, "logs")) open(os.path.join(self.target_dir, 'logging.conf'), 'w').write(common_logging_conf_contents.format(log_path='./logs/admin.log')) open(os.path.join(self.target_dir, 'zato-admin.conf'), 'w').write(config_template.format(**config)) open(os.path.join(self.target_dir, ZATO_ADMIN_DIR), 'w').close() msg = """\nSuccessfully created a Zato Admin instance. You can now go to {path} and start it with the 'zato start zato-admin' command. """.format(path=os.path.abspath(os.path.join(os.getcwd(), self.target_dir))) print(msg)
def _migrate_from_1_1_to_2_0_logging(self): logging_conf_path = os.path.join(self.component_dir, 'config', 'repo', 'logging.conf') cp = ConfigParser() cp.read(logging_conf_path) args = literal_eval((cp.get('handler_rotating_file_handler', 'args'))) log_path = args[0] logging_conf = common_logging_conf_contents.format(log_path=log_path) open(logging_conf_path, 'w').write(logging_conf)
def execute(self, args): os.mkdir(os.path.join(self.target_dir, 'config')) os.mkdir(os.path.join(self.target_dir, 'config', 'zdaemon')) os.mkdir(os.path.join(self.target_dir, 'logs')) log_path = os.path.join(self.target_dir, 'logs', 'lb-agent.log') stats_socket = os.path.join(self.target_dir, 'haproxy-stat.sock') open(os.path.join(self.target_dir, 'config', 'lb-agent.conf'), 'w').write(config_template) open(os.path.join(self.target_dir, 'config', 'logging.conf'), 'w').write((common_logging_conf_contents.format(log_path=log_path))) open(os.path.join(self.target_dir, ZATO_LB_DIR), 'w').close() zato_config = zato_config_template.format(stats_socket=stats_socket, stats_password=uuid.uuid4().hex, http_plain_server_port=http_plain_server_port) open(os.path.join(self.target_dir, 'config', 'zato.config'), 'w').write(zato_config) msg = """\nSuccessfully created a Load Balancer's agent. You can now go to {path} and start it with the 'zato start lb-agent' command. """.format(path=os.path.abspath(os.path.join(os.getcwd(), self.target_dir))) print(msg)
'work/hot-deploy', 'work/hot-deploy/current', 'work/hot-deploy/backup', 'work/hot-deploy/backup/last', 'config/repo/lua', 'config/repo/lua/internal', 'config/repo/lua/user', 'config/repo/static', 'config/repo/tls', 'config/repo/tls/keys-certs', 'config/repo/tls/ca-certs', ) files = { 'config/repo/logging.conf': common_logging_conf_contents.format(log_path='./logs/server.log'), 'config/repo/service-sources.txt': service_sources_contents, 'config/repo/lua/internal/zato.rename_if_exists.lua': lua_zato_rename_if_exists, 'config/repo/sql.conf': sql_conf_contents } priv_key_location = './config/repo/config-priv.pem' priv_key_location = './config/repo/config-pub.pem' class Create(ZatoCommand): """ Creates a new Zato server """
def execute(self, args, show_output=True, needs_created_flag=False): os.chdir(self.target_dir) repo_dir = os.path.join(self.target_dir, 'config', 'repo') conf_path = os.path.join(repo_dir, 'scheduler.conf') startup_jobs_conf_path = os.path.join(repo_dir, 'startup_jobs.conf') sql_conf_path = os.path.join(repo_dir, 'sql.conf') os.mkdir(os.path.join(self.target_dir, 'logs')) os.mkdir(os.path.join(self.target_dir, 'config')) os.mkdir(repo_dir) self.copy_scheduler_crypto(repo_dir, args) if hasattr(args, 'get'): secret_key = args.get('secret_key') else: secret_key = args.secret_key secret_key = secret_key or SchedulerCryptoManager.generate_key() cm = SchedulerCryptoManager.from_secret_key(secret_key) odb_engine=args.odb_type if odb_engine.startswith('postgresql'): odb_engine = 'postgresql+pg8000' if args.cluster_id: cluster_id = args.cluster_id else: cluster_id = self._get_cluster_id_by_name(args, args.cluster_name) odb_password = args.odb_password or '' odb_password = odb_password.encode('utf8') odb_password = cm.encrypt(odb_password) odb_password = odb_password.decode('utf8') kvdb_password = args.kvdb_password or '' kvdb_password = kvdb_password.encode('utf8') kvdb_password = cm.encrypt(kvdb_password) kvdb_password = kvdb_password.decode('utf8') user1_password = cm.generate_password() user1_password = cm.encrypt(user1_password) user1_password = user1_password.decode('utf8') zato_well_known_data = well_known_data.encode('utf8') zato_well_known_data = cm.encrypt(zato_well_known_data) zato_well_known_data = zato_well_known_data.decode('utf8') secret_key = secret_key.decode('utf8') # We will use TLS only if we were given crypto material on input use_tls = bool(args.get('priv_key_path')) config = { 'odb_db_name': args.odb_db_name or args.sqlite_path, 'odb_engine': odb_engine, 'odb_host': args.odb_host or '', 'odb_port': args.odb_port or '', 'odb_password': odb_password, 'odb_username': args.odb_user or '', 'broker_host': args.kvdb_host, 'broker_port': args.kvdb_port, 'broker_password': kvdb_password, 'user1_password': user1_password, 'cluster_id': cluster_id, 'cluster_name': args.cluster_name, 'secret_key1': secret_key, 'well_known_data': zato_well_known_data, 'use_tls': 'true' if use_tls else 'false' } open(os.path.join(repo_dir, 'logging.conf'), 'w').write( common_logging_conf_contents.format(log_path='./logs/scheduler.log')) open(conf_path, 'w').write(config_template.format(**config)) open(startup_jobs_conf_path, 'w').write(startup_jobs) open(sql_conf_path, 'w').write(sql_conf_contents) # Initial info self.store_initial_info(self.target_dir, self.COMPONENTS.SCHEDULER.code) if show_output: if self.verbose: msg = """Successfully created a scheduler instance. You can start it with the 'zato start {path}' command.""".format(path=os.path.abspath(os.path.join(os.getcwd(), self.target_dir))) self.logger.debug(msg) else: self.logger.info('OK') # We return it only when told to explicitly so when the command runs from CLI # it doesn't return a non-zero exit code. if needs_created_flag: return True
def execute(self, args, show_output=True, password=None, needs_admin_created_flag=False): os.chdir(self.target_dir) repo_dir = os.path.join(self.target_dir, 'config', 'repo') web_admin_conf_path = os.path.join(repo_dir, 'web-admin.conf') initial_data_json_path = os.path.join(repo_dir, 'initial-data.json') os.mkdir(os.path.join(self.target_dir, 'logs')) os.mkdir(os.path.join(self.target_dir, 'config')) os.mkdir(repo_dir) user_name = 'admin' password = password if password else generate_password() self.copy_web_admin_crypto(repo_dir, args) priv_key = open(os.path.join(repo_dir, 'web-admin-priv-key.pem')).read() config = { 'host': web_admin_host, 'port': web_admin_port, 'db_type': args.odb_type, 'log_config': 'logging.conf', 'DATABASE_NAME': args.odb_db_name or args.sqlite_path, 'DATABASE_USER': args.odb_user or '', 'DATABASE_PASSWORD': encrypt(args.odb_password, priv_key) if args.odb_password else '', 'DATABASE_HOST': args.odb_host or '', 'DATABASE_PORT': args.odb_port or '', 'SITE_ID': getrandbits(20), 'SECRET_KEY': encrypt(uuid.uuid4().hex, priv_key), 'ADMIN_INVOKE_NAME': 'admin.invoke', 'ADMIN_INVOKE_PASSWORD': encrypt( getattr(args, 'admin_invoke_password', None) or getattr(args, 'tech_account_password'), priv_key), } open(os.path.join(repo_dir, 'logging.conf'), 'w').write( common_logging_conf_contents.format( log_path='./logs/web-admin.log')) open(web_admin_conf_path, 'w').write(config_template.format(**config)) open(initial_data_json_path, 'w').write(initial_data_json.format(**config)) # Initial info self.store_initial_info(self.target_dir, self.COMPONENTS.WEB_ADMIN.code) config = json.loads( open(os.path.join(repo_dir, 'web-admin.conf')).read()) config['config_dir'] = self.target_dir update_globals(config, self.target_dir) os.environ['DJANGO_SETTINGS_MODULE'] = 'zato.admin.settings' import django django.setup() self.reset_logger(args, True) # Can't import these without DJANGO_SETTINGS_MODULE being set from django.contrib.auth.models import User from django.db import connection from django.db.utils import IntegrityError call_command('migrate', run_syncdb=True, interactive=False, verbosity=0) call_command('loaddata', initial_data_json_path, verbosity=0) try: call_command('createsuperuser', interactive=False, username=user_name, first_name='admin-first-name', last_name='admin-last-name', email='*****@*****.**') admin_created = True user = User.objects.get(username=user_name) user.set_password(password) user.save() except IntegrityError, e: admin_created = False connection._rollback() self.logger.info('Ignoring IntegrityError e:[%s]', format_exc(e).decode('utf-8'))
# ./my-services will resolve to /opt/zato/server1/my-services if a server has been # installed into /opt/zato/server1 # List your service sources below, each on a separate line. # Recommended to be always the very last line so all services that have been # hot-deployed are picked up last. ./work/hot-deploy/current # Visit https://zato.io/docs for more information.""" default_odb_pool_size = 1 directories = ('config', 'config/repo', 'config/zdaemon', 'logs', 'pickup-dir', 'profiler', 'work', 'work/hot-deploy', 'work/hot-deploy/current', 'work/hot-deploy/backup', 'work/hot-deploy/backup/last') files = {'config/repo/logging.conf':common_logging_conf_contents.format(log_path='./logs/server.log'), 'config/repo/service-sources.txt':service_sources_contents} priv_key_location = './config/repo/config-priv.pem' priv_key_location = './config/repo/config-pub.pem' class Create(ZatoCommand): """ Creates a new Zato server """ needs_empty_dir = True allow_empty_secrets = True opts = deepcopy(common_odb_opts) opts.extend(kvdb_opts) opts.append({'name':'pub_key_path', 'help':"Path to the server's public key in PEM"})
# ./my-services will resolve to /opt/zato/server1/my-services if a server has been # installed into /opt/zato/server1 # List your service sources below, each on a separate line. # Recommended to be always the very last line so all services that have been # hot-deployed are picked up last. ./work/hot-deploy/current # Visit https://zato.io/docs for more information.""" default_odb_pool_size = 1 directories = ('config', 'config/repo', 'config/zdaemon', 'pickup-dir', 'logs', 'work', 'work/hot-deploy', 'work/hot-deploy/current', 'work/hot-deploy/backup', 'work/hot-deploy/backup/last') files = {'config/repo/logging.conf':common_logging_conf_contents.format(log_path='./logs/server.log'), 'config/repo/service-sources.txt':service_sources_contents} priv_key_location = './config/repo/config-priv.pem' pub_key_location = './config/repo/config-pub.pem' class Create(ZatoCommand): """ Creates a new Zato server """ needs_empty_dir = True allow_empty_secrets = True opts = deepcopy(common_odb_opts) opts.extend(kvdb_opts) opts.append({'name':'pub_key_path', 'help':"Path to the server's public key in PEM"})
def execute(self, args, show_output=True, admin_password=None, needs_admin_created_flag=False): os.chdir(self.target_dir) repo_dir = os.path.join(self.target_dir, 'config', 'repo') web_admin_conf_path = os.path.join(repo_dir, 'web-admin.conf') initial_data_json_path = os.path.join(repo_dir, 'initial-data.json') os.mkdir(os.path.join(self.target_dir, 'logs')) os.mkdir(os.path.join(self.target_dir, 'config')) os.mkdir(repo_dir) user_name = 'admin' admin_password = admin_password if admin_password else WebAdminCryptoManager.generate_password() self.copy_web_admin_crypto(repo_dir, args) zato_secret_key = WebAdminCryptoManager.generate_key() cm = WebAdminCryptoManager.from_secret_key(zato_secret_key) django_secret_key = uuid4().hex.encode('utf8') django_site_id = getrandbits(20) admin_invoke_password = getattr(args, 'admin_invoke_password', None) or getattr(args, 'tech_account_password') admin_invoke_password = admin_invoke_password.encode('utf8') odb_password = args.odb_password or '' odb_password = odb_password.encode('utf8') config = { 'host': web_admin_host, 'port': web_admin_port, 'db_type': args.odb_type, 'log_config': 'logging.conf', 'zato_secret_key':zato_secret_key, 'well_known_data': cm.encrypt(well_known_data.encode('utf8')), 'DATABASE_NAME': args.odb_db_name or args.sqlite_path, 'DATABASE_USER': args.odb_user or '', 'DATABASE_PASSWORD': cm.encrypt(odb_password), 'DATABASE_HOST': args.odb_host or '', 'DATABASE_PORT': args.odb_port or '', 'SITE_ID': django_site_id, 'SECRET_KEY': cm.encrypt(django_secret_key), 'ADMIN_INVOKE_NAME':'admin.invoke', 'ADMIN_INVOKE_PASSWORD':cm.encrypt(admin_invoke_password), } for name in 'zato_secret_key', 'well_known_data', 'DATABASE_PASSWORD', 'SECRET_KEY', 'ADMIN_INVOKE_PASSWORD': config[name] = config[name].decode('utf8') open(os.path.join(repo_dir, 'logging.conf'), 'w').write( common_logging_conf_contents.format(log_path='./logs/web-admin.log')) open(web_admin_conf_path, 'w').write(config_template.format(**config)) open(initial_data_json_path, 'w').write(initial_data_json.format(**config)) # Initial info self.store_initial_info(self.target_dir, self.COMPONENTS.WEB_ADMIN.code) config = json.loads(open(os.path.join(repo_dir, 'web-admin.conf')).read()) config['config_dir'] = self.target_dir update_globals(config, self.target_dir) os.environ['DJANGO_SETTINGS_MODULE'] = 'zato.admin.settings' import django django.setup() self.reset_logger(args, True) # Can't import these without DJANGO_SETTINGS_MODULE being set from django.contrib.auth.models import User from django.db import connection from django.db.utils import IntegrityError call_command('migrate', run_syncdb=True, interactive=False, verbosity=0) call_command('loaddata', initial_data_json_path, verbosity=0) try: call_command( 'createsuperuser', interactive=False, username=user_name, first_name='admin-first-name', last_name='admin-last-name', email='*****@*****.**') admin_created = True user = User.objects.get(username=user_name) user.set_password(admin_password) user.save() except IntegrityError: # This will happen if user 'admin' already exists, e.g. if this is not the first cluster in this database admin_created = False connection._rollback() # Needed because Django took over our logging config self.reset_logger(args, True) if show_output: if self.verbose: msg = """Successfully created a web admin instance. You can start it with the 'zato start {path}' command.""".format(path=os.path.abspath(os.path.join(os.getcwd(), self.target_dir))) self.logger.debug(msg) else: self.logger.info('OK') # We return it only when told to explicitly so when the command runs from CLI # it doesn't return a non-zero exit code. if needs_admin_created_flag: return admin_created
def execute(self, args, show_output=True, password=None): os.chdir(self.target_dir) repo_dir = os.path.join(self.target_dir, 'config', 'repo') zato_admin_conf_path = os.path.join(repo_dir, 'zato-admin.conf') initial_data_json_path = os.path.join(repo_dir, 'initial-data.json') os.mkdir(os.path.join(self.target_dir, 'logs')) os.mkdir(os.path.join(self.target_dir, 'config')) os.mkdir(os.path.join(self.target_dir, 'config', 'zdaemon')) os.mkdir(repo_dir) user_name = 'admin' password = password if password else generate_password() self.copy_zato_admin_crypto(repo_dir, args) pub_key = open(os.path.join(repo_dir, 'zato-admin-pub-key.pem')).read() config = { 'host': zato_admin_host, 'port': zato_admin_port, 'db_type': args.odb_type, 'log_config': 'logging.conf', 'DATABASE_NAME': args.odb_db_name, 'DATABASE_USER': args.odb_user, 'DATABASE_PASSWORD': encrypt(args.odb_password, pub_key), 'DATABASE_HOST': args.odb_host, 'DATABASE_PORT': args.odb_port, 'SITE_ID': getrandbits(20), 'SECRET_KEY': encrypt(uuid.uuid4().hex, pub_key), 'TECH_ACCOUNT_NAME':args.tech_account_name, 'TECH_ACCOUNT_PASSWORD':encrypt(args.tech_account_password, pub_key), } open(os.path.join(repo_dir, 'logging.conf'), 'w').write(common_logging_conf_contents.format(log_path='./logs/zato-admin.log')) open(zato_admin_conf_path, 'w').write(config_template.format(**config)) open(initial_data_json_path, 'w').write(initial_data_json.format(**config)) # Initial info self.store_initial_info(self.target_dir, self.COMPONENTS.ZATO_ADMIN.code) config = json.loads(open(os.path.join(repo_dir, 'zato-admin.conf')).read()) config['config_dir'] = self.target_dir update_globals(config, self.target_dir) os.environ['DJANGO_SETTINGS_MODULE'] = 'zato.admin.settings' # Can't import these without DJANGO_SETTINGS_MODULE being set from django.contrib.auth.models import User from django.db import connection from django.db.utils import IntegrityError call_command('syncdb', interactive=False, verbosity=0) call_command('loaddata', initial_data_json_path, verbosity=0) try: call_command('createsuperuser', interactive=False, username=user_name, first_name='admin-first-name', last_name='admin-last-name', email='*****@*****.**') admin_created = True except IntegrityError, e: admin_created = False connection._rollback() msg = 'Ignoring IntegrityError e:[{}]'.format(format_exc(e)) self.logger.info(msg)
def execute(self, args, show_output=True, password=None, needs_admin_created_flag=False): os.chdir(self.target_dir) repo_dir = os.path.join(self.target_dir, 'config', 'repo') web_admin_conf_path = os.path.join(repo_dir, 'web-admin.conf') initial_data_json_path = os.path.join(repo_dir, 'initial-data.json') os.mkdir(os.path.join(self.target_dir, 'logs')) os.mkdir(os.path.join(self.target_dir, 'config')) os.mkdir(repo_dir) user_name = 'admin' password = password if password else generate_password() self.copy_web_admin_crypto(repo_dir, args) priv_key = open(os.path.join(repo_dir, 'web-admin-priv-key.pem')).read() config = { 'host': web_admin_host, 'port': web_admin_port, 'db_type': args.odb_type, 'log_config': 'logging.conf', 'DATABASE_NAME': args.odb_db_name or args.sqlite_path, 'DATABASE_USER': args.odb_user or '', 'DATABASE_PASSWORD': encrypt(args.odb_password, priv_key) if args.odb_password else '', 'DATABASE_HOST': args.odb_host or '', 'DATABASE_PORT': args.odb_port or '', 'SITE_ID': getrandbits(20), 'SECRET_KEY': encrypt(uuid.uuid4().hex, priv_key), 'ADMIN_INVOKE_NAME':'admin.invoke', 'ADMIN_INVOKE_PASSWORD':encrypt(getattr(args, 'admin_invoke_password', None) or getattr(args, 'tech_account_password'), priv_key), } open(os.path.join(repo_dir, 'logging.conf'), 'w').write(common_logging_conf_contents.format(log_path='./logs/web-admin.log')) open(web_admin_conf_path, 'w').write(config_template.format(**config)) open(initial_data_json_path, 'w').write(initial_data_json.format(**config)) # Initial info self.store_initial_info(self.target_dir, self.COMPONENTS.WEB_ADMIN.code) config = json.loads(open(os.path.join(repo_dir, 'web-admin.conf')).read()) config['config_dir'] = self.target_dir update_globals(config, self.target_dir) os.environ['DJANGO_SETTINGS_MODULE'] = 'zato.admin.settings' # Can't import these without DJANGO_SETTINGS_MODULE being set from django.contrib.auth.models import User from django.db import connection from django.db.utils import IntegrityError call_command('syncdb', interactive=False, verbosity=0) call_command('loaddata', initial_data_json_path, verbosity=0) try: call_command( 'createsuperuser', interactive=False, username=user_name, first_name='admin-first-name', last_name='admin-last-name', email='*****@*****.**') admin_created = True user = User.objects.get(username=user_name) user.set_password(password) user.save() except IntegrityError, e: admin_created = False connection._rollback() self.logger.info('Ignoring IntegrityError e:[%s]', format_exc(e).decode('utf-8'))
def execute(self, args, show_output=True, needs_created_flag=False): os.chdir(self.target_dir) repo_dir = os.path.join(self.target_dir, 'config', 'repo') conf_path = os.path.join(repo_dir, 'scheduler.conf') startup_jobs_conf_path = os.path.join(repo_dir, 'startup_jobs.conf') sql_conf_path = os.path.join(repo_dir, 'sql.conf') os.mkdir(os.path.join(self.target_dir, 'logs')) os.mkdir(os.path.join(self.target_dir, 'config')) os.mkdir(repo_dir) self.copy_scheduler_crypto(repo_dir, args) secret_key = args.get( 'secret_key') or SchedulerCryptoManager.generate_key() cm = SchedulerCryptoManager.from_secret_key(secret_key) config = { 'odb_db_name': args.odb_db_name or args.sqlite_path, 'odb_engine': args.odb_type, 'odb_host': args.odb_host or '', 'odb_port': args.odb_port or '', 'odb_password': cm.encrypt(args.odb_password) if args.odb_password else '', 'odb_username': args.odb_user or '', 'broker_host': args.kvdb_host, 'broker_port': args.kvdb_port, 'broker_password': cm.encrypt(args.kvdb_password) if args.kvdb_password else '', 'user1_password': cm.encrypt(cm.generate_password()), 'cluster_id': args.cluster_id, 'secret_key1': secret_key, 'well_known_data': cm.encrypt(well_known_data) } open(os.path.join(repo_dir, 'logging.conf'), 'w').write( common_logging_conf_contents.format( log_path='./logs/scheduler.log')) open(conf_path, 'w').write(config_template.format(**config)) open(startup_jobs_conf_path, 'w').write(startup_jobs) open(sql_conf_path, 'w').write(sql_conf_contents) # Initial info self.store_initial_info(self.target_dir, self.COMPONENTS.SCHEDULER.code) if show_output: if self.verbose: msg = """Successfully created a scheduler instance. You can start it with the 'zato start {path}' command.""".format( path=os.path.abspath( os.path.join(os.getcwd(), self.target_dir))) self.logger.debug(msg) else: self.logger.info('OK') # We return it only when told to explicitly so when the command runs from CLI # it doesn't return a non-zero exit code. if needs_created_flag: return True
def execute(self, args, show_output=True, admin_password=None, needs_admin_created_flag=False): try: import pymysql pymysql.install_as_MySQLdb() except ImportError: pass # stdlib import os, json from random import getrandbits from uuid import uuid4 # Django from django.core.management import call_command # Python 2/3 compatibility from past.builtins import unicode # Zato # TODO: There really shouldn't be any direct dependency between zato-cli and zato-web-admin from zato.admin.zato_settings import update_globals from zato.cli import common_logging_conf_contents, is_arg_given from zato.common.crypto.api import WebAdminCryptoManager from zato.common.crypto.const import well_known_data from zato.common.defaults import web_admin_host, web_admin_port os.chdir(self.target_dir) repo_dir = os.path.join(self.target_dir, 'config', 'repo') web_admin_conf_path = os.path.join(repo_dir, 'web-admin.conf') initial_data_json_path = os.path.join(repo_dir, 'initial-data.json') os.mkdir(os.path.join(self.target_dir, 'logs')) os.mkdir(os.path.join(self.target_dir, 'config')) os.mkdir(repo_dir) user_name = 'admin' admin_password = admin_password if admin_password else WebAdminCryptoManager.generate_password( ) # If we have a CA's certificate then it implicitly means that there is some CA # which tells us that we are to trust both the CA and the certificates that it issues, # and the only certificate we are interested in is the one to the load-balancer. # This is why, if we get ca_certs_path, it must be because we are to use TLS # in communication with the load-balancer's agent which in turn means that we have crypto material on input. has_crypto = is_arg_given(args, 'ca_certs_path') if has_crypto: self.copy_web_admin_crypto(repo_dir, args) zato_secret_key = WebAdminCryptoManager.generate_key() cm = WebAdminCryptoManager.from_secret_key(zato_secret_key) django_secret_key = uuid4().hex.encode('utf8') django_site_id = getrandbits(20) admin_invoke_password = getattr(args, 'admin_invoke_password', None) if not admin_invoke_password: admin_invoke_password = '******' + uuid4().hex if isinstance(admin_invoke_password, unicode): admin_invoke_password = admin_invoke_password.encode('utf8') odb_password = args.odb_password or '' odb_password = odb_password.encode('utf8') config = { 'host': web_admin_host, 'port': web_admin_port, 'db_type': args.odb_type, 'log_config': 'logging.conf', 'lb_agent_use_tls': 'true' if has_crypto else 'false', 'zato_secret_key': zato_secret_key, 'well_known_data': cm.encrypt(well_known_data.encode('utf8')), 'DATABASE_NAME': args.odb_db_name or args.sqlite_path, 'DATABASE_USER': args.odb_user or '', 'DATABASE_PASSWORD': cm.encrypt(odb_password), 'DATABASE_HOST': args.odb_host or '', 'DATABASE_PORT': args.odb_port or '', 'SITE_ID': django_site_id, 'SECRET_KEY': cm.encrypt(django_secret_key), 'ADMIN_INVOKE_NAME': 'admin.invoke', 'ADMIN_INVOKE_PASSWORD': cm.encrypt(admin_invoke_password), } for name in 'zato_secret_key', 'well_known_data', 'DATABASE_PASSWORD', 'SECRET_KEY', 'ADMIN_INVOKE_PASSWORD': config[name] = config[name].decode('utf8') open(os.path.join(repo_dir, 'logging.conf'), 'w').write( common_logging_conf_contents.format( log_path='./logs/web-admin.log')) open(web_admin_conf_path, 'w').write(config_template.format(**config)) open(initial_data_json_path, 'w').write(initial_data_json.format(**config)) # Initial info self.store_initial_info(self.target_dir, self.COMPONENTS.WEB_ADMIN.code) config = json.loads( open(os.path.join(repo_dir, 'web-admin.conf')).read()) config['config_dir'] = self.target_dir update_globals(config, self.target_dir) os.environ['DJANGO_SETTINGS_MODULE'] = 'zato.admin.settings' import django django.setup() self.reset_logger(args, True) # Can't import these without DJANGO_SETTINGS_MODULE being set from django.contrib.auth.models import User from django.db import connection from django.db.utils import IntegrityError call_command('migrate', run_syncdb=True, interactive=False, verbosity=0) call_command('loaddata', initial_data_json_path, verbosity=0) try: call_command('createsuperuser', interactive=False, username=user_name, first_name='admin-first-name', last_name='admin-last-name', email='*****@*****.**') admin_created = True user = User.objects.get(username=user_name) user.set_password(admin_password) user.save() except IntegrityError: # This will happen if user 'admin' already exists, e.g. if this is not the first cluster in this database admin_created = False connection._rollback() # Needed because Django took over our logging config self.reset_logger(args, True) if show_output: if self.verbose: msg = """Successfully created a web admin instance. You can start it with the 'zato start {path}' command.""".format( path=os.path.abspath( os.path.join(os.getcwd(), self.target_dir))) self.logger.debug(msg) else: self.logger.info('OK') # We return it only when told to explicitly so when the command runs from CLI # it doesn't return a non-zero exit code. if needs_admin_created_flag: return admin_created
def execute(self, args): config = { 'host': args.broker_host, 'start_port': args.broker_start_port, 'log_config': 'logging.conf', 'token': self.token } os.mkdir(os.path.join(self.target_dir)) os.mkdir(os.path.join(self.target_dir, 'config')) os.mkdir(os.path.join(self.target_dir, 'config/repo')) os.mkdir(os.path.join(self.target_dir, 'config/zdaemon')) os.mkdir(os.path.join(self.target_dir, 'logs')) open(os.path.join(self.target_dir, 'config/repo', 'logging.conf'), 'w').write(common_logging_conf_contents.format(log_path='../../logs/broker.log')) open(os.path.join(self.target_dir, 'config/repo', 'broker.conf'), 'w').write(config_template.format(**config)) open(os.path.join(self.target_dir, ZATO_BROKER_DIR), 'w').close() msg = """\nSuccessfully created a Zato broker. You can now go to {path} and start it with the 'zato start .' command. """.format(path=os.path.abspath(os.path.join(os.getcwd(), self.target_dir))) print(msg)