46.[python] Easy to manage with Pyolite Gitolite__python

Source: Internet
Author: User
Tags md5 parent directory python list

Introduction to installation Use methods User Management Warehouse Management API detailed Pyolite class Usermanager class User class Listkeys class Repositorymanager class repository class Listusers class Git class repo GER Class Considerations

Reprint please indicate the original source: http://blog.csdn.net/a464057216/article/details/52661365 Introduction

Gitolite manages the GIT server by manipulating its Gitolite-admin warehouse, which describes how to use Python's pyolite management gitolite to write scripts to improve productivity.

Before you learn this article, you'll need to know some basic gitolite principles, SSH Public/private key management, and more. Install the pip install pyolite command to install Pyolite. Create the Repos subdirectory under the gitolite-admin/conf directory, and then append the following on the last line of the gitolite-admin/conf/gitolite.conf file:

Include     "repos/*.conf"
Delete the Testing.git item in gitolite.conf and delete the repository's directory on the GIT server.

Finally push the changes to the GIT server. How to use

The examples directory of Pyolite's GitHub project has shown a simple way to use it. Because it does not have detailed documentation, the following is my summary after reading the source code.
Code word is not easy, if you feel that your work is helpful or want to reprint, welcome at the end of the text message, thank you. User Management

#-*-Coding:utf-8-*-from pyolite import pyolite admin_repository = '/users/mars/learnspace/gitolite-admin ' # Pyolite Object Used to manage the Gitolite-admin warehouse Olite = Pyolite (admin_repository=admin_repository) # users represents a collection of users, all () returns all Git user print "All git" Users: "Olite.users.all () # Gets the git user specified by the first parameter # if the git user does not exist, create a git user by Key_path and name and return to #alice = Olite.users.get_or_  Create ("Alice", # key_path= "/users/linglingguo/.ssh/alice5.pub") # Specify key and name to create a git user and return to Alice = Olite.users.create (name= "Alice", key= ' ssh-ed25519 aaaac3nz ...) 0wq4s aliceed25519 ') # get a git user Alice = Olite.users.get (name= "Alice") # Get a git user's public key list print "Alice ' s Keys:", Alice.key
S # Add public key for a git user (by specifying public key content) Alice.keys.append (' Ssh-rsa aaaab3n...qpmu57 alice5 ') # Add public key for a git user (by specifying a public key file path) Alice.keys.append ('/users/linglingguo/.ssh/alice4.pub ') # Deletes a git user's public key (the deleted parameter must be the content of the public key) Alice.keys.remove (' Ssh-rsa Aaaab3nza ... l3r51eihqpmu57 Alice5 ') # to determine if the user is Gitolite Admin user print "If%s is admin:%s" % (Alice.name, alice.is_admin) 
Warehouse Management
#-*-Coding:utf-8-*-from
pyolite import pyolite
admin_repository = '/users/linglingguo/learnspace/ Gitolite-admin '
# Pyolite object to manage Gitolite-admin warehouse
olite = Pyolite (admin_repository=admin_repository)

# Get a warehouse, if not on creation after return
loo = olite.repos.get_or_create ("Loo")

# Get a warehouse
Mars = Olite.repos.get ("Mars")

# Create a warehouse and return
Suson = olite.repos.create ("Suson")

# Add users to the warehouse and set access rights (RW+CD) to complete Alice by passing in the user object
Olite.users.get_or_create ("Alice",
                           key_path= "/users/linglingguo/.ssh/alice5.pub")
Mars.users.add ( Alice, "rw+")

# Add a user to the warehouse and set access rights, complete by passing in the user name
# so that when added, it does not determine whether it is a legitimate git user and needs to be judged by the caller himself
Mars.users.add (' New_ Person ', ' R '

# Edit user rights under a warehouse
# Edit before not determining whether the user is in this project, nor is the permission string legal (I have submitted the pull request to the original project modification)
Mars.users.edit (' New_person ', RW)

# Delete a user in a warehouse
mars.users.remove (' New_person ')

# View the list of users in the warehouse
print '%s ' Users:%s '% (Mars.name, mars.users)
API Detailed Pyolite class

Pyolite. The Pyolite class is an abstraction of the Gitolite-admin warehouse that contains the following attributes (the type of the property in parentheses): (str) admin_repository:gitolite-admin the absolute path of the code library. (Usermanager) A collection of users users:gitolite. (Repositorymanager) Repos:gitolite collection of code libraries.

When instantiating a Pyolite instance, Admin_repository is a required parameter and throws an exception if Admin_repository is not a directory: ValueError (' admin repository path should point To directory '). Usermanager class

The Pyolite.managers.user.UserManager class is a subclass of the manager class used to manage git users. The following properties are inherited from the manager class: (PATH) path: The Unipath that represents the gitolite-admin absolute path. The Path object. (git) git: A Git object that represents a gitolite-admin absolute path.

The following methods are inherited from the manager class: Create (Name, Key=none, Key_path=none): Creates a user object named name and returns it, must provide key (SSH public key content) or Key_path ( SSH public key file path) parameter, if provided at the same time, the Key_path parameter is ignored, if none is provided, the report exception: ValueError (' You need to specify a key or Key_path ').

The operation is actually in the Gitolite-admin/keydir directory, first use GIT user name to create a directory, and then follow the SSH public key MD5 to create subdirectories, and finally the public key content to the subdirectory in the directory named git user name of the public key file (the same public key will not be repeatedly added), Like what:

Keydir/alice
├──7a0db30e3d42c0e044bdbbe7d71dc96e
│   └──alice.pub
└── caf5253d8ea374498a40c89d864b6d8e
    └──alice.pub

2 directories, 2 files
Get (name): Returns the user object named name and none if it is not found in the Keydir and conf directories. Get_or_create (Name, Key=none,key_path=none): A method inherited from the manager class that returns its user object if get (name) returns not none; if get (name) returns none , the Create (name, Key=none, Key_path=none) is called and the newly created user object is returned.

The special methods of the Usermanager class are as follows: All (): According to the public key in Keydir, the list of user objects that currently have permission to access the GIT server is returned. User Class

The Pyolite.models.user.User class represents a git user, managed by the Usermanger class, and the initialization method is as follows:

User (path, git, name, repos, keys) Initializes a user object that initializes the following properties: (path) path: The Unipath that represents the gitolite-admin absolute path. The Path object. (git) git: A Git object that represents a gitolite-admin absolute path. (str) name:git the user's name. (list) Repos:git the list of warehouses to which the user has permission to access, by default []. (Listkeys) keys:git the user's SSH public key list, which defaults to an empty list.

Includes the following methods:

Get_by_name (name, Path, git): Class method that returns the user object named name. Where: (path) path: The Unipath that represents the gitolite-admin absolute path. The Path object. (git) git: A Git object that represents a gitolite-admin absolute path. (str) name:git the user's name.

Get (user, path, git): Class method that returns the user object with the Word user (user argument is a str), or returns the user object itself (the user argument is a user object).

Returns a string such as < name > when the User object is an argument for STR () or repr ().

The user class also contains the following properties:
* Is_admin: Returns whether the user object is a git administrator with a value of true or false. Listkeys class

The Pyolite.models.lists.ListKeys class is an abstraction of all the public keys for Git users, and is a subclass of the Python-built list. Contains the following properties: (user) User:user object that indicates which user this Listkeys object belongs to. (list) A Self:python list object that stores all the SSH public keys for a user.

Includes the following methods: Append (Key): Add key to Listkeys, where key can be SSH public key content or SSH public key file path (duplicate key will not be added).
*+ keys: If you want to add more than one key to Listkeys, you can use the addition operator directly, where the keys parameter is a list of lists, where each element is an SSH public key content or an SSH public key file path. Remove (key): Delete a key from the Listkeys object and the key must be the SSH public key content at the time of deletion. It actually deletes the corresponding public key file and its parent directory under the gitolite-admin/keydir/directory (by calculating the directory generated by MD5). If this key does not exist in the Listkeys, it is reported as an exception: ValueError ("Invalid key"). Repositorymanager class

The Pyolite.managers.reopsitory.RepositoryManager class inherits from the manager class and is used to manage the Git repository. The following properties are inherited from the manager class: (PATH) path: The Unipath that represents the gitolite-admin absolute path. The Path object. (git) git: A Git object that represents a gitolite-admin absolute path.

The following method was inherited from the manager class: Get (Lookup_repo): Returns the Repository object with the name Lookup_repo, if none is returned. Create (Lookup_repo): Creates a new warehouse named Lookup_repo and returns the Repository object. If the warehouse already exists, report an exception: ValueError (' Repository%s already exists '% Lookup_repo).
Calling this method creates a file named lookup_repo.conf in the Gitolite-admin/conf/repos directory, and the content is repo lookup_repo\n. Get_or_create (name): If Get (Lookup_repo) returns not none, it returns its repository object directly, and if get (Lookup_repo) returns NONE, call Create (Lookup_ Repo), the newly created repository object is returned after the warehouse is created. Repository Class

The Pyolite.models.repository.Repository class is an abstraction of a git warehouse that contains the following attributes: (STR) name: the warehouse names. (path) path: A unipath that represents the gitolite-admin absolute path. The Path object. (git) git: A Git object that represents a gitolite-admin absolute path. (listusers) Users: The user list for this warehouse.

The initialization function for the Repository class is repository (name, path, git), with the parameter meaning above (the users parameter is not required when initializing).

Includes the following methods:

Get_by_name (name, Path, git): Class method that returns the repository object named name. Where: (path) path: The Unipath that represents the gitolite-admin absolute path. The Path object. (git) git: A Git object that represents a gitolite-admin absolute path. (STR) Name: the names of the warehouses.

Repository object as a parameter of STR (), returns a string of type < name >. Listusers class

The Pyolite.models.lists.ListUsers class is used to manage users in a warehouse, including the following properties: (Repository) Repository_model: Represents which Repository object is belonging to. (repo) Repo: The Repo object that represents the configuration file for the warehouse.

Contains the following methods: Get_or_create (user): The user parameter can be the name of the STR type, or it can be a client object, directly fetched or created to return a user object. Add (user, permission): Users who add corresponding permissions to the warehouse, the user parameter can be a str-type username, or it can be a users object, and the Permissoin parameter can be any combination of characters in ' RW+CD ', and the method returns the added user object. Edit (user, permission): Modifies the permissions of the corresponding user in the warehouse, the user parameter can be the STR type's username, or it can be a users object, and the Permissoin parameter can be ' rw+ Any combination of characters in the CD, method returns the modified user object. Remove (user): Deletes the access rights of a user in the warehouse, which can be the user name of the str type, or it can be a users object. Listusers object as a parameter of STR (), returns a string form of a list of user names, such as [Mars, Loo]. Set (Users): This method in the code does not have a table called, from the implementation of the view is slightly overdue, here is not introduced. Repo class

Note that the Pyolite.repo.Repo class is an abstraction of a specific configuration file, as distinguished from the Repository class. Contains the following properties: (path) path: A unipath that points to a specific configuration file. Path object, such as Unipath. Path (U '/users/mars/learnspace/gitolite-admin/conf/repos/mars.conf '). (list) Users: A list of the names of all git users who have permission to manipulate the repo, such as [Mars, Loo].

Contains the following methods: replace (pattern, string): Replaces the part of the configuration file that matches the regular expression pattern with a string. Write (String): Appends the string's contents to the configuration file. Overwrite (string): Overwrites the contents of a string to the configuration file. git class

Because Gitolite operations are implemented by operating gitolite-admin warehouses, Pyolite abstracts the operations of Git into pyolite.git.Git classes. Contains the following properties: (path) path: The Unipath that represents the gitolite-admin absolute path. The Path object.

Contains the following methods: Commit (objects, message): The objects parameter is a list of directories to submit, such as [' Keydir '] or [' Keydir ', ' conf '],message is the annotation information that represents the Git action submission. Manager Class

The Pyolite.managers.manager.Manager class is an abstract base class that cannot be instantiated and is not used daily, so it is presented at the end. Contains the following properties: (path) path: The Unipath that represents the gitolite-admin absolute path. The Path object. (git) git: A Git object that represents a gitolite-admin absolute path.

Includes methods such as: Get_or_create (lookup_entity, *args, **kwargs): Non-abstract, subclasses do not have to be implemented. Get (Entity): Abstract method, subclasses must implement. Create (Entity): Abstract methods, subclasses must be implemented. Attention Matters

I read the source code of Pyolite This tool carefully, and found some problems as follows:
1. [Bug] The public key file operation for git users is not locked and there is a problem with concurrent operations.
2. [Bugs] When adding, updating, and deleting user rights for a warehouse profile, there is no judgment that the user is a legitimate user on the GIT server, and if not, throw an exception.
3. [Improvement] does not implement the support of the user group and Warehouse group in Gitolite.
4. [Bug] When updating user rights for a profile of a warehouse, there is no judgment as to whether the user has permission to operate the warehouse, and not to judge the legality of the updated permissions (should belong to set ("RW+CD")).

Regarding the last bug I have repaired and submitted the pull request to the original project.
If you feel that my article is helpful to you, please pay attention to me (csdn:mars Loo blog) or for this article point praise, thank

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.