#6949 Some scripts for orphan/ownership management.
Merged 7 years ago by mohanboddu. Opened 7 years ago by ralph.

empty or binary file added
@@ -0,0 +1,49 @@ 

+ #! /usr/bin/python -tt

+ """ Give a package in pagure-on-dist-git from one user to another.

+ 

+ This can also be used to give the package to the 'orphan' user.

+ 

+ You need a privileged pagure token in /etc/fedrepo_req/config.ini

+ 

+     [admin]

+     pagure_api_token = something secret

+ 

+ You can generate such a token on pkgs02 with:

+ 

+     $ PAGURE_CONFIG=/etc/pagure/pagure.cfg pagure-admin admin-token --help

+ """

+ # Copyright (c) 2017 Red Hat

+ # SPDX-License-Identifier:	GPL-2.0

+ #

+ # Authors:

+ #     Ralph Bean <rbean@redhat.com>

+ 

+ import argparse

+ import sys

+ 

+ try:

+     import utilities

+ except ImportError:

+     print("Try setting PYTHONPATH to find the utilities.py file.")

+     raise

+ 

+ PAGURE_URL = 'https://src.fedoraproject.org/api/0/'

+ 

+ 

+ def main():

+     parser = argparse.ArgumentParser(usage=__doc__)

+     parser.add_argument("package", help="The package that should be given.")

+     parser.add_argument("custodian", help="The user taking over the package.")

+     args = parser.parse_args()

+     session = utilities.retry_session()

+     try:

+         namespace, package = args.package.split('/')

+     except:

+         print("Package must be like <namespace>/<name>, not %r" % args.package)

+         sys.exit(1)

+ 

+     utilities.give_package(session, namespace, package, args.custodian)

+ 

+ 

+ if __name__ == "__main__":

+     main()

@@ -0,0 +1,94 @@ 

+ #! /usr/bin/python -tt

+ """ Orphan all packages of a given set of users.

+ 

+ If there are other committers on a package, the first one is promoted to be the

+ new owner.

+ 

+ If there are no other committers, then the package is given to the `orphan`

+ user.

+ 

+ You need a privileged pagure token in /etc/fedrepo_req/config.ini

+ 

+     [admin]

+     pagure_api_token = something secret

+ 

+ You can generate such a token on pkgs02 with:

+ 

+     $ PAGURE_CONFIG=/etc/pagure/pagure.cfg pagure-admin admin-token --help

+ """

+ # Copyright (c) 2017 Red Hat

+ # SPDX-License-Identifier:	GPL-2.0

+ #

+ # Authors:

+ #     Ralph Bean <rbean@redhat.com>

+ 

+ import argparse

+ 

+ try:

+     import utilities

+ except ImportError:

+     print("Try setting PYTHONPATH to find the utilities.py file.")

+     raise

+ 

+ 

+ PAGURE_URL = 'https://src.fedoraproject.org/api/0/'

+ 

+ 

+ def get_all_packages_for_user(session, user):

+     url = PAGURE_URL + 'projects'

+     params = dict(owner=user, fork=False)

+     response = session.get(url, params=params, timeout=400)

+     if not bool(response):

+         raise IOError("Failed GET %r %r" % (response.request.url, response))

+     for project in response.json()['projects']:

+         yield project

+ 

+ 

+ def triage_packages(packages, user):

+     for package in packages:

+         for kind in ('admin', 'commit'):

+             others = package['access_users'][kind]

+             try:

+                 others.remove(user)

+             except ValueError:

+                 # Owner doesn't have commit.  Weird, but ok.

+                 pass

+             if others:

+                 # Select the first one to become the new owner.

+                 yield package, sorted(others)[0]

+                 break

+         else:

+             yield package, 'orphan'

+ 

+ 

+ def main():

+     parser = argparse.ArgumentParser(usage=__doc__)

+     parser.add_argument("users", nargs="*",

+                         help="Users to remove.")

+     args = parser.parse_args()

+ 

+     session = utilities.retry_session()

+     for user in args.users:

+         print("Investigating packages for user %r" % user)

+         packages = get_all_packages_for_user(session, user)

+         transfers = triage_packages(packages, user)

+ 

+         # Exhaust the generator

+         transfers = list(transfers)

+ 

+         for package, custodian in transfers:

+             print("%s/%s will be given to %s" % (

+                 package['namespace'], package['name'], custodian))

+ 

+         response = raw_input("Is this okay? [y/N]")

+         if response.lower() not in ('y', 'yes'):

+             print("!! OK.  Bailing out for %r" % user)

+             continue

+ 

+         print("Starting transfers")

+         for package, custodian in transfers:

+             utilities.give_package(session, package, custodian)

+ 

+ 

+ if __name__ == "__main__":

+     main()

@@ -0,0 +1,61 @@ 

+ #! /usr/bin/python -tt

+ """ Utilities for manipulating dist-git (pagure). """

+ # Copyright (c) 2017 Red Hat

+ # SPDX-License-Identifier:	GPL-2.0

+ #

+ # Authors:

+ #     Ralph Bean <rbean@redhat.com>

+ 

+ import pprint

+ import sys

+ import traceback

+ 

+ import requests

+ 

+ from requests.adapters import HTTPAdapter

+ from requests.packages.urllib3.util.retry import Retry

+ 

+ try:

+     from fedrepo_req.pagure import get_pagure_auth_header

+     admin_headers = get_pagure_auth_header('admin', token_type='global')

+ except:

+     traceback.print_exc()

+     print("Failed to load admin tokens from fedrepo-req-admin")

+     sys.exit(1)

+ 

+ PAGURE_URL = 'https://src.fedoraproject.org/api/0/'

+ 

+ 

+ def retry_session():

+     session = requests.Session()

+     retry = Retry(

+         total=5,

+         read=5,

+         connect=5,

+         backoff_factor=0.3,

+         status_forcelist=(500, 502, 504),

+     )

+     adapter = HTTPAdapter(max_retries=retry)

+     session.mount('http://', adapter)

+     session.mount('https://', adapter)

+     return session

+ 

+ 

+ def give_package(session, namespace, package, custodian):

+     print("Giving %s/%s to %s" % (

+         namespace, package, custodian))

+ 

+     url = PAGURE_URL + namespace + '/' + package

+     payload = {'main_admin': custodian}

+     response = session.patch(

+         url,

+         data=payload,

+         headers=admin_headers,

+         timeout=60,

+     )

+     if not bool(response):

+         try:

+             pprint.pprint(response.json())

+         except:

+             pass

+         raise IOError("Failed PATCH %r %r" % (response.request.url, response))

At the request of @kevin, this lets us:

  • Orphan all packages of a given user.
  • Give a package from one user to another.
  • Give a package from one user to the 'orphan' user.
  • Unorphan a package, by giving it from 'orphan' to someone nice.

Signed-off-by: Ralph Bean rbean@redhat.com

rebased

7 years ago

FYI - I think this is blocked on https://pagure.io/pagure/issue/2515

The token I have (which is a project-less token with the modify_project acl) is unable to perform the give action... unless I already own the project. The above pagure issue should resolve that.

I would consider to put them in one script with a slightly better argument parser since the scripts share enough code and are closely related.

Since you are already using fedrepo-req, you could get this value from the config.

Edit: probably not worthwhile though.

I agree with @till's comment. I made a small comment additionally. +1 otherwise.

rebased

7 years ago

Thanks guys.

I would consider to put them in one script with a slightly better argument parser since the scripts share enough code and are closely related.

I refactored the scripts to still be separate, but they now share code and live in a scripts/distgit/ dir.

Commit 8747688 fixes this pull-request

Pull-Request has been merged by mboddu@bhujji.com

7 years ago

Pull-Request has been merged by mohanboddu

7 years ago