📜 ⬆️ ⬇️

Trac and his friends Gitolite, Nginx and UWSGI

Everyone probably knows the wonderful OpenSource project Trac , which allows you to organize the workflow when developing software. However, its deployment and configuration is not an easy task. Information on the Internet is fragmented and often already outdated. I was able to make sure of this by customizing Trac integrated with Gitolite on the Nginx + UWSGI bundle.

After a short overture with a tambourine, I was born with this document, which is almost a step-by-step instruction on how to deploy the Trac project. I hope this is useful to someone else.

The instruction is user-oriented with basic knowledge of Linux administration and the Python package installation system. I have to say that the deployment process is described on a system with a freshly installed Ubuntu 12.04 distribution, but I think the information will be useful for fans of other Linux builds.

Initializing the Trac project


Perform the following steps if the Trac distribution is not installed on the system:

sudo apt-get install python-pip sudo pip install babel sudo pip install "Trac==1.0" 

Now you need to initialize the project placement directory. Do the following:

 sudo adduser --disabled-password --gecos 'Trac' trac sudo addgroup tracsys sudo adduser trac tracsys sudo adduser `whoami` tracsys sudo mkdir -p /var/www/trac sudo trac-admin /var/www/trac initenv sudo chown -R nobody:tracsys /var/www/trac sudo chmod -R g+rwXs /var/www/trac tracd -p 8000 /var/www/trac/ 

Everything is rather trivial, except for manipulations with the tracsys group and the assignment of rights to the project directory. But this is a breakthrough for integration with gitolite and hosting a project based on the Nginx + UWSGI bundle.
')

Hosting with Nginx and UWSGI


UWSGI is a WSGI application server. To launch our Trac project under it you need to present it as a WSGI application. This is not difficult to do with the script below. Copy it to the /home/trac/wsgi_trac.py file, assign the user trac the owner. Next, you need to create Nginx and UWSGI configuration files. For Nginx, the file is standard in such cases, and for UWSGI there are subtleties. Our WSGI will run with trac user and tracsys user rights. The wsgi_trac.nginx and wsgi_trac.uwsgi configuration files are listed below.

wsgi_trac.py

 import os import sys import trac.web.main sys.stdout=sys.stderr os.environ['TRAC_ENV'] = '/var/www/trac' application = trac.web.main.dispatch_request 

wsgi_trac.nginx

 server { listen 80; server_name trac.local; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:5544; } } 

wsgi_trac.uwsgi

 [uwsgi] uid=trac gid=tracsys chmod-socket=777 chown-socket=trac chdir = /home/trac socket = 127.0.0.1:5544 module = wsgi_trac:application processes = 2 master = 1 

If Nginx and / or UWSGI are not installed, install them. Symlinks activate the configuration files and restart the services.
 sudo apt-get install nginx uwsgi uwsgi-plugin-python sudo ln -s /home/trac/wsgi_trac.nginx /etc/nginx/sites-enabled/trac.conf sudo ln -s /home/trac/wsgi_trac.uwsgi /etc/uwsgi/apps-enabled/trac.ini sudo service nginx restart sudo service uwsgi restart 


Installing the AccountManagerPlugin plugin


Distribution Trac 1.0 does not include a component that would allow to manage user accounts. For this, various plugins are used. In our example, the AccountManagerPlugin plugin will be used. Download it and build as an egg distribution. Copy the resulting file with the .egg extension into the plugins directory, in our case this is / var / www / trac / plugins . Add lines to the trac.ini file.
 [components] trac.web.auth.LoginModule = disabled acct_mgr.web_ui.LoginModule = enabled acct_mgr.web_ui.RegistrationModule = enabled 

A file in the HtDigest format will be used for storing passwords. To do this, we will add the following lines to the trac.ini file.
 [account-manager] authentication_url = force_passwd_change = False hash_method = HtDigestHashMethod htdigest_realm = trac htpasswd_hash_type = crypt password_file = /var/www/trac/db/users.htdigest password_store = HtDigestStore persistent_sessions = False refresh_passwd = False user_lock_max_time = 0 verify_email = False 

After that, an item will appear in the web interface that allows you to register a user. Register user. And execute the command to assign it as admin.
 trac-admin /var/www/trac permission add <  > TRAC_ADMIN 

Now we have an admin, going under which you can create, delete and assign rights to other users. Please note that a link to the new user registration form is activated in the web interface, disable it if self-registration of the user is not provided by the project. You can already do this in the admin panel in the plugin management section by unchecking the RegistrationModule in the configuration form of the AccountManagerPlugin plugin.

Gitolite integration


To manage the Git repositories there is a great Gitolite tool that can also be integrated with Trac projects. To do this, follow these steps:


 sudo apt-get install gitolite sudo adduser --home /home/gitolite --disabled-password --gecos 'Git' git sudo adduser git tracsys sudo su trac cd ~ ssh-keygen -t rsa cp ~/.ssh/id_rsa.pub /tmp/trac.pub exit 

The next step, under the git user, is initializing the gitolite working directory. This opens the configuration file, where you need to correct the line '$ REPO_UMASK = 0077;' at '$ REPO_UMASK = 0007;'. If vim is troublesome, just hit ZZ. You can change the mask and later edit the file /home/gitolite/.gitolite.rc . We assign rights to the directory with repositories similar to those assigned to the directory with the Trac project.
 sudo su git cd ~ gl-setup /tmp/trac.pub exit sudo chown -R git:tracsys /home/gitolite/repositories/ sudo chmod -R g+rwXs /home/gitolite/repositories/ 

It remains to enable Git integration with our Trac project. To do this, add the following lines to trac.ini .
 [components] tracopt.versioncontrol.git.* = enabled [trac] repository_type = git 

Now in the admin panel, in the Repositories section, connect the testing repository gitolite / home / gitolite/repositories/testing.git with the name testing, and the managing repository / home / gitolite / repositories / gitolite-admin.git with the name gitolite-admin . If everything was done correctly, a section Source code will appear in the web interface menu in which both of them will be present.

Everything is fine, but I would also like to manage Gitolite repositories through the interface of our project Trac. Not a problem; the trac-GitolitePlugin plugin will help in this matter. Download it from the provided page and build as an egg distribution. Copy the resulting file to the plugins directory and activate by adding the following lines to the trac.ini file.
 [components] trac_gitolite.* = enabled [trac] permission_policies = GitolitePermissionPolicy, AuthzPolicy, DefaultPermissionPolicy, LegacyAttachmentPolicy [trac-gitolite] admin_reponame = gitolite-admin admin_real_reponame = gitolite-admin admin_ssh_path = git@localhost:gitolite-admin.git admin_system_user = trac default_private = True all_includes_anonymous = False 

After that, a new section will appear in the admin panel, in which you can add users to Gitolite and assign repository rights to them. However, when I first added a user, I got an error when executing the git clone command: git @ localhost: gitolite-admin.git / tmp / bla-bla. I had to execute this command from under the user trac and confirm the addition of the key to manual, then everything worked without problems.

It remains to implement one of the most interesting features of the integration of Trac and Git, this is closing the ticket by specifying the code word in the commit. For example, a commit in which the string “close # 777” will be, automatically closes the corresponding ticket. To do this, first you will need to activate the CommitTicketUpdater component through the admin panel or add the following lines to the trac.ini file.
 [components] tracopt.ticket.commit_updater.* = enabled 

Next, download the Python script that implements the corresponding hook, for example, to the / var / www / trac / hooks directory. It has a configuration section that needs to be configured based on the location and configuration of a particular Trac project. In our case, it will look something like this:
 # config TRAC_ENV = '/var/www/trac' GIT_PATH = '/usr/bin/git' TRAC_ADMIN = '/usr/local/bin/trac-admin' #REPO_NAME = '(default)' # if you are using gitolite or sth similar, you can get the repo name from envir REPO_NAME = os.getenv('GL_REPO') 

Gitolite has its own system of organizing hooks, they are correctly spelled out and work on all the repositories they control, and this is exactly what is needed. To do this, under the git user, create a file ~ / .gitolite / hooks / post-receive with the following contents:
 #!/bin/bash python /var/www/trac/hooks/trac-post-receive-hook-0.12-new-commits-from-all-branches.py 

Finally, activate this hook by executing the following commands.
 sudo su git cd ~/.gitolite/hooks/ chmod 755 post-receive gl-setup 

Conclusion


I hope my article will help someone to deal with this issue or at least save time.

Source: https://habr.com/ru/post/159153/


All Articles