2019-08-15 01:18:59 +03:00
|
|
|
{-
|
|
|
|
# Booting a Ship
|
|
|
|
|
|
|
|
- TODO Don't just boot, also run the ship (unless `-x` is set).
|
|
|
|
- TODO Figure out why ships booted by us don't work.
|
|
|
|
|
|
|
|
# Event Pruning
|
|
|
|
|
|
|
|
- `king discard-events NUM_EVENTS`: Delete the last `n` events from
|
|
|
|
the event log.
|
|
|
|
|
|
|
|
- `king discard-events-interactive`: Iterate through the events in
|
|
|
|
the event log, from last to first, pretty-print each event, and
|
|
|
|
ask if it should be pruned.
|
|
|
|
|
|
|
|
|
|
|
|
# `-L` -- Local-Only Networking
|
|
|
|
|
|
|
|
Localhost-only networking, even on real ships.
|
|
|
|
|
|
|
|
|
|
|
|
# `-O` -- Networking Disabled
|
|
|
|
|
|
|
|
Run networking drivers, but configure them to never send any packages
|
|
|
|
and to never open any ports.
|
|
|
|
|
|
|
|
|
|
|
|
# `-N` -- Dry Run
|
|
|
|
|
|
|
|
Disable all persistence and use no-op networking.
|
|
|
|
|
|
|
|
|
|
|
|
# `-x` -- Exit Immediately
|
|
|
|
|
|
|
|
When creating a new ship, or booting an existing one, simply get to
|
|
|
|
a good state, snapshot, and then exit. Don't do anything that has
|
|
|
|
any effect on the outside world, just boot or catch the snapshot up
|
|
|
|
to the event log.
|
|
|
|
|
|
|
|
|
|
|
|
# Implement subcommands to test event and effect parsing.
|
|
|
|
|
|
|
|
- `king * --collect-fx`: All effects that come from the serf get
|
|
|
|
written into the `effects` LMDB database.
|
|
|
|
|
|
|
|
- `king clear-fx PIER`: Deletes all collected effects.
|
|
|
|
|
|
|
|
- `king full-replay PIER`: Replays the whole event log events, print
|
|
|
|
any failures. On success, replace the snapshot.
|
|
|
|
|
|
|
|
|
|
|
|
# Full Replay -- An Integration Test
|
|
|
|
|
|
|
|
- Copy the event log:
|
|
|
|
|
|
|
|
- Create a new event log at the destination.
|
|
|
|
- Stream events from the first event log.
|
|
|
|
- Parse each event.
|
|
|
|
- Re-Serialize each event.
|
|
|
|
- Verify that the round-trip was successful.
|
|
|
|
- Write the event into the new database.
|
|
|
|
|
|
|
|
- Replay the event log at the destination.
|
|
|
|
- If `--collect-fx` is set, then record effects as well.
|
|
|
|
|
|
|
|
- Snapshot.
|
|
|
|
|
|
|
|
- Verify that the final mug is the same as it was before.
|
|
|
|
|
|
|
|
# Implement Remaining Serf Flags
|
|
|
|
|
|
|
|
- `DebugRam`: Memory debugging.
|
|
|
|
- `DebugCpu`: Profiling
|
|
|
|
- `CheckCorrupt`: Heap Corruption Tests
|
|
|
|
- `CheckFatal`: TODO What is this?
|
|
|
|
- `Verbose`: TODO Just the `-v` flag?
|
|
|
|
- `DryRun`: TODO Just the `-N` flag?
|
|
|
|
- `Quiet`: TODO Just the `-q` flag?
|
|
|
|
- `Hashless`: Don't use hashboard for jets.
|
|
|
|
- `Trace`: TODO What does this do?
|
|
|
|
-}
|
|
|
|
|
2019-08-28 14:06:48 +03:00
|
|
|
module Main (main) where
|
2019-05-31 00:24:14 +03:00
|
|
|
|
2019-08-28 14:00:26 +03:00
|
|
|
import UrbitPrelude
|
2019-07-16 03:01:45 +03:00
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
import Arvo
|
2019-07-19 03:52:53 +03:00
|
|
|
import Data.Acquire
|
|
|
|
import Data.Conduit
|
2019-10-02 21:50:03 +03:00
|
|
|
import Data.Conduit.List hiding (catMaybes, replicate, take)
|
2019-09-18 06:14:49 +03:00
|
|
|
import Data.RAcquire
|
2019-09-17 21:19:53 +03:00
|
|
|
import Noun hiding (Parser)
|
2019-09-24 02:42:12 +03:00
|
|
|
import Noun.Atom
|
2019-09-18 06:14:49 +03:00
|
|
|
import RIO.Directory
|
2019-07-24 04:34:16 +03:00
|
|
|
import Vere.Pier
|
|
|
|
import Vere.Pier.Types
|
|
|
|
import Vere.Serf
|
2019-07-16 03:01:45 +03:00
|
|
|
|
2019-09-13 09:55:10 +03:00
|
|
|
import Control.Concurrent (myThreadId, runInBoundThread)
|
2019-09-18 06:14:49 +03:00
|
|
|
import Control.Exception (AsyncException(UserInterrupt))
|
2019-09-13 09:55:10 +03:00
|
|
|
import Control.Lens ((&))
|
|
|
|
import Data.Default (def)
|
2019-09-18 06:14:49 +03:00
|
|
|
import KingApp (runApp)
|
2019-09-13 09:55:10 +03:00
|
|
|
import System.Environment (getProgName)
|
|
|
|
import System.Posix.Signals (Handler(Catch), installHandler, sigTERM)
|
|
|
|
import Text.Show.Pretty (pPrint)
|
|
|
|
import Urbit.Time (Wen)
|
2019-09-20 01:40:23 +03:00
|
|
|
import Vere.Dawn
|
2019-09-18 05:45:51 +03:00
|
|
|
import Vere.LockFile (lockFile)
|
2019-06-25 04:10:41 +03:00
|
|
|
|
2019-09-18 06:14:49 +03:00
|
|
|
import qualified CLI as CLI
|
2019-09-13 09:55:10 +03:00
|
|
|
import qualified Data.Set as Set
|
2019-09-24 02:42:12 +03:00
|
|
|
import qualified Data.Text as T
|
2019-09-18 06:14:49 +03:00
|
|
|
import qualified EventBrowser as EventBrowser
|
2019-09-13 09:55:10 +03:00
|
|
|
import qualified System.IO.LockFile.Internal as Lock
|
2019-10-01 21:23:34 +03:00
|
|
|
import qualified Urbit.Ob as Ob
|
2019-09-13 09:55:10 +03:00
|
|
|
import qualified Vere.Log as Log
|
|
|
|
import qualified Vere.Pier as Pier
|
|
|
|
import qualified Vere.Serf as Serf
|
2019-09-18 10:24:10 +03:00
|
|
|
import qualified Vere.Term as Term
|
2019-07-21 22:56:18 +03:00
|
|
|
|
2019-08-28 14:00:26 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2019-07-21 22:56:18 +03:00
|
|
|
zod :: Ship
|
|
|
|
zod = 0
|
2019-06-25 04:10:41 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2019-05-31 00:24:14 +03:00
|
|
|
|
2019-08-28 14:00:26 +03:00
|
|
|
removeFileIfExists :: HasLogFunc env => FilePath -> RIO env ()
|
2019-07-19 03:52:53 +03:00
|
|
|
removeFileIfExists pax = do
|
2019-09-17 21:19:53 +03:00
|
|
|
exists <- doesFileExist pax
|
2019-07-19 03:52:53 +03:00
|
|
|
when exists $ do
|
2019-09-17 21:19:53 +03:00
|
|
|
removeFile pax
|
2019-07-21 22:56:18 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2019-10-02 21:50:03 +03:00
|
|
|
toSerfFlags :: CLI.Opts -> Serf.Flags
|
|
|
|
toSerfFlags CLI.Opts{..} = catMaybes m
|
|
|
|
where
|
|
|
|
-- TODO: This is not all the flags.
|
|
|
|
m = [ from oQuiet Serf.Quiet
|
|
|
|
, from oTrace Serf.Trace
|
|
|
|
, from oHashless Serf.Hashless
|
|
|
|
, from oQuiet Serf.Quiet
|
|
|
|
, from oVerbose Serf.Verbose
|
|
|
|
, from oDryRun Serf.DryRun
|
|
|
|
]
|
|
|
|
from True flag = Just flag
|
|
|
|
from False _ = Nothing
|
|
|
|
|
|
|
|
|
2019-10-02 00:44:14 +03:00
|
|
|
tryBootFromPill :: HasLogFunc e
|
2019-10-02 21:50:03 +03:00
|
|
|
=> FilePath -> FilePath -> Serf.Flags -> Ship -> LegacyBootEvent
|
|
|
|
-> RIO e ()
|
|
|
|
tryBootFromPill pillPath shipPath flags ship boot = do
|
2019-09-13 09:55:10 +03:00
|
|
|
rwith bootedPier $ \(serf, log, ss) -> do
|
2019-08-28 14:00:26 +03:00
|
|
|
logTrace "Booting"
|
|
|
|
logTrace $ displayShow ss
|
|
|
|
io $ threadDelay 500000
|
2019-08-28 15:22:56 +03:00
|
|
|
ss <- shutdown serf 0
|
2019-08-28 14:00:26 +03:00
|
|
|
logTrace $ displayShow ss
|
|
|
|
logTrace "Booted!"
|
2019-09-13 09:55:10 +03:00
|
|
|
where
|
|
|
|
bootedPier = do
|
|
|
|
lockFile shipPath
|
2019-10-02 21:50:03 +03:00
|
|
|
Pier.booted pillPath shipPath flags ship boot
|
2019-08-28 14:00:26 +03:00
|
|
|
|
|
|
|
runAcquire :: (MonadUnliftIO m, MonadIO m)
|
|
|
|
=> Acquire a -> m a
|
2019-08-01 08:16:02 +03:00
|
|
|
runAcquire act = with act pure
|
|
|
|
|
2019-08-28 14:00:26 +03:00
|
|
|
runRAcquire :: (MonadUnliftIO (m e), MonadIO (m e), MonadReader e (m e))
|
|
|
|
=> RAcquire e a -> m e a
|
|
|
|
runRAcquire act = rwith act pure
|
|
|
|
|
2019-10-02 21:50:03 +03:00
|
|
|
tryPlayShip :: HasLogFunc e => FilePath -> Serf.Flags -> RIO e ()
|
|
|
|
tryPlayShip shipPath flags = do
|
2019-08-28 14:00:26 +03:00
|
|
|
runRAcquire $ do
|
2019-09-13 09:55:10 +03:00
|
|
|
lockFile shipPath
|
2019-08-28 14:45:49 +03:00
|
|
|
rio $ logTrace "RESUMING SHIP"
|
2019-10-02 21:50:03 +03:00
|
|
|
sls <- Pier.resumed shipPath flags
|
2019-08-28 14:45:49 +03:00
|
|
|
rio $ logTrace "SHIP RESUMED"
|
|
|
|
Pier.pier shipPath Nothing sls
|
2019-08-01 08:16:02 +03:00
|
|
|
|
2019-10-02 21:50:03 +03:00
|
|
|
tryResume :: HasLogFunc e => FilePath -> Serf.Flags -> RIO e ()
|
|
|
|
tryResume shipPath flags = do
|
2019-09-13 09:55:10 +03:00
|
|
|
rwith resumedPier $ \(serf, log, ss) -> do
|
2019-08-28 14:00:26 +03:00
|
|
|
logTrace (displayShow ss)
|
2019-07-19 03:52:53 +03:00
|
|
|
threadDelay 500000
|
2019-08-28 15:22:56 +03:00
|
|
|
ss <- shutdown serf 0
|
2019-08-28 14:00:26 +03:00
|
|
|
logTrace (displayShow ss)
|
|
|
|
logTrace "Resumed!"
|
2019-09-13 09:55:10 +03:00
|
|
|
where
|
|
|
|
resumedPier = do
|
|
|
|
lockFile shipPath
|
2019-10-02 21:50:03 +03:00
|
|
|
Pier.resumed shipPath flags
|
2019-06-25 04:10:41 +03:00
|
|
|
|
2019-10-02 21:50:03 +03:00
|
|
|
tryFullReplay :: HasLogFunc e => FilePath -> Serf.Flags -> RIO e ()
|
|
|
|
tryFullReplay shipPath flags = do
|
2019-09-13 09:55:10 +03:00
|
|
|
wipeSnapshot
|
2019-10-02 21:50:03 +03:00
|
|
|
tryResume shipPath flags
|
2019-09-13 09:55:10 +03:00
|
|
|
where
|
|
|
|
wipeSnapshot = do
|
|
|
|
logTrace "wipeSnapshot"
|
|
|
|
logDebug $ display $ pack @Text ("Wiping " <> north)
|
|
|
|
logDebug $ display $ pack @Text ("Wiping " <> south)
|
|
|
|
removeFileIfExists north
|
|
|
|
removeFileIfExists south
|
|
|
|
|
|
|
|
north = shipPath <> "/.urb/chk/north.bin"
|
|
|
|
south = shipPath <> "/.urb/chk/south.bin"
|
|
|
|
|
2019-07-17 08:32:36 +03:00
|
|
|
|
2019-07-21 22:56:18 +03:00
|
|
|
--------------------------------------------------------------------------------
|
2019-07-20 02:18:58 +03:00
|
|
|
|
2019-08-28 14:00:26 +03:00
|
|
|
checkEvs :: forall e. HasLogFunc e => FilePath -> Word64 -> Word64 -> RIO e ()
|
2019-08-22 03:54:00 +03:00
|
|
|
checkEvs pierPath first last = do
|
2019-08-29 03:26:59 +03:00
|
|
|
rwith (Log.existing logPath) $ \log -> do
|
2019-07-20 02:18:58 +03:00
|
|
|
let ident = Log.identity log
|
2019-08-28 14:00:26 +03:00
|
|
|
logTrace (displayShow ident)
|
2019-07-20 02:18:58 +03:00
|
|
|
runConduit $ Log.streamEvents log first
|
2019-08-22 03:54:00 +03:00
|
|
|
.| showEvents first (fromIntegral $ lifecycleLen ident)
|
2019-07-20 02:18:58 +03:00
|
|
|
where
|
2019-08-22 03:54:00 +03:00
|
|
|
logPath :: FilePath
|
|
|
|
logPath = pierPath <> "/.urb/log"
|
|
|
|
|
2019-08-28 14:00:26 +03:00
|
|
|
showEvents :: EventId -> EventId -> ConduitT ByteString Void (RIO e) ()
|
2019-08-22 03:54:00 +03:00
|
|
|
showEvents eId _ | eId > last = pure ()
|
|
|
|
showEvents eId cycle =
|
|
|
|
await >>= \case
|
2019-08-28 14:00:26 +03:00
|
|
|
Nothing -> lift $ logTrace "Everything checks out."
|
2019-08-22 03:54:00 +03:00
|
|
|
Just bs -> do
|
2019-08-28 14:00:26 +03:00
|
|
|
lift $ do
|
|
|
|
n <- io $ cueBSExn bs
|
2019-08-22 03:54:00 +03:00
|
|
|
when (eId > cycle) $ do
|
|
|
|
(mug, wen, evNoun) <- unpackJob n
|
2019-08-28 14:00:26 +03:00
|
|
|
fromNounErr evNoun &
|
|
|
|
either (logError . displayShow) pure
|
2019-08-22 03:54:00 +03:00
|
|
|
showEvents (succ eId) cycle
|
2019-07-21 07:13:21 +03:00
|
|
|
|
2019-08-28 14:00:26 +03:00
|
|
|
unpackJob :: Noun -> RIO e (Mug, Wen, Noun)
|
|
|
|
unpackJob = io . fromNounExn
|
2019-07-20 02:18:58 +03:00
|
|
|
|
2019-07-21 22:56:18 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2019-08-21 03:42:53 +03:00
|
|
|
{-
|
|
|
|
This runs the serf at `$top/.tmpdir`, but we disable snapshots,
|
|
|
|
so this should never actually be created. We just do this to avoid
|
|
|
|
letting the serf use an existing snapshot.
|
|
|
|
-}
|
2019-08-28 14:00:26 +03:00
|
|
|
collectAllFx :: ∀e. HasLogFunc e => FilePath -> RIO e ()
|
2019-07-21 22:56:18 +03:00
|
|
|
collectAllFx top = do
|
2019-08-28 14:00:26 +03:00
|
|
|
logTrace $ display $ pack @Text top
|
|
|
|
rwith collectedFX $ \() ->
|
|
|
|
logTrace "Done collecting effects!"
|
2019-08-21 03:42:53 +03:00
|
|
|
where
|
|
|
|
tmpDir :: FilePath
|
|
|
|
tmpDir = top <> "/.tmpdir"
|
|
|
|
|
2019-08-28 14:00:26 +03:00
|
|
|
collectedFX :: RAcquire e ()
|
2019-08-21 03:42:53 +03:00
|
|
|
collectedFX = do
|
2019-09-13 09:55:10 +03:00
|
|
|
lockFile top
|
2019-08-29 03:26:59 +03:00
|
|
|
log <- Log.existing (top <> "/.urb/log")
|
2019-08-28 15:22:56 +03:00
|
|
|
serf <- Serf.run (Serf.Config tmpDir serfFlags)
|
|
|
|
rio $ Serf.collectFX serf log
|
2019-08-21 03:42:53 +03:00
|
|
|
|
|
|
|
serfFlags :: Serf.Flags
|
|
|
|
serfFlags = [Serf.Hashless, Serf.DryRun]
|
2019-07-21 22:56:18 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2019-08-22 02:49:08 +03:00
|
|
|
{-
|
|
|
|
Interesting
|
|
|
|
-}
|
2019-08-28 14:00:26 +03:00
|
|
|
testPill :: HasLogFunc e => FilePath -> Bool -> Bool -> RIO e ()
|
2019-08-22 02:49:08 +03:00
|
|
|
testPill pax showPil showSeq = do
|
|
|
|
putStrLn "Reading pill file."
|
|
|
|
pillBytes <- readFile pax
|
|
|
|
|
|
|
|
putStrLn "Cueing pill file."
|
2019-08-28 14:00:26 +03:00
|
|
|
pillNoun <- io $ cueBS pillBytes & either throwIO pure
|
2019-08-22 02:49:08 +03:00
|
|
|
|
|
|
|
putStrLn "Parsing pill file."
|
2019-08-28 14:45:49 +03:00
|
|
|
pill <- fromNounErr pillNoun & either (throwIO . uncurry ParseErr) pure
|
2019-08-22 02:49:08 +03:00
|
|
|
|
|
|
|
putStrLn "Using pill to generate boot sequence."
|
2019-10-02 00:44:14 +03:00
|
|
|
bootSeq <- generateBootSeq zod pill (Fake $ Ship 0)
|
2019-08-22 02:49:08 +03:00
|
|
|
|
|
|
|
putStrLn "Validate jam/cue and toNoun/fromNoun on pill value"
|
|
|
|
reJam <- validateNounVal pill
|
|
|
|
|
|
|
|
putStrLn "Checking if round-trip matches input file:"
|
|
|
|
unless (reJam == pillBytes) $ do
|
|
|
|
putStrLn " Our jam does not match the file...\n"
|
|
|
|
putStrLn " This is surprising, but it is probably okay."
|
|
|
|
|
|
|
|
when showPil $ do
|
|
|
|
putStrLn "\n\n== Pill ==\n"
|
2019-08-28 14:00:26 +03:00
|
|
|
io $ pPrint pill
|
2019-08-22 02:49:08 +03:00
|
|
|
|
|
|
|
when showSeq $ do
|
|
|
|
putStrLn "\n\n== Boot Sequence ==\n"
|
2019-08-28 14:00:26 +03:00
|
|
|
io $ pPrint bootSeq
|
2019-08-22 02:49:08 +03:00
|
|
|
|
2019-08-28 14:00:26 +03:00
|
|
|
validateNounVal :: (HasLogFunc e, Eq a, ToNoun a, FromNoun a)
|
|
|
|
=> a -> RIO e ByteString
|
2019-08-22 02:49:08 +03:00
|
|
|
validateNounVal inpVal = do
|
|
|
|
putStrLn " jam"
|
|
|
|
inpByt <- evaluate $ jamBS $ toNoun inpVal
|
|
|
|
|
|
|
|
putStrLn " cue"
|
|
|
|
outNon <- cueBS inpByt & either throwIO pure
|
|
|
|
|
|
|
|
putStrLn " fromNoun"
|
|
|
|
outVal <- fromNounErr outNon & either (throwIO . uncurry ParseErr) pure
|
|
|
|
|
|
|
|
putStrLn " toNoun"
|
|
|
|
outNon <- evaluate (toNoun outVal)
|
|
|
|
|
|
|
|
putStrLn " jam"
|
|
|
|
outByt <- evaluate $ jamBS outNon
|
|
|
|
|
|
|
|
putStrLn "Checking if: x == cue (jam x)"
|
|
|
|
unless (inpVal == outVal) $
|
|
|
|
error "Value fails test: x == cue (jam x)"
|
|
|
|
|
|
|
|
putStrLn "Checking if: jam x == jam (cue (jam x))"
|
|
|
|
unless (inpByt == outByt) $
|
|
|
|
error "Value fails test: jam x == jam (cue (jam x))"
|
|
|
|
|
|
|
|
pure outByt
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2019-08-28 14:00:26 +03:00
|
|
|
newShip :: HasLogFunc e => CLI.New -> CLI.Opts -> RIO e ()
|
2019-10-02 21:50:03 +03:00
|
|
|
newShip CLI.New{..} opts
|
2019-10-02 00:44:14 +03:00
|
|
|
| CLI.BootComet <- nBootType =
|
|
|
|
error "Comets don't work yet"
|
|
|
|
|
|
|
|
| CLI.BootFake name <- nBootType =
|
|
|
|
let ship = shipFrom name
|
2019-10-02 21:50:03 +03:00
|
|
|
in tryBootFromPill nPillPath (pierPath name) flags ship (Fake ship)
|
2019-10-02 00:44:14 +03:00
|
|
|
|
|
|
|
| CLI.BootFromKeyfile keyFile <- nBootType = do
|
|
|
|
text <- readFileUtf8 keyFile
|
|
|
|
asAtom <- case cordToUW (Cord $ T.strip text) of
|
|
|
|
Nothing -> error "Couldn't parse keyfile. Hint: keyfiles start with 0w?"
|
|
|
|
Just (UW a) -> pure a
|
|
|
|
|
|
|
|
asNoun <- cueExn asAtom
|
|
|
|
seed :: Seed <- case fromNoun asNoun of
|
|
|
|
Nothing -> error "Keyfile does not seem to contain a seed."
|
|
|
|
Just s -> pure s
|
|
|
|
|
|
|
|
ethReturn <- dawnVent seed
|
|
|
|
|
|
|
|
case ethReturn of
|
|
|
|
Left x -> error $ unpack x
|
|
|
|
Right dawn ->
|
|
|
|
let ship = sShip $ dSeed dawn
|
|
|
|
path = (pierPath $ nameFromShip ship)
|
2019-10-02 21:50:03 +03:00
|
|
|
in tryBootFromPill nPillPath path flags ship (Dawn dawn)
|
2019-10-02 00:44:14 +03:00
|
|
|
|
2019-08-15 05:42:48 +03:00
|
|
|
where
|
2019-10-02 00:44:14 +03:00
|
|
|
shipFrom :: Text -> Ship
|
|
|
|
shipFrom name = case Ob.parsePatp name of
|
|
|
|
Left x -> error "Invalid ship name"
|
|
|
|
Right p -> Ship $ fromIntegral $ Ob.fromPatp p
|
|
|
|
|
|
|
|
pierPath :: Text -> FilePath
|
|
|
|
pierPath name = case nPierPath of
|
|
|
|
Just x -> x
|
|
|
|
Nothing -> "./" <> unpack name
|
|
|
|
|
|
|
|
nameFromShip :: Ship -> Text
|
|
|
|
nameFromShip s = name
|
|
|
|
where
|
|
|
|
nameWithSig = Ob.renderPatp $ Ob.patp $ fromIntegral s
|
|
|
|
name = case stripPrefix "~" nameWithSig of
|
|
|
|
Nothing -> error "Urbit.ob didn't produce string with ~"
|
|
|
|
Just x -> x
|
|
|
|
|
2019-10-02 21:50:03 +03:00
|
|
|
flags = toSerfFlags opts
|
|
|
|
|
2019-10-02 00:44:14 +03:00
|
|
|
|
2019-08-13 08:56:31 +03:00
|
|
|
|
2019-08-28 14:00:26 +03:00
|
|
|
runShip :: HasLogFunc e => CLI.Run -> CLI.Opts -> RIO e ()
|
2019-10-02 21:50:03 +03:00
|
|
|
runShip (CLI.Run pierPath) opts = tryPlayShip pierPath (toSerfFlags opts)
|
|
|
|
|
2019-08-13 08:56:31 +03:00
|
|
|
|
2019-08-30 02:48:46 +03:00
|
|
|
startBrowser :: HasLogFunc e => FilePath -> RIO e ()
|
2019-09-13 09:55:10 +03:00
|
|
|
startBrowser pierPath = runRAcquire $ do
|
|
|
|
lockFile pierPath
|
|
|
|
log <- Log.existing (pierPath <> "/.urb/log")
|
|
|
|
rio $ EventBrowser.run log
|
2019-08-30 02:48:46 +03:00
|
|
|
|
2019-09-24 02:42:12 +03:00
|
|
|
cordToUW :: Cord -> Maybe UW
|
|
|
|
cordToUW = fromNoun . toNoun
|
|
|
|
|
|
|
|
checkDawn :: HasLogFunc e => FilePath -> RIO e ()
|
|
|
|
checkDawn keyfilePath = do
|
|
|
|
-- The keyfile is a jammed Seed then rendered in UW format
|
|
|
|
text <- readFileUtf8 keyfilePath
|
|
|
|
asAtom <- case cordToUW (Cord $ T.strip text) of
|
|
|
|
Nothing -> error "Couldn't parse keyfile. Hint: keyfiles start with 0w?"
|
|
|
|
Just (UW a) -> pure a
|
|
|
|
|
|
|
|
asNoun <- cueExn asAtom
|
|
|
|
seed :: Seed <- case fromNoun asNoun of
|
|
|
|
Nothing -> error "Keyfile does not seem to contain a seed."
|
|
|
|
Just s -> pure s
|
|
|
|
|
|
|
|
print $ show seed
|
|
|
|
|
|
|
|
e <- dawnVent seed
|
2019-09-26 01:52:19 +03:00
|
|
|
print $ show e
|
2019-09-20 01:40:23 +03:00
|
|
|
|
2019-08-13 07:57:30 +03:00
|
|
|
main :: IO ()
|
2019-09-13 09:55:10 +03:00
|
|
|
main = do
|
|
|
|
mainTid <- myThreadId
|
|
|
|
|
|
|
|
let onTermSig = throwTo mainTid UserInterrupt
|
|
|
|
|
|
|
|
installHandler sigTERM (Catch onTermSig) Nothing
|
|
|
|
|
|
|
|
CLI.parseArgs >>= runApp . \case
|
|
|
|
CLI.CmdRun r o -> runShip r o
|
|
|
|
CLI.CmdNew n o -> newShip n o
|
|
|
|
CLI.CmdBug (CLI.CollectAllFX pax) -> collectAllFx pax
|
|
|
|
CLI.CmdBug (CLI.EventBrowser pax) -> startBrowser pax
|
|
|
|
CLI.CmdBug (CLI.ValidatePill pax pil seq) -> testPill pax pil seq
|
|
|
|
CLI.CmdBug (CLI.ValidateEvents pax f l) -> checkEvs pax f l
|
|
|
|
CLI.CmdBug (CLI.ValidateFX pax f l) -> checkFx pax f l
|
2019-09-24 02:42:12 +03:00
|
|
|
CLI.CmdBug (CLI.CheckDawn pax) -> checkDawn pax
|
2019-09-18 10:24:10 +03:00
|
|
|
CLI.CmdCon port -> connTerm port
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
connTerm :: ∀e. HasLogFunc e => Word16 -> RIO e ()
|
|
|
|
connTerm port =
|
|
|
|
Term.runTerminalClient (fromIntegral port)
|
|
|
|
|
2019-06-25 04:10:41 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2019-08-28 14:00:26 +03:00
|
|
|
checkFx :: HasLogFunc e
|
|
|
|
=> FilePath -> Word64 -> Word64 -> RIO e ()
|
2019-08-22 03:54:00 +03:00
|
|
|
checkFx pierPath first last =
|
2019-08-29 03:26:59 +03:00
|
|
|
rwith (Log.existing logPath) $ \log ->
|
2019-08-22 03:54:00 +03:00
|
|
|
runConduit $ streamFX log first last
|
|
|
|
.| tryParseFXStream
|
|
|
|
where
|
|
|
|
logPath = pierPath <> "/.urb/log"
|
2019-07-23 00:26:40 +03:00
|
|
|
|
2019-08-29 03:26:59 +03:00
|
|
|
streamFX :: HasLogFunc e
|
|
|
|
=> Log.EventLog -> Word64 -> Word64
|
|
|
|
-> ConduitT () ByteString (RIO e) ()
|
2019-08-22 03:54:00 +03:00
|
|
|
streamFX log first last = do
|
|
|
|
Log.streamEffectsRows log first .| loop
|
2019-07-23 00:46:14 +03:00
|
|
|
where
|
2019-08-22 03:54:00 +03:00
|
|
|
loop = await >>= \case Nothing -> pure ()
|
|
|
|
Just (eId, bs) | eId > last -> pure ()
|
|
|
|
Just (eId, bs) -> yield bs >> loop
|
2019-07-23 00:46:14 +03:00
|
|
|
|
2019-08-28 14:00:26 +03:00
|
|
|
tryParseFXStream :: HasLogFunc e => ConduitT ByteString Void (RIO e) ()
|
|
|
|
tryParseFXStream = loop
|
2019-07-23 03:46:06 +03:00
|
|
|
where
|
2019-08-28 14:00:26 +03:00
|
|
|
loop = await >>= \case
|
|
|
|
Nothing -> pure ()
|
2019-07-23 03:46:06 +03:00
|
|
|
Just bs -> do
|
2019-08-28 14:00:26 +03:00
|
|
|
n <- liftIO (cueBSExn bs)
|
|
|
|
fromNounErr n & either (logError . displayShow) pure
|
|
|
|
loop
|
2019-07-24 04:34:16 +03:00
|
|
|
|
2019-08-01 08:16:02 +03:00
|
|
|
|
|
|
|
{-
|
2019-06-25 04:10:41 +03:00
|
|
|
tryCopyLog :: IO ()
|
|
|
|
tryCopyLog = do
|
|
|
|
let logPath = "/Users/erg/src/urbit/zod/.urb/falselog/"
|
2019-05-31 02:04:06 +03:00
|
|
|
falselogPath = "/Users/erg/src/urbit/zod/.urb/falselog2/"
|
2019-05-31 00:24:14 +03:00
|
|
|
|
2019-07-20 06:00:23 +03:00
|
|
|
persistQ <- newTQueueIO
|
|
|
|
releaseQ <- newTQueueIO
|
2019-07-19 03:52:53 +03:00
|
|
|
(ident, nextEv, events) <-
|
2019-07-20 06:00:23 +03:00
|
|
|
with (do { log <- Log.existing logPath
|
|
|
|
; Pier.runPersist log persistQ (writeTQueue releaseQ)
|
|
|
|
; pure log
|
|
|
|
})
|
|
|
|
\log -> do
|
|
|
|
ident <- pure $ Log.identity log
|
|
|
|
events <- runConduit (Log.streamEvents log 1 .| consume)
|
|
|
|
nextEv <- Log.nextEv log
|
2019-07-19 03:52:53 +03:00
|
|
|
pure (ident, nextEv, events)
|
2019-05-31 00:24:14 +03:00
|
|
|
|
2019-06-25 04:10:41 +03:00
|
|
|
print ident
|
2019-07-19 03:52:53 +03:00
|
|
|
print nextEv
|
2019-06-25 04:10:41 +03:00
|
|
|
print (length events)
|
2019-05-31 00:24:14 +03:00
|
|
|
|
2019-07-20 06:00:23 +03:00
|
|
|
persistQ2 <- newTQueueIO
|
|
|
|
releaseQ2 <- newTQueueIO
|
|
|
|
with (do { log <- Log.new falselogPath ident
|
|
|
|
; Pier.runPersist log persistQ2 (writeTQueue releaseQ2)
|
|
|
|
; pure log
|
|
|
|
})
|
|
|
|
$ \log2 -> do
|
2019-07-19 03:52:53 +03:00
|
|
|
let writs = zip [1..] events <&> \(id, a) ->
|
2019-07-21 22:56:18 +03:00
|
|
|
(Writ id Nothing a, [])
|
2019-06-25 04:10:41 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
print "About to write"
|
2019-05-31 00:24:14 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
for_ writs $ \w ->
|
|
|
|
atomically (writeTQueue persistQ2 w)
|
2019-05-31 00:24:14 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
print "About to wait"
|
2019-06-25 04:10:41 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
replicateM_ 100 $ do
|
|
|
|
atomically $ readTQueue releaseQ2
|
2019-06-25 04:10:41 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
print "Done"
|
2019-08-01 05:34:14 +03:00
|
|
|
-}
|