~nabeken/diary/

Gentoo Linux(6年くらい)とFreeBSD(1年くらい)とOpenBSD(新参者)を使う日々。


IHANet BGP peering overview

Puppet での俺プラクティスメモ

Posted on Thu Aug 14 02:28:28 +0900 2008 by nabeken

ntp だとクライアントとサーバに別れている。そんなときは

サーバとクライアントが別れているサービスとか

class ntp {
	file { "/etc/ntp.conf":
		mode => "0644",
		owner => root,
		group => root,
		content => template("default/etc/ntp.conf"),
		notify => Service["ntpd"],
	}
}

class ntp_server inherits ntp {
	File["/etc/ntp.conf"] {
		content => template("${hostname}/etc/ntp.conf"),
	}
}

として、クライアント側はたんに include ntp すればよい。サーバ側は include ntp_server をして、設定ファイルは自分のホスト名以下を使うようにすれば簡潔に分離できそう。

class vs define

本家にも書いてあるが、最大の違いはインスタンスが作れるか否か。

http://reductivelabs.com/trac/puppet/wiki/LanguageTutorial#classes-vs-definitions

Definitions are used to define reusable objects which will have multiple instances on a given host, so they cannot include any resources that will only have one instance, such as a package or a root-level service. Classes, on the other hand, are guaranteed to be singletons -- you can include them as many times as you want and you'll only ever get one copy of the resources -- so they are exactly meant to include these singleton objects.

PowerDNS などは複数のインスタンスを作ることができるので、classは似合わない。この場合、defineで定義するのがよさでである。とりあえず、うまく書けたのでモジュールにしてみる。

もともとはこんな感じ。

class powerdns {
	$pdns_server_pkg = $os ? {
		debian => "pdns-server",
		gentoo => "pdns",
		default => "pdns",
	}

	case $os {
		centos: { }
		default: { package { "pdns_server": name => $pdns_server_pkg, ensure => installed } }
	}

	case $os {
		debian: { package { "pdns-backend-mysql": ensure => installed } }
		debian: { }
	}
}

class powerdns_auth inherits powerdns {
	service { "pdns": ensure => running, enable => true, require => [Service["mysql_server"], Package["pdns_server"]]}
}

class powerdns_local inherits powerdns {
	service { "pdns.local":
		name => "pdns.local",
		ensure => running,
		enable => true,
		require => [Service["mysql_server"], Package["pdns_server"]],
		pattern => "/usr/sbin/pdns_server-local-instance"
	}

	file { "/etc/init.d/pdns.local":
		ensure => link,
		target => "/etc/init.d/pdns"
	}
}

class powerdns_rec inherits powerdns {
	case $os {
		centos: {}
		default: { package { "pdns-recursor": ensure => installed } }
	}

	file { "/etc/named.root":
		owner => root, 
		group => root, 
		mode => 0644, 
		ensure => present, 
		content => template("named.root")
	}

	$pdns_recursor_service = $os ? {
		gentoo => "precursor",
		centos => "pdns-recursor",
		default => "pdns-recursor",
	}

	service { "pdns_recursor":
		name => $pdns_recursor_service,
		ensure => running,
		enable => true,
		pattern => "pdns_recursor" 
	}
}


define powerdns_conf_auth( $pdns_bind_v4, $pdns_bind_v6, $pdns_db_user = "pdns", $pdns_db_name = "pdns", $pdns_recursor = false ) {
	$pdns_db_host = $name
	file { "/etc/powerdns/pdns.conf":
		content => template("pdns.conf.erb"),
		notify => Service["pdns"]
	}
}


define powerdns_conf_local( $pdns_recursor = "127.0.0.1", $pdns_bind_v4 = "127.0.0.1", $pdns_bind_v6 = "::1", $pdns_db_user = "pdns", $pdns_db_name = "pdns-local" ) {
	$pdns_db_host = $name
	file { "/etc/powerdns/pdns-local.conf":
		content => template("pdns.conf.erb"),
		notify => Service["pdns.local"]
	}
}


define powerdns_conf_rec() {
	file { "/etc/powerdns/recursor.conf":
		content => template("recursor.conf.erb"),
		notify => Service["pdns_recursor"]
	}

	file { "/var/empty": ensure => directory }
}

非常にアドホックに書いてあるのがよくわかる。そんなわけで、最近書きなおしたのが次。

class powerdns::auth {
	$pdns_server_pkg = $os ? {
		debian => "pdns-server",
		gentoo => "pdns",
		default => "pdns",
	}

	case $os {
		centos: { }
		default: { package { $pdns_server_pkg: ensure => installed } }
	}

	case $os {
		debian: { package { "pdns-backend-mysql": ensure => installed } }
		default: { }
	}
}

define powerdns::contents( $db_host = "127.0.0.1", $db_user = "pdns", $db_name = "pdns", $recursor = false, $bind_v4, $bind_v6) {
	case $name {
		"pdns": {
			service { "pdns":
				ensure => running,
				enable => true, 
				require => Service["$mysql::mysql_pkg"]
			}

			file { "/etc/powerdns/pdns.conf":
				content => template("default/etc/powerdns/pdns.conf"),
				notify => Service["pdns"],
			}

		}
		default: {
			file { "/etc/init.d/pdns.${name}":
				ensure => link,
				target => "/etc/init.d/pdns"
			}

			service { "pdns.${name}":
				ensure => running,
				enable => true, 
				require => Service["$mysql::mysql_pkg"],
				pattern => "/usr/sbin/pdns_server-${name}-instance",
			}

			file { "/etc/powerdns/pdns-${name}.conf":
				content => template("default/etc/powerdns/pdns.conf"),
				notify => Service["pdns.${name}"],
			}
		}
	}
}

class powerdns::recursor {
	case $os {
		centos: {}
		default: { package { "pdns-recursor": ensure => installed } }
	}
}

define powerdns::cache() {

	file { "/etc/named.root":
		owner => root, 
		group => root, 
		mode => 0644, 
		ensure => present, 
		content => template("default/etc/named.root")
	}

	$pdns_recursor_service = $os ? {
		gentoo => "precursor",
		default => "pdns-recursor",
	}

	service { $pdns_recursor_service:
		ensure => running,
		enable => true,
		pattern => "pdns_recursor" 
	}

	file { "/etc/powerdns/recursor.conf":
		content => template("default/etc/powerdns/recursor.conf"),
	}
}

パッケージ定義はインスタンスの数によらず1つで済むので、class で定義している。気に入らないのは class と define で名前が同じにできないところ。

キャッシュサーバだけ使う場合は

 include powerdns::recursor

 powerdns::cache { "127.0.0.1": }

で 127.0.0.1 に pdns_recursor が上がる。

更新履歴

  • powerdns モジュールの話 (Thu, 14 Aug 2008 02:28:28 +0900)
  • 初稿 (Thu, 14 Aug 2008 02:28:28 +0900)