Skip to content

Commit

Permalink
If the keys have not changed, then avoid sudo. (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
j6carey authored and Gabriella439 committed Apr 8, 2018
1 parent e8577c5 commit d9fe20d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
1 change: 1 addition & 0 deletions nix-deploy.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ executable nix-deploy
ghc-options: -Wall
build-depends:
base >= 4.9.0.0 && < 5
, bytestring >= 0.10.8.1 && < 1.0
, optparse-generic >= 1.2.0 && < 1.3
, optparse-applicative >= 0.13.0.0 && < 0.15
, neat-interpolation < 0.4
Expand Down
68 changes: 40 additions & 28 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Control.Exception (SomeException)
import qualified Control.Exception
import Control.Monad
import Control.Monad.IO.Class (MonadIO)
import qualified Data.ByteString.Lazy as BL
import qualified Data.List.NonEmpty as NonEmpty
import Data.Maybe
import Data.Monoid ((<>))
Expand Down Expand Up @@ -259,7 +260,7 @@ main =
Turtle.die [text|[x] Failed to switch $targetText to $pathText|]

when (method == Reboot)$ do
let success =
let success =
stderrLines [text|[+] $pathText successfully activated, $targetText is rebooting|]
rebootCmd targetText >>= \case
-- This is the exit code returned by `ssh` when the machine closes
Expand Down Expand Up @@ -412,31 +413,42 @@ exchangeKeys host = do

liftIO (Control.Exception.handle handler1 download)

exitCode <- Turtle.shell "sudo -n true 2>/dev/null" empty

-- NB: path shouldn't is a FilePath and won't have any
-- newlines, so this should be okay
Turtle.err (Turtle.unsafeTextToLine $ Turtle.format ("[+] Installing: "%fp) path)

case exitCode of
ExitFailure _ -> do
Turtle.err ""
Turtle.err " This will prompt you for your `sudo` password"
_ -> do
return ()

let install =
Turtle.procs "sudo"
[ "mv"
, Turtle.format fp localPath
, Turtle.format fp path
]
empty
let handler2 :: SomeException -> IO ()
handler2 e = do
let pathText = Turtle.format fp path
let exceptionText = Text.pack (show e)
let msg = [text|
new <- liftIO . BL.readFile . Text.unpack $
Turtle.format fp localPath

old <- liftIO . BL.readFile . Text.unpack $
Turtle.format fp path

if new == old
then do
let same = Turtle.format ("[+] Unchanged: "%fp) path
mapM_ Turtle.err (Turtle.Line.textToLines same)
else do
exitCode <- Turtle.shell "sudo -n true 2>/dev/null" empty

-- NB: path shouldn't is a FilePath and won't have any
-- newlines, so this should be okay
Turtle.err (Turtle.unsafeTextToLine $ Turtle.format ("[+] Installing: "%fp) path)

case exitCode of
ExitFailure _ -> do
Turtle.err ""
Turtle.err " This will prompt you for your `sudo` password"
_ -> do
return ()

let install =
Turtle.procs "sudo"
[ "mv"
, Turtle.format fp localPath
, Turtle.format fp path
]
empty
let handler2 :: SomeException -> IO ()
handler2 e = do
let pathText = Turtle.format fp path
let exceptionText = Text.pack (show e)
let msg = [text|
[x] Could not install: $pathText

Debugging tips:
Expand All @@ -449,9 +461,9 @@ exchangeKeys host = do

Original error: $exceptionText
|]
Turtle.die msg
Turtle.die msg

liftIO (Control.Exception.handle handler2 install)
liftIO (Control.Exception.handle handler2 install)

mirror privateKey
mirror publicKey
Expand Down

0 comments on commit d9fe20d

Please sign in to comment.