Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ let

configurePhase = attrs.configurePhase or "export HOME=$(mktemp -d)";

nativeBuildInputs = (mkDerivationAttrs.nativeBuildInputs or [])
++ pkgs.lib.singleton pkgs.ip2unix;

buildPhase = attrs.buildPhase or ''
runHook preBuild

Expand All @@ -175,15 +178,16 @@ let

echo "Starting napalm registry"

napalm-registry --snapshot ${snapshot} &
registrySock="$TMPDIR/registry.sock"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
registrySock="$TMPDIR/registry.sock"
registrySock="$(mktemp -d)/registry.sock"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why nest this into another directory? We already set this up accordingly in the builder sandbox and if this would interfere with build-specific files, the mktemp variant would have the same issue.

napalm-registry --path "$registrySock" --snapshot ${snapshot} &
napalm_REGISTRY_PID=$!

while ! nc -z localhost 8081; do
echo waiting for registry to be alive on port 8081
while [ ! -e "$registrySock" ]; do
echo waiting for registry socket to be alive in "$registrySock"
sleep 1
done

npm config set registry 'http://localhost:8081'
npm config set registry 'http://127.0.0.100'

export CPATH="${pkgs.nodejs}/include/node:$CPATH"

Expand All @@ -195,7 +199,8 @@ let
while IFS= read -r c
do
echo "Running npm command: $c"
$c || (echo "$c: failure, aborting" && kill $napalm_REGISTRY_PID && exit 1)
ip2unix -r out,addr=127.0.0.100,path="$registrySock" -- $c \
|| (echo "$c: failure, aborting" && kill $napalm_REGISTRY_PID && exit 1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
|| (echo "$c: failure, aborting" && kill $napalm_REGISTRY_PID && exit 1)
|| (echo "$c: failure, aborting" && (rm -rf $(dirname $registrySock) || true) && kill $napalm_REGISTRY_PID && exit 1)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The socket file should already be cleaned up by ip2unix, but since the kill signal is SIGTERM it depends on the signal handler of the registry. Nevertheless, if we assume that things are running unsandboxed and are not cleaned up properly by Nix, I'd probably avoid unquoted rm -rf at all costs, eg. more like this (without the extra directory, as mentioned above):

Suggested change
|| (echo "$c: failure, aborting" && kill $napalm_REGISTRY_PID && exit 1)
|| (echo "$c: failure, aborting" && kill "$napalm_REGISTRY_PID" && rm -f "$TMPDIR/registry.sock"; exit 1)

echo "Overzealously patching shebangs"
if [ -d node_modules ]; then find node_modules -type d -name bin | \
while read file; do patchShebangs $file; done; fi
Expand Down
29 changes: 17 additions & 12 deletions napalm-registry/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ import qualified Data.HashMap.Strict as HMS
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Encoding as T
import qualified Network.Socket as Socket
import qualified Network.URI.Encode as URI
import qualified Network.Wai.Handler.Warp as Warp
import qualified Servant as Servant

-- | See 'parseConfig' for field descriptions
data Config = Config
{ configVerbose :: Bool
, configEndpoint :: T.Text
, configPort :: Int
, configHost :: T.Text
, configSocketPath :: FilePath
, configSnapshot :: FilePath
}

Expand All @@ -50,7 +51,13 @@ main = do
snapshot <- Aeson.decodeFileStrict (configSnapshot config) >>= \case
Just snapshot -> pure snapshot
Nothing -> error $ "Could not parse packages"
Warp.run (configPort config) (Servant.serve api (server config snapshot))

socket <- Socket.socket Socket.AF_UNIX Socket.Stream 0
Socket.bind socket . Socket.SockAddrUnix $ configSocketPath config
Socket.listen socket Socket.maxListenQueue

Warp.runSettingsSocket Warp.defaultSettings socket $
Servant.serve api (server config snapshot)

parseConfig :: Opts.Parser Config
parseConfig = Config <$>
Expand All @@ -61,13 +68,13 @@ parseConfig = Config <$>
) <*>
Opts.strOption (
Opts.long "endpoint" <>
Opts.value "localhost" <>
Opts.help "The endpoint of this server, used in the Tarball URL"
Opts.value "127.0.0.100" <>
Opts.help "The fake endpoint of this server, used in the Tarball URL"
) <*>
Opts.option Opts.auto (
Opts.long "port" <>
Opts.value 8081 <>
Opts.help "The to serve on, also used in the Tarball URL"
Opts.strOption (
Opts.long "path" <>
Opts.value "registry.sock" <>
Opts.help "The Unix socket path to serve on"
) <*>
Opts.strOption (
Opts.long "snapshot" <>
Expand Down Expand Up @@ -251,9 +258,7 @@ mkTarballURL
(URI.encodeText . unTarballName -> tarName)
= "http://" <>
T.intercalate "/"
[ configEndpoint config <> ":" <> tshow (configPort config), pn, "-", tarName ]
where
tshow = T.pack . show
[ configHost config, pn, "-", tarName ]

readPackageJson :: FilePath -> IO Aeson.Object
readPackageJson fp = do
Expand Down
1 change: 1 addition & 0 deletions napalm-registry/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies:
- time
- unordered-containers
- uri-encode
- network
- warp
- zlib

Expand Down