def backup_previous_token_dir(self): """ Backs up the previous token database :return: """ if os.path.exists(self.SOFTHSM_DB_DIR): util.make_or_verify_dir(self.SOFTHSM_DB_BACKUP_DIR) backup_slot_dir = util.dir_backup(self.SOFTHSM_DB_DIR, chmod=None, backup_dir=self.SOFTHSM_DB_BACKUP_DIR) return backup_slot_dir return None
def backup_current_config_file(self): """ Copies current configuration file to a new file - backup. softhsm.conf -> 0001_softhsm.conf Used when generating a new SoftHSM configuration file, to preserve the old one if user accidentally reinitializes the system. :return: """ cur_name = self.CONFIG_FILE if os.path.exists(cur_name): util.make_or_verify_dir(self.CONFIG_FILE_BACKUP_DIR) return util.file_backup(cur_name, chmod=None, backup_dir=self.CONFIG_FILE_BACKUP_DIR) return None
def init_token(self, user=None): """ Initializes a new SoftHSM token created by the configuration :param user: user to initialize token under :return: """ util.make_or_verify_dir(self.SOFTHSM_DB_DIR, mode=0o755) cmd = 'softhsm --init-token --slot 0 --pin 0000 --so-pin 0000 --label ejbca' if user is None: out, err = util.run_script(cmd.split(' ')) return out, err else: util.chown(self.SOFTHSM_DB_DIR, user, user) cmd_sudo = ['sudo', '-E', '-H', '-u', user, '/bin/bash', '-c', cmd] return util.run_script(cmd_sudo)
def _configure(self): """ Configures supervisord after manual installation :return: """ cmd_prep = '%s echo_supervisord_conf > %s' % ( self.sysconfig.epiper_path(), self.CONFIG_FILE) ret = self.sysconfig.exec_shell(cmd_prep) if ret != 0: raise errors.SetupError( 'Could not initialize supervisord config file') if not os.path.exists(self.CONFIG_FILE_DIR): util.make_or_verify_dir(self.CONFIG_FILE_DIR) with open(self.CONFIG_FILE, 'a') as fh: fh.write('\n') fh.write('[include]\n') fh.write('files = /etc/supervisord.d/*.conf\n\n')
def _install_extauth(self): """ Installs external authentication plugin :return: """ url = self._file_extauth base_file = 'extauth-nc.tgz' try: logger.debug('Going to download Ejabberd/extauth') tmpdir = util.safe_new_dir('/tmp/ejabberd-extauth-install') archive_path = os.path.join(tmpdir, base_file) try: self.audit.audit_evt('prov-extauth', url=url) self._download_file(url, archive_path, attempts=3) unpacked_dir = util.untar_get_single_dir( archive_path, self.sysconfig) if os.path.exists(self._extauth_path): shutil.rmtree(self._extauth_path) shutil.move(unpacked_dir, self._extauth_path) # Setup log dir for ext auth if not os.path.exists(self._extauth_log_dir): util.make_or_verify_dir(self._extauth_log_dir) self.sysconfig.chown_recursive(self._extauth_log_dir, self._user, self._group, throw_on_error=False) finally: if os.path.exists(tmpdir): shutil.rmtree(tmpdir) return 0 except Exception as e: logger.debug('Exception when fetching Ejabberd/extauth: %s' % e) self.audit.audit_exception(e) raise errors.SetupError('Could not install Ejabberd/extauth', cause=e)
def backup_database(self, database_name, backup_dir): """ Backups given database to the backup dir. Uses mysqldump command to create SQL dump. :param database_name: :param backup_dir: :return: """ util.make_or_verify_dir(backup_dir) db_fpath = os.path.abspath( os.path.join(backup_dir, database_name + '.sql')) fh, backup_file = util.safe_create_with_backup(db_fpath, mode='w', chmod=0o600) fh.close() self.audit.add_secrets(self.get_root_password()) cmd = 'sudo mysqldump --database \'%s\' -u \'%s\' -p\'%s\' > \'%s\'' \ % (database_name, 'root', self.get_root_password(), db_fpath) return self.sysconfig.exec_shell(cmd)
def _install_composer(self): """ Installs PHP composer :return: """ tmpdir = os.path.join('/tmp', 'tmp-composer.%08d' % random.randint(0, 2**31)) if os.path.exists(tmpdir): shutil.rmtree(tmpdir) util.make_or_verify_dir(tmpdir) composer_installer = os.path.join(tmpdir, 'composer-setup.php') cmd = 'curl -s "%s" > "%s"' % ('https://getcomposer.org/installer', composer_installer) ret, out, err = self.sysconfig.cli_cmd_sync(cmd, cwd=tmpdir) if ret != 0: raise errors.SetupError('Could not download composer') cmd = 'sudo php "%s" --install-dir=/bin --filename=composer' % composer_installer ret, out, err = self.sysconfig.cli_cmd_sync(cmd, cwd=tmpdir) if ret != 0: raise errors.SetupError('Could not install composer') shutil.rmtree(tmpdir)