def post_install() -> None: if not os.geteuid() == 0: sys.exit('Script must be run as root') app = create_app(parse_options()) config_path = os.path.join('/', 'etc', 'gitlab-tools', 'config.yml') configuration = {} if os.path.isfile(config_path): with open(config_path) as f: loaded_data = yaml.load(f) if isinstance(loaded_data, dict): configuration.update(loaded_data) if not configuration.get('USER') and OPTIONS['--user']: app.config['USER'] = configuration['USER'] = OPTIONS['--user'] # Generate database and config if nothing is specified if 'SQLALCHEMY_DATABASE_URI' not in configuration or not configuration[ 'SQLALCHEMY_DATABASE_URI']: database_path = 'sqlite:///{}/gitlab-tools.db'.format( get_home_dir(app.config['USER'])) configuration['SQLALCHEMY_DATABASE_URI'] = database_path # We need to set DB config to make stamp work app.config['SQLALCHEMY_DATABASE_URI'] = configuration[ 'SQLALCHEMY_DATABASE_URI'] # Create empty database with app.app_context(): db.create_all() with app.app_context(): stamp() # Generate secret key if 'SECRET_KEY' not in configuration or not configuration['SECRET_KEY']: app.config['SECRET_KEY'] = configuration[ 'SECRET_KEY'] = random_password() # Set port and host if 'HOST' not in configuration or not configuration['HOST']: configuration['HOST'] = '0.0.0.0' if 'PORT' not in configuration or not configuration['PORT']: configuration['PORT'] = 80 # Write new configuration with open(config_path, 'w') as f: yaml.dump(configuration, f, default_flow_style=False, allow_unicode=True)
def new_mirror(): form = NewForm(flask.request.form, is_force_update=False, is_prune_mirrors=False) if flask.request.method == 'POST' and form.validate(): project_mirror_str = form.project_mirror.data.strip() project_mirror = GitRemote(project_mirror_str) target = GitRemote(project_mirror_str) if target.vcs_protocol == ProtocolEnum.SSH: # If protocol is SSH we need to convert URL to use USER RSA pair target = GitRemote( convert_url_for_user(project_mirror_str, current_user)) mirror_new = PushMirror() # PushMirror mirror_new.project_mirror = project_mirror_str mirror_new.project = process_project(form.project.data) # Mirror mirror_new.is_force_update = form.is_force_update.data mirror_new.is_prune_mirrors = form.is_prune_mirrors.data mirror_new.is_deleted = False mirror_new.user = current_user mirror_new.foreign_vcs_type = target.vcs_type mirror_new.note = form.note.data mirror_new.target = target.url mirror_new.source = None # We are getting source wia gitlab API mirror_new.last_sync = None mirror_new.hook_token = random_password() db.session.add(mirror_new) db.session.commit() if target.vcs_protocol == ProtocolEnum.SSH: # If target is SSH, create SSH Config for it also task_result = chain( create_ssh_config.si(current_user.id, target.hostname, project_mirror.hostname), save_push_mirror.si(mirror_new.id)).apply_async() parent = log_task_pending(task_result.parent, mirror_new, create_ssh_config, InvokedByEnum.MANUAL) log_task_pending(task_result, mirror_new, save_push_mirror, InvokedByEnum.MANUAL, parent) else: task = save_push_mirror.delay(mirror_new.id) log_task_pending(task, mirror_new, save_push_mirror, InvokedByEnum.MANUAL) flask.flash('New push mirror item was added successfully.', 'success') return flask.redirect(flask.url_for('push_mirror.index.get_mirror')) return flask.render_template('push_mirror.index.new.html', form=form)
def request_login(): state = random_password() new_oauth_state = OAuth2State() new_oauth_state.state = state db.session.add(new_oauth_state) redirect_url = '{}?{}'.format( os.path.join(flask.current_app.config['GITLAB_URL'], 'oauth', 'authorize'), urllib.parse.urlencode({ 'client_id': flask.current_app.config['GITLAB_APP_ID'], 'redirect_uri': flask.url_for('sign.index.do_login', _external=True), 'response_type': 'code', 'state': state })) db.session.commit() return flask.redirect(redirect_url)
def new_mirror(): form = NewForm( flask.request.form, is_no_create=False, is_force_create=False, is_no_remote=False, is_issues_enabled=False, is_wall_enabled=False, is_wiki_enabled=False, is_snippets_enabled=False, is_merge_requests_enabled=False, visibility=PullMirror.VISIBILITY_PRIVATE, is_force_update=False, is_prune_mirrors=False, is_jobs_enabled=True ) if flask.request.method == 'POST' and form.validate(): project_mirror_str = form.project_mirror.data.strip() project_mirror = GitRemote(project_mirror_str) source = GitRemote(project_mirror_str) if source.vcs_protocol == ProtocolEnum.SSH: # If protocol is SSH we need to convert URL to use USER RSA pair source = GitRemote(convert_url_for_user(project_mirror_str, current_user)) mirror_new = PullMirror() # PullMirror mirror_new.project_name = form.project_name.data mirror_new.project_mirror = project_mirror_str mirror_new.is_no_create = form.is_no_create.data mirror_new.is_force_create = form.is_force_create.data mirror_new.is_no_remote = form.is_no_remote.data mirror_new.is_issues_enabled = form.is_issues_enabled.data mirror_new.is_wall_enabled = form.is_wall_enabled.data mirror_new.is_wiki_enabled = form.is_wiki_enabled.data mirror_new.is_snippets_enabled = form.is_snippets_enabled.data mirror_new.is_jobs_enabled = form.is_jobs_enabled.data mirror_new.is_merge_requests_enabled = form.is_merge_requests_enabled.data mirror_new.visibility = form.visibility.data mirror_new.group = process_group(form.group.data) mirror_new.periodic_sync = form.periodic_sync.data # Mirror mirror_new.is_force_update = form.is_force_update.data mirror_new.is_prune_mirrors = form.is_prune_mirrors.data mirror_new.is_deleted = False mirror_new.user = current_user mirror_new.foreign_vcs_type = source.vcs_type mirror_new.note = form.note.data mirror_new.target = None # We are getting target wia gitlab API mirror_new.source = source.url mirror_new.last_sync = None mirror_new.hook_token = random_password() if process_cron_expression(mirror_new): PeriodicTasks.changed() db.session.add(mirror_new) db.session.commit() if source.vcs_protocol == ProtocolEnum.SSH: # If source is SSH, create SSH Config for it also task_result = chain( create_ssh_config.si( current_user.id, source.hostname, project_mirror.hostname ), save_pull_mirror.si( mirror_new.id ) ).apply_async() parent = log_task_pending(task_result.parent, mirror_new, create_ssh_config, InvokedByEnum.MANUAL) log_task_pending(task_result, mirror_new, save_pull_mirror, InvokedByEnum.MANUAL, parent) else: task = save_pull_mirror.delay(mirror_new.id) log_task_pending(task, mirror_new, save_pull_mirror, InvokedByEnum.MANUAL) flask.flash('New pull mirror item was added successfully.', 'success') return flask.redirect(flask.url_for('pull_mirror.index.get_mirror')) return flask.render_template('pull_mirror.index.new.html', form=form)