Permalink
Browse files

enhanced user and groups support

  • Loading branch information...
1 parent 693657a commit ade64d063abd8c006d0be98fd86d10398c8b5bed @yyyc514 yyyc514 committed Sep 16, 2011
View
15 lib/sprinkle/installers/group.rb
@@ -0,0 +1,15 @@
+module Sprinkle
+ module Installers
+ class Group < Installer
+ def initialize(package, groupname, options, &block)
+ super package, &block
+ @groupname = groupname
+ @options = options
+ end
+ protected
+ def install_commands
+ "addgroup #{@options[:flags]} #{@groupname}"
+ end
+ end
+ end
+end
View
4 lib/sprinkle/installers/user.rb
@@ -3,8 +3,8 @@ module Installers
class User < Installer
def initialize(package, username, options, &block)
super package, &block
- @username=username
- @options =options
+ @username = username
+ @options = options
end
protected
def install_commands
View
6 lib/sprinkle/package.rb
@@ -122,7 +122,11 @@ def initialize(name, metadata = {}, &block)
self.instance_eval &block
end
def add_user(username, options={}, &block)
- @installers<<Sprinkle::Installers::User.new(self, username, options, &block)
+ @installers << Sprinkle::Installers::User.new(self, username, options, &block)
+ end
+
+ def add_group(group, options={}, &block)
+ @installers << Sprinkle::Installers::Group.new(self, group, options, &block)
end
def freebsd_pkg(*names, &block)
View
2 lib/sprinkle/verifiers/file.rb
@@ -22,7 +22,7 @@ def file_contains(path, text)
@commands << "grep '#{text}' #{path}"
end
def user_present(username)
- @commands << %Q{grep -q -e \'^#{username}:x\' /etc/passwd && test -d ~#{username}}
+ has_user username
end
def matches_local(localfile, remotefile, mode=nil)
raise "Couldn't find local file #{localfile}" unless ::File.exists?(localfile)
View
15 lib/sprinkle/verifiers/user.rb
@@ -1,15 +0,0 @@
-module Sprinkle
- module Verifiers
- # = User Verifier
- # This was added so we dont have to verify a file to see if user was created
- # Defines a verify which can be used to test the existence of a user.
- module Users
- Sprinkle::Verify.register(Sprinkle::Verifiers::Users)
-
- # Tests that the user exists
- def has_user(user)
- @commands << "id #{user}"
- end
- end
- end
-end
View
33 lib/sprinkle/verifiers/users_groups.rb
@@ -0,0 +1,33 @@
+module Sprinkle
+ module Verifiers
+ # = Users and groups Verifier
+ #
+ # Tests for the existance of users and groups.
+ #
+ # == Example Usage
+ #
+ # verify do
+ # has_user 'ntp'
+ # has_user 'noone', :in_group => 'nobody'
+ # has_group 'nobody'
+ # end
+ #
+ module UsersGroups
+ Sprinkle::Verify.register(Sprinkle::Verifiers::UsersGroups)
+
+ # Tests that the user exists
+ def has_user(user, opts = {})
+ if opts[:in_group]
+ @commands << "id -G #{user} | xargs -n1 echo | grep #{opts[:in_group]}"
+ else
+ @commands << "id #{user}"
+ end
+ end
+
+ # Tests that the group exists
+ def has_group(group)
+ @commands << "id -g #{group}"
+ end
+ end
+ end
+end
View
18 spec/sprinkle/package_spec.rb
@@ -141,14 +141,30 @@ def create_package_with_blank_verify(n = 1)
pkg.should respond_to(:gem)
pkg.installers.first.class.should == Sprinkle::Installers::Gem
end
-
+
+ it 'should optionally accept an group installer' do
+ pkg = package @name do
+ add_group 'bob'
+ end
+ pkg.should respond_to(:add_group)
+ pkg.installers.first.class.should == Sprinkle::Installers::Group
+ end
+
it 'should optionally accept a source installer' do
pkg = package @name do
source 'archive'
end
pkg.should respond_to(:source)
pkg.installers.first.class.should == Sprinkle::Installers::Source
end
+
+ it 'should optionally accept an user installer' do
+ pkg = package @name do
+ add_user 'bob'
+ end
+ pkg.should respond_to(:add_user)
+ pkg.installers.first.class.should == Sprinkle::Installers::User
+ end
it 'should allow multiple installer steps to be defined and respect order' do
pkg = package @name do
View
28 spec/sprinkle/verify_spec.rb
@@ -14,6 +14,18 @@
# Check a directory exists
has_directory 'mydir'
+
+ # Check for a user
+ has_user "bob"
+
+ # Check for user with old API
+ user_present "someuser"
+
+ # Check for user in a group
+ has_user "alf", :in_group => "alien"
+
+ # Check for a group
+ has_group "bobgroup"
# Check a symlink exists
has_symlink 'mypointer'
@@ -64,6 +76,22 @@
it 'should do a "test -d" on the has_directory check' do
@verification.commands.should include('test -d mydir')
end
+
+ it 'should use id to check for user in group' do
+ @verification.commands.should include("id -G alf | xargs -n1 echo | grep alien")
+ end
+
+ it 'should use id to check for user' do
+ @verification.commands.should include('id bob')
+ end
+
+ it 'should use id to check for user via user_present' do
+ @verification.commands.should include('id someuser')
+ end
+
+ it 'should use id to check for group' do
+ @verification.commands.should include('id -g bobgroup')
+ end
it 'should do a "test -L" to check something is a symbolic link' do
@verification.commands.should include('test -L mypointer')

0 comments on commit ade64d0

Please sign in to comment.