Skip to content

Commit dc72e9d

Browse files
authored
feat: added site pruning logic (#427)
<!-- Describe in detail the changes you are proposing, and the rationale. --> <!-- Link all GitHub issues fixed by this PR, and add references to prior related PRs. --> Fixes # ### NEW FEATURES | UPGRADE NOTES | ENHANCEMENTS | BUG FIXES | EXPERIMENTS <!-- Write a short description of your changes. Examples: - Fixed a bug - Added a new feature - Updated documentation --> -
2 parents c8a8492 + 4ed1bd6 commit dc72e9d

22 files changed

+226
-39
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Added
2+
body: Added options for filtering of nodes as replacement for site and component targeting
3+
time: 2025-09-19T15:56:24.62356674+02:00
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Filtering commands
2+
3+
> For site-component targeting with `--filter` the deployment type of the component must be marked as `site-component`!
4+
5+
By default, mach composer cli will run all nodes in the configuration in the order inferred from the configuration. You
6+
can filter which nodes to run by using one or more `--filter` flags on the commands that support it. With this you can
7+
more selectively target specific nodes in your configuration.
8+
9+
The filter expression is a simple string that is matched against the node names:
10+
11+
- Run a component: `mach-composer apply --filter component-1`
12+
- Run a site: `mach-composer apply --filter site-1`
13+
- Run a specific component in a specific site: `mach-composer apply --filter site-1/component-1`
14+
15+
Some additional microsyntaxes are available to further define which nodes to run:
16+
17+
- `site-1...` will run site-1 and all dependent components
18+
- `site-1/component-1...` will run component-1 in site-1 and all its dependencies
19+
20+
Syntaxes can also be combined by using multiple `--filter` flags. Note that when multiple filters are provided that
21+
could match the same node, priority is given to the most generic filter. This means that any less generic filters are
22+
ignored:
23+
24+
- Ignores `site-1/component-1` because `site-1...` already matches it:
25+
`mach-composer apply --filter site-1... --filter site-1/component-1`
26+
27+
28+

docs/src/reference/cli/mach-composer.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ MACH composer is a framework that you use to orchestrate and extend modern digit
1818

1919
### SEE ALSO
2020

21-
* [mach-composer apply](mach-composer_apply.md) - Apply the configuration.
21+
* [mach-composer apply](mach-composer_apply.md) - Apply the configuration. See [the documentation](/howto/cli/filtering-commands) for filtering options.
2222
* [mach-composer cloud](mach-composer_cloud.md) - Manage your Mach Composer Cloud
2323
* [mach-composer components](mach-composer_components.md) - List all components.
2424
* [mach-composer generate](mach-composer_generate.md) - Generate the Terraform files.
2525
* [mach-composer graph](mach-composer_graph.md) - Print the execution graph for this project
26-
* [mach-composer init](mach-composer_init.md) - Initialize site directories Terraform files.
27-
* [mach-composer plan](mach-composer_plan.md) - Plan the configuration.
26+
* [mach-composer init](mach-composer_init.md) - Initialize site directories Terraform files. See [the documentation](/howto/cli/filtering-commands) for filtering options.
27+
* [mach-composer plan](mach-composer_plan.md) - Plan the configuration. See [the documentation](/howto/cli/filtering-commands) for filtering options.
2828
* [mach-composer schema](mach-composer_schema.md) - Generate a JSON schema for your config based on the plugins.
29-
* [mach-composer show-plan](mach-composer_show-plan.md) - Show the planned configuration.
29+
* [mach-composer show-plan](mach-composer_show-plan.md) - Show the planned configuration. See [the documentation](/howto/cli/filtering-commands) for filtering options.
3030
* [mach-composer sites](mach-composer_sites.md) - List all sites.
31-
* [mach-composer terraform](mach-composer_terraform.md) - Execute terraform commands directly
31+
* [mach-composer terraform](mach-composer_terraform.md) - Execute terraform commands directly. See [the documentation](/howto/cli/filtering-commands) for filtering options.
3232
* [mach-composer update](mach-composer_update.md) - Update all (or a given) component.
33-
* [mach-composer validate](mach-composer_validate.md) - Validate the generated terraform configuration.
33+
* [mach-composer validate](mach-composer_validate.md) - Validate the generated terraform configuration. See [the documentation](/howto/cli/filtering-commands) for filtering options.
3434
* [mach-composer version](mach-composer_version.md) - Return version information of the mach-composer cli
3535

docs/src/reference/cli/mach-composer_apply.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## mach-composer apply
22

3-
Apply the configuration.
3+
Apply the configuration. See [the documentation](/howto/cli/filtering-commands) for filtering options.
44

55
```
66
mach-composer apply [flags]
@@ -14,6 +14,7 @@ mach-composer apply [flags]
1414
-c, --component stringArray
1515
--destroy Destroy option is a convenient way to destroy all remote objects managed by this mach config
1616
-f, --file string YAML file to parse. (default "main.yml")
17+
--filter stringArray Run only nodes matching the filter expression
1718
--force-init Force terraform initialization. By default mach-composer will reuse existing terraform resources
1819
-g, --github Whether logs should be decorated with github-specific formatting
1920
-h, --help help for apply

docs/src/reference/cli/mach-composer_init.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## mach-composer init
22

3-
Initialize site directories Terraform files.
3+
Initialize site directories Terraform files. See [the documentation](/howto/cli/filtering-commands) for filtering options.
44

55
```
66
mach-composer init [flags]
@@ -11,6 +11,7 @@ mach-composer init [flags]
1111
```
1212
-b, --buffer Whether logs should be buffered and printed at the end of the run
1313
-f, --file string YAML file to parse. (default "main.yml")
14+
--filter stringArray Run only nodes matching the filter expression
1415
-g, --github Whether logs should be decorated with github-specific formatting
1516
-h, --help help for init
1617
--ignore-version Skip MACH composer version check

docs/src/reference/cli/mach-composer_plan.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## mach-composer plan
22

3-
Plan the configuration.
3+
Plan the configuration. See [the documentation](/howto/cli/filtering-commands) for filtering options.
44

55
```
66
mach-composer plan [flags]
@@ -12,6 +12,7 @@ mach-composer plan [flags]
1212
-b, --buffer Whether logs should be buffered and printed at the end of the run
1313
-c, --component stringArray
1414
-f, --file string YAML file to parse. (default "main.yml")
15+
--filter stringArray Run only nodes matching the filter expression
1516
--force-init Force terraform initialization. By default mach-composer will reuse existing terraform resources
1617
-g, --github Whether logs should be decorated with github-specific formatting
1718
-h, --help help for plan

docs/src/reference/cli/mach-composer_show-plan.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## mach-composer show-plan
22

3-
Show the planned configuration.
3+
Show the planned configuration. See [the documentation](/howto/cli/filtering-commands) for filtering options.
44

55
```
66
mach-composer show-plan [flags]
@@ -11,6 +11,7 @@ mach-composer show-plan [flags]
1111
```
1212
-b, --buffer Whether logs should be buffered and printed at the end of the run
1313
-f, --file string YAML file to parse. (default "main.yml")
14+
--filter stringArray Run only nodes matching the filter expression.
1415
--force-init Force terraform initialization. By default mach-composer will reuse existing terraform resources
1516
-g, --github Whether logs should be decorated with github-specific formatting
1617
-h, --help help for show-plan

docs/src/reference/cli/mach-composer_terraform.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## mach-composer terraform
22

3-
Execute terraform commands directly
3+
Execute terraform commands directly. See [the documentation](/howto/cli/filtering-commands) for filtering options.
44

55
```
66
mach-composer terraform [flags]
@@ -11,6 +11,7 @@ mach-composer terraform [flags]
1111
```
1212
-b, --buffer Whether logs should be buffered and printed at the end of the run
1313
-f, --file string YAML file to parse. (default "main.yml")
14+
--filter stringArray Run only nodes matching the filter expression
1415
-g, --github Whether logs should be decorated with github-specific formatting
1516
-h, --help help for terraform
1617
--ignore-change-detection Ignore change detection to run even if the components are considered up to date. Per default the proxy will ignore change detection (default true)

docs/src/reference/cli/mach-composer_validate.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## mach-composer validate
22

3-
Validate the generated terraform configuration.
3+
Validate the generated terraform configuration. See [the documentation](/howto/cli/filtering-commands) for filtering options.
44

55
### Synopsis
66

@@ -19,6 +19,7 @@ mach-composer validate [flags]
1919
```
2020
-b, --buffer Whether logs should be buffered and printed at the end of the run
2121
-f, --file string YAML file to parse. (default "main.yml")
22+
--filter stringArray Run only nodes matching the filter expression
2223
-g, --github Whether logs should be decorated with github-specific formatting
2324
-h, --help help for validate
2425
--ignore-version Skip MACH composer version check

internal/cmd/apply.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ var applyFlags struct {
1818
github bool
1919
bufferLogs bool
2020
components []string
21+
filters []string
2122
numWorkers int
2223
ignoreChangeDetection bool
2324
}
2425

2526
var applyCmd = &cobra.Command{
2627
Use: "apply",
27-
Short: "Apply the configuration.",
28+
Short: "Apply the configuration. See [the documentation](/howto/cli/filtering-commands) for filtering options.",
2829
PreRun: func(cmd *cobra.Command, args []string) {
2930
preprocessCommonFlags(cmd)
3031
},
@@ -39,6 +40,7 @@ func init() {
3940
applyCmd.Flags().BoolVarP(&applyFlags.forceInit, "force-init", "", false, "Force terraform initialization. By default mach-composer will reuse existing terraform resources")
4041
applyCmd.Flags().BoolVarP(&applyFlags.autoApprove, "auto-approve", "", false, "Suppress a terraform init for improved speed (not recommended for production usage)")
4142
applyCmd.Flags().BoolVarP(&applyFlags.destroy, "destroy", "", false, "Destroy option is a convenient way to destroy all remote objects managed by this mach config")
43+
applyCmd.Flags().StringArrayVarP(&applyFlags.filters, "filter", "", nil, "Run only nodes matching the filter expression")
4244
applyCmd.Flags().StringArrayVarP(&applyFlags.components, "component", "c", nil, "")
4345
applyCmd.Flags().BoolVarP(&applyFlags.ignoreChangeDetection, "ignore-change-detection", "", false, "Ignore change detection to run even if the components are considered up to date")
4446
applyCmd.Flags().BoolVarP(&applyFlags.github, "github", "g", false, "Whether logs should be decorated with github-specific formatting")
@@ -47,7 +49,7 @@ func init() {
4749

4850
func applyFunc(cmd *cobra.Command, _ []string) error {
4951
if len(applyFlags.components) > 0 {
50-
log.Warn().Msgf("Components option not implemented")
52+
log.Warn().Msgf("Component option is deprecated. Please use `--filter your-component-name instead.")
5153
}
5254

5355
if applyFlags.github && !applyFlags.bufferLogs {
@@ -84,5 +86,6 @@ func applyFunc(cmd *cobra.Command, _ []string) error {
8486
IgnoreChangeDetection: applyFlags.ignoreChangeDetection,
8587
BufferLogs: applyFlags.bufferLogs,
8688
Github: applyFlags.github,
89+
Filters: applyFlags.filters,
8790
})
8891
}

0 commit comments

Comments
 (0)