def init(): # Create the necessary supporting directories create_directory(get_log_directory()) create_directory(get_repository_directory()) create_directory(get_temp_directory()) create_directory(get_migration_directory()) create_directory(get_doc_central_directory()) if not is_ldap_supported(): print('LDAP authentication is not supported because it has not been installed.')
def home(): if request.method == 'POST': file = request.files['file'] if file: filename = secure_filename(file.filename) file.save(os.path.join(get_temp_directory(), filename)) custom_command_profile_form = CustomCommandProfileForm(request.form) return render_template('custom_command/custom_command_profile.html', form=custom_command_profile_form)
def init(): # Create the necessary supporting directories create_directory(get_log_directory()) create_directory(get_repository_directory()) create_directory(get_temp_directory()) create_directory(get_migration_directory()) create_directory(get_doc_central_directory()) if not is_ldap_supported(): print( 'LDAP authentication is not supported because it has not been installed.' )
def api_export_conformance_report(id): locale_datetime = request.args.get('locale_datetime') include_host_packages = request.args.get('include_host_packages') db_session = DBSession() filename = get_temp_directory() + current_user.username + '_report.xls' conformance_report = get_conformance_report_by_id(db_session, id) if conformance_report is not None: xls_writer = XLSWriter(conformance_report, filename, locale_datetime=locale_datetime, include_host_packages=bool(int(include_host_packages))) xls_writer.write_report() return send_file(filename, as_attachment=True)
def api_export_conformance_report(id): locale_datetime = request.args.get('locale_datetime') include_host_packages = request.args.get('include_host_packages') db_session = DBSession() filename = get_temp_directory() + current_user.username + '_report.xls' conformance_report = get_conformance_report_by_id(db_session, id) if conformance_report is not None: xls_writer = XLSWriter(conformance_report, filename, locale_datetime=locale_datetime, include_host_packages=bool( int(include_host_packages))) xls_writer.write_report() return send_file(filename, as_attachment=True)
def start(self, db_session, logger, process_name): self.db_session = db_session try: self.create_tar_job = self.db_session.query(CreateTarJob).filter(CreateTarJob.id == self.job_id).first() if self.create_tar_job is None: logger.error('Unable to retrieve create tar job: %s' % self.job_id) return self.create_tar_job.set_status(JobStatus.PROCESSING) server_id = self.create_tar_job.server_id server_directory = self.create_tar_job.server_directory source_tars = self.create_tar_job.source_tars contents = self.create_tar_job.contents additional_packages = self.create_tar_job.additional_packages new_tar_name = self.create_tar_job.new_tar_name created_by = self.create_tar_job.created_by date_string = datetime.datetime.utcnow().strftime("%Y_%m_%d_%H_%M_%S") repo_dir = get_repository_directory() temp_path = get_temp_directory() + str(date_string) new_tar_path = os.path.join(temp_path, str(date_string)) try: if not os.path.exists(temp_path): self.create_tar_job.set_status('Creating temporary directories.') self.db_session.commit() os.makedirs(temp_path) os.makedirs(new_tar_path, 7777) # Untar source tars into the temp/timestamp directory if source_tars: self.create_tar_job.set_status('Extracting from source tar files.') self.db_session.commit() for source in source_tars.split(','): with tarfile.open(os.path.join(repo_dir, source)) as tar: tar.extractall(temp_path) # Copy the selected contents from the temp/timestamp directory # to the new tar directory if contents: self.create_tar_job.set_status('Copying selected tar contents.') self.db_session.commit() for f in contents.strip().split(','): _, filename = os.path.split(f) shutil.copy2(os.path.join(temp_path, filename), new_tar_path) # Copy the selected additional packages from the repository to the new tar directory if additional_packages: self.create_tar_job.set_status('Copying selected additional files.') for pkg in additional_packages.split(','): self.db_session.commit() shutil.copy2(os.path.join(repo_dir, pkg), new_tar_path) self.create_tar_job.set_status('Tarring new file.') self.db_session.commit() tarname = os.path.join(temp_path, new_tar_name) shutil.make_archive(tarname, format='tar', root_dir=new_tar_path) make_file_writable(os.path.join(new_tar_path, tarname) + '.tar') server = self.db_session.query(Server).filter(Server.id == server_id).first() if server is not None: self.create_tar_job.set_status('Uploading to external repository.') self.db_session.commit() server_impl = get_server_impl(server) # If the new file already exists on the remote host, delete it if new_tar_name in server_impl.get_file_list(): server_impl.delete_file(new_tar_name) statinfo = os.stat(tarname + '.tar') self.new_tar_size = statinfo.st_size self.chunk_list = self.get_chunks(self.new_tar_size, self.new_tar_size / 1048576) if isinstance(server_impl, FTPServer): server_impl.upload_file(tarname + '.tar', new_tar_name + ".tar", sub_directory=server_directory, callback=self.ftp_progress_listener) elif isinstance(server_impl, SFTPServer): server_impl.upload_file(tarname + '.tar', new_tar_name + ".tar", sub_directory=server_directory, callback=self.sftp_progress_listener) else: server_impl.upload_file(tarname + '.tar', new_tar_name + ".tar", sub_directory=server_directory) shutil.rmtree(temp_path, onerror=self.handleRemoveReadonly) self.create_tar_job.set_status(JobStatus.COMPLETED) self.db_session.commit() except Exception: self.create_tar_job.set_status(JobStatus.FAILED) self.db_session.commit() logger.exception('Exception while creating %s requested by %s - job id = %s', new_tar_name, created_by, self.job_id) shutil.rmtree(temp_path, onerror=self.handleRemoveReadonly) os.remove(temp_path + '.tar') finally: self.db_session.close()
def home(): if not can_install(current_user): abort(401) msg = '' # Software Profile import if request.method == 'POST': file = request.files['file'] if file: if not allowed_file(file.filename): msg = "Incorrect file format -- " + file.filename + " must be .json" else: file_path = os.path.join(get_temp_directory(), "software_profiles.json") file.save(file_path) failed = "" with open(file_path, 'r') as f: try: s = json.load(f) except: msg = "Incorrect file format -- " + file.filename + " must be a valid JSON file." flash(msg, 'import_feedback') return redirect(url_for(".home")) if "CSM Server:Software Profile" not in s.keys(): msg = file.filename + " is not in the correct Software Profile format." else: db_session = DBSession profiles = [p for (p,) in DBSession().query(SoftwareProfile.name).all()] d = s["CSM Server:Software Profile"] for software_profile_name in d.keys(): name = '' if software_profile_name in profiles: name = software_profile_name # Will keep appending ' - copy' until it hits a unique name while name in profiles: name += " - copy" msg += software_profile_name + ' -> ' + name + '\n' profiles.append(name) if len(name) < 100 and len(software_profile_name) < 100: try: profile = SoftwareProfile( name=name if name else software_profile_name, packages=d[software_profile_name], created_by=current_user.username ) db_session.add(profile) db_session.commit() except: failed += software_profile_name + '\n' else: failed += software_profile_name + ' (name too long)\n' if msg: msg = "The following profiles already exist and will try to be imported under modified " \ "names:\n\n" + msg + '\n' if failed: msg += 'The following profiles failed to import:\n\n' + failed elif failed: msg = 'The following profiles failed to import:\n\n' + failed else: msg = "Software Profile import was successful!" # delete file os.remove(file_path) flash(msg, 'import_feedback') return redirect(url_for(".home")) db_session = DBSession() conformance_form = ConformanceForm(request.form) assign_software_profile_to_hosts_form = AssignSoftwareProfileToHostsForm(request.form) view_host_software_profile_form = ViewHostSoftwareProfileForm(request.form) conformance_report_dialog_form = ConformanceReportDialogForm(request.form) make_conform_dialog_form = MakeConformDialogForm(request.form) fill_custom_command_profiles(db_session, make_conform_dialog_form.custom_command_profile.choices) select_server_form = SelectServerForm(request.form) export_conformance_report_form = ExportConformanceReportForm(request.form) export_conformance_report_form.include_host_packages.data = True export_conformance_report_form.exclude_conforming_hosts.data = True return render_template('conformance/index.html', conformance_form=conformance_form, assign_software_profile_to_hosts_form=assign_software_profile_to_hosts_form, view_host_software_profile_form=view_host_software_profile_form, conformance_report_dialog_form=conformance_report_dialog_form, install_actions=[InstallAction.PRE_UPGRADE, InstallAction.INSTALL_ADD, InstallAction.INSTALL_ACTIVATE, InstallAction.POST_UPGRADE, InstallAction.INSTALL_COMMIT, InstallAction.ALL], make_conform_dialog_form=make_conform_dialog_form, export_conformance_report_form=export_conformance_report_form, select_server_form=select_server_form, server_time=datetime.datetime.utcnow(), system_option=SystemOption.get(DBSession()))
def create_temp_user_directory(username): if not os.path.isdir(os.path.join(get_temp_directory(), username)): os.makedirs(os.path.join(get_temp_directory(), username)) make_file_writable(os.path.join(get_temp_directory(), username)) return os.path.join(get_temp_directory(), username)
def log_directory(self): return get_temp_directory()
# ============================================================================= from models import initialize from models import SystemVersion from sqlalchemy import inspect from database import DBSession, CURRENT_SCHEMA_VERSION, engine from utils import create_directory, is_ldap_supported from constants import get_log_directory, get_repository_directory, get_temp_directory from schema.loader import get_schema_migrate_class import traceback # Create the necessary supporting directories create_directory(get_log_directory()) create_directory(get_repository_directory()) create_directory(get_temp_directory()) def init(): if not is_ldap_supported(): print('LDAP authentication is not supported because it has not been installed.') db_session = DBSession() system_version = SystemVersion.get(db_session) # Handles database schema migration starting from the next schema version for version in range(system_version.schema_version + 1, CURRENT_SCHEMA_VERSION + 1): handler_class = get_schema_migrate_class(version) if handler_class is not None: try: handler_class(version).execute()
def home(): if not can_install(current_user): abort(401) msg = '' # Software Profile import if request.method == 'POST': file = request.files['file'] if file: if not allowed_file(file.filename): msg = "Incorrect file format -- " + file.filename + " must be .json" else: file_path = os.path.join(get_temp_directory(), "software_profiles.json") file.save(file_path) failed = "" with open(file_path, 'r') as f: try: s = json.load(f) except: msg = "Incorrect file format -- " + file.filename + " must be a valid JSON file." flash(msg, 'import_feedback') return redirect(url_for(".home")) if "CSM Server:Software Profile" not in s.keys(): msg = file.filename + " is not in the correct Software Profile format." else: db_session = DBSession profiles = [ p for (p, ) in DBSession().query( SoftwareProfile.name).all() ] d = s["CSM Server:Software Profile"] for software_profile_name in d.keys(): name = '' if software_profile_name in profiles: name = software_profile_name # Will keep appending ' - copy' until it hits a unique name while name in profiles: name += " - copy" msg += software_profile_name + ' -> ' + name + '\n' profiles.append(name) if len(name) < 100 and len( software_profile_name) < 100: try: profile = SoftwareProfile( name=name if name else software_profile_name, packages=d[software_profile_name], created_by=current_user.username) db_session.add(profile) db_session.commit() except: failed += software_profile_name + '\n' else: failed += software_profile_name + ' (name too long)\n' if msg: msg = "The following profiles already exist and will try to be imported under modified " \ "names:\n\n" + msg + '\n' if failed: msg += 'The following profiles failed to import:\n\n' + failed elif failed: msg = 'The following profiles failed to import:\n\n' + failed else: msg = "Software Profile import was successful!" # delete file os.remove(file_path) flash(msg, 'import_feedback') return redirect(url_for(".home")) db_session = DBSession() conformance_form = ConformanceForm(request.form) assign_software_profile_to_hosts_form = AssignSoftwareProfileToHostsForm( request.form) view_host_software_profile_form = ViewHostSoftwareProfileForm(request.form) conformance_report_dialog_form = ConformanceReportDialogForm(request.form) make_conform_dialog_form = MakeConformDialogForm(request.form) fill_custom_command_profiles( db_session, make_conform_dialog_form.custom_command_profile.choices) select_server_form = SelectServerForm(request.form) export_conformance_report_form = ExportConformanceReportForm(request.form) export_conformance_report_form.include_host_packages.data = True export_conformance_report_form.exclude_conforming_hosts.data = True return render_template( 'conformance/index.html', conformance_form=conformance_form, assign_software_profile_to_hosts_form= assign_software_profile_to_hosts_form, view_host_software_profile_form=view_host_software_profile_form, conformance_report_dialog_form=conformance_report_dialog_form, install_actions=[ InstallAction.PRE_UPGRADE, InstallAction.INSTALL_ADD, InstallAction.INSTALL_ACTIVATE, InstallAction.POST_UPGRADE, InstallAction.INSTALL_COMMIT, InstallAction.ALL ], make_conform_dialog_form=make_conform_dialog_form, export_conformance_report_form=export_conformance_report_form, select_server_form=select_server_form, server_time=datetime.datetime.utcnow(), system_option=SystemOption.get(DBSession()))
# By importing models here, it forces creation of tables in the database for a new installation. # This will prevent gunicorn workers from trying to create the database tables all at the same time. # See csmserver launch script import models from utils import create_directory from constants import get_autlogs_directory, get_repository_directory, get_temp_directory # Create the necessary supporting directories create_directory(get_autlogs_directory()) create_directory(get_repository_directory()) create_directory(get_temp_directory())
def start(self, db_session, logger, process_name): self.db_session = db_session try: self.create_tar_job = self.db_session.query(CreateTarJob).filter( CreateTarJob.id == self.job_id).first() if self.create_tar_job is None: logger.error('Unable to retrieve create tar job: %s' % self.job_id) return self.create_tar_job.set_status(JobStatus.PROCESSING) server_id = self.create_tar_job.server_id server_directory = self.create_tar_job.server_directory source_tars = self.create_tar_job.source_tars contents = self.create_tar_job.contents additional_packages = self.create_tar_job.additional_packages new_tar_name = self.create_tar_job.new_tar_name created_by = self.create_tar_job.created_by date_string = datetime.datetime.utcnow().strftime( "%Y_%m_%d_%H_%M_%S") repo_dir = get_repository_directory() temp_path = get_temp_directory() + str(date_string) new_tar_path = os.path.join(temp_path, str(date_string)) try: if not os.path.exists(temp_path): self.create_tar_job.set_status( 'Creating temporary directories.') self.db_session.commit() os.makedirs(temp_path) os.makedirs(new_tar_path, 7777) # Untar source tars into the temp/timestamp directory if source_tars: self.create_tar_job.set_status( 'Extracting from source tar files.') self.db_session.commit() for source in source_tars.split(','): with tarfile.open(os.path.join(repo_dir, source)) as tar: tar.extractall(temp_path) # Copy the selected contents from the temp/timestamp directory # to the new tar directory if contents: self.create_tar_job.set_status( 'Copying selected tar contents.') self.db_session.commit() for f in contents.strip().split(','): _, filename = os.path.split(f) shutil.copy2(os.path.join(temp_path, filename), new_tar_path) # Copy the selected additional packages from the repository to the new tar directory if additional_packages: self.create_tar_job.set_status( 'Copying selected additional files.') for pkg in additional_packages.split(','): self.db_session.commit() shutil.copy2(os.path.join(repo_dir, pkg), new_tar_path) self.create_tar_job.set_status('Tarring new file.') self.db_session.commit() tarname = os.path.join(temp_path, new_tar_name) shutil.make_archive(tarname, format='tar', root_dir=new_tar_path) make_file_writable( os.path.join(new_tar_path, tarname) + '.tar') server = self.db_session.query(Server).filter( Server.id == server_id).first() if server is not None: self.create_tar_job.set_status( 'Uploading to external repository.') self.db_session.commit() server_impl = get_server_impl(server) # If the new file already exists on the remote host, delete it if new_tar_name in server_impl.get_file_list(): server_impl.delete_file(new_tar_name) statinfo = os.stat(tarname + '.tar') self.new_tar_size = statinfo.st_size self.chunk_list = self.get_chunks( self.new_tar_size, self.new_tar_size / 1048576) if isinstance(server_impl, FTPServer): server_impl.upload_file( tarname + '.tar', new_tar_name + ".tar", sub_directory=server_directory, callback=self.ftp_progress_listener) elif isinstance(server_impl, SFTPServer): server_impl.upload_file( tarname + '.tar', new_tar_name + ".tar", sub_directory=server_directory, callback=self.sftp_progress_listener) else: server_impl.upload_file(tarname + '.tar', new_tar_name + ".tar", sub_directory=server_directory) shutil.rmtree(temp_path, onerror=self.handleRemoveReadonly) self.create_tar_job.set_status(JobStatus.COMPLETED) self.db_session.commit() except Exception: self.create_tar_job.set_status(JobStatus.FAILED) self.db_session.commit() logger.exception( 'Exception while creating %s requested by %s - job id = %s', new_tar_name, created_by, self.job_id) shutil.rmtree(temp_path, onerror=self.handleRemoveReadonly) os.remove(temp_path + '.tar') finally: self.db_session.close()
def home(): msg = '' # Custom Command Profile import if request.method == 'POST': file = request.files['file'] if file: if not allowed_file(file.filename): msg = "Incorrect file format -- " + file.filename + " must be .json" else: file_path = os.path.join(get_temp_directory(), "custom_command_profiles.json") file.save(file_path) failed = "" with open(file_path, 'r') as f: try: s = json.load(f) except: msg = "Incorrect file format -- " + file.filename + " must be a valid JSON file." flash(msg, 'import_feedback') return redirect(url_for(".home")) if "CSM Server:Custom Command Profile" not in s.keys(): msg = file.filename + " is not in the correct Custom Command Profile format." else: db_session = DBSession custom_profiles = [p for (p,) in DBSession().query(CustomCommandProfile.profile_name).all()] d = s["CSM Server:Custom Command Profile"] for profile_name in d.keys(): name = '' if profile_name in custom_profiles: name = profile_name # Will keep appending ' - copy' until it hits a unique name while name in custom_profiles: name += " - copy" msg += profile_name + ' -> ' + name + '\n' custom_profiles.append(name) if len(name) < 100 and len(profile_name) < 100: try: profile = CustomCommandProfile( profile_name=name if name else profile_name, command_list=d[profile_name], created_by=current_user.username ) db_session.add(profile) db_session.commit() except: failed += profile_name + '\n' else: failed += profile_name + ' (name too long)\n' if msg: msg = "The following profiles already exist and will try to be imported under modified names:\n\n" + \ msg + '\n' if failed: msg += 'The following profiles failed to import:\n\n' + failed elif failed: msg = 'The following profiles failed to import:\n\n' + failed else: msg = "Custom Command Profile import was successful!" # delete file os.remove(file_path) flash(msg, 'import_feedback') return redirect(url_for(".home")) custom_command_profile_form = CustomCommandProfileForm(request.form) return render_template('custom_command/custom_command_profile.html', form=custom_command_profile_form)