-
Notifications
You must be signed in to change notification settings - Fork 21
feat: attic cache support #671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CertainLach
wants to merge
7
commits into
hercules-ci:master
Choose a base branch
from
CertainLach:push-rxxoonvsyulw
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1d83270
feat: attic cache support
CertainLach 5a73c8a
refactor(attic): use tomland for attic config rendering
CertainLach 68f5fa4
feat(attic): hide CLI stdout, output stderr
CertainLach 3b01d6f
feat(attic): handle netrc
CertainLach c08bc8b
feat(attic): use persistent CA secretState subdir
CertainLach a079a95
feat(attic): prune ca secret state dirs on startup
CertainLach ba8d6b6
feat(attic): always push the full closure
CertainLach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
hercules-ci-agent/hercules-ci-agent/Hercules/Agent/Attic.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| {-# LANGUAGE BlockArguments #-} | ||
|
|
||
| module Hercules.Agent.Attic | ||
| ( push, | ||
| substituterURL, | ||
| toNetrcLines, | ||
| ) | ||
| where | ||
|
|
||
| import Crypto.Hash | ||
| import Data.Map (singleton) | ||
| import Data.Map qualified as M | ||
| import Data.Text.IO qualified as T | ||
| import Hercules.Agent.Env (App) | ||
| import Hercules.Agent.Log | ||
| import Hercules.Agent.Token (withContentAddressedSecretState) | ||
| import Hercules.CNix qualified as CNix | ||
| import Hercules.CNix.Store (StorePath) | ||
| import Hercules.Formats.AtticCache (AtticCache) | ||
| import Hercules.Formats.AtticCache qualified as AtticCache | ||
| import Network.URI (URIAuth (uriPort, uriRegName), parseURI, uriAuthority) | ||
| import Protolude hiding (hash) | ||
| import System.Directory (createDirectoryIfMissing) | ||
| import System.Environment qualified | ||
| import System.FilePath ((</>)) | ||
| import System.Process hiding (readCreateProcessWithExitCode) | ||
| import System.Process.ByteString (readCreateProcessWithExitCode) | ||
| import Toml (TomlCodec, (.=)) | ||
| import Toml qualified | ||
|
|
||
| substituterURL :: AtticCache -> Text | ||
| substituterURL c = | ||
| AtticCache.serverEndpoint c <> "/" <> AtticCache.cacheName c | ||
|
|
||
| -- TODO: Should reject attic caches with the same endpoint, given that | ||
| -- netrc is hostname-based, but attic caches are path-based. | ||
| -- The token leak should not cause any problems here, | ||
| -- but user might encounter authorization failure. Does netrc even works with the | ||
| -- duplicate machine entry? | ||
| toNetrcLines :: Map Text AtticCache -> [Text] | ||
| toNetrcLines = mapMaybe toLine . M.elems | ||
| where | ||
| toLine cache = do | ||
| uri <- parseURI (toS (AtticCache.serverEndpoint cache)) | ||
| auth <- uriAuthority uri | ||
| let host = toS (uriRegName auth <> uriPort auth) :: Text | ||
| pure $ "machine " <> host <> " password " <> AtticCache.token cache | ||
|
|
||
| sha256 :: ByteString -> Digest SHA256 | ||
| sha256 = hash | ||
|
|
||
| -- Attic only supports loading the token and cache location from config file, | ||
| -- thus we need to create a synthetic one first. | ||
| -- TODO: Maybe attic can be improved for that? | ||
| push :: CNix.Store -> Text -> AtticCache -> [StorePath] -> App () | ||
| push store localName cache paths = do | ||
| pathStrings <- | ||
| liftIO $ | ||
| mapM | ||
| ( \p -> do | ||
| bs <- CNix.storePathToPath store p | ||
| return (decodeUtf8With lenientDecode bs) | ||
| ) | ||
| paths | ||
| logLocM DebugS (logStr ("Pushing to attic cache " <> localName)) | ||
| let configText = renderConfig cache | ||
| configHash = sha256 $ encodeUtf8 configText | ||
| (exitCode, _out, err) <- | ||
| withContentAddressedSecretState | ||
| "attic-config" | ||
| configHash | ||
| ( \tmpCfgDir -> liftIO $ do | ||
| let atticDir = tmpCfgDir </> "attic" | ||
| let cfgFile = atticDir </> "config.toml" | ||
| createDirectoryIfMissing True atticDir | ||
| T.writeFile cfgFile configText | ||
| ) | ||
| ( \cfgDir -> liftIO $ do | ||
| oldEnv <- System.Environment.getEnvironment | ||
| let isXdg k = k == "XDG_CONFIG_HOME" || k == "XDG_CACHE_HOME" | ||
| let newEnv = | ||
| [("XDG_CONFIG_HOME", cfgDir), ("XDG_CACHE_HOME", cfgDir)] | ||
| ++ filter (\(k, _) -> not (isXdg k)) oldEnv | ||
| let args = | ||
| ["push", toS (AtticCache.cacheName cache)] | ||
| ++ map toS pathStrings | ||
| let p = (proc "attic" args) {env = Just newEnv, close_fds = True} | ||
| readCreateProcessWithExitCode p "" | ||
| ) | ||
| case exitCode of | ||
| ExitSuccess -> pure () | ||
| ExitFailure c -> throwIO $ FatalError $ "Attic push failed with exit code " <> show c <> ", stderr: " <> (decodeUtf8With lenientDecode err) | ||
|
|
||
| data AtticServer = AtticServer | ||
| { endpoint :: Text, | ||
| token :: Text | ||
| } | ||
|
|
||
| data AtticClientConfig = AtticClientConfig | ||
| { defaultServer :: Text, | ||
| servers :: Map Text AtticServer | ||
| } | ||
|
|
||
| atticServerCodec :: TomlCodec AtticServer | ||
| atticServerCodec = | ||
| AtticServer | ||
| <$> Toml.text "endpoint" | ||
| .= endpoint | ||
| <*> Toml.text "token" .= token | ||
|
|
||
| atticClientConfigCodec :: TomlCodec AtticClientConfig | ||
| atticClientConfigCodec = | ||
| AtticClientConfig | ||
| <$> Toml.text "default-server" | ||
| .= defaultServer | ||
| <*> Toml.tableMap Toml._KeyText (Toml.table atticServerCodec) "servers" .= servers | ||
|
|
||
| renderConfig :: AtticCache -> Text | ||
| renderConfig c = | ||
| Toml.encode atticClientConfigCodec $ | ||
| AtticClientConfig | ||
| { defaultServer = "hercules", | ||
| servers = | ||
| singleton "hercules" $ | ||
| AtticServer | ||
| { endpoint = AtticCache.serverEndpoint c, | ||
| token = AtticCache.token c | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| module Hercules.Formats.AtticCache where | ||
|
|
||
| import Data.Aeson | ||
| import Data.Foldable | ||
| import Data.Text (Text) | ||
| import Hercules.Formats.Common | ||
| ( noVersion, | ||
| withKind, | ||
| withVersions, | ||
| ) | ||
| import Prelude | ||
|
|
||
| data AtticCache = AtticCache | ||
| { serverEndpoint :: Text, | ||
| cacheName :: Text, | ||
| token :: Text, | ||
| publicKeys :: [Text] | ||
| } | ||
|
|
||
| instance ToJSON AtticCache where | ||
| toJSON a = | ||
| object $ | ||
| [ "kind" .= String "AtticCache", | ||
| "serverEndpoint" .= serverEndpoint a, | ||
| "cacheName" .= cacheName a, | ||
| "token" .= token a | ||
| ] | ||
| <> ["publicKeys" .= publicKeys a] | ||
|
|
||
| toEncoding a = | ||
| pairs | ||
| ( "kind" | ||
| .= String "AtticCache" | ||
| <> "serverEndpoint" | ||
| .= serverEndpoint a | ||
| <> "cacheName" | ||
| .= cacheName a | ||
| <> "token" | ||
| .= token a | ||
| <> "publicKeys" | ||
| .= publicKeys a | ||
| ) | ||
|
|
||
| instance FromJSON AtticCache where | ||
| parseJSON = | ||
| withKind "AtticCache" $ | ||
| withVersions | ||
| [ noVersion $ \o -> | ||
| AtticCache | ||
| <$> o .: "serverEndpoint" | ||
| <*> o .: "cacheName" | ||
| <*> o .: "token" | ||
| <*> (fold <$> o .:? "publicKeys") | ||
| ] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Untested for now, I'm also not sure why cachix handles it in a different way