mirror of
https://github.com/zhigang1992/boxen.git
synced 2026-01-12 17:12:46 +08:00
Add preflight check for boxen homedir
This commit is contained in:
@@ -43,11 +43,6 @@ module Boxen
|
||||
|
||||
Boxen::Preflight.run config
|
||||
|
||||
# Okay, we're gonna run Puppet. Let's make some dirs.
|
||||
|
||||
Boxen::Util.sudo("/bin/mkdir", "-p", config.homedir) &&
|
||||
Boxen::Util.sudo("/usr/sbin/chown", "#{config.user}:staff", config.homedir)
|
||||
|
||||
# Save the config for Puppet (and next time).
|
||||
|
||||
Boxen::Config.save config
|
||||
|
||||
32
lib/boxen/preflight/directories.rb
Normal file
32
lib/boxen/preflight/directories.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
require "boxen/preflight"
|
||||
require "boxen/util"
|
||||
|
||||
class Boxen::Preflight::Directories < Boxen::Preflight
|
||||
def ok?
|
||||
homedir_directory_exists? &&
|
||||
homedir_owner == config.user &&
|
||||
homedir_group == 'staff'
|
||||
end
|
||||
|
||||
def run
|
||||
Boxen::Util.sudo("/bin/mkdir", "-p", config.homedir) &&
|
||||
Boxen::Util.sudo("/usr/sbin/chown", "#{config.user}:staff", config.homedir)
|
||||
end
|
||||
|
||||
private
|
||||
def homedir_directory_exists?
|
||||
File.directory?(config.homedir)
|
||||
end
|
||||
|
||||
def homedir_owner
|
||||
Etc.getpwuid(homedir_stat.uid).name
|
||||
end
|
||||
|
||||
def homedir_group
|
||||
Etc.getgrgid(homedir_stat.gid).name
|
||||
end
|
||||
|
||||
def homedir_stat
|
||||
@homedir_stat ||= File.stat(config.homedir)
|
||||
end
|
||||
end
|
||||
40
test/boxen_directories_test.rb
Normal file
40
test/boxen_directories_test.rb
Normal file
@@ -0,0 +1,40 @@
|
||||
require 'boxen/test'
|
||||
require 'boxen/preflight/directories'
|
||||
|
||||
class BoxenPreflightDirectoriesTest < Boxen::Test
|
||||
class TestConfig
|
||||
def user; "foobar"; end
|
||||
def homedir; "foobar"; end
|
||||
end
|
||||
|
||||
def setup
|
||||
@config = TestConfig.new
|
||||
end
|
||||
|
||||
def test_not_okay_if_homedir_group_wrong
|
||||
directories = Boxen::Preflight::Directories.new(@config)
|
||||
directories.stubs(:homedir_group).returns(false)
|
||||
refute directories.ok?
|
||||
end
|
||||
|
||||
def test_not_okay_if_homedir_owner_wrong
|
||||
directories = Boxen::Preflight::Directories.new(@config)
|
||||
directories.stubs(:homedir_owner).returns(false)
|
||||
refute directories.ok?
|
||||
end
|
||||
|
||||
def test_not_okay_unless_homedir_exists
|
||||
directories = Boxen::Preflight::Directories.new(@config)
|
||||
directories.stubs(:homedir_directory_exists?).returns(false)
|
||||
refute directories.ok?
|
||||
end
|
||||
|
||||
def test_okay_if_allchecks_fine
|
||||
directories = Boxen::Preflight::Directories.new(@config)
|
||||
directories.stubs(:homedir_directory_exists?).returns(true)
|
||||
directories.stubs(:homedir_owner).returns("foobar")
|
||||
directories.stubs(:homedir_group).returns("staff")
|
||||
|
||||
assert directories.ok?
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user