Coding style, abstracting "cron" vs "crond"


#1

hiya. i have a lot of WET code when it comes to controlling cron on CentOS vs
Ubuntu. anyone have suggestions on abstracting this, making it DRY?

on CentOS, the service is known as “crond”, Ubuntu, “cron”. bummer.

thots? thanks!
kallen

template “/etc/cron.d/blah-maint” do
source "blah-maint-cron.erb"
mode 0644
if platform?(“redhat”, “centos”)
notifies :reload, “service[crond]”, :delayed
elsif platform?(“debian”, “ubuntu”)
notifies :reload, “service[cron]”, :delayed
end
end

if platform?(“redhat”, “centos”)
service “crond” do
supports [ :reload, :status ]
action [ :reload ]
end
end
if platform?(“debian”, “ubuntu”)
service “cron” do
supports [ :reload, :status ]
action [ :reload ]
end
end


#2

My shorter version follows, which could be optimized further using
platform_family instead of platform (at least on recent versions).

On Tue, Sep 18, 2012 at 2:15 PM, kallen@groknaut.net wrote:

hiya. i have a lot of WET code when it comes to controlling cron on CentOS
vs
Ubuntu. anyone have suggestions on abstracting this, making it DRY?

on CentOS, the service is known as “crond”, Ubuntu, “cron”. bummer.

thots? thanks!
kallen

default to cron

cron_service_name = “cron”

if platform?(“redhat”, “centos”)

cron_service_name = “crond”

elsif platform?(“debian”, “ubuntu”)

cron_service_name = “cron”

end

template “/etc/cron.d/blah-maint” do

source “blah-maint-cron.erb”

mode 0644
notifies :reload, “service[cron]”, :delayed

end

service “cron” do
service_name cron_service_name

supports [ :reload, :status ]

action [ :reload ]

end


Joshua Buysse, System Engineer
College of Liberal Arts, Office of Information Technology
University of Minnesota

“On two occasions I have been asked, ‘Pray, Mr. Babbage, if you
put into the machine wrong figures, will the right answers come
out?’ I am not able rightly to apprehend the kind of confusion of
ideas that could provoke such a question.”

  • Charles Babbage

#3

On Tue, Sep 18, 2012 at 12:15 PM, kallen@groknaut.net wrote:

template “/etc/cron.d/blah-maint” do
source "blah-maint-cron.erb"
mode 0644
if platform?(“redhat”, “centos”)
notifies :reload, “service[crond]”, :delayed
elsif platform?(“debian”, “ubuntu”)
notifies :reload, “service[cron]”, :delayed
end
end

if platform?(“redhat”, “centos”)
service “crond” do
supports [ :reload, :status ]
action [ :reload ]
end
end
if platform?(“debian”, “ubuntu”)
service “cron” do
supports [ :reload, :status ]
action [ :reload ]
end
end

Paraphrasing from the cron cookbook and extrapolating a bit:

case node[‘platform’]
when “redhat”, “centos”, “scientific”, “fedora”, "amazon"
svc = "crond"
when “debian”, “ubuntu”, "suse"
svc = "cron"
end

service ‘cron’ do
service_name svc
supports [ :reload, :status ]
action :reload
end

template “/etc/cron.d/blah-maint” do
source "blah-maint-cron.erb"
mode 0644
notifies :reload, "service[cron]"
end

If you can just agree to call the cron service cron, you can set the
actual service_name that chef will use based on the platform driven
variable and no one else has to care.

KC


#4

cool, thanks y’all

k