ubuntu のインストール直後の調整 (その 4)

[自動アップグレードの設定]

先の記事でメールサーバーをインストールしたので、次にインストールされているパッケージのアップグレードを自動的かつ定期的に行うためのパッケージをインストールする。
ここでインストールするパッケージは cron-apt というパッケージだ。

実は ubuntu でパッケージのアップグレードを自動的かつ定期的に行わせること自体は、そんなに難しいことではない。
sudo aptitude update; sudo aptitude –y safe-upgrade というコマンドと、cron の仕組みを知っていれば簡単に作ることができる。
それでもきちんとパッケージ化されているものを使う方が (設定の仕方を知っていれば) 圧倒的に楽で便利なことは間違いない。

それでは cron-apt パッケージをインストールするために、以下のコマンドを実行する。

ここではパッケージの操作 (インストール、アンインストール) に aptitude コマンドを使っている。
ubuntu (debian 系 linux ディストリビューション) ではパッケージのインストール、アンインストールを行うコマンドが複数あるが、インストールされたパッケージが明示的に指定されたのか、依存関係によって非明示的にインストールされたのかを管理する方法が別であるため、これらを混在させて使用しない方が望ましい。
~$ sudo apttiude update; sudo aptitude install cron-apt

パスワードを訊いてきたらログインしたときのパスワードを入力する (直前の sudo コマンドの使用から時間が空いていなければパスワードは訊かれない。)

cron-apt と bsd-mailx の二つのパッケージをインストールして良いか確認してくるので ‘y’ を入力してEnter を押す (すでに bsd-mailx パッケージがインストールされているなら、bsd-mailx は表示されない。)
パッケージのダウンロードと展開が順次行われ、それらが終わるとコマンドプロンプトが表示される。
Postfix をインストールしたときとは異なり、設定のためのダイアログが表示されることはない。

cron-apt の設定は /etc/cron-apt/config ファイルを直接編集することで行う。
また cron-apt をインストールしたときに作成される /etc/cron-apt/config ファイルでは、/etc/cron-apt/action.d ディレクトリや /etc/cron-apt/config.d ディレクトリなど、/etc/cron-apt ディレクトリ以下のディレクトリに含まれるファイルを読み込むようになっている。
/etc/cron-apt/config ファイルを編集する際には、この /etc/cron-apt ディレクトリ以下のディレクトリに含まれるファイルを読み込むようになっている箇所は、手を触れない方が良いだろう。

インストールしたままで何も設定変更しなかった場合、cron-apt は以下のコマンドを定期的に実行し、コマンド実行中に何らかのエラーが起きると、それを root 宛てにメールで通知されるようになっている。

apt-get update -o quiet=2
apt-get autoclean -y
apt-get dist-upgrade -d -y -o APT::Get::Show-Upgraded=true

残念なことに、これでは期待通りの動作にならないことが、すぐに分かる。
これだとアップグレード対象のパッケージをダウンロードしているだけだ。
更に、パッケージの操作には apt-get コマンドを使っている。

これまで一貫して、パッケージのインストールに aptitude コマンドを使ってきた。
繰り返しになるが、aptitude と apt-get ではパッケージの管理方法 (依存関係による自動インストール状態の管理) が異なるために、混在させて使うことはしたくない。
そこでまずは cron-apt が使うパッケージ管理コマンドを apt-get から aptitude に変更する。
そのためには /etc/cron-apt/config ファイル中の APTCOMMAND エントリを aptitude にすればよい。

通知メールの宛先も root から別のものに変えておく。
/etc/cron-apt/config ファイル中の MAILTO エントリを root から upgrede-report にしておこう。
これで cron-apt からのメール通知は upgrede-report 宛てに送られるようになる。
なおここで指定した宛先はドメイン部が無いので、/etc/aliases にupgrede-report 宛てのメールを実在するメールアドレスに転送するように設定するのを忘れないようにする。

これら 2 箇所を変更した /etc/cron-apt/config の内容を以下に記載する。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Configuration for cron-apt.
# The cron config is located in /etc/cron.d/cron-apt
# This shows the defaults.
#

# The command used to execute all actions.  By default, apt-get is used.
# Change this to /usr/bin/aptitude to use aptitude instead, which will
# resolve changed Recommends (and Suggests as well, if aptitude is so
# configured). You can also set other utilities (especially useful in the
# config.d directory) so set some completely different tool.
# OBSERVE that this tool is indended for apt-get and tools like aptitude do not
# have full support for noninteractive upgrades. You may have to tune options
# to not create infinit logfiles for example.
# APTCOMMAND=/usr/bin/apt-get
# APTCOMMAND=/usr/bin/aptitude
# APTCOMMAND=/usr/bin/apt-file
APTCOMMAND=/usr/bin/aptitude

# A path is needed for this to work. This is the default PATH.
# export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin

# The random sleep time in seconds. This is used to prevent clients from
# accessing the APT sources all at the same time and overwhelming them.
# Default is 3600 seconds which means one hour.
# RUNSLEEP=3600

# The minimum amount of disc space (in kB) that need to exist on the
# device where temporary files are created (mktemp) to allow cron-apt
# to run. If set to 0 it will always continue even if empty.
# MINTMPDIRSIZE=10

# The directory where the actions is stored.
# ACTIONDIR="/etc/cron-apt/action.d"

# The directory where configuration per action is stored. The message file
# must have the same name as the action file.
# ACTIONCONFDIR="/etc/cron-apt/config.d"

# The directory where messages that will be prepended to the email that is
# sent (per action) is stored. The message file must have the same name as
# the action file.
# MAILMSGDIR="/etc/cron-apt/mailmsg.d"

# The directory where messages that will be prepended to text that is
# sent (per action) to syslog. The message file must have the same name as
# the action file.
# SYSLOGMSGDIR="/etc/cron-apt/syslogmsg.d"

# The directory where messages that will be prepended to the error message
# (per action) is stored. The message file must have the same name as
# the action file.
# ERRORMSGDIR="/etc/cron-apt/errormsg.d"

# The directory where messages that will be prepended to the log (debug)
# message (per action) is stored. The message file must have the same name as
# the action file.
# LOGMSGDIR="/etc/cron-apt/logmsg.d"

# The directory where messages that will be prepended to the mail message
# (per MAILON type) is stored. The message file must have the same name as
# the $MAILON directive.
# MAILONMSGSDIR="/etc/cron-apt/mailonmsgs"

# The directory where messages that will be prepended to the syslog message
# (per SYSLOGON type) is stored. The message file must have the same name as
# the $SYSLOGON directive.
# SYSLOGONMSGSDIR="/etc/cron-apt/syslogonmsgs"

# Value: ""       (warn if dotlockfile not installed)
#        "nowarn" (don't give warning if dotlockfile not installed)
# NOLOCKWARN=""

# The file that contains error messages.
# ERROR="/var/log/cron-apt/error"

# The file that contains current run information
# when still running the script.
# TEMP="/var/log/cron-apt/temp"

# The logfile (for debugging). Use syslog for normal logging.
# LOG="/var/log/cron-apt/log"

# The mail file.
# MAIL="/var/log/cron-apt/mail"

# The email address to send mail to.
# MAILTO="root"
MAILTO="upgrade-report"

# When to send email about the cron-apt results.
# Value: error   (send mail on error runs)
#        upgrade (when packages are upgraded)
#        changes (mail when change in output from an action)
#        output  (send mail when output is generated)
#        always  (always send mail)
#                (else never send mail)
# MAILON="error"

# Value: error   (syslog on error runs)
#        upgrade (when packages is upgraded)
#        changes (syslog when change in output from an action)
#        output  (syslog when output is generated)
#        always  (always syslog)
#                (else never syslog)
# SYSLOGON="upgrade"

# Value: error   (exit on error only)
#                (else never exit)
# EXITON="error"

# Value: verbose (log everything)
#        always  (always log)
#        upgrade (when packages is upgraded)
#        changes (log when change in output from an action)
#        output  (log when output is generated)
#        error   (log error runs only)
#                (else log nothing)
# DEBUG="output"

# What to do with the diff when *ON=changes.
# Value: prepend (prepend to the output)
#        append  (append to the output)
#        only    (only show the diff, not the output itself)
#                (else do nothing)
# DIFFONCHANGES=prepend

# General apt options that will be passed to all APTCOMMAND calls.
# Use "-o quiet" instead of "-q" for aptitude compatibility.
#  OPTIONS="-o quiet=1"
# You can for example add an additional sources.list file here.
#  OPTIONS="-o quiet=1 -o Dir::Etc::SourceList=/etc/apt/security.sources.list"
# You can also set an alternative sources.list file here.
#  OPTIONS="-o quiet=1 -o Dir::Etc::SourceList=/etc/apt/security.sources.list -o Dir::Etc::SourceParts=\"/dev/null\""
# If you want to allow unauthenticated and untrusted packages add the
# following to your options directive.
#  OPTIONS="-o quiet=1 -o APT::Get::AllowUnauthenticated=true -o aptitude::Cmdline::ignore-trust-violations=yes"
# To limit the bandwidth used use the following line. This example limit the
# bandwidth usage to 25 kB/s.
OPTIONS="-o Acquire::http::Dl-Limit=25"

# additional APT configuration file that is loaded first. This can be set in
# order to use a completely different APT configuration for cron-apt. See the
# /usr/share/doc/cron-apt/README and apt.conf(5) for details
# export APT_CONFIG=/etc/apt/cron.apt.paths

# Do not run the command, if there is an error in the previous run (default).
# Value: error   (do not run if there is an error on last run)
#                (else always run, remove previous error file and run)
# DONTRUN=""

# If this file exist cron-apt will silently exit.
# REFRAINFILE=/etc/cron-apt/refrain

# If this is non-empty, it will be used as the host name in subjects of
# generated e-mail messages. If this is empty, the output of uname -n
# will be used.
# HOSTNAME=""

# Ignore lines matching this regexp to determine whether changes occurred
# for MAILON="changes". If empty no lines will be ignored.
# Suggested value for aptitude:
#  DIFFIGNORE="^\(Get:[[:digit:]]\+\|Hit\|Ign\|Del\|Fetched\|Freed\|Reading\)[[:space:]]"

# Suggested value for apt-get:
#  DIFFIGNORE="^\(Get:[[:digit:]]\+\|Hit\|Ign\)[[:space:]]"
# Default:
#  DIFFIGNORE=""

# Support for mail headers. You can add up to 9 headers to the mail sent.
# This below example can be used for OTRS.
# The default is no headers added.
# XHEADER1="X-OTRS-Queue: updates"
# XHEADER2="X-OTRS-Loop: true"
# ...

# Umask for the files created by the APT command used. In all other cases
# umask 077 will be used.
# UMASK_APT="022"

/etc/cron-apt/config ファイルを 2 箇所変更したことで、cron-apt は以下のコマンドを定期的に実行し、コマンド実行中に何らかのエラーが起きると、それを cupgrede-report 宛てにメールで通知するようになった。

aptitude update -o quiet=2
aptitude autoclean -y
aptitude dist-upgrade -d -y -o APT::Get::Show-Upgraded=true

しかし、未だにアップグレード対象のパッケージをダウンロードしているだけで、実際にアップグレードするようにはなっていない。
実際にアップグレードまでを行うには、/etc/cron-apt/action.d ディレクトリにファイルを一つ追加すればいい。

/etc/cron-apt/action.d ディレクトリには、既に /etc/cron-apt/acriont.d/0-update と /etc/cron-apt/acriont.d/3-download の二つのファイルがある。
これらのファイルの内容は以下のようになっており、各行の頭に /etc/cron-apt/config ファイルの APTCOMMAND エントリで指定したコマンドを付け、ファイル名の順番に並べると、先に挙げた cron-apt が実行するコマンドになる。

/etc/cron-atp/action.d/0-update
update -o quiet=2
/etc/cron-atp/action.d/3-download
uautoclean -y
dist-upgrade -d -y -o APT::Get::Show-Upgraded=true

つまり、アップグレードを行うコマンドになるような内容で、3-donwload よりも後になる名前のファイルをこの /etc/cron-apt/action.d ディレクトrに作成すればいいということだ。
具体的には 4-upgrade という名前で以下の内容のファイルを /etc/cron-atp/action.d ディレクトリに作成するだけだ。

/etc/cron-atp/action.d/4-upgrade
safe-upgrade -y -o APT::Get::Show-Upgraded=true

これで cron-apt が以下のコマンドを実行するようになる。

aptitude update -o quiet=2
aptitude autoclean -y
aptitude dist-upgrade -d -y -o APT::Get::Show-Upgraded=true
aptitude safe-upgrade -y -o APT::Get::Show-Upgraded=true

ところで、cron-apt は毎日午前 4 時に定期的に実行されるようになっている。
これは /etc/cron.d/cron-apt ファイルで指定されている。
/etc/cron.d ディレクトリにあることからも分かるように、cron-apt は定期的な実行には cron を使用している。
起動時刻や回数などを変更したいときは、この /etc/cron.d/cron-apt ファイルを編集すればよい。
このファイルを編集した後に cron デーモンを再起動や再読込をする必要は無い。

関連する記事 (Related posts):

  1. ubuntu のインストール直後の調整 (その 3)
  2. ubuntu のインストール直後の調整 (その 1)
  3. ubuntu のインストール直後の調整 (その 2)
  4. ubuntu 10.04 をメールサーバーに (DKIM 署名を付加)
  5. ubuntu 10.04 をメールサーバーに (DKIM 署名の検証)

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <img localsrc="" alt="">