Skip to content

Commit

Permalink
Test Cases for Ha Backend
Browse files Browse the repository at this point in the history
  • Loading branch information
sparshadotel committed Sep 12, 2019
1 parent 532868b commit 379fe51
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 6 deletions.
2 changes: 1 addition & 1 deletion deployment/services/mq/mq.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func failOnError(err error, msg string) {

// GetConnection returns the connection to rabbitmq
func GetConnection() *amqp.Connection {
conn, err := amqp.Dial("amqp://shift:shiftdeveloper@dev-shiftmq.lftechnology.com:5672")
conn, err := amqp.Dial("amqp://shift:shiftdeveloper@localhost:5672")

failOnError(err, "Failed to connect to RabbitMQ")

Expand Down
30 changes: 30 additions & 0 deletions infrastructure/infrastructure/infrastrucutre.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package infrastrucuture

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/briandowns/spinner"
"github.com/leapfrogtechnology/shift/core/utils/logger"
"github.com/leapfrogtechnology/shift/infrastructure/internal/terraform"
backendHaArchitecture "github.com/leapfrogtechnology/shift/infrastructure/internal/terraform/templates/providers/aws/backend-ha-architecture"
frontendArchitecture "github.com/leapfrogtechnology/shift/infrastructure/internal/terraform/templates/providers/aws/frontend-architecture"
"github.com/leapfrogtechnology/shift/infrastructure/utils"
"io/ioutil"
"os/exec"
"path/filepath"
"strings"
"time"
)

func Initialize(ClientArgs []byte) (string, error) {
Expand Down Expand Up @@ -113,5 +117,31 @@ func InitializeBackend(ClientArgs []byte) (string, error) {
return "", err
}
logger.LogOutput(string(out))

// Deployment
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) // Build our new spinner
s.Prefix = " "
s.Suffix = " Deploying"
_ = s.Color("cyan", "bold")
s.Start()
region := "us-east-1"
cloneUrl := "https://" + result.Deployment.GitToken + "@" + result.Deployment.CloneUrl[8:]
command := fmt.Sprintf("rm -rf code && git clone %s code && $(AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s aws ecr get-login --no-include-email --region %s) && docker build/%s code -t %s && docker push %s && terraform apply -var repo_url=%s -var port=%s --auto-approve", cloneUrl, result.Deployment.AccessKey, result.Deployment.SecretKey, region, result.Deployment.DockerFilePath,result.Data.RepoUrl.Value, result.Data.RepoUrl.Value, result.Data.RepoUrl.Value, result.Deployment.Port)
logger.LogInfo(command)
cmd := exec.Command("bash", "-c", command)
cmd.Dir = workspaceDir
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
runError := cmd.Run()
if runError != nil {
logger.LogError(runError, stderr.String())
s.Stop()
} else {
logger.LogOutput(stdout.String())
s.Stop()
}

return string(out), err
}
2 changes: 1 addition & 1 deletion infrastructure/internal/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Initialize() {
go func() {
for message := range messages {
log.Printf("Received a message: %s", message.MessageId)
infrastructureInfo, err := infrastrucuture.InitializeFrontend(message.Body)
infrastructureInfo, err := infrastrucuture.Initialize(message.Body)
if err != nil {
logger.LogError(err, "Cannot Init Infrastructure")
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ variable "health_check_path" {
default = "{{ info.Client.Deployment.HealthCheckPath }}"
}
variable "repo_name" {
type = "string"
default = "leapfrogtechnology/shift-ui"
}
// Provider Initialization
provider "aws" {
Expand Down Expand Up @@ -227,7 +231,7 @@ variable "fargate_container_port" {
variable "ecr_name" {
type = "string"
default = "{{ info.Client.Deployment|lower }}"
default = "{{ info.Client.Project|lower }}/{{ info.Client.Deployment.Name|lower }}-backend"
}
resource "aws_iam_role" "ECSAutoScalingRole" {
Expand Down Expand Up @@ -363,12 +367,17 @@ resource "aws_iam_role_policy_attachment" "attach-tasks-execution" {
policy_arn = aws_iam_policy.ECSTasksExecutionPolicy.arn
}
variable "container_port" {
default = 80
}
data "template_file" "container_definition" {
template = file("sample.json.tpl")
vars = {
fargate_container_name = var.fargate_cluster_name
fargate_cluster_name = var.fargate_cluster_name
fargate_container_port = 80
fargate_container_port = var.container_port
repo_name = var.repo_name
}
}
Expand Down Expand Up @@ -404,7 +413,7 @@ output "backendServiceId" {
}
output "backendClusterName" {
value = var.fargate.cluster_name
value = var.fargate_cluster_name
}
output "backendTaskDefinitionId" {
Expand All @@ -418,12 +427,13 @@ output "backendContainerDefinition" {
output "appUrl" {
value = aws_alb.main.dns_name
}
// Template
`

const ContainerTemplate = `[
{
"name": "${fargate_container_name}",
"image": "leapfrogtechnology/shift-ui",
"image": "${repo_name}",
"cpu": 0,
"memoryReservation": 256,
"portMappings": [
Expand Down
1 change: 1 addition & 0 deletions infrastructure/utils/mq-structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type deployment struct {
DistFolder string `json:"distFolder"`
Port string `json:"port"`
HealthCheckPath string `json:"healthCheckPath"`
DockerFilePath string `json:"dockerFilePath"`
}
type Client struct {
Project string `json:"projectName"`
Expand Down
38 changes: 38 additions & 0 deletions tests/terraform/ha-backend/deploy/deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"bytes"
"fmt"
"os"
"os/exec"
)

const accessKey = ""
const secretKey = ""
const repoUrl = ""

func main() {
_ = os.Setenv("AWS_ACCESS_KEY_ID", accessKey)
_ = os.Setenv("AWS_SECRET_ACCESS_KEY", secretKey)
_ = os.Setenv("REGION_NAME", "us-east-1")
path, err := os.Getwd()
if err != nil {
panic(err)
}
command := fmt.Sprintf("$(AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s aws ecr get-login --no-include-email --region %s) && docker build docker -t %s && docker push %s", accessKey, secretKey, "us-east-1", repoUrl, repoUrl)
fmt.Println(command)
cmd := exec.Command("/bin/bash", "-c", command)
cmd.Dir = path
fmt.Println(cmd.Env)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
fmt.Println(stderr.String())
panic(err)
} else {
fmt.Println(stdout.String())
}
}
3 changes: 3 additions & 0 deletions tests/terraform/ha-backend/deploy/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html
EXPOSE 80
42 changes: 42 additions & 0 deletions tests/terraform/ha-backend/deploy/docker/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!doctype html>
<title>Site is currently Deploying</title>
<style>
body {
text-align: center;
padding: 150px;
}

h1 {
font-size: 50px;
}

body {
font: 20px Helvetica, sans-serif;
color: #333;
}

article {
display: block;
text-align: left;
width: 650px;
margin: 0 auto;
}

a {
color: #dc8100;
text-decoration: none;
}

a:hover {
color: #333;
text-decoration: none;
}
</style>

<article>
<h1>We&rsquo;ll be back soon!</h1>
<div>
<p>These things to take some time. While you wait why not <a href="https://kiteykite.netlify.com" target="_blank" rel="noopener">play this game</a> </p>
<p>&mdash; The Team</p>
</div>
</article>
1 change: 1 addition & 0 deletions tests/terraform/ha-backend/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ output "backendUrl" {
}
`


const ContainerTemplate = `[
{
"name": "${fargate_container_name}",
Expand Down

0 comments on commit 379fe51

Please sign in to comment.