Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial stab at issue/1 #2

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ Type documentation can be generated with `puppet doc -r type` or viewed on the

### apache_directive provider

#### Composite namevars
This type supports composite namevars in order to easily specify the entry you want to manage. The format is:
<directive> of <context>
or
<directive> of <context> in <target>

#### manage simple entry

apache_directive { "StartServers":
Expand All @@ -89,6 +95,7 @@ Type documentation can be generated with `puppet doc -r type` or viewed on the

The `SetEnv` directive is not unique per scope: the first arg identifies the entry we want to update, and needs to be taken into account. For this reason, we set `args_params` to `1`.


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That newline seems to have been added by mistake.

#### set a value in a given context

apache_directive { 'StartServers for mpm_prefork_module':
Expand All @@ -103,6 +110,17 @@ The directive is nested in the context of the `mpm_prefork_module` module, so we

The value of `StartServers` for the `mpm_prefork_module` module will be set/updated to `4`. Note that the `IfModule` entry will not be created if it is missing.

#### manage entry with composite namevars
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need a newline here.

apache_directive { 'Options of Directory[arg=\'"/var/www/html"\']':
ensure => present,
args => ['FollowSymLinks', ],
}

apache_directive { 'Options of Directory[arg=\'"/var/www/icons"\']':
ensure => present,
args => ['MultiViews', 'FollowSymLinks', ],
}


### apache_setenv provider

Expand Down
8 changes: 4 additions & 4 deletions lib/puppet/provider/apache_directive/augeas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
path = '$target'
path += "/#{resource[:context]}" unless resource[:context].empty?
if supported?(:regexpi)
path += "/directive[.=~regexp('#{resource[:name]}', 'i')"
path += "/directive[.=~regexp('#{resource[:directive]}', 'i')"
else
debug "Warning: Augeas >= 1.0.0 is required for case-insensitive support in apache_directive resources"
path += "/directive[.='#{resource[:name]}'"
path += "/directive[.='#{resource[:directive]}'"
end
if resource[:args]
resource[:args][0, resource[:args_params].to_i].each_with_index do |a, i|
Expand Down Expand Up @@ -60,7 +60,7 @@ def create
augopen! do |aug|
top_path = '$target'
top_path += "/#{resource[:context]}" unless resource[:context].empty?
last_path = "#{top_path}/directive[.='#{resource[:name]}'][last()]"
last_path = "#{top_path}/directive[.='#{resource[:directive]}'][last()]"
if aug.match(last_path).empty?
aug.clear("#{top_path}/directive[last()+1]")
else
Expand All @@ -70,7 +70,7 @@ def create

# The new node is the only directive without a value
aug.defvar('new', "#{top_path}/directive[.='']")
aug.set('$new', resource[:name])
aug.set('$new', resource[:directive])
resource[:args].each_with_index do |a,i|
aug.set("$new/arg[#{i+1}]", a)
end
Expand Down
42 changes: 41 additions & 1 deletion lib/puppet/type/apache_directive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def insync?(is)
isnamevar
end

newparam(:directive) do
desc 'The apache directive to modify'
defaultto self[:name]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Travis CI builds are failing because of this line. What you need instead is:

defaultto { @resource[:name] }

end

newparam(:context) do
desc 'The path where the directive is located. Expressed as an Augeas path expression.'
defaultto ''
Expand All @@ -50,6 +55,41 @@ def insync?(is)
end

newparam(:target) do
desc 'The config file to use'
desc 'The config file to modify'
end

def self.title_patterns
identity = lambda { |x| x }
[
[
/^((\w+)\s+of\s+(.+)\s+in\s+(.*))$/,
[
[ :name, identity ],
[ :directive, identity ],
[ :context, identity ],
[ :target, identity ],
]
],
[
/^((\w+)\s+of\s+(.+))$/,
[
[ :name, identity ],
[ :directive, identity ],
[ :context, identity ],
]
],
[
/((.*))/,
[
[ :name, identity ],
[ :directive, identity ],
]
]
]
end

autorequire(:file) do
self[:target]
end

end
13 changes: 13 additions & 0 deletions spec/fixtures/unit/puppet/provider/apache_directive/augeas/full
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ LogFormat "%{User-agent}i" agent
# Include of directories ignores editors' and dpkg's backup files,
# see README.Debian for details.

#
# Each directory to which Apache has access can be configured with respect
# to which services and features are allowed and/or disabled in that
# directory (and its subdirectories).
#
# First, we configure the "default" to be a very restrictive set of
# features.
#
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>

# Include generic snippets of statements
Include conf.d/

Expand Down
18 changes: 16 additions & 2 deletions spec/unit/puppet/provider/apache_directive/augeas_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
}
}

inst.size.should == 49
inst.size.should == 51
inst[0].should == {:args=>["${APACHE_LOCK_DIR}/accept.lock"], :name=>"LockFile", :ensure=>:present, :context=>""}
inst[5].should == {:args=>["5"], :name=>"KeepAliveTimeout", :ensure=>:present, :context=>""}
inst[30].should == {:args=>["150"], :context=>"IfModule[1]", :name=>"MaxClients", :ensure=>:present}
Expand Down Expand Up @@ -167,10 +167,24 @@
aug.get("IfModule[arg='mpm_worker_module']/directive[.='StartServers']/arg").should == '2'
end
end

it "updating should update value" do
apply!(Puppet::Type.type(:apache_directive).new(
:name => 'Options of Directory[arg="/"]',
:args_params => 0,
:args => ['FollowSymLinks', 'Indexes'],
:ensure => "present",
:target => target,
:provider => "augeas"
))

aug_open(target, "Httpd.lns") do |aug|
aug.get('Directory[arg="/"]/directive[1]/arg').should == ['FollowSymLinks', 'Indexes']
end
end
end
end


context "with broken file" do
let(:tmptarget) { aug_fixture("broken") }
let(:target) { tmptarget.path }
Expand Down