### 配置 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/) 。
- Puppet 2.7 Cookbook 中文版
- 中文翻譯版
- 譯者序
- 項目緣起
- 翻譯方法
- 社區鏈接
- 社區建議
- 貢獻者
- 原書版權頁
- 關于作者
- 前言
- 本書內容
- 閱讀前提
- 適用讀者
- 格式約定
- 讀者反饋
- 客戶支持
- 下載案例代碼
- 勘誤表
- Puppet 基礎設施
- 使用版本控制
- 使用提交鉤子
- 使用 Rake 部署變更
- 配置 Puppet 的文件服務器
- 從 cron 運行 Puppet
- 使用自動簽名
- 預簽名證書
- 從 Puppet 的 filebucket 檢索文件
- 使用 Passenger 擴展 Puppet 的部署規模
- 創建去中心化的分布式 Puppet 架構
- 監控、報告和排錯
- 生成報告
- 通過 Email 發送包含特定標簽的日志信息
- 創建圖形化報告
- 自動生成 HTML 文檔
- 繪制依賴關系圖
- 測試你的 Puppet 配置清單
- 執行模擬運行
- 檢測編譯錯誤
- 理解 Puppet 的錯誤信息
- 顯示命令的輸出結果
- 輸出調試信息
- 檢查配置設置
- 使用標簽
- 使用運行階段
- 使用不同的環境
- Puppet 語言及其寫作風格
- 使用 Puppet 社區規范
- 使用模塊
- 使用標準的命名規范
- 使用嵌入式 Ruby 代碼
- 使用純 Ruby 代碼書寫配置清單
- 遍歷多個項目
- 書寫強大的條件語句
- 在 if 語句中使用正則表達式
- 使用選擇器和 case 語句
- 檢測字符串中是否包含指定的值
- 使用正則表達式替換
- 書寫更優質的配置清單
- 使用資源的數組
- 使用 define 資源
- 指定資源的依賴關系
- 使用節點繼承
- 使用類的繼承和重載
- 給類傳遞參數
- 書寫可重用的跨平臺配置清單
- 獲得系統的環境信息
- 導入動態信息
- 從 CSV 文件導入數據
- 給 Shell 命令傳遞參數
- 使用文件和軟件包
- 為配置文件添加配置行
- 使用 Augeas 自動修改配置文件
- 使用配置片段構建配置文件
- 使用 ERB 模板
- 在模板中遍歷數組
- 從第三方倉庫安裝軟件包
- 配置 APT 軟件倉庫
- 配置 GEM 倉庫
- 從源碼包自動構建軟件
- 比較軟件包的版本
- 用戶和虛擬資源
- 使用虛擬資源
- 使用虛擬資源管理用戶
- 管理用戶基于密鑰的 SSH 訪問
- 管理用戶的自定義文件
- 有效地分發 cron 任務
- 當文件更新時運行命令
- 使用主機資源
- 為文件資源指定多個源
- 使用文件資源遞歸地分發整個目錄樹
- 清理過期的舊文件
- 使用日程表資源
- 資源的審計
- 臨時禁用資源
- 管理時區
- 應用程序
- 管理 Apache 服務
- 創建 Apache 虛擬主機
- 創建 Nginx 虛擬主機
- 創建 MySQL 數據庫及用戶
- 管理 Drupal 站點
- 管理 Rails 應用程序
- 服務器和云基礎設施
- 部署 Nagios 監控服務器
- 使用 Heartbeat 構建高可用服務
- 管理 NFS 服務和文件共享
- 使用 HAProxy 為多個 web 服務器實現負載均衡
- 使用 iptables 管理防火墻
- 管理 Amazon 的 EC2 實例
- 使用 Vagrant 管理虛擬機
- 外部工具和 Puppet 生態環境
- 創建 Facter 的自定義 fact
- 在運行 Puppet 之前和之后執行命令
- 從 Shell 會話生成 Puppet 配置清單
- 從運行的系統上生成 Puppet 配置清單
- 使用 Puppet Dashboard
- 使用 Foreman
- 使用 MCollective
- 使用公共模塊
- 使用外部節點分類器
- 創建自定義的資源類型
- 創建自定義的提供者