-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5e583b
commit 157bf8c
Showing
54 changed files
with
1,424 additions
and
1,243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.