Skip to content

Commit

Permalink
Merge pull request #591 from amazonlinux/models-refactor
Browse files Browse the repository at this point in the history
Refactor models to reduce duplication
  • Loading branch information
tjkirch authored Dec 17, 2019
2 parents 4783862 + 5cf7da6 commit 6edfc28
Show file tree
Hide file tree
Showing 20 changed files with 724 additions and 982 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ RUN --mount=target=/host \
USER builder
RUN --mount=source=.cargo,target=/home/builder/.cargo \
--mount=type=cache,target=/home/builder/.cache,from=cache,source=/cache \
--mount=type=cache,target=/home/builder/rpmbuild/BUILD/workspaces/models/current,from=variantcache,source=/variantcache \
--mount=type=cache,target=/home/builder/rpmbuild/BUILD/workspaces/models/src/variant,from=variantcache,source=/variantcache \
--mount=source=workspaces,target=/home/builder/rpmbuild/BUILD/workspaces \
rpmbuild -ba --clean rpmbuild/SPECS/${PACKAGE}.spec

Expand Down
1 change: 1 addition & 0 deletions workspaces/Cargo.lock

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

2 changes: 1 addition & 1 deletion workspaces/api/storewolf/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ fn populate_default_datastore<P: AsRef<Path>>(
}

// Read and parse defaults
let defaults_str = include_str!("../../../models/current/src/defaults.toml");
let defaults_str = include_str!("../../../models/src/variant/current/defaults.toml");
let mut defaults_val: toml::Value =
toml::from_str(defaults_str).context(error::DefaultsFormatting)?;

Expand Down
3 changes: 2 additions & 1 deletion workspaces/models/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/current/src
/src/variant/current
/src/variant/mod.rs
9 changes: 8 additions & 1 deletion workspaces/models/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name = "models"
version = "0.1.0"
authors = ["Tom Kirchner <tjk@amazon.com>"]
edition = "2018"
publish = false
build = "build.rs"

[dependencies]
base64 = "0.10"
Expand All @@ -13,6 +15,11 @@ snafu = "0.5"
toml = "0.5"
url = "2.1"

[build-dependencies]
cargo-readme = "3.1"

[lib]
# We're picking the current *model* with build.rs, so users shouldn't think
# about importing *models* (plural), just the one current model.
name = "model"
path = "current/src/model.rs"
path = "src/lib.rs"
40 changes: 30 additions & 10 deletions workspaces/models/README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
# API models
# models

Current version: 0.1.0

## API models

Thar has different variants supporting different features and use cases.
Each variant has its own set of software, and therefore needs its own configuration.
We support having an API model for each variant to support these different configurations.

## aws-k8s: Kubernetes
Each model defines a top-level `Settings` structure.
It can use pre-defined structures inside, or custom ones as needed.

This `Settings` essentially becomes the schema for the variant's data store.
`apiserver::datastore` offers serialization and deserialization modules that make it easy to map between Rust types and the data store, and thus, all inputs and outputs are type-checked.

At the field level, standard Rust types can be used, or ["modeled types"](src/modeled_types) that add input validation.

* [Model](aws-k8s/lib.rs)
* [Defaults](aws-k8s/defaults.toml)
### aws-k8s: Kubernetes

## aws-dev: Development build
* [Model](src/aws-k8s/mod.rs)
* [Defaults](src/aws-k8s/defaults.toml)

* [Model](aws-dev/lib.rs)
* [Defaults](aws-dev/defaults.toml)
### aws-dev: Development build

# This directory
* [Model](src/aws-dev/mod.rs)
* [Defaults](src/aws-dev/defaults.toml)

## This directory

We use `build.rs` to symlink the proper API model source code for Cargo to build.
We determine the "proper" model by using the `VARIANT` environment variable.
Expand All @@ -25,7 +37,15 @@ When building with the Thar build system, `VARIANT` is based on `BUILDSYS_VARIAN

Note: when building with the build system, we can't create the symlink in the source directory during a build - the directories are owned by `root`, but we're `builder`.
We can't use a read/write bind mount with current Docker syntax.
To get around this, in the top-level `Dockerfile`, we mount a "cache" directory at `current` that we can modify.
We set Cargo (via `Cargo.toml`) to look for the source at `current/src`, rather than the default `src`.
To get around this, in the top-level `Dockerfile`, we mount a "cache" directory at `src/variant` that we can modify, and create a `current` symlink inside.
The code in `src/lib.rs` then imports the requested model using `variant/current`.

Note: for the same reason, we symlink `variant/mod.rs` to `variant_mod.rs`.
Rust needs a `mod.rs` file to understand that a directory is part of the module structure, so we have to have `variant/mod.rs`.
`variant/` is the cache mount that starts empty, so we have to store the file elsewhere and link it in.

Note: all models share the same `Cargo.toml`.

## Colophon

This text was generated from `README.tpl` using [cargo-readme](https://crates.io/crates/cargo-readme), and includes the rustdoc from `src/lib.rs`.
9 changes: 9 additions & 0 deletions workspaces/models/README.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# {{crate}}

Current version: {{version}}

{{readme}}

## Colophon

This text was generated from `README.tpl` using [cargo-readme](https://crates.io/crates/cargo-readme), and includes the rustdoc from `src/lib.rs`.
110 changes: 0 additions & 110 deletions workspaces/models/aws-dev/model.rs

This file was deleted.

Loading

0 comments on commit 6edfc28

Please sign in to comment.