Merge lp:~thumper/juju-core/local-ensure-root into lp:~go-bot/juju-core/trunk
Proposed by
Tim Penhey
Status: | Merged |
---|---|
Approved by: | Tim Penhey |
Approved revision: | no longer in the source branch. |
Merged at revision: | 2452 |
Proposed branch: | lp:~thumper/juju-core/local-ensure-root |
Merge into: | lp:~go-bot/juju-core/trunk |
Diff against target: |
136 lines (+81/-4) 3 files modified
cmd/plugins/local/export_test.go (+8/-1) cmd/plugins/local/main.go (+34/-0) cmd/plugins/local/main_test.go (+39/-3) |
To merge this branch: | bzr merge lp:~thumper/juju-core/local-ensure-root |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Juju Engineering | Pending | ||
Review via email: mp+211849@code.launchpad.net |
Commit message
Implement a helper function for sudo.
Most of the local plugin helper commands will need
to run as sudo. Instead of each writing their own
code to handle this, provide a helper function.
Description of the change
Implement a helper function for sudo.
Most of the local plugin helper commands will need
to run as sudo. Instead of each writing their own
code to handle this, provide a helper function.
To post a comment you must log in.
Reviewers: mp+211849_ code.launchpad. net,
Message:
Please take a look.
Description:
Implement a helper function for sudo.
Most of the local plugin helper commands will need
to run as sudo. Instead of each writing their own
code to handle this, provide a helper function.
https:/ /code.launchpad .net/~thumper/ juju-core/ local-ensure- root/+merge/ 211849
(do not edit description out of merge proposal)
Please review this at https:/ /codereview. appspot. com/78030044/
Affected files (+79, -4 lines): local/export_ test.go local/main. go local/main_ test.go
A [revision details]
M cmd/plugins/
M cmd/plugins/
M cmd/plugins/
Index: [revision details] 20140320011142- 7ljymhlzvpq44ap l
=== added file '[revision details]'
--- [revision details] 2012-01-01 00:00:00 +0000
+++ [revision details] 2012-01-01 00:00:00 +0000
@@ -0,0 +1,2 @@
+Old revision: tarmac-
+New revision: <email address hidden>
Index: cmd/plugins/ local/export_ test.go local/export_ test.go' local/export_ test.go 2014-02-27 05:16:54 +0000 local/export_ test.go 2014-03-19 21:53:07 +0000
=== modified file 'cmd/plugins/
--- cmd/plugins/
+++ cmd/plugins/
@@ -3,4 +3,11 @@
package local
-var JujuLocalPlugin = jujuLocalPlugin
+var (
+ // Variable is the address so we can PatchValue
+ CheckIfRoot = &checkIfRoot
+
+ // function exports for tests
+ EnsureRoot = ensureRoot
+ JujuLocalPlugin = jujuLocalPlugin
+)
Index: cmd/plugins/ local/main. go local/main. go' local/main. go 2014-03-05 19:41:34 +0000 local/main. go 2014-03-19 22:29:47 +0000
=== modified file 'cmd/plugins/
--- cmd/plugins/
+++ cmd/plugins/
@@ -5,6 +5,7 @@
import (
"os"
+ "os/exec"
"github. com/juju/ loggo"
@@ -39,3 +40,32 @@ cmd.Main( plugin, cmd.DefaultCont ext(), args[1:])) Debugf( "running as root") Debugf( "running as user") args[0] ) "--preserve- env", fullpath} "sudo", sudoArgs...)
plugin := jujuLocalPlugin()
os.Exit(
}
+
+var checkIfRoot = func() bool {
+ return os.Getuid() == 0
+}
+
+func ensureRoot(args []string, context *cmd.Context, call
func(*cmd.Context) error) error {
+ if checkIfRoot() {
+ logger.
+ return call(context)
+ }
+
+ logger.
+
+ fullpath, err := exec.LookPath(
+ if err != nil {
+ return err
+ }
+
+ sudoArgs := []string{
+ sudoArgs = append(sudoArgs, args[1:]...)
+
+ command := exec.Command(
+ // Now hook up stdin, stdout, stderr
+ command.Stdin = context.Stdin
+ command.Stdout = context.Stdout
+ command.Stderr = context.Stderr
+ // And run it!
+ return command.Run()
+}
Index: cmd/plugins/ local/main_ test.go local/main_ test.go' local/main_ test.go 2014-02-27 05:16:54 +0000 local/main_ test.go 2014-03-20 01:58:59 +0000
=== modified file 'cmd/plugins/
--- cmd/plugins/
+++ cmd/plugins/
@@ -4,12 +4,17 @@
package local_test
import (
+ "fmt"
+ "os/exec"
"strings"
+ "github. com/juju/ testing" com/juju/ testing/ checkers" net/gocheck"
+ jc "github.
gc "launchpad.
+ "launchpad. net/juju- core/cmd" net/juju- core/cmd/ plugins/ local" net/juju- core/testing" net/juju- core/testing" net/juju- core/testing/ testbase"
"launchpad.
- "launchpad.
+ coretesting "launchpad.
"launchpad.
)
@@ -25,10 +30,10 @@
// TODO: add some as they get registered
}
plugin := local.JujuLocal...