1
0
mirror of https://github.com/raseels-repos/golang-saas-starter-kit.git synced 2025-06-19 00:27:42 +02:00

checkpoint

This commit is contained in:
Lee Brown
2019-07-08 12:21:22 -08:00
parent aef8054cbd
commit 5c477b4dc9
6 changed files with 1092 additions and 388 deletions

View File

@ -0,0 +1,126 @@
{
"family": "{SERVICE}",
"executionRoleArn": "",
"taskRoleArn": "",
"networkMode": "awsvpc",
"containerDefinitions": [
{
"name": "{ECS_SERVICE}",
"image": "421734222910.dkr.ecr.us-west-2.amazonaws.com/procedures:{ECS_SERVICE}",
"essential": true,
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "{AWSLOGS_GROUP}",
"awslogs-region": "us-west-2",
"awslogs-stream-prefix": "ecs"
}
},
"portMappings": [
{
"hostPort": 80,
"protocol": "tcp",
"containerPort": 80
}
],
"cpu": 768,
"memoryReservation": 2560,
"volumesFrom": [],
"environment": [
{"name": "AWSLOGS_GROUP", "value": "{AWSLOGS_GROUP}"},
{"name": "ECS_CLUSTER", "value": "{ECS_CLUSTER}"},
{"name": "ECS_SERVICE", "value": "{ECS_SERVICE}"},
{"name": "HAS_LB", "value": "{HAS_LB}"},
{"name": "DATADOG_ADDR", "value": "127.0.0.1:8125"},
{"name": "DD_TRACE_AGENT_HOSTNAME", "value": "127.0.0.1"},
{"name": "DD_TRACE_AGENT_PORT", "value": "8126"},
{"name": "DD_SERVICE_NAME", "value": "{ECS_SERVICE}"},
{"name": "DD_ENV", "value": "{ENV}"}
],
"healthCheck": {
"retries": 3,
"command": [
"CMD-SHELL",
"curl -f http://localhost/ping || exit 1"
],
"timeout": 5,
"interval": 60,
"startPeriod": 60
},
"dockerLabels": {
"com.datadoghq.ad.check_names": "[\"{ECS_SERVICE}\"]",
"com.datadoghq.ad.logs": "[{\"source\": \"docker\", \"service\": \"{ECS_SERVICE}\", \"service_name\": \"{SERVICE}\", \"cluster\": \"{ECS_CLUSTER}\", \"env\": \"{ENV}\"}]",
"com.datadoghq.ad.init_configs": "[{}]",
"com.datadoghq.ad.instances": "[{\"host\": \"%%host%%\", \"port\": 80}]"
},
"ulimits": [
{
"name": "nofile",
"softLimit": 987654,
"hardLimit": 999999
}
]
},
{
"name": "datadog-agent",
"image": "421734222910.dkr.ecr.us-west-2.amazonaws.com/procedures:datadog-agent",
"essential": true,
"cpu": 256,
"memoryReservation": 512,
"portMappings": [
{
"containerPort": 8125
},
{
"containerPort": 8126
}
],
"environment": [
{
"name": "DD_API_KEY",
"value": "{DATADOG_APIKEY}"
},
{
"name": "DD_LOGS_ENABLED",
"value": "true"
},
{
"name": "DD_APM_ENABLED",
"value": "true"
},
{
"name": "DD_RECEIVER_PORT",
"value": "8126"
},
{
"name": "DD_APM_NON_LOCAL_TRAFFIC",
"value": "true"
},
{
"name": "DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL",
"value": "true"
},
{
"name": "DD_TAGS",
"value": "source:docker service:{ECS_SERVICE} service_name:{SERVICE} cluster:{ECS_CLUSTER} env:{ENV}"
},
{
"name": "DD_DOGSTATSD_ORIGIN_DETECTION",
"value": "true"
},
{
"name": "DD_DOGSTATSD_NON_LOCAL_TRAFFIC",
"value": "true"
},
{
"name": "ECS_FARGATE",
"value": "true"
}
]
}
],
"volumes": [],
"requiresCompatibilities": [
"FARGATE"
]
}

View File

@ -56,8 +56,6 @@ func findServiceDockerFile(projectRoot, targetService string) (string, error) {
// Check to see if directory contains Dockerfile. // Check to see if directory contains Dockerfile.
tf := filepath.Join(cd, "Dockerfile") tf := filepath.Join(cd, "Dockerfile")
fmt.Println(tf)
ok, _ := exists(tf) ok, _ := exists(tf)
if ok { if ok {
dockerFile = tf dockerFile = tf

View File

@ -1,213 +0,0 @@
package devops
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/tests"
"github.com/pkg/errors"
)
// requiredCmdsBuild proves a list of required executables for completing build.
var requiredCmdsBuild = [][]string{
[]string{"docker", "version", "-f", "{{.Client.Version}}"},
}
// Run is the main entrypoint for building a service for a given target env.
func ServiceBuild(log *log.Logger, projectRoot, targetService, targetEnv, releaseImage string, noPush, noCache bool) error {
log.SetPrefix(log.Prefix() + " build : ")
//
log.Println("Verify required commands are installed.")
for _, cmdVals := range requiredCmdsBuild {
cmd := exec.Command(cmdVals[0], cmdVals[1:]...)
cmd.Env = os.Environ()
out, err := cmd.CombinedOutput()
if err != nil {
return errors.WithMessagef(err, "failed to execute %s - %s\n%s", strings.Join(cmdVals, " "), string(out))
}
log.Printf("\t%s\t%s - %s", tests.Success, cmdVals[0], string(out))
}
// When project root directory is empty or set to current working path, then search for the project root by locating
// the go.mod file.
var goModFile string
if projectRoot == "" || projectRoot == "." {
log.Println("Attempting to location project root directory from current working directory.")
var err error
goModFile, err = findProjectGoModFile()
if err != nil {
return err
}
projectRoot = filepath.Dir(goModFile)
} else {
log.Println("Using supplied project root directory.")
goModFile = filepath.Join(projectRoot, "go.mod")
}
log.Printf("\t\tproject root: %s", projectRoot)
log.Printf("\t\tgo.mod: %s", goModFile)
log.Printf("\t%s\tFound project root directory.", tests.Success)
log.Println("Extracting go module name from go.mod.")
modName, err := loadGoModName(goModFile)
if err != nil {
return err
}
log.Printf("\t\tmodule name: %s", modName)
log.Printf("\t%s\tgo module name.", tests.Success)
log.Println("Determining the project name.")
projectName := getTargetEnv(targetEnv, "PROJECT_NAME")
if projectName != "" {
log.Printf("\t\tproject name: %s", projectName)
log.Printf("\t%s\tFound env variable.", tests.Success)
} else {
projectName = filepath.Base(modName)
log.Printf("\t\tproject name: %s", projectName)
log.Printf("\t%s\tSet from go module.", tests.Success)
}
log.Println("Attempting to locate service directory from project root directory.")
dockerFile, err := findServiceDockerFile(projectRoot, targetService)
if err != nil {
return err
}
serviceDir := filepath.Dir(dockerFile)
log.Printf("\t\tservice directory: %s", serviceDir)
log.Printf("\t\tdockerfile: %s", dockerFile)
log.Printf("\t%s\tFound service directory.", tests.Success)
log.Println("Verify release image.")
if releaseImage == "" {
if v := os.Getenv("CI_REGISTRY_IMAGE"); v != "" {
releaseImage = fmt.Sprintf("%s:%s-%s", v, targetService, targetEnv)
} else {
releaseImage = fmt.Sprintf("%s/%s:latest", targetService, targetEnv)
noPush = true
}
}
log.Printf("\t\trelease image: %s", releaseImage)
log.Printf("\t%s\tRelease image valid.", tests.Success)
// Load the AWS
log.Println("Verify AWS credentials.")
awsCreds, err := GetAwsCredentials(targetEnv)
if err != nil {
return err
}
log.Printf("\t\tAccessKeyID: %s", awsCreds.AccessKeyID)
log.Printf("\t\tRegion: %s", awsCreds.Region)
// Set default AWS Registry Name if needed.
if awsCreds.RepositoryName == "" {
awsCreds.RepositoryName = projectName
log.Printf("\t\tSetting Repository Name to Project Name.")
}
log.Printf("\t\tRepository Name: %s", awsCreds.RepositoryName)
log.Printf("\t%s\tAWS credentials valid.", tests.Success)
// Pull the current env variables to be passed in for command execution.
envVars := EnvVars(os.Environ())
// Do the docker build.
{
cmdVals := []string{
"docker",
"build",
"--file=" + dockerFile,
"--build-arg", "service=" + targetService,
"--build-arg", "env=" + targetEnv,
"-t", releaseImage,
}
if noCache {
cmdVals = append(cmdVals, "--no-cache")
}
cmdVals = append(cmdVals, ".")
log.Printf("starting docker build: \n\t%s\n", strings.Join(cmdVals, " "))
out, err := execCmds(projectRoot, &envVars, cmdVals)
if err != nil {
return err
}
log.Printf("build complete\n\t%s\n", string(out[0]))
}
// Push the newly built docker container to the registry.
if !noPush {
log.Println("Push release image.")
_, err = execCmds(projectRoot, &envVars, []string{"docker", "push", releaseImage})
if err != nil {
return err
}
}
if awsCreds.RepositoryName != "" {
awsRepo, err := EcrGetOrCreateRepository(awsCreds, projectName, targetEnv)
if err != nil {
return err
}
maxImages := defaultAwsRegistryMaxImages
if v := getTargetEnv(targetEnv, "AWS_REPOSITORY_MAX_IMAGES"); v != "" {
maxImages, err = strconv.Atoi(v)
if err != nil {
return errors.WithMessagef(err, "Failed to parse max ECR images")
}
}
log.Println("Purging old ECR images.")
err = EcrPurgeImages(log, awsCreds, maxImages)
if err != nil {
return err
}
log.Println("Retrieve ECR authorization token used for docker login.")
dockerLogin, err := GetEcrLogin(awsCreds)
if err != nil {
return err
}
awsRegistryImage := *awsRepo.RepositoryUri
// Login to AWS docker registry and pull release image locally.
log.Println("Push release image to ECR.")
log.Printf("\t\t%s", awsRegistryImage)
_, err = execCmds(projectRoot, &envVars, dockerLogin, []string{"docker", "tag", releaseImage, awsRegistryImage}, []string{"docker", "push", awsRegistryImage})
if err != nil {
return err
}
tag1 := targetEnv+"-"+targetService
log.Printf("\t\ttagging as %s", tag1)
_, err = execCmds(projectRoot, &envVars, []string{"docker", "tag", releaseImage, awsRegistryImage+":"+tag1}, []string{"docker", "push", awsRegistryImage+":"+tag1})
if err != nil {
return err
}
if v := os.Getenv("CI_COMMIT_REF_NAME"); v != "" {
tag2 := tag1 + "-" + v
log.Printf("\t\ttagging as %s", tag2)
_, err = execCmds(projectRoot, &envVars, []string{"docker", "tag", releaseImage, awsRegistryImage+ ":" + tag2}, []string{"docker", "push", awsRegistryImage + ":" + tag2})
if err != nil {
return err
}
}
}
return nil
}

File diff suppressed because it is too large Load Diff

View File

@ -121,6 +121,8 @@ func main() {
// ========================================================================= // =========================================================================
// Start Truss // Start Truss
var deployFlags devops.ServiceDeployFlags
app := cli.NewApp() app := cli.NewApp()
app.Commands = []cli.Command{ app.Commands = []cli.Command{
{ {
@ -206,61 +208,25 @@ func main() {
return dbtable2crud.Run(masterDb, log, cfg.DB.Database, dbTable, modelFile, modelName, templateDir, projectPath, c.Bool("saveChanges")) return dbtable2crud.Run(masterDb, log, cfg.DB.Database, dbTable, modelFile, modelName, templateDir, projectPath, c.Bool("saveChanges"))
}, },
}, },
{
Name: "build",
Aliases: []string{"serviceBuild"},
Usage: "-service=web-api -env=dev [-image=gitlab.com/example-project:latest] [-root=.]",
Flags: []cli.Flag{
cli.StringFlag{Name: "service", Usage: "name of cmd"},
cli.StringFlag{Name: "env", Usage: "dev, stage, or prod"},
cli.StringFlag{Name: "image", Usage: "release image used to tag docker build"},
cli.StringFlag{Name: "root", Usage: "project root directory"},
cli.BoolFlag{Name: "no_push", Usage: "skip docker push after build"},
cli.BoolFlag{Name: "no_cache", Usage: "skip docker cache"},
},
Action: func(c *cli.Context) error {
service := strings.TrimSpace(c.String("service"))
env := strings.TrimSpace(c.String("env"))
image := strings.TrimSpace(c.String("image"))
projectRoot := strings.TrimSpace(c.String("root"))
noPush := c.Bool("no_push")
noCache := c.Bool("no_cache")
if image == "-" {
image = ""
}
return devops.ServiceBuild(log, projectRoot, service, env, image, noPush, noCache)
},
},
{ {
Name: "deploy", Name: "deploy",
Aliases: []string{"serviceDeploy"}, Aliases: []string{"serviceDeploy"},
Usage: "-service=web-api -env=dev [-image=gitlab.com/example-project:latest] [-root=.]", Usage: "-service=web-api -env=dev [-image=gitlab.com/example-project:latest] [-root=.]",
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.StringFlag{Name: "service", Usage: "name of cmd"}, cli.StringFlag{Name: "service", Usage: "name of cmd", Destination: &deployFlags.ServiceName},
cli.StringFlag{Name: "env", Usage: "dev, stage, or prod"}, cli.StringFlag{Name: "env", Usage: "dev, stage, or prod", Destination: &deployFlags.Env},
cli.StringFlag{Name: "image", Usage: "release image used to tag docker build"}, cli.StringFlag{Name: "root", Usage: "project root directory", Destination: &deployFlags.ProjectRoot},
cli.StringFlag{Name: "root", Usage: "project root directory"}, cli.BoolTFlag{Name: "vpc, enable_vpc", Usage: "deploy contain behind VPC", Destination: &deployFlags.VPC},
cli.StringFlag{Name: "cluster, ecs_cluster", Usage: "name of the AWS EC2 cluster."}, cli.BoolFlag{Name: "elb, enable_elb", Usage: "enable deployed to use Elastic Load Balancer", Destination: &deployFlags.ELB},
cli.BoolFlag{Name: "vpc, enable_vpc", Usage: "skip docker push after build"}, cli.BoolFlag{Name: "sd, enable_sd", Usage: "register tasks with service discovery", Destination: &deployFlags.SD},
cli.BoolFlag{Name: "no_build", Usage: "skip docker push after build"}, cli.BoolFlag{Name: "no_build", Usage: "skip build and continue directly to deploy", Destination: &deployFlags.NoBuild},
cli.BoolFlag{Name: "no_deploy", Usage: "skip docker push after build"}, cli.BoolFlag{Name: "no_deploy", Usage: "skip deploy after build", Destination: &deployFlags.NoDeploy},
cli.BoolFlag{Name: "no_cache", Usage: "skip docker cache"}, cli.BoolFlag{Name: "no_cache", Usage: "skip docker cache", Destination: &deployFlags.NoCache},
cli.BoolFlag{Name: "no_push", Usage: "skip docker push after build", Destination: &deployFlags.NoPush},
cli.BoolFlag{Name: "debug", Usage: "print additional information", Destination: &deployFlags.Debug},
}, },
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
service := strings.TrimSpace(c.String("service")) return devops.ServiceDeploy(log, &deployFlags)
env := strings.TrimSpace(c.String("env"))
image := strings.TrimSpace(c.String("image"))
projectRoot := strings.TrimSpace(c.String("root"))
ecsCluster := strings.TrimSpace(c.String("cluster"))
enableVpc := c.Bool("vpc")
if image == "-" {
image = ""
}
return devops.ServiceDeploy(log, projectRoot, service, env, image, ecsCluster, enableVpc)
}, },
}, },
} }

View File

@ -6,15 +6,23 @@ export TRUSS_DB_DISABLE_TLS=true
# Variables to configure service build and deploy. # Variables to configure service build and deploy.
# export PROJECT_NAME=example-project #export PROJECT_NAME=example-project
#export DEPLOY_MIN_HEALTH=100 # percentage expressed as int
#export DEPLOY_MAX_HEALTH=200 # percentage expressed as int
#export DEPLOY_DEREG_DELAY=0 # seconds to wait for connections to drain
# Variables to configure AWS for service build and deploy. # Variables to configure AWS for service build and deploy.
# Use the same set for AWS credentials for all target envinments. # Use the same set for AWS credentials for all target envinments.
#AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXX #AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXX
#AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXX #AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXX
#AWS_REGION=us-west-2 #AWS_REGION=us-west-2
#AWS_REPOSITORY_NAME=example-project #AWS_ECR_REPOSITORY_NAME=example-project
#AWS_REPOSITORY_MAX_IMAGES=1000 #AWS_ECR_REPOSITORY_MAX_IMAGES=1000
#AWS_ECS_CLUSTER_NAME=example-project
#AWS_ECS_SERVICE_NAME=web-api-dev
#AWS_CLOUDWATCH_LOG_GROUP_NAME=logs/env/dev
# AWS credentials can be prefixed with the target uppercased target envinments. # AWS credentials can be prefixed with the target uppercased target envinments.
@ -28,5 +36,4 @@ export TRUSS_DB_DISABLE_TLS=true
# GitLab CI/CD environment variables. These are set by the GitLab when the build # GitLab CI/CD environment variables. These are set by the GitLab when the build
# pipeline is running. These can be optional set for testing/debugging locally. # pipeline is running. These can be optional set for testing/debugging locally.
#CI_REGISTRY_IMAGE="registry.example.com/gitlab-org/gitlab-ce"
#CI_COMMIT_REF_NAME=master #CI_COMMIT_REF_NAME=master