Skip to content

Commit

Permalink
Run rhel services as selenium user
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Hoer committed Jun 9, 2015
1 parent 39dd3f1 commit a670de2
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 20 deletions.
11 changes: 10 additions & 1 deletion libraries/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,30 @@ def autologon(username, password, domain = nil)
end

def linux_service(name, exec, args, port, display)
# TODO: make selenium username default and pass it in as a param
username = 'selenium'

user "ensure user #{username} exits for #{name}" do
username username
end

template "/etc/init.d/#{name}" do
source "#{node['platform_family']}_initd.erb"
cookbook 'selenium'
mode '0755'
variables(
name: name,
user: username,
exec: exec,
args: args.join(' '),
args: args.join(' ').gsub('"', '\"'),
port: port,
display: display
)
notifies :restart, "service[#{name}]"
end

service name do
supports restart: true, reload: true, status: true
action [:enable]
end
end
Expand Down
13 changes: 9 additions & 4 deletions spec/unit/htmlunit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
expect(chef_run).to install_selenium_node('selenium_htmlunit')
end

it 'creates selenium user' do
expect(chef_run).to create_user('ensure user selenium exits for selenium_htmlunit').with(username: 'selenium')
end

it 'creates node config file' do
expect(chef_run).to create_template('/usr/local/selenium/config/selenium_htmlunit.json')
end
Expand All @@ -69,11 +73,12 @@
mode: '0755',
variables: {
name: 'selenium_htmlunit',
user: 'selenium',
exec: '/usr/bin/java',
args: '-jar "/usr/local/selenium/server/selenium-server-standalone.jar" -role node '\
'-nodeConfig "/usr/local/selenium/config/selenium_htmlunit.json"',
display: ':0',
port: 5556
args: '-jar \"/usr/local/selenium/server/selenium-server-standalone.jar\" -role node '\
'-nodeConfig \"/usr/local/selenium/config/selenium_htmlunit.json\"',
port: 5556,
display: ':0'
}
)
end
Expand Down
13 changes: 9 additions & 4 deletions spec/unit/hub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
expect(chef_run).to install_selenium_hub('selenium_hub')
end

it 'creates selenium user' do
expect(chef_run).to create_user('ensure user selenium exits for selenium_hub').with(username: 'selenium')
end

it 'creates hub config file' do
expect(chef_run).to create_template('/usr/local/selenium/config/selenium_hub.json').with(
source: 'hub_config.erb',
Expand All @@ -71,11 +75,12 @@
mode: '0755',
variables: {
name: 'selenium_hub',
user: 'selenium',
exec: '/usr/bin/java',
args: '-jar "/usr/local/selenium/server/selenium-server-standalone.jar" -role hub '\
'-hubConfig "/usr/local/selenium/config/selenium_hub.json"',
display: nil,
port: 4444
args: '-jar \"/usr/local/selenium/server/selenium-server-standalone.jar\" -role hub '\
'-hubConfig \"/usr/local/selenium/config/selenium_hub.json\"',
port: 4444,
display: nil
}
)
end
Expand Down
15 changes: 10 additions & 5 deletions spec/unit/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@
expect(chef_run).to install_selenium_node('selenium_node')
end

it 'creates selenium user' do
expect(chef_run).to create_user('ensure user selenium exits for selenium_node').with(username: 'selenium')
end

it 'creates node config file' do
expect(chef_run).to create_template('/usr/local/selenium/config/selenium_node.json')
end
Expand All @@ -95,12 +99,13 @@
mode: '0755',
variables: {
name: 'selenium_node',
user: 'selenium',
exec: '/usr/bin/java',
args: '-jar "/usr/local/selenium/server/selenium-server-standalone.jar" -role node '\
'-nodeConfig "/usr/local/selenium/config/selenium_node.json" '\
'-Dwebdriver.chrome.driver="/usr/local/selenium/drivers/chromedriver/chromedriver"',
display: ':0',
port: 5555
args: '-jar \"/usr/local/selenium/server/selenium-server-standalone.jar\" -role node '\
'-nodeConfig \"/usr/local/selenium/config/selenium_node.json\" '\
'-Dwebdriver.chrome.driver=\"/usr/local/selenium/drivers/chromedriver/chromedriver\"',
port: 5555,
display: ':0'
}
)
end
Expand Down
6 changes: 6 additions & 0 deletions spec/unit/phantomjs_nogrid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
expect(chef_run).to install_selenium_phantomjs('selenium_phantomjs_nogrid')
end

it 'creates selenium user' do
expect(chef_run).to create_user('ensure user selenium exits for selenium_phantomjs_nogrid').with(
username: 'selenium')
end

# it 'creates phantomjs nogrid config file' do
# expect(chef_run).to create_template('/usr/local/selenium/config/selenium_phantomjs_nogrid.json')
# end
Expand All @@ -74,6 +79,7 @@
mode: '0755',
variables: {
name: 'selenium_phantomjs_nogrid',
user: 'selenium',
exec: '/usr/local/bin/phantomjs',
# args: '--config="/usr/local/selenium/config/selenium_phantomjs.json"',
args: '--webdriver=localhost:8911',
Expand Down
5 changes: 5 additions & 0 deletions spec/unit/phantomjs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
expect(chef_run).to install_selenium_phantomjs('selenium_phantomjs')
end

it 'creates selenium user' do
expect(chef_run).to create_user('ensure user selenium exits for selenium_phantomjs').with(username: 'selenium')
end

# it 'creates phantomjs config file' do
# expect(chef_run).to create_template('/usr/local/selenium/config/selenium_phantomjs.json')
# end
Expand All @@ -67,6 +71,7 @@
mode: '0755',
variables: {
name: 'selenium_phantomjs',
user: 'selenium',
exec: '/usr/local/bin/phantomjs',
# args: '--config="/usr/local/selenium/config/selenium_phantomjs.json"',
args: '--webdriver=10.0.0.2:8910 --webdriver-selenium-grid-hub=http://10.0.0.2:4444',
Expand Down
20 changes: 14 additions & 6 deletions templates/default/rhel_initd.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Source function library.
. /etc/init.d/functions

user=<%=@user%>
exec="<%=@exec%>"
args="<%=@args%>"
lockfile="/var/lock/subsys/<%=@name%>"
Expand All @@ -21,11 +22,20 @@ RETVAL=0

start() {
echo -n $"Starting $prog: "
export DISPLAY=$display
$exec $args >> $logfile 2>&1 &

touch $pidfile
chown $user $pidfile

touch $logfile
chown $user $logfile

/bin/su - $user -c "DISPLAY=\"$display\" $exec $args >> $logfile 2>&1 & echo \$! > $pidfile"

sleep 2

pgrep -fl $prog
RETVAL=$?
pid=$!
echo $pid > $pidfile
[ $RETVAL -eq 0 ] && echo_success || echo_failure

listen=0
while [ $listen -eq 0 ];
Expand All @@ -34,8 +44,6 @@ start() {
sleep 1
done

[ $RETVAL -eq 0 ] && echo_success || echo_failure

echo
[ $RETVAL -eq 0 ] && touch $lockfile
return $RETVAL
Expand Down

0 comments on commit a670de2

Please sign in to comment.