Skip to content

Commit

Permalink
The release config can now specify additional in-release json (#30)
Browse files Browse the repository at this point in the history
* The release config can now specify additional in-release json
files to be merged into the config interpolation context

* Updated comment
  • Loading branch information
timbod7 authored Jan 4, 2021
1 parent 18773f7 commit ba9298c
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 17 deletions.
7 changes: 6 additions & 1 deletion adl/release.adl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ struct ReleaseConfig
String prestartCommand;
String startCommand;
String stopCommand;

// Files inside the release to be injected into
// the template at release time. The file paths are
// relative to the root of the unpacked release.
StringMap<FilePath> configSources = {};
};

};
};
8 changes: 6 additions & 2 deletions src/ADL/Release.hs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 22 additions & 10 deletions src/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ injectContext modifyContextFn templatePath destPath = do
tcfg <- getToolConfig
liftIO $ do
-- load and merge the infrastructure context
ctx <- fmap modifyContextFn (loadMergedContext tcfg)
ctx <- fmap (modifyContextFn . JS.Object) (loadMergedInfrastructureContext tcfg)

-- interpolate the context into each templated file
expandTemplateFileToDest ctx templatePath destPath
Expand Down Expand Up @@ -141,7 +141,9 @@ unpackRelease modifyContextFn release toDir = do
rcfg <- adlFromJsonFile' (toDir </> "release.json")

-- load and merge the infrastructure context
ctx <- fmap modifyContextFn (loadMergedContext tcfg)
ctx1 <- loadMergedInfrastructureContext tcfg
ctx2 <- loadMergedReleaseContext toDir rcfg
let ctx = modifyContextFn (JS.Object (HM.union ctx1 ctx2))

-- interpolate the context into each templated file
for_ (rc_templates rcfg) $ \templatePath -> do
Expand All @@ -155,20 +157,30 @@ checkReleaseExists release = do
error ("Release " <> T.unpack release <> " does not exist in release store at " <> T.unpack (bs_label bs ))
return ()

loadMergedContext :: ToolConfig -> IO JS.Value
loadMergedContext tcfg = do
let cacheDir = T.unpack (tc_contextCache tcfg)

values <- for (M.toList (SM.toMap (tc_configSources tcfg))) $ \name_src -> do
let name = fst name_src
let src = snd name_src
-- Load template context from the infrastructure
loadMergedInfrastructureContext :: ToolConfig -> IO (HM.HashMap T.Text JS.Value)
loadMergedInfrastructureContext tcfg = do
values <- for (M.toList (SM.toMap (tc_configSources tcfg))) $ \(name,src) -> do
let cacheFilePath = configContextCacheFilePath tcfg name
lbs <- LBS.readFile cacheFilePath
case JS.eitherDecode' lbs of
(Left e) -> error ("Unable to parse json from " <> cacheFilePath)
(Right jv) -> return (takeBaseName cacheFilePath, jv)

return (JS.Object (HM.fromList ([(T.pack file,jv) | (file,jv) <- values]) ))
return (HM.fromList ([(T.pack file,jv) | (file,jv) <- values]) )

-- Load template context from the release package
loadMergedReleaseContext :: FilePath -> ReleaseConfig -> IO (HM.HashMap T.Text JS.Value)
loadMergedReleaseContext dir rcfg = do

values <- for (M.toList (SM.toMap (rc_configSources rcfg))) $ \(name,src) -> do
let filepath = dir </> T.unpack src
lbs <- LBS.readFile filepath
case JS.eitherDecode' lbs of
(Left e) -> error ("Unable to parse json from " <> filepath)
(Right jv) -> return (name, jv)

return (HM.fromList values)

expandTemplateFileToDest :: JS.Value -> FilePath -> FilePath -> IO ()
expandTemplateFileToDest ctx templatePath destPath = do
Expand Down
3 changes: 1 addition & 2 deletions test/adl-gen/config.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion test/adl-gen/release.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 25 additions & 1 deletion test/httpd-proxy-local.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,21 @@ import { makeMaybe } from "./adl-gen/sys/types";
import { makePair } from "./adl-gen/runtime/sys/types";

const testfilePath = "testfile.txt";
const extrafilePath = "extrafile.txt";

/// Release zip of a simple http server
export function makeReleaseHttpdProxyMode(
setup: TestSetup,
testfileContents: string
): JSZip {
const releaseConfig = makeReleaseConfig({
templates: ["docker-compose.yml.tpl"],
templates: ["docker-compose.yml.tpl", extrafilePath + ".tpl"],
prestartCommand: "",
startCommand: "touch start && docker-compose up -d && touch started",
stopCommand: "docker-compose kill && docker-compose rm -f",
configSources: {
"releasevals" : "releasevals.json"
},
});

const zip = new JSZip();
Expand All @@ -57,6 +61,8 @@ services:
- ./:/usr/local/apache2/htdocs/:ro
`
);
zip.file(extrafilePath + '.tpl', '{{releasevals.v1}}'),
zip.file('releasevals.json', '{"v1" : "foobazbar"}'),
zip.file(testfilePath, testfileContents);
return zip;
}
Expand Down Expand Up @@ -166,6 +172,24 @@ describe(`Run httpd-proxy-local`, () => {
if (res) {
expect(res.data).toEqual(rel.testcontents);
}

const res2 = await promiseRetry(async (retry) => {
try {
console.log("try http get file extra");
const res2 = await axios.get(
`http://${rel.endpoint}.localhost/${extrafilePath}`
);
return res2;
} catch (error) {
console.error("http get file extra error, retrying...");
retry(error);
}
});

expect(res2);
if (res2) {
expect(res2.data).toEqual("foobazbar");
}
}

{
Expand Down

0 comments on commit ba9298c

Please sign in to comment.