Permalink
Browse files

Adds #shell.run_as = superuser option

  • Loading branch information...
1 parent 0785f34 commit d4e2a591e287eba64754f23629145cb044f65b76 Yurii Rashkovskii committed with Feb 5, 2012
Showing with 29 additions and 4 deletions.
  1. +2 −1 apps/htoad/include/stdlib.hrl
  2. +27 −3 apps/htoad/src/htoad_shell.erl
@@ -36,5 +36,6 @@
-record(shell,
{
- cmd
+ cmd,
+ run_as :: undefined | superuser | term()
}).
@@ -3,13 +3,37 @@
-include_lib("htoad/include/toadie.hrl").
-include_lib("htoad/include/stdlib.hrl").
--rules([init, command]).
+-rules([init, user, superuser, command_run_in_superuser,
+ command_run_as_superuser, command]).
+
+-neg_rule({command_run_as_superuser, [{?MODULE, superuser}]}).
init(Engine, #init{}, {operating_system_type, unix}) ->
lager:debug("Initialized htoad_shell"),
- Engine.
+ htoad:assert(Engine, #shell{ cmd = "whoami" }).
+
+user(Engine, {output, #shell{ cmd = "whoami"}, User}) ->
+ lager:debug("Current user: ~s", [User]),
+ htoad:assert(Engine, {user, User}).
+
+superuser(Engine, #init{}, {operating_system_type, unix},
+ {user, "root"}) ->
+ htoad:assert(Engine, {?MODULE, superuser}).
+
+command_run_in_superuser(Engine, #shell{ run_as = superuser } = Shell,
+ {?MODULE, superuser}) ->
+ command(Engine, Shell).
+
+command_run_as_superuser(Engine, #shell{ cmd = Cmd, run_as = superuser } = Shell,
+ #file{ path = "/usr/bin/sudo", producer = fs,
+ content = dontread }) when not {rule, [{?MODULE, superuser}]} ->
+ lager:debug("Executing shell command as a super user (via sudo): ~s", [Cmd]),
+ Result = os:cmd("/usr/bin/sudo -n " ++ Cmd),
+ lager:debug("Shell output for `~s`: ~s", [Cmd, Result]),
+ htoad:assert(Engine, {output, Shell, Result}).
+
-command(Engine, #shell{ cmd = Cmd } = Shell) ->
+command(Engine, #shell{ cmd = Cmd, run_as = undefined } = Shell) ->
lager:debug("Executing shell command: ~s", [Cmd]),
Result = os:cmd(Cmd),
lager:debug("Shell output for `~s`: ~s", [Cmd, Result]),

0 comments on commit d4e2a59

Please sign in to comment.