🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
### 配置 APT 软件仓库 运行自己的软件仓库有几个优点。你可以在自己的仓库中发布自己的软件包。 你可以在自己的软件仓库中放置上游软件包或第三方软件包,从而控制你使用的软件版本。 你可以将自己的软件仓库放置在其他服务器附近,从而避免网速缓慢或镜像站点无法访问的问题。 即使你不需要创建自己的软件包,也可能想要下载特定版本软件包所需的关键依赖包, 并将这些依赖包存储在自己的仓库中,从而防止因上游发生变故而产生的任何意外 (例如,你的发行版本已到达生命周期的终结或者上游仓库已经被关闭)。 这也使得通过 Puppet 自动更新软件包便得更容易。你可能偶尔需要更新一个软件包 (例如,当有一个安全更新可用时),只要在 package 资源中指定 ensure => latest 就可以方便地实现包更新。但是如果你不能控制仓库,就可能遭遇意想不到的升级风险, 使你的系统受到某种破坏。 使用自己的软件仓库是件两全其美的事情:你可以放心地使用 Puppet 从自己的软件仓库自动更新软件包,当有新版本的软件可用时,只需要将其纳入自己的软件仓库; 你可以首先测试上游的软件版本,确保其可用的情况下再纳入用于生产环境的软件仓库。 #### 准备工作 你需要第 9 章 [管理 Apache 服务](#ch07sec01) 一节中讲述的 apache 模块, 如果还没有此模块请先创建它。 在本例中,我将我的仓库命名为 packages.bitfieldconsulting.com。 你可能想要使用一个不同的仓库名,这需要替换本例中的所有的仓库名 packages.bitfieldconsulting.com 为你想要的仓库名。 #### 操作步骤 1. 创建一个新的 repo 模块: ``` # mkdir /etc/puppet/modules/repo # mkdir /etc/puppet/modules/repo/manifests # mkdir /etc/puppet/modules/repo/files ``` 2. 使用如下内容创建 /etc/puppet/modules/repo/manifests/bitfield-server.pp 文件: ``` class repo::bitfield-server { include apache package { "reprepro": ensure => installed } file { [ "/var/apt", "/var/apt/conf" ]: ensure => directory, } file { "/var/apt/conf/distributions": source => "puppet:///modules/repo/distributions", require => File["/var/apt/conf"], } file { "/etc/apache2/sites-available/apt-repo": source => "puppet:///modules/repo/apt-repo.conf", require => Package["apache2-mpm-worker"], } file { "/etc/apache2/sites-enabled/apt-repo": ensure => symlink, target => "/etc/apache2/sites-available/apt-repo", require => File["/etc/apache2/sites-available/apt-repo"], notify => Service["apache2"], } } ``` 3. 使用如下内容创建 /etc/puppet/modules/repo/files/distributions 文件: ``` Origin: Bitfield Consulting Label: bitfield Suite: stable Codename: lucid Architectures: amd64 i386 Components: main non-free contrib Description: Custom and cached packages for Bitfield Consulting ``` 4. 使用如下内容创建 /etc/puppet/modules/repo/files/apt-repo.conf 文件: ``` <VirtualHost *:80> DocumentRoot /var/apt ServerName packages.bitfieldconsulting.com ErrorLog /var/log/apache2/packages.bitfieldconsulting.com.error.log LogLevel warn CustomLog /var/log/apache2/packages.bitfieldconsulting.com.access.log combined ServerSignature On # Allow directory listings so that people can browse the # repository from their browser too <Directory "/var/apt"> Options Indexes FollowSymLinks MultiViews DirectoryIndex index.html AllowOverride Options Order allow,deny allow from all </Directory> # Hide the conf/ directory for all repositories <Directory "/var/apt/conf"> Order allow,deny Deny from all Satisfy all </Directory> # Hide the db/ directory for all repositories <Directory "/var/apt/db"> Order allow,deny Deny from all Satisfy all </Directory> </VirtualHost> ``` 5. 在一个节点的配置清单中添加如下代码: ``` include repo::bitfield-server ``` 6. 运行 Puppet: ``` # puppet agent --test info: Retrieving plugin info: Caching catalog for cookbook.bitfieldconsulting.com info: Applying configuration version '1304775601' notice: /Stage[main]/Repo::Bitfield-server/File[/var/apt]/ensure: created notice: /Stage[main]/Repo::Bitfield-server/File[/var/apt/conf]/ ensure: created notice: /Stage[main]/Repo::Bitfield-server/File[/var/apt/conf/ distributions]/ensure: defined content as '{md5}65dc791b876f53318a 35fcc42c770283' notice: /Stage[main]/Repo::Bitfield-server/Package[reprepro]/ ensure: created notice: /Stage[main]/Repo::Bitfield-server/File[/etc/apache2/ sites-enabled/apt-repo]/ensure: created notice: /Stage[main]/Repo::Bitfield-server/File[/etc/apache2/ sites-available/apt-repo]/ensure: defined content as '{md5}2da4686 957e5acf49220047fe6f6e6e1' info: /Stage[main]/Repo::Bitfield-server/File[/etc/apache2/sitesenabled/ apt-repo]: Scheduling refresh of Service[apache2] notice: /Stage[main]/Apache/Service[apache2]: Triggered 'refresh' from 1 events notice: Finished catalog run in 16.32 seconds ``` #### 工作原理 其实,你无需创建一个 APT 仓库。因为可以通过 HTTP 下载软件包,所以你只需要一个 Apache 虚拟主机。 你可以将实际的软件包随意放置在任何地方,只要有一个 conf/distributions 文件并在其中给出 APT 仓库的相关信息。 1. bitfield-server 类的第一部分确保 Apache 已经被设置: ``` class repo::bitfield-server { include apache ``` 2. reprepro 是用于管理仓库本身的非常有用的工具(例如,添加一个新的软件包): ``` package { "reprepro": ensure => installed } ``` 3. 我们创建一个仓库的根目录 /var/apt,以及该目录下的 conf/distributions 文件: ``` file { [ "/var/apt", "/var/apt/conf" ]: ensure => directory, } file { "/var/apt/conf/distributions": source => "puppet:///modules/repo/distributions", require => File["/var/apt/conf"], } ``` 4. 这个类的其余部分部署了一个 Apache 虚拟主机的配置文件,用于响应 packages.bitfieldconsulting.com 的请求: ``` file { "/etc/apache2/sites-available/apt-repo": source => "puppet:///modules/repo/apt-repo.conf", require => Package["apache2-mpm-worker"], } file { "/etc/apache2/sites-enabled/apt-repo": ensure => symlink, target => "/etc/apache2/sites-available/apt-repo", require => File["/etc/apache2/sites-available/apt-repo"], notify => Service["apache2"], } ``` #### 更多用法 当然,一个可用的仓库里不能没有软件包。下面将介绍如何添加软件包, 以及如何配置主机并从你的仓库下载软件包。 ##### 向仓库添加软件包 要添加一个软件包到你的仓库,首先下载它然后使用 reprepro 将其添加到仓库: ``` # cd /tmp # wget http://archive.ubuntu.com/ubuntu/pool/main/n/ntp/\ ntp_4.2.4p8+dfsg-1ubuntu2.1_i386.deb # cd /var/apt # reprepro includedeb lucid /tmp/ntp_4.2.4p8+dfsg-1ubuntu2.1_i386.deb Exporting indices... ``` ##### 配置节点使用仓库 1. 使用如下内容创建 /etc/puppet/modules/repo/manifests/bitfield.pp 文件 (请根据你自己的仓库服务器的 IP 地址替换如下的 IP 地址): ``` class repo::bitfield { host { "packages.bitfieldconsulting.com": ip => "10.0.2.15", ensure => present, target => "/etc/hosts", } file { "/etc/apt/sources.list.d/bitfield.list": content => "deb http://packages.bitfieldconsulting.com/lucid main\n", require => Host["packages.bitfieldconsulting.com"], notify => Exec["bitfield-update"], } exec { "bitfield-update": command => "/usr/bin/apt-get update", require => File["/etc/apt/sources.list.d/bitfield.list"], refreshonly => true, } } ``` 如果你有 DNS 服务器或者你可以控制你的 DNS 区域,可以省略 host 资源的设置。 2. 应用这个类到一个节点: ``` node cookbook { include repo::bitfield } ``` 3. 测试你仓库中的 ntp 软件包是否可用: ``` # apt-cache madison ntp ntp | 1:4.2.4p8+dfsg-1ubuntu2.1 | http://us.archive.ubuntu. com/ubuntu/ lucid-updates/main Packages ntp | 1:4.2.4p8+dfsg-1ubuntu2.1 | http://packages. bitfieldconsulting.com/ lucid/main Packages ntp | 1:4.2.4p8+dfsg-1ubuntu2 | http://us.archive.ubuntu. com/ubuntu/ lucid/main Packages ntp | 1:4.2.4p8+dfsg-1ubuntu2 | http://us.archive.ubuntu. com/ubuntu/ lucid/main Sources ntp | 1:4.2.4p8+dfsg-1ubuntu2.1 | http://us.archive.ubuntu. com/ubuntu/ lucid-updates/main Sources ``` ##### 对软件包签名 对于生产环境,你应该对软件仓库设置 GPG 密钥并且对软件包进行签名,关于如何设置密钥和签名的信息, 请参考 Sander Marechal 撰写的关于 “设置和管理 APT 仓库” 的文章: [http://www.jejik.com/articles/2006/09/setting_up_and_managing_an_apt_repository_with_reprepro/](http://www.jejik.com/articles/2006/09/setting_up_and_managing_an_apt_repository_with_reprepro/) 。