Trac 环境
Trac 使用目录结构和数据库来存储项目数据。该目录被称为环境。
Trac 支持 SQLite、PostgreSQL 和 MySQL 数据库。对于 PostgreSQL 和 MySQL,您必须在运行 trac-admin initenv
之前创建数据库。
创建环境
使用 initenv 命令创建新的 Trac 环境
$ trac-admin /path/to/myproject initenv
trac-admin
将询问您项目名称和数据库连接字符串。
可以使用 --inherit
或 --config
选项指定基本配置。--inherit
选项可以指定一次或多次以利用全局配置。在初始化 trac.ini
时,--config
文件中指定的选项将用于覆盖默认值。有关在配置文件中定义仓库的示例,请参见下文。
实用提示
- 请将您的环境目录放置在支持亚秒级时间戳的文件系统上,因为 Trac 会监控其配置文件的修改时间,在时间戳精度过低的文件系统上发生的更改可能在 Trac < 1.0.2 中无法被检测到。这对于使用TracStandalone 时认证文件的位置也同样适用。
- 运行 Web 服务器的用户将需要对环境目录及其内部所有文件具有文件系统写入权限。请务必设置相应的权限。这同样适用于源代码仓库,尽管运行 Trac 的用户只需要对使用 BDB 文件系统创建的 Subversion 仓库具有写入权限;对于其他仓库类型,请查阅相应插件的文档。
- 不支持非 ASCII 字符的环境路径。
- 位于继承配置中定义的共享插件目录中的Trac 插件默认情况下未启用,这与环境
plugins
目录中的插件不同。因此,如果它们需要创建额外的数据表(例如),这些数据表将不会在环境创建期间创建,您需要升级环境。或者,您可以通过使用--inherit
或--config
选项在配置文件中明确启用插件来避免升级环境。更多信息请参阅TracAdmin#FullCommandReference。
注意:不要混淆Trac 环境目录与源代码仓库目录。
这是初学者常犯的错误。Trac 环境的结构恰好松散地模仿了 Subversion 仓库的目录结构,但它们是两个独立的实体,它们不应该也绝不能位于同一个位置。
数据库连接字符串
在创建环境时,您需要指定数据库连接字符串。默认是 SQLite,这对于大多数项目来说已经足够。SQLite 数据库文件存储在环境目录中,可以与环境的其余部分一起轻松备份。
请注意,如果连接字符串的用户名或密码(如果适用)包含 :
、/
或 @
字符,则需要对其进行URL 编码。
$ python -c "import urllib; print urllib.quote('password@:/123', '')"
password%40%3A%2F123
SQLite 连接字符串
SQLite 数据库的连接字符串是
sqlite:db/trac.db
其中 db/trac.db
是 Trac 环境中数据库文件的路径。
请参阅 DatabaseBackend#SQLite 获取更多信息。
PostgreSQL 连接字符串
PostgreSQL 的连接字符串稍微复杂一些。例如,要连接到 localhost
上名为 trac
的 PostgreSQL 数据库,用户名为 johndoe
,密码为 letmein
,请使用
postgres://johndoe:letmein@localhost/trac
如果 PostgreSQL 运行在非标准端口,例如 9342,请使用
postgres://johndoe:letmein@localhost:9342/trac
在 UNIX 上,您可能希望选择一个 UNIX 套接字进行传输,可以是 PGHOST 环境变量定义的默认套接字
postgres://user:password@/database
或指定一个
postgres://user:password@/database?host=/path/to/socket/dir
有关如何管理 PostgreSQL 的详细说明,请参阅 PostgreSQL 文档。通常,以下操作足以创建一个名为 tracuser
的数据库用户和名为 trac
的数据库
$ createuser -U postgres -E -P tracuser $ createdb -U postgres -O tracuser -E UTF8 trac
运行 createuser
时,系统会提示您输入 tracuser
的密码。这个新用户将不是超级用户,不允许创建其他数据库,也不允许创建其他角色。运行 Trac 实例不需要这些权限。如果用户不需要密码,只需从 createuser
命令中删除 -P
和 -E
选项。另请注意,数据库应创建为 UTF8 编码。LATIN1 编码会导致错误,因为 Trac 使用 Unicode。
在某些默认配置(Debian)下,以 postgres
用户身份运行 createuser
和 createdb
脚本
$ sudo su - postgres -c 'createuser -U postgres -S -D -R -E -P tracuser' $ sudo su - postgres -c 'createdb -U postgres -O tracuser -E UTF8 trac'
Trac 默认使用 public
模式,但您可以在连接字符串中指定不同的模式
postgres://user:pass@server/database?schema=yourschemaname
MySQL 连接字符串
MySQL 连接字符串的格式与 PostgreSQL 类似,只需将 postgres
方案替换为 mysql
。例如,要连接到 localhost
上名为 trac
的 MySQL 数据库,用户名为 johndoe
,密码为 letmein
mysql://johndoe:letmein@localhost:3306/trac
更改数据库后端
TracAdmin 的 convert_db
命令允许在 SQLite、MySQL 和 PostgreSQL 数据库之间进行迁移。
convert_db <dburi> [new_env] Convert database Converts the database backend in the environment in which the command is run (in-place), or in a new copy of the environment. For an in-place conversion, the data is copied to the database specified in <dburi> and the [trac] database setting is changed to point to the new database. The new database must be empty, which for an SQLite database means the file should not exist. The data in the existing database is left unmodified. For a database conversion in a new copy of the environment, the environment in which the command is executed is copied and the [trac] database setting is changed in the new environment. The existing environment is left unmodified. Be sure to create a backup (see `hotcopy`) before converting the database, particularly when doing an in-place conversion.
删除 Trac 环境
可以使用标准文件系统工具删除传递给 initenv
命令的目录来删除 Trac 环境。对于使用 SQLite 的环境,这将同时删除位于环境 db
目录中磁盘上的 SQLite 数据库。对于使用 PostgreSQL 或 MySQL 的环境,您需要使用相关的数据库工具来删除数据库。
源代码仓库
单个环境可以连接到多个仓库。但是,默认情况下 Trac 不连接任何源代码仓库,并且不会显示浏览源代码导航项。有几种方法可以将仓库连接到环境,请参阅TracRepositoryAdmin。
在创建环境时,可以通过使用 --config
或 --inherit
选项向 initenv
命令指定配置文件来定义仓库。如果仓库连接器已启用,缓存的仓库将被同步。示例
$ trac-admin $ENV initenv --config=config.ini $ cat config.ini [components] tracopt.versioncontrol.* = enabled [repositories] .dir = /path/to/default/type/repos repos1.type = git repos1.dir = /path/to/git/repos repos2.type = svn repos2.dir = /path/to/svn/repos
目录结构
一个环境包含以下文件和目录
README
- 环境的简要描述。VERSION
- 环境版本标识符。files
attachments
- 维基页面和工单的附件。
conf
trac.ini
- 主要配置文件。
db
trac.db
- SQLite 数据库,如果您使用 SQLite。
htdocs
- 包含 Web 资源的目录,这些资源可以在模板中使用路径/chrome/site/...
引用。log
- 当启用file
日志记录并给出相对路径时,日志文件的默认目录。plugins
- 特定于环境的插件。templates
- 自定义 Genshi 特定于环境的模板。site.html
- 自定义站点页眉、页脚和样式的方法。
另请参阅:TracAdmin、TracBackup、TracIni