Skip to content

Commit

Permalink
refactor: reorganise code base
Browse files Browse the repository at this point in the history
  • Loading branch information
Ric Featherstone authored and 06kellyjac committed Dec 21, 2023
1 parent b5e583b commit 157bf8c
Show file tree
Hide file tree
Showing 54 changed files with 1,424 additions and 1,243 deletions.
3 changes: 0 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
.git/
.simulator/
bin/
config/
docs/
internal/
packer/.cache/
packer/github.com/
simulation-scripts/
terraform/workspaces/simulator/.terraform/
.dockerignore
.editorconfig
.gitignore
aws-environment
dev.Dockerfile
Dockerfile
LICENSE
Expand Down
8 changes: 4 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ linters:
# - gci
# - ginkgolinter # not using ginkgo
- gocheckcompilerdirectives
# - gochecknoglobals
# - gochecknoinits
- gochecknoglobals
- gochecknoinits
- gocognit # complexity
- goconst
- gocritic
Expand All @@ -74,7 +74,7 @@ linters:
- importas # configure this
- interfacebloat # maybe configure
- ineffassign
# - ireturn
- ireturn
- lll
- loggercheck
# - maintidx # using gocognit
Expand Down Expand Up @@ -111,7 +111,7 @@ linters:
- unparam
- usestdlibvars
- unused
# - varnamelen
- varnamelen
- wastedassign
- whitespace
- wrapcheck
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ COPY --chown=ubuntu:ubuntu packer packer
COPY --chown=ubuntu:ubuntu terraform terraform
COPY --chown=ubuntu:ubuntu ansible ansible

RUN cd packer && packer init bastion.pkr.hcl && packer init k8s.pkr.hcl
RUN cd terraform/workspaces/simulator && terraform init -backend=false
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ simulator-image: simulator-dev-image
docker build -t $(SIMULATOR_IMAGE) .

simulator-cli: lint
go build -v -o bin/simulator internal/cmd/main.go
go build -v -o bin/simulator cmd/simulator/main.go
5 changes: 0 additions & 5 deletions aws-environment

This file was deleted.

77 changes: 77 additions & 0 deletions cmd/container/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"log/slog"
"os"
"path/filepath"

"github.com/spf13/cobra"

"github.com/controlplaneio/simulator/v2/core/tools"
"github.com/controlplaneio/simulator/v2/internal/cli"
"github.com/controlplaneio/simulator/v2/internal/config"
"github.com/controlplaneio/simulator/v2/internal/logging"
)

func main() {
// create variables for the file paths within the container
simulatorDir := "/simulator"
adminBundleDir := filepath.Join(simulatorDir, "config", "admin")
packerDir := filepath.Join(simulatorDir, "packer")
terraformWorkspaceDir := filepath.Join(simulatorDir, "terraform/workspaces/simulator")
ansiblePlaybookDir := filepath.Join(simulatorDir, "ansible/playbooks")

// configure slog
logging.Configure()

conf := config.Config{}
if err := conf.Read(); err != nil {
slog.Error("failed to read config", "error", err)
os.Exit(1)
}

amiBuilder := tools.Packer{
WorkingDir: packerDir,
Output: os.Stdout,
}

infraManager := tools.Terraform{
WorkingDir: terraformWorkspaceDir,
Output: os.Stdout,
}

scenarioManager := tools.AnsiblePlaybook{
WorkingDir: adminBundleDir,
PlaybookDir: ansiblePlaybookDir,
Output: os.Stdout,
}

withStateBucketFlag := cli.WithFlag("stateBucket", "", "the name of the S3 bucket to store Terraform state")
withStateKeyFlag := cli.WithFlag("stateKey", "", "the path to the state file in the S3 bucket")
withNameFlag := cli.WithFlag("name", "", "the name used for the Simulator infrastructure")

simulator := cli.NewSimulatorCmd(
cli.WithAMICmd(
cli.WithAmiBuildCmd(amiBuilder),
),
cli.WithInfraCmd(
cli.WithInfraCreateCmd(infraManager,
withStateBucketFlag,
withStateKeyFlag,
withNameFlag,
),
cli.WithInfraDestroyCmd(infraManager,
withStateBucketFlag,
withStateKeyFlag,
withNameFlag,
),
),
cli.WithScenarioCmd(
cli.WithScenarioInstallCmd(scenarioManager),
cli.WithScenarioUninstallCmd(scenarioManager),
),
)

err := simulator.Execute()
cobra.CheckErr(err)
}
158 changes: 158 additions & 0 deletions cmd/simulator/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package main

import (
"errors"
"log/slog"
"os"
"path/filepath"

"github.com/spf13/cobra"

"github.com/controlplaneio/simulator/v2/core/aws"
"github.com/controlplaneio/simulator/v2/core/tools"
"github.com/controlplaneio/simulator/v2/internal/cli"
"github.com/controlplaneio/simulator/v2/internal/config"
"github.com/controlplaneio/simulator/v2/internal/docker"
"github.com/controlplaneio/simulator/v2/internal/logging"
)

const (
ownerReadWriteExecute = 0700
)

func main() {
logging.Configure()

conf := config.Config{}
if err := conf.Read(); err != nil {
slog.Error("failed to read config", "error", err)
os.Exit(1)
}

adminBundleDir, err := conf.AdminBundleDir()
if err != nil {
slog.Error("failed to determine admin bundle dir", "error", err)
os.Exit(1)
}

playerBundleDir, err := conf.PlayerBundleDir()
if err != nil {
slog.Error("failed to determine player bundle dir", "error", err)
os.Exit(1)
}

mkDirsIfNotExist(adminBundleDir, playerBundleDir)

mounts := []docker.MountConfig{
{
Source: adminBundleDir,
Target: "/simulator/config/admin",
},
{
Source: playerBundleDir,
Target: "/simulator/config/player",
},
{
Source: "/home/ric/.aws",
Target: "/home/ubuntu/.aws",
ReadOnly: true,
},
}

// If running in dev mode, mount the configuration directories
if conf.Cli.Dev {
mounts = append(mounts, []docker.MountConfig{
{
Source: filepath.Join(conf.BaseDir, "packer"),
Target: "/simulator/packer",
},
{
Source: filepath.Join(conf.BaseDir, "terraform"),
Target: "/simulator/terraform",
},
{
Source: filepath.Join(conf.BaseDir, "ansible"),
Target: "/simulator/ansible",
},
}...)
}

dockerConfig := &docker.Config{
Image: conf.Container.Image,
Rootless: conf.Container.Rootless,
Env: aws.EnvVars(),
Mounts: mounts,
}

dockerClient, err := docker.NewClient()
if err != nil {
slog.Error("failed to create docker client", "error", err)
os.Exit(1)
}

awsBucketCreator := aws.S3{}
amiManager := aws.EC2{}
amiCreator := tools.PackerContainer{
Client: dockerClient,
Config: dockerConfig,
}
infraManager := tools.TerraformContainer{
Client: dockerClient,
Config: dockerConfig,
}
scenarioManager := tools.AnsiblePlaybookContainer{
Client: dockerClient,
Config: dockerConfig,
}

withStateBucketFlag := cli.WithFlag("stateBucket", conf.Bucket, "the name of the S3 bucket to store Terraform state")
withStateKeyFlag := cli.WithFlag("stateKey", "terraform.tfstate", "the path to the state file in the S3 bucket")
withNameFlag := cli.WithFlag("name", conf.Name, "the name used for the Simulator infrastructure")

simulator := cli.NewSimulatorCmd(
cli.WithConfigCmd(conf),
cli.WithBucketCmd(
cli.WithCreateBucketCmd(conf, awsBucketCreator),
),
cli.WithContainerCmd(
cli.WithContainerPullCmd(conf, dockerClient),
),
cli.WithAMICmd(
cli.WithAmiBuildCmd(amiCreator),
cli.WithAMIListCmd(amiManager),
cli.WithAMIDeleteCmd(amiManager),
),
cli.WithInfraCmd(
cli.WithInfraCreateCmd(infraManager,
withStateBucketFlag,
withStateKeyFlag,
withNameFlag,
),
cli.WithInfraDestroyCmd(infraManager,
withStateBucketFlag,
withStateKeyFlag,
withNameFlag,
),
),
cli.WithScenarioCmd(
cli.WithScenarioListCmd(),
cli.WithScenarioDescribeCmd(),
cli.WithScenarioInstallCmd(scenarioManager),
cli.WithScenarioUninstallCmd(scenarioManager),
),
)

err = simulator.Execute()
cobra.CheckErr(err)
}

func mkDirsIfNotExist(dirs ...string) {
for _, dir := range dirs {
if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) {
if err := os.Mkdir(dir, ownerReadWriteExecute); err != nil {
slog.Error("failed to bundle directory", "dir", dir, "error", err)
os.Exit(1)
}
}
}
}
34 changes: 0 additions & 34 deletions controlplane/cli/bucket.go

This file was deleted.

38 changes: 0 additions & 38 deletions controlplane/cli/image.go

This file was deleted.

Loading

0 comments on commit 157bf8c

Please sign in to comment.