Added service provider for OpenBSD's new rc scripts

Old provider for OpenBSD renamed to openbsd_old.
This commit is contained in:
Ossi Salmi 2011-11-28 00:58:50 +02:00 committed by Timo Mkinen
parent a82489244e
commit 8ce5c0b5b7
2 changed files with 238 additions and 107 deletions

View file

@ -1,124 +1,127 @@
# Manage OpenBSD services. Enable/disable using /etc/rc.local, /etc/rc.conf.local
# Manage OpenBSD services. Enable/disable using /etc/rc.conf.local
Puppet::Type.type(:service).provide :openbsd, :parent => :base do
desc "OpenBSD service management."
defaultfor :operatingsystem => [:openbsd]
version = ["4.9", "5.0"]
confine :operatingsystem => :openbsd
confine :operatingsystemrelease => version
defaultfor :operatingsystem => :openbsd
@@rclocal = "/etc/rc.local"
@@rcconf = "/etc/rc.conf"
@@rcconflocal = "/etc/rc.conf.local"
@@rc_dir = '/etc/rc.d'
@@rcconf = '/etc/rc.conf'
@@rcconf_local = '/etc/rc.conf.local'
def getrcconf
File.readlines(@@rcconf).each { |line|
if line =~ /^#{@resource[:name]}_flags=.*/
return "#{@resource[:name]}_flags"
elsif line =~ /^#{@resource[:name]}=.*/
return @resource[:name]
end
}
return false
def rcscript
return File.join(@@rc_dir, @resource[:name])
end
def rcvar
name = @resource[:name]
File.open(@@rcconf).each do |line|
return line.strip.split("=", 2) if line =~ /^#{name}(_flags)?=/
end
nil
end
def rcvar_local
name = @resource[:name]
File.open(@@rcconf_local).each do |line|
return line.strip.split("=", 2) if line =~ /^#{name}(_flags)?=/
end
nil
end
def rcvar_name
[self.rcvar_local, self.rcvar].each do |rcvar|
return rcvar[0] unless rcvar.nil?
end
"%s_flags" % @resource[:name]
end
def rcvar_value
[self.rcvar_local, self.rcvar].each do |rcvar|
return rcvar[1].gsub(/"?([^"]*)"?/, '\1') unless rcvar.nil?
end
nil
end
def pkg_scripts
File.open(@@rcconf_local).each do |line|
if line =~ /^pkg_scripts=/
return line.strip.gsub(/pkg_scripts="?([^"]*)"?/, '\1').split
end
end
Array.new
end
def enabled?
if not defined? @resource[:start]
raise Puppet::Error,
"Services must specify a start command or a binary"
end
flag = getrcconf()
if flag
File.readlines(@@rcconflocal).each { |line|
line = line.strip.split(/=/, 2)
next unless line[0] == flag
if line[1] == "NO"
return :false
end
return :true
}
return :false
else
inlocal = false
File.readlines(@@rclocal).each { |line|
line = line.strip
if not inlocal
next unless \
line == "# Add your local startup actions here."
inlocal = true
else
if line == "echo '.'"
inlocal = false
break
end
next unless line =~ /^echo -n \" #{@resource[:name]}\" ; .*/
return :true
end
}
return :false
end
name = @resource[:name]
rcvar = self.rcvar
rcvar_value = self.rcvar_value
pkg_scripts = self.pkg_scripts
if rcvar.nil? and not pkg_scripts.include?(name)
return :false
end
if rcvar_value.nil? or rcvar_value == "NO"
return :false
end
:true
end
def enable
flag = getrcconf()
if flag
newdata = ""
File.readlines(@@rcconflocal).each { |line|
if line.strip.split(/=/, 2)[0] == flag
next
else
newdata += line
end
}
if flag == @resource[:name]
newdata += "%s=YES\n" % flag
elsif flag == "#{@resource[:name]}_flags"
if @resource[:start] != nil and @resource[:binary] != nil
args = @resource[:start][/^#{@resource[:binary]} (.*)/, 1]
end
newdata += "%s=\"%s\"\n" % [flag, args]
end
f = File.open(@@rcconflocal, "w")
f.write(newdata)
f.close
else
newdata = ""
inlocal = false
File.readlines(@@rclocal).each { |line|
if line == "# Add your local startup actions here.\n"
newdata += line
newdata += "echo -n \" %s\" ; %s\n" % [@resource[:name],
@resource[:start]]
next
end
newdata += line
}
f = File.open(@@rclocal, "w")
f.write(newdata)
f.close
end
return :true
name = @resource[:name]
rcvar = self.rcvar
rcvar_name = self.rcvar_name
rcvar_value = ""
pkg_scripts = self.pkg_scripts
start = @resource[:start]
binary = @resource[:binary]
s = ""
File.open(@@rcconf_local).each do |line|
next if line =~ /^(#{rcvar_name}|pkg_scripts)=/
s += line
end
unless start.nil?
if binary.nil?
rcvar_value = start[/^\S+\s*(.*)/, 1]
else
rcvar_value = start[/^#{binary}\s*(.*)/, 1]
end
end
s += "%s=\"%s\"\n" % [rcvar_name, rcvar_value]
pkg_scripts << name if rcvar.nil? and not pkg_scripts.include?(name)
s += "pkg_scripts=\"%s\"\n" % pkg_scripts.join(" ")
File.open(@@rcconf_local, "w") { |f| f << s }
end
def disable
flag = getrcconf()
if flag
newdata = ""
File.readlines(@@rcconflocal).each { |line|
if line.strip.split(/=/, 2)[0] == flag
next
else
newdata += line
end
}
if flag == @resource[:name] or flag == "#{@resource[:name]}_flags"
newdata += "%s=NO\n" % flag
end
f = File.open(@@rcconflocal, "w")
f.write(newdata)
f.close
else
print "disabling services from rc.local not implemented\n"
end
return :true
name = @resource[:name]
rcvar = self.rcvar
rcvar_name = self.rcvar_name
pkg_scripts = self.pkg_scripts
s = ""
File.open(@@rcconf_local).each do |line|
next if line =~ /^(#{rcvar_name}|pkg_scripts)=/
s += line
end
s += "%s=NO\n" % rcvar_name unless rcvar.nil?
pkg_scripts.delete(name)
s += "pkg_scripts=\"%s\"\n" % pkg_scripts.join(" ")
File.open(@@rcconf_local, "w") { |f| f << s }
end
end
def startcmd
[self.rcscript, :start]
end
def stopcmd
[self.rcscript, :stop]
end
def statuscmd
[self.rcscript, :check]
end
end

View file

@ -0,0 +1,128 @@
# Manage OpenBSD services. Enable/disable using /etc/rc.local, /etc/rc.conf.local
Puppet::Type.type(:service).provide :openbsd_old, :parent => :base do
desc "OpenBSD service management."
version = ["4.1", "4.2", "4.3", "4.4", "4.5", "4.6", "4.7", "4.8"]
confine :operatingsystem => :openbsd
confine :operatingsystemrelease => version
defaultfor :operatingsystem => :openbsd
@@rclocal = "/etc/rc.local"
@@rcconf = "/etc/rc.conf"
@@rcconflocal = "/etc/rc.conf.local"
def getrcconf
File.readlines(@@rcconf).each { |line|
if line =~ /^#{@resource[:name]}_flags=.*/
return "#{@resource[:name]}_flags"
elsif line =~ /^#{@resource[:name]}=.*/
return @resource[:name]
end
}
return false
end
def enabled?
if not defined? @resource[:start]
raise Puppet::Error,
"Services must specify a start command or a binary"
end
flag = getrcconf()
if flag
File.readlines(@@rcconflocal).each { |line|
line = line.strip.split(/=/, 2)
next unless line[0] == flag
if line[1] == "NO"
return :false
end
return :true
}
return :false
else
inlocal = false
File.readlines(@@rclocal).each { |line|
line = line.strip
if not inlocal
next unless \
line == "# Add your local startup actions here."
inlocal = true
else
if line == "echo '.'"
inlocal = false
break
end
next unless line =~ /^echo -n \" #{@resource[:name]}\" ; .*/
return :true
end
}
return :false
end
end
def enable
flag = getrcconf()
if flag
newdata = ""
File.readlines(@@rcconflocal).each { |line|
if line.strip.split(/=/, 2)[0] == flag
next
else
newdata += line
end
}
if flag == @resource[:name]
newdata += "%s=YES\n" % flag
elsif flag == "#{@resource[:name]}_flags"
if @resource[:start] != nil and @resource[:binary] != nil
args = @resource[:start][/^#{@resource[:binary]} (.*)/, 1]
end
newdata += "%s=\"%s\"\n" % [flag, args]
end
f = File.open(@@rcconflocal, "w")
f.write(newdata)
f.close
else
newdata = ""
inlocal = false
File.readlines(@@rclocal).each { |line|
if line == "# Add your local startup actions here.\n"
newdata += line
newdata += "echo -n \" %s\" ; %s\n" % [@resource[:name],
@resource[:start]]
next
end
newdata += line
}
f = File.open(@@rclocal, "w")
f.write(newdata)
f.close
end
return :true
end
def disable
flag = getrcconf()
if flag
newdata = ""
File.readlines(@@rcconflocal).each { |line|
if line.strip.split(/=/, 2)[0] == flag
next
else
newdata += line
end
}
if flag == @resource[:name] or flag == "#{@resource[:name]}_flags"
newdata += "%s=NO\n" % flag
end
f = File.open(@@rcconflocal, "w")
f.write(newdata)
f.close
else
print "disabling services from rc.local not implemented\n"
end
return :true
end
end