Skip to content

Commit fafcb2a

Browse files
committed
fix: re-added site targeting
1 parent 0dd1968 commit fafcb2a

25 files changed

+346
-37
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Fixed
2+
body: Re-added site targeting on plan and apply
3+
time: 2024-12-10T12:08:46.512121798+01:00

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mach-composer apply [flags]
1717
-h, --help help for apply
1818
--ignore-change-detection Ignore change detection to run even if the components are considered up to date
1919
--ignore-version Skip MACH composer version check
20-
--output-path string Outputs path to store the generated files. (default "deployments")
20+
-o, --output-path string Outputs path to store the generated files. (default "deployments")
2121
-s, --site string Site to parse. If not set parse all sites.
2222
--var-file string Use a variable file to parse the configuration with.
2323
-w, --workers int The number of workers to use (default 1)

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ mach-composer components [flags]
1212
-f, --file string YAML file to parse. (default "main.yml")
1313
-h, --help help for components
1414
--ignore-version Skip MACH composer version check
15-
--output-path string Outputs path to store the generated files. (default "deployments")
16-
-s, --site string Site to parse. If not set parse all sites.
15+
-o, --output-path string Outputs path to store the generated files. (default "deployments")
1716
--var-file string Use a variable file to parse the configuration with.
1817
-w, --workers int The number of workers to use (default 1)
1918
```

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ mach-composer generate [flags]
1212
-f, --file string YAML file to parse. (default "main.yml")
1313
-h, --help help for generate
1414
--ignore-version Skip MACH composer version check
15-
--output-path string Outputs path to store the generated files. (default "deployments")
16-
-s, --site string Site to parse. If not set parse all sites.
15+
-o, --output-path string Outputs path to store the generated files. (default "deployments")
1716
--var-file string Use a variable file to parse the configuration with.
1817
-w, --workers int The number of workers to use (default 1)
1918
```

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ mach-composer graph [flags]
2525
-f, --file string YAML file to parse. (default "main.yml")
2626
-h, --help help for graph
2727
--ignore-version Skip MACH composer version check
28-
--output-path string Outputs path to store the generated files. (default "deployments")
29-
-s, --site string Site to parse. If not set parse all sites.
28+
-o, --output-path string Outputs path to store the generated files. (default "deployments")
3029
--var-file string Use a variable file to parse the configuration with.
3130
-w, --workers int The number of workers to use (default 1)
3231
```

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mach-composer init [flags]
1212
-f, --file string YAML file to parse. (default "main.yml")
1313
-h, --help help for init
1414
--ignore-version Skip MACH composer version check
15-
--output-path string Outputs path to store the generated files. (default "deployments")
15+
-o, --output-path string Outputs path to store the generated files. (default "deployments")
1616
-s, --site string Site to parse. If not set parse all sites.
1717
--var-file string Use a variable file to parse the configuration with.
1818
-w, --workers int The number of workers to use (default 1)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mach-composer plan [flags]
1616
--ignore-change-detection Ignore change detection to run even if the components are considered up to date
1717
--ignore-version Skip MACH composer version check
1818
--lock Acquire a lock on the state file before running terraform plan (default true)
19-
--output-path string Outputs path to store the generated files. (default "deployments")
19+
-o, --output-path string Outputs path to store the generated files. (default "deployments")
2020
-s, --site string Site to parse. If not set parse all sites.
2121
--var-file string Use a variable file to parse the configuration with.
2222
-w, --workers int The number of workers to use (default 1)

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ mach-composer show-plan [flags]
1515
--ignore-change-detection Ignore change detection to run even if the components are considered up to date
1616
--ignore-version Skip MACH composer version check
1717
--no-color Disable color output
18-
--output-path string Outputs path to store the generated files. (default "deployments")
19-
-s, --site string Site to parse. If not set parse all sites.
18+
-o, --output-path string Outputs path to store the generated files. (default "deployments")
2019
--var-file string Use a variable file to parse the configuration with.
2120
-w, --workers int The number of workers to use (default 1)
2221
```

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ mach-composer sites [flags]
1212
-f, --file string YAML file to parse. (default "main.yml")
1313
-h, --help help for sites
1414
--ignore-version Skip MACH composer version check
15-
--output-path string Outputs path to store the generated files. (default "deployments")
16-
-s, --site string Site to parse. If not set parse all sites.
15+
-o, --output-path string Outputs path to store the generated files. (default "deployments")
1716
--var-file string Use a variable file to parse the configuration with.
1817
-w, --workers int The number of workers to use (default 1)
1918
```

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ mach-composer terraform [flags]
1313
-h, --help help for terraform
1414
--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)
1515
--ignore-version Skip MACH composer version check
16-
--output-path string Outputs path to store the generated files. (default "deployments")
17-
-s, --site string Site to parse. If not set parse all sites.
16+
-o, --output-path string Outputs path to store the generated files. (default "deployments")
1817
--var-file string Use a variable file to parse the configuration with.
1918
-w, --workers int The number of workers to use (default 1)
2019
```

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ mach-composer validate [flags]
2020
-f, --file string YAML file to parse. (default "main.yml")
2121
-h, --help help for validate
2222
--ignore-version Skip MACH composer version check
23-
--output-path string Outputs path to store the generated files. (default "deployments")
24-
-s, --site string Site to parse. If not set parse all sites.
23+
-o, --output-path string Outputs path to store the generated files. (default "deployments")
2524
--validation-path string Directory path to store files required for configuration validation. (default "validations")
2625
--var-file string Use a variable file to parse the configuration with.
2726
-w, --workers int The number of workers to use (default 1)

internal/cmd/apply.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var applyFlags struct {
1616
autoApprove bool
1717
destroy bool
1818
components []string
19+
site string
1920
numWorkers int
2021
ignoreChangeDetection bool
2122
}
@@ -37,6 +38,7 @@ func init() {
3738
applyCmd.Flags().BoolVarP(&applyFlags.forceInit, "force-init", "", false, "Force terraform initialization. By default mach-composer will reuse existing terraform resources")
3839
applyCmd.Flags().BoolVarP(&applyFlags.autoApprove, "auto-approve", "", false, "Suppress a terraform init for improved speed (not recommended for production usage)")
3940
applyCmd.Flags().BoolVarP(&applyFlags.destroy, "destroy", "", false, "Destroy option is a convenient way to destroy all remote objects managed by this mach config")
41+
applyCmd.Flags().StringVarP(&applyFlags.site, "site", "s", "", "Site to parse. If not set parse all sites.")
4042
applyCmd.Flags().StringArrayVarP(&applyFlags.components, "component", "c", nil, "")
4143
applyCmd.Flags().BoolVarP(&applyFlags.ignoreChangeDetection, "ignore-change-detection", "", false, "Ignore change detection to run even if the components are considered up to date")
4244
}
@@ -50,7 +52,7 @@ func applyFunc(cmd *cobra.Command, _ []string) error {
5052
defer cfg.Close()
5153
ctx := cmd.Context()
5254

53-
dg, err := graph.ToDeploymentGraph(cfg, commonFlags.outputPath)
55+
dg, err := graph.ToDeploymentGraph(cfg, commonFlags.outputPath, graph.WithTargetSiteName(applyFlags.site))
5456
if err != nil {
5557
return err
5658
}

internal/cmd/common.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717

1818
type CommonFlags struct {
1919
configFile string
20-
siteName string
2120
ignoreVersion bool
2221
outputPath string
2322
varFile string
@@ -29,20 +28,15 @@ var commonFlags CommonFlags
2928
func registerCommonFlags(cmd *cobra.Command) {
3029
cmd.Flags().StringVarP(&commonFlags.configFile, "file", "f", "main.yml", "YAML file to parse.")
3130
cmd.Flags().StringVarP(&commonFlags.varFile, "var-file", "", "", "Use a variable file to parse the configuration with.")
32-
cmd.Flags().StringVarP(&commonFlags.siteName, "site", "s", "", "Site to parse. If not set parse all sites.")
3331
cmd.Flags().BoolVarP(&commonFlags.ignoreVersion, "ignore-version", "", false, "Skip MACH composer version check")
34-
cmd.Flags().StringVarP(&commonFlags.outputPath, "output-path", "", "deployments",
32+
cmd.Flags().StringVarP(&commonFlags.outputPath, "output-path", "o", "deployments",
3533
"Outputs path to store the generated files.")
3634
cmd.Flags().IntVarP(&commonFlags.workers, "workers", "w", 1, "The number of workers to use")
3735

3836
_ = cmd.RegisterFlagCompletionFunc("site", AutocompleteSiteName)
3937
}
4038

4139
func preprocessCommonFlags(cmd *cobra.Command) {
42-
if commonFlags.siteName != "" {
43-
log.Warn().Msgf("Site option not implemented")
44-
}
45-
4640
handleError(cmd.MarkFlagFilename("var-file", "yml", "yaml"))
4741
handleError(cmd.MarkFlagFilename("file", "yml", "yaml"))
4842

internal/cmd/init.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import (
1111
"github.com/mach-composer/mach-composer-cli/internal/runner"
1212
)
1313

14+
var initFlags struct {
15+
site string
16+
}
17+
1418
var initCmd = &cobra.Command{
1519
Use: "init",
1620
Short: "Initialize site directories Terraform files.",
@@ -28,14 +32,15 @@ var initCmd = &cobra.Command{
2832

2933
func init() {
3034
registerCommonFlags(initCmd)
35+
initCmd.Flags().StringVarP(&initFlags.site, "site", "s", "", "Site to parse. If not set parse all sites.")
3136
}
3237

3338
func initFunc(cmd *cobra.Command, _ []string) error {
3439
cfg := loadConfig(cmd, true)
3540
defer cfg.Close()
3641
ctx := cmd.Context()
3742

38-
dg, err := graph.ToDeploymentGraph(cfg, commonFlags.outputPath)
43+
dg, err := graph.ToDeploymentGraph(cfg, commonFlags.outputPath, graph.WithTargetSiteName(initFlags.site))
3944
if err != nil {
4045
return err
4146
}

internal/cmd/plan.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
var planFlags struct {
1515
forceInit bool
16+
site string
1617
components []string
1718
lock bool
1819
ignoreChangeDetection bool
@@ -33,6 +34,7 @@ var planCmd = &cobra.Command{
3334
func init() {
3435
registerCommonFlags(planCmd)
3536
planCmd.Flags().BoolVarP(&planFlags.forceInit, "force-init", "", false, "Force terraform initialization. By default mach-composer will reuse existing terraform resources")
37+
planCmd.Flags().StringVarP(&planFlags.site, "site", "s", "", "Site to parse. If not set parse all sites.")
3638
planCmd.Flags().StringArrayVarP(&planFlags.components, "component", "c", nil, "")
3739
planCmd.Flags().BoolVarP(&planFlags.lock, "lock", "", true, "Acquire a lock on the state file before running terraform plan")
3840
planCmd.Flags().BoolVarP(&planFlags.ignoreChangeDetection, "ignore-change-detection", "", false, "Ignore change detection to run even if the components are considered up to date")
@@ -47,7 +49,7 @@ func planFunc(cmd *cobra.Command, _ []string) error {
4749
defer cfg.Close()
4850
ctx := cmd.Context()
4951

50-
dg, err := graph.ToDeploymentGraph(cfg, commonFlags.outputPath)
52+
dg, err := graph.ToDeploymentGraph(cfg, commonFlags.outputPath, graph.WithTargetSiteName(planFlags.site))
5153
if err != nil {
5254
return err
5355
}

internal/graph/deployment.go

+59-3
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,45 @@ import (
77
"github.com/mach-composer/mach-composer-cli/internal/config"
88
)
99

10+
type options struct {
11+
siteTarget string
12+
}
13+
14+
type Option func(o *options)
15+
16+
func WithTargetSiteName(site string) Option {
17+
return func(o *options) {
18+
o.siteTarget = site
19+
}
20+
}
21+
1022
// ToDeploymentGraph converts a MachConfig to a Graph ready for deployment.
1123
// This means that all nodes that are not independently deployable are pruned from the graph.
12-
func ToDeploymentGraph(cfg *config.MachConfig, outPath string) (*Graph, error) {
24+
func ToDeploymentGraph(cfg *config.MachConfig, outPath string, opts ...Option) (*Graph, error) {
25+
o := options{
26+
siteTarget: "",
27+
}
28+
29+
for _, opt := range opts {
30+
opt(&o)
31+
}
32+
1333
g, err := ToDependencyGraph(cfg, outPath)
1434
if err != nil {
1535
return nil, err
1636
}
1737

18-
if err = validateDeployment(g); err != nil {
38+
if err := validateDeployment(g); err != nil {
1939
return nil, err
2040
}
2141

2242
// Remove all nodes that are not independent to site node
23-
if err = reduceNodes(g); err != nil {
43+
if err := reduceNodes(g); err != nil {
44+
return nil, err
45+
}
46+
47+
//Prune to only include the site node if provided
48+
if err := targetSiteNode(g, o.siteTarget); err != nil {
2449
return nil, err
2550
}
2651

@@ -132,3 +157,34 @@ func reduceNodes(g *Graph) error {
132157

133158
return pErr
134159
}
160+
161+
func targetSiteNode(g *Graph, site string) error {
162+
if site == "" {
163+
return nil
164+
}
165+
166+
if !g.VertexExists(site) {
167+
return fmt.Errorf("site node %s does not exist", site)
168+
}
169+
170+
for _, v := range g.VerticesByType(SiteType) {
171+
if v.Identifier() != site {
172+
var pErr error
173+
_ = graph.DFS(g.Graph, v.Path(), func(p string) bool {
174+
n, err := g.Graph.Vertex(p)
175+
if err != nil {
176+
pErr = err
177+
return true
178+
}
179+
180+
n.SetTargeted(false)
181+
182+
return false
183+
})
184+
185+
return pErr
186+
}
187+
}
188+
189+
return nil
190+
}

0 commit comments

Comments
 (0)