myfinder's blog

myfinderの技術や周辺的活動のblog

2010年5月9日日曜日

最近VMをセットアップするときに使っているKickstartファイル vol.2

先日書いた記事からちょっと運用を変更したので、その件についてまとめます。

今までは各サーバごとに同じような内容のkickstartファイルを書いていたが、共通するようなセットアップファイルがたくさんできて大変気持ち悪かった。
それをどうにかするために、kickstartファイルから設定部分外だししてみた次第。


# myfinder's Kickstart file.

install
url --url http://{Local YUM Server}/centos/5/os/x86_64
lang ja_JP.UTF-8
keyboard jp106
network --device eth0 --bootproto static --ip {Local IP} --netmask {NETMASK} --gateway {GATEWAY} --nameserver {NAME SERVER} --hostname {HOSTNAME}
network --device eth1 --bootproto static --ip {GLOBAL IP} --netmask {NETMASK} --gateway {GATEWAY} --nameserver {NAME SERVER} --hostname {HOSTNAME}
rootpw --iscrypted {ENCRYPTED PASSWORD}
firewall --enabled --port=22:tcp
authconfig --enableshadow --enablemd5
selinux --enforcing
timezone Asia/Tokyo
bootloader --location=mbr
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
clearpart --all
part /boot --fstype ext3 --size=100
part pv.2 --size=0 --growvolgroup VolGroup00 --pesize=32768 pv.2
logvol / --fstype ext3 --name=LogVol00 --vgname=VolGroup00 --size=1024 --growlogvol swap --fstype swap --name=LogVol01 --vgname=VolGroup00 --size=512 --grow --maxsize=1024
reboot

%packages
@base
@core
@development-libs
@development-tools
@editors
keyutils
trousers
fipscheck
device-mapper-multipath
imake
ntp

%post
# post setting
wget -P /tmp http://{SVN Server}/svn/server-settings/trunk/kickstart/setup.sh
bash /tmp/setup.sh
rm -f /tmp/setup.sh


と、言う風にしておいて、setup.shを下記のように書いておく。


# server settings

# SELinux off
setenforce 0
# ntp setting
curl -o /etc/ntp.conf -O http://{SVN Server}/svn/server-settings/trunk/etc/ntp.confchmod 644 /etc/ntp.conf
chkconfig ntpd on
service ntpd start

# ldap setting
authconfig --enableldap --enableldapauth --ldapserver={LDAP Server} --ldapbasedn=dc=example,dc=jp --updateall
curl -o /etc/ldap.conf -O http://{SVN Server}/svn/server-settings/trunk/etc/ldap.conf
chmod 644 /etc/ldap.conf
curl -o /etc/pam.d/system-auth-ac -O http://{SVN Server}/svn/server-settings/trunk/etc/pam.d/system-auth-ac
chmod 644 /etc/pam.d/system-auth-ac

# root pw delete
perl -pi.bak -e "s/(root:)[^:].*?(:.*?)$/\1\!\!\2/" /etc/shadow
# ssh setting
yum remove -y openssh openssh-server openssh-clientsrpm -ivh http://{Local YUM Server}/rpms/i386/openssh-4.3p2-26.lpk.i686.rpm
rpm -ivh http://{Local YUM Server}/rpms/i386/openssh-clients-4.3p2-26.lpk.i686.rpm
rpm -ivh http://{Local YUM Server}/rpms/i386/openssh-server-4.3p2-26.lpk.i686.rpm
curl -o /etc/ssh/sshd_config -O http://{SVN Server}/svn/server-settings/trunk/etc/ssh/sshd_config
chmod 600 /etc/ssh/sshd_config
service sshd restart

# local yum repo setting
curl -o /etc/yum.repos.d/myfinder.repo -O http://{SVN Server}/svn/server-settings/trunk/etc/yum.repos.d/myfinder.repo
chmod 644 /etc/yum.repos.d/myfinder.repo

# iptables setting
curl -o /etc/sysconfig/iptables -O http://{SVN Server}/svn/server-settings/trunk/etc/sysconfig/iptables
chmod 600 /etc/sysconfig/iptables
service iptables restart

# system service setting
chkconfig ntpd on
chkconfig bluetooth off
chkconfig cups off
chkconfig firstboot off
chkconfig gpm off
chkconfig haldaemon off
chkconfig hidd off
chkconfig ip6tables off
chkconfig kudzu off
chkconfig pcscd off
chkconfig xfs off
chkconfig yum-updatesd off

# SELinux on
setenforce 1

# report mail
echo "$HOSTNAME setup completed" | mail -s "$HOSTNAME setup report" medianetworks@gmail.com


こうやっておけば、kickstartファイルや、その先のセットアップスクリプトもバージョン管理できるし、サーバ特有にすべき内容を分割できる。
あと、セットアップ後でもスクリプトを実行すれば新しい設定ファイルを適用できる。

2010年5月5日水曜日

最近VMをセットアップするときに使っているKickstartファイル

Linuxインストールの時にはkickstartを使うというのはよくやる手ですが、自分はVMでいっぱいノードを作るので、インストール時点でこのくらいまで設定済ませるようにしていますよという例をば。


# myfinder's Kickstart file.

install
url --url http://{インストール対象のOSが置いてあるWebサーバのURL}
lang ja_JP.UTF-8
keyboard jp106
network --device eth0 --bootproto static --ip {LOCAL IP} --netmask {NETMASK} --gateway {GATEWAY IP} --nameserver {LOCAL NAME SERVER} --hostname {HOSTNAME}
network --device eth1 --bootproto static --ip {GLOBAL IP} --netmask {NETMASK} --gateway {GATEWAY IP} --nameserver {LOCAL NAME SERVER} --hostname {HOSTNAME}
rootpw --iscrypted {ENCRYPTED PASSWORD}
firewall --enabled --port=22:tcp
authconfig --enableshadow --enablemd5
selinux --enforcing
timezone Asia/Tokyo
bootloader --location=mbr
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
clearpart --all
part /boot --fstype ext3 --size=100
part pv.2 --size=0 --grow
volgroup VolGroup00 --pesize=32768 pv.2
logvol / --fstype ext3 --name=LogVol00 --vgname=VolGroup00 --size=1024 --grow
logvol swap --fstype swap --name=LogVol01 --vgname=VolGroup00 --size=512 --grow --maxsize=1024
reboot

%packages
@base
@core
@development-libs
@development-tools
@editors
keyutils
trousers
fipscheck
device-mapper-multipath
imake
ntp

%post
# ntp setting
echo 'server {NTP SERVER}' >> /etc/ntp.conf
chkconfig ntpd on
/etc/init.d/ntpd start

# ldap
authconfig --enableldap --enableldapauth --ldapserver={LDAP SERVER} --ldapbasedn=dc=example,dc=com --update
echo 'session required pam_mkhomedir.so skel=/etc/skel umask=0022' >> /etc/pam.d/system-auth
echo 'sudoers_base ou=SUDOers,dc=example,dc=com' >> /etc/ldap.conf

# ssh
yum remove -y openssh openssh-server openssh-clients
rpm -ivh http://{LOCAL YUM SERVER}/rpms/i386/openssh-4.3p2-26.lpk.i686.rpm
rpm -ivh http://{LOCAL YUM SERVER}/rpms/i386/openssh-clients-4.3p2-26.lpk.i686.rpm
rpm -ivh http://{LOCAL YUM SERVER}/rpms/i386/openssh-server-4.3p2-26.lpk.i686.rpm
echo 'UseLPK yes' >> /etc/ssh/sshd_config
echo 'LpkLdapConf /etc/ldap.conf' >> /etc/ssh/sshd_config
echo 'LpkServers ldap://{LDAP SERVER}' >> /etc/ssh/sshd_config
echo 'LpkUserDN ou=People,dc=example,dc=com' >> /etc/ssh/sshd_config
echo 'LpkGroupDN ou=Group,dc=example,dc=com' >> /etc/ssh/sshd_config
echo 'LpkForceTLS no' >> /etc/ssh/sshd_config
sed -i.bak "s/PasswordAuthentication yes/PasswordAuthentication no/" /etc/ssh/sshd_config
/etc/init.d/sshd restart

# local yum repo
cd /etc/yum.repos.d
wget http://{LOCAL YUM SERVER}/yum/myfinder.repo

# system service
chkconfig ntpd on
chkconfig bluetooth off
chkconfig cups off
chkconfig firstboot off
chkconfig gpm off
chkconfig haldaemon off
chkconfig hidd off
chkconfig ip6tables off
chkconfig kudzu off
chkconfig pcscd off
chkconfig xfs off
chkconfig yum-updatesd off

# iptables
sed -i.bak "s/-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT/-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp -i eth0 --dport 22 -j ACCEPT/" /etc/sysconfig/iptables
/etc/init.d/iptables restart

# report mail
echo "Server setup completed" | mail -s 'server seetup report' medianetworks@gmail.com


肝心なのは「%post」セクションで、ここにインストール後に実施したいスクリプトを書いておく事ができる。
sedとかも使えるので便利。

2010年4月28日水曜日

「全部 わか子」スクリプトを書いてみる

某所でネタになっていたので、全部わか子的な変換を適当にするスクリプトを書いた。


package Acme::Zenbu::Waka::ko;

use strict;
use warnings;
use utf8;

use Data::Dumper;
use Encode;
use LWP::Simple;
use YAML;
use XML::Simple;

# request base
my $base_url = 'http://jlp.yahooapis.jp/MAService/V1/parse?';
# app key
my $app_id = 'your app id';
# sentence
my $sentence = $ARGV[0];

# encoding
$sentence =~ s/([^\w])/"%" . unpack("H2", $1)/eg;

# request url
my $request_url = $base_url . "appid=$app_id" . "&results=ma&sentence=" . $sentence;

# response
my $response = LWP::Simple::get($request_url) or die "Cannot get $request_url: $!";

# parse
my $parser = XML::Simple->new();
my $doc = $parser->XMLin($response);

my $words = $doc->{ma_result}->{word_list}->{word};

my $resentence = '';

while (my $word = shift(@{ $words })) {
if ($word->{pos} eq '形容詞') {
$resentence .= $word->{surface} . 'の';
}
elsif ($word->{pos} eq '名詞') {
$resentence .= $word->{surface} . ' ';
}
elsif ($word->{pos} eq '助詞') {
$resentence .= $word->{surface} . ' ';
}
else {
$resentence .= $word->{surface};
}
}

print Encode::encode('utf8', $resentence);


これの結果


「へやくるもOK」->「へ や くるも OK 」


とか


「台北一番安い店」->「台北 一番安いの店」


とか変換された。
形容詞の前に「の」を入れたり、名詞や助詞の後に空白を入れるだけで割と雰囲気でるもんですね。
ニーズはないだろうけどもうちょっと作り込むと、アジア各国で見かけるえせ日本語ジェネレータとかになるのかな〜と思う。


SEE ALSO : http://twitter.com/wakako_zenbu

2010年1月24日日曜日

SELinuxにソフトウェアの動作を阻まれた場合の対応

SELinuxは真っ先に切られがちなものですが、予期しない動作、挙動を防ぐ意味でも切らずに運用すべきです。
が、SELinuxに動作が阻まれたとき、どうやってそれを知るか、ということと、どうやって許可するのかということについてはあまり情報がないです。
ここでは自分がやっている方法についてまとめます。

SELinuxのアクセス制限に阻まれたものは、/var/log/audit/audit.logに出力されるので、このログからポリシーを作って許可するのが楽でいいです。
阻まれているかどうかは上記ファイルをtailしてソフトウェアを動かせばわかります。

以下手順


# setenforce 0
# cat /var/log/audit/audit.log | audit2allow -m local > local.te
# checkmodule -M -m -o local.mod local.te
checkmodule: loading policy configuration from local.te
checkmodule: policy configuration loaded
checkmodule: writing binary representation (version 6) to local.mod
# semodule_package -o local.pp -m local.mod
# semodule -i local.pp
# setenforce 1


必要に応じてlocal.teの内容は書き換えています。

2010年1月23日土曜日

アカウントに閉じたPerl5の環境を作る(またはぐちょぐちょなPerl環境をなかったことにする話)

Perlは大抵の環境にはじめから入っているので大変便利なんだけども、Perlのバージョン含めていろいろとコントロールしたいニーズもあったりすると思います。

例えばRedHat系のLinuxだとyumとかで何かのパッケージを入れるときに、CPANモジュールもrpmで入ってしまったりなどして、システムのPerlべったりにものを作るとあとあと面倒なことになったりすることもあるかもしれません。
そんなときには、アカウントに閉じたPerlの環境を作ってしまうのがよいでしょう。

今回はCentOS5.3(x86_64)でのやり方。(CentOS5.3のPerlって5.8.8と古いので丁度いいかなと思う)
hogeというユーザでやる事を想定します。

1.とりあえずperlをビルドしたりする場所を作る

今回は「~/local/perl」とかにしときます。
まず、Perlのソースコードを落としてくるディレクトリを作っときます。
~/local/srcとかでいいでしょう。


$ mkdir -p ~/local/src


2.Perlのダウンロードとインストール


$ cd ~/local/src
$ wget http://www.cpan.org/src/perl-5.10.1.tar.gz
$ tar zxvf perl-5.10.1.tar.gz
$ cd perl-5.10.1
$ sh Configure -Dprefix=$HOME/local/perl-5.10.1 -Dusethreads -Dusemultiplicity -Duse64bitall
$ make
$ make test
$ make install
$ ln -s ~/local/perl-5.10.1 ~/local/perl


これで~/local/perl/bin/perlとか打てば呼べるようになります。

3.PATHに追加

インストールできたとはいえ、いちいち~/local/perl/bin/perlとか呼ぶのはめんどくさいです。
そういうときは環境変数PATHに~/local/perl/binを追加しときましょう。
下記行を~/.bashrcとかに入れとけばOKです。


export PATH="$HOME/local/perl/bin:$PATH"


とりあえずログアウトしてログインするか、sourceコマンドで環境変数に反映させときましょう。

4.おわりに

ここまでやっとくと、hogeユーザにおけるperl関係のコマンドは全部~/local/perl/binの方が優先して使われるようになります。
なので、cpanコマンドとかも/usr/bin/cpanとか明示しない限りは~/local/perl/bin/cpanが使われ、hogeユーザでインストールしたcpanモジュールは~/local/perl/libに入るようになります。

また、この辺は重要なポイントの一つですが、コマンドラインから使うPerlスクリプトを書くときにはshebang行に「#!/usr/bin/perl」とか書くと思いますが、今回のように独自にインストールした場合には「#!/usr/bin/env perl」と書いておくようにしましょう。
(#!/home/hoge/local/perl/bin/perlでもいいです)

2009年12月15日火曜日

DBIのDBハンドルメソッドとステートメントハンドルメソッドの対応メモ

DBIの各メソッドの呼び出し関係のメモ。

各メソッドと裏で呼んでるメソッド
・DBハンドルメソッド
1. selectrow_array
- fetchrow_arrayref

2. selectrow_arrayref
- fetchrow_arrayref

3. selectrow_hashref
- fetchrow_hashref

4. selectall_arrayref
- fetchall_arrayref

5. selectall_hashref
- fetchall_hashref

6. selectcol_arrayref
- fetch

7. prepare_cached
- prepare

・ステートメントハンドルメソッド

8. execute_array, execute_for_fetch
- execute

9. fetchrow_arrayref
- execute

10. fetchrow_array
- none

11. fetchrow_hashref
- execute

12. fetchall_arrayref
- fetch, fetchrow_hashref

13. fetchall_hashref
- fetch

2009年9月27日日曜日

Snow LeopardでShipItを使えるようにする

PerlモジュールのリリースツールShipItを勧められたので入れてみた記録。

1.必要なモジュールのインストール
ShipItを使うのに必要なモジュールをインストールする

$ cpan -i ShipIt
$ cpan -i AppConfig::Std
$ wget http://search.cpan.org/CPAN/authors/id/B/BR/BRADFITZ/cpan-upload-http-2.4.tar.gz
$ tar zxvf cpan-upload-http-2.4.tar.gz
$ cd cpan-upload-http-2.4
$ perl Makefile.PL
$ make
$ make install
$ cpan -i ShipIt::Step::CommitMessageWrap

2.「~/.pause」ファイルの準備
ホームディレクトリに.pauseファイルを作成

$ vim ~/.pause
$ cat ~/.pause
user xxxxxx
password xxxxxxx
mailto xxxxxxx@xxxxxx.xxx
$ chmod 600 ~/.pause

3.「.shipit」ファイルを用意する
ShipItを利用するには、プロジェクトのトップディレクトリに.shipitファイルが必要。
Module::Setupとかを使っていれば自動的に生成されているので特に気にする必要はない。

4.dry-runオプションで試してみる
いきなり実行するのはよくないので、「--dry-run」をつけて実行して結果を確認する。

5.おわり
ここまでで使えるようになっているはず