Skip to content

Commit

Permalink
Merge pull request #3 from Sunbird-Serve/release-need-1.1.0.0
Browse files Browse the repository at this point in the history
Release need 1.1.0.0 to main
  • Loading branch information
Sowmya-Raghuram authored Feb 5, 2024
2 parents 19e1c0b + 13524f8 commit 4cdff0d
Show file tree
Hide file tree
Showing 97 changed files with 2,444 additions and 0 deletions.
104 changes: 104 additions & 0 deletions .github/workflows/aws.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
name: Deploy to Amazon ECS

on:
push:
branches: ["main", "release-*"]
#pull_request:
# The branches below must be a subset of the branches above
#branches: [ "main" ]

env:
AWS_REGION: ${{ github.ref == 'refs/heads/main' && 'us-east-1' || startsWith(github.ref, 'refs/heads/release-') && 'ap-south-1' }}
ECR_REPOSITORY: sunbird-serve-need # set this to your Amazon ECR repository name
ECS_SERVICE: sunbird-serve-need # set this to your Amazon ECS service name
ECS_CLUSTER: ${{ github.ref == 'refs/heads/main' && 'ecs-staging' || startsWith(github.ref, 'refs/heads/release-') && 'ecs-production' }}
ECS_TASK_DEFINITION:
.github/workflows/task-definition.json # set this to the path to your Amazon ECS task definition
# file, e.g. .aws/task-definition.json
CONTAINER_NAME:
sunbird-serve-need # set this to the name of the container in the
# containerDefinitions section of your task definition
SHELL_SCRIPT_NAME: .github/workflows/create-json.sh
PYTHON_FILE_NAME: .github/workflows/replace-secrets.py

permissions:
contents: read
id-token: write

jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
#environment: ${{ github.ref == 'refs/heads/main' && 'stage-' || github.ref == 'refs/heads/release-' && '' }}

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set environment variables for the "release-" branch
run: |
if [ "${GITHUB_REF##*/}" = "release-" ]; then
echo "Setting environment variables for release- branch"
echo "environment=stage-" >> $GITHUB_ENV
else
echo "release- branch not detected. No environment variables to set."
fi
- name: Shell script to create JSON
run: |
chmod +x ${{ env.SHELL_SCRIPT_NAME }}
${{ env.SHELL_SCRIPT_NAME }}
shell: bash

- name: Dump older task definition
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
run: |
aws ecs describe-task-definition --region ${{ env.AWS_REGION }} --task-definition ${{ env.ECS_SERVICE }} --query taskDefinition > .github/workflows/task-definition.json
aws ecs list-clusters --region ${{ env.AWS_REGION }}
cat .github/workflows/task-definition.json
- name: Python to update TD
run: python ${{ env.PYTHON_FILE_NAME }}

- name: Print new task definition
run: |
cat .github/workflows/task-definition.json
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ env.ECS_TASK_DEFINITION }}
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ steps.build-image.outputs.image }}

- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: false
36 changes: 36 additions & 0 deletions .github/workflows/create-json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#/bin/bash
account_id=049302583731
if [ -f /tmp/__secrets.json ] ; then
rm -f /tmp/__secrets.json
fi

branch_name=${GITHUB_REF#refs/heads/}
echo $branch_name

if [ "$branch_name" == "main" ] ; then
prefix=stage
#region="ap-south-1"
elif [ "$branch_name" == "release" ] ; then
prefix=prod
#region=us-east-1
fi
printf '{
"secrets": [' > /tmp/__secrets.json


for line in `cat .${prefix}env`
do
key=$line
if [[ -z "$line" || "$line" =~ ^[[:space:]]*# ]]; then
continue
fi
echo "{
"'"name"'": "'"'$key'"'",
"'"valueFrom"'": "'"'"arn:aws:ssm:ap-south-1:$account_id:parameter/$key"'"'"
}," >> /tmp/__secrets.json
done
sed '$ s/,$//' /tmp/__secrets.json > .github/workflows/secrets.json

echo ']}' >> .github/workflows/secrets.json

cat .github/workflows/secrets.json
44 changes: 44 additions & 0 deletions .github/workflows/replace-secrets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import json

original_taskdef = ".github/workflows/task-definition.json"
secrets_array = ".github/workflows/secrets.json"


with open(original_taskdef, 'r') as file:
task_definition = json.load(file)

for item in task_definition:
if "containerDefinitions" in item:
containerDefinitions = task_definition['containerDefinitions']
for item in containerDefinitions:
if "secrets" in item:
print("secret block exists")
# print(json.dumps(containerDefinitions[0]['secrets'], indent=4)) # if want to print


with open(secrets_array, 'r') as new_file:
new_data = json.load(new_file)
new_secrets = new_data['secrets']
# print(json.dumps(new_secrets, indent=4))

# __task_definition = task_definition["taskDefinition"]
# __secrets_to_replace = __task_definition["containerDefinitions"]
# secrets_to_replace = __secrets_to_replace[0]["secrets"]
# __secrets_to_replace[0]["secrets"] = new_secrets


__secrets_to_replace = task_definition['containerDefinitions']
secrets_to_replace = __secrets_to_replace[0]['secrets']
__secrets_to_replace[0]['secrets'] = new_secrets

# task_definition['secrets'] = new_secrets #replace old secrets block with new_secrets


print(json.dumps(task_definition, indent=4))


# writing to new file
with open(".github/workflows/task-definition.json", 'w') as outfile:
json.dump(task_definition, outfile, indent=4)

print("Updated JSON has been written to new_task_definition.json file.")
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/
Binary file added .gradle/8.5/checksums/checksums.lock
Binary file not shown.
Binary file added .gradle/8.5/checksums/md5-checksums.bin
Binary file not shown.
Binary file added .gradle/8.5/checksums/sha1-checksums.bin
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file added .gradle/8.5/executionHistory/executionHistory.lock
Binary file not shown.
Binary file added .gradle/8.5/fileChanges/last-build.bin
Binary file not shown.
Binary file added .gradle/8.5/fileHashes/fileHashes.bin
Binary file not shown.
Binary file added .gradle/8.5/fileHashes/fileHashes.lock
Binary file not shown.
Binary file added .gradle/8.5/fileHashes/resourceHashesCache.bin
Binary file not shown.
Empty file added .gradle/8.5/gc.properties
Empty file.
Binary file added .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
2 changes: 2 additions & 0 deletions .gradle/buildOutputCleanup/cache.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Fri Dec 01 16:04:51 IST 2023
gradle.version=8.5
Binary file added .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
Binary file added .gradle/file-system.probe
Binary file not shown.
Empty file added .gradle/vcs-1/gc.properties
Empty file.
1 change: 1 addition & 0 deletions .prodenv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
1 change: 1 addition & 0 deletions .stageenv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM gradle:7.6.0-jdk17 as build

COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
COPY . /home/gradle/src

#RUN gradle build -x test
#RUN gradle build --debug
RUN gradle build


# TODO: check slim version of eclipse-temurin:17 and update it here
FROM eclipse-temurin:17
RUN mkdir /usr/src/app
COPY --from=build /home/gradle/src/build/libs/*SNAPSHOT.war /usr/src/app/app.war
USER root

EXPOSE 9000
RUN apt-get update
RUN apt-get -y upgrade
RUN apt-get install curl

CMD ["java", "-jar", "/usr/src/app/app.war"]
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
//Swagger
implementation "org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2"
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions build/resolvedMainClassName
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.sunbird.serve.need.NeedMicroServiceApplication
21 changes: 21 additions & 0 deletions build/resources/main/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
server.port=9090

# Evean db end point
spring.datasource.url=jdbc:postgresql://postgres-db.cmwxiaefrrfi.ap-south-1.rds.amazonaws.com:5432/postgres
# Ekal db end point
#spring.datasource.url=jdbc:postgresql://postgres-db.civ7kwpwucar.ap-south-1.rds.amazonaws.com:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=23798ak3fr3409k58t4

spring.jpa.show-sql=true
spring.jpa.database=POSTGRESQL
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

server.error.include-exception=true
server.error.include-stacktrace=always
logging.level.com.sunbird.serve.need = TRACE
server.servlet.context-path=/api/v1/
logging.pattern.console=%d [%level] %c{1.} [%t] %m%n
logging.file = appLog.log
logging.pattern.file=%d [%level] %c{1.} [%t] %m%n
logging.level.reactor.netty.http.client.HttpClient: DEBUG
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// NeedDeliverableController.java
package com.sunbird.serve.need;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.sunbird.serve.need.models.Need.NeedDeliverable;
import com.sunbird.serve.need.models.enums.NeedDeliverableStatus;
import com.sunbird.serve.need.models.request.NeedDeliverableRequest;
import org.springframework.http.HttpStatus;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Parameter;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;
import java.util.UUID;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
public class NeedDeliverableController {

private static final Logger logger = LoggerFactory.getLogger(NeedDeliverableController.class);

private final NeedDeliverableService needDeliverableService;

public NeedDeliverableController(NeedDeliverableService needDeliverableService) {
this.needDeliverableService = needDeliverableService;
}

//Fetch all needplan based on needId
@Operation(summary = "Fetch Need Deliverable by providing NeedPlanId", description = "Fetch Need Deliverable by providing Need Plan Id")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully Fetched Need Deliverable", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE)),
@ApiResponse(responseCode = "400", description = "Bad Input"),
@ApiResponse(responseCode = "500", description = "Server Error")}
)
@GetMapping("/need-deliverable/{needPlanId}")
public ResponseEntity<List<NeedDeliverable>> getByNeedId(@PathVariable String needPlanId) {
List<NeedDeliverable> needDeliverable = needDeliverableService.getByNeedPlanId(needPlanId);
return ResponseEntity.ok(needDeliverable);
}

//Create Need Deliverable with Need Deliverable Request and Request Header
@Operation(summary = "Create Need Deliverable", description = "Initiate the process of creating a Need Deliverable")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully Created Need Deliverable", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE)),
@ApiResponse(responseCode = "400", description = "Bad Input"),
@ApiResponse(responseCode = "500", description = "Server Error")}
)
@PostMapping("/need-deliverable/create")
public ResponseEntity<NeedDeliverable> createNeedDeliverable(@RequestBody NeedDeliverableRequest request, @RequestHeader Map<String, String> headers) {
NeedDeliverable response = needDeliverableService.createNeedDeliverable(request, headers);
return ResponseEntity.status(HttpStatus.CREATED).body(response);
}

//Update Need Deliverable
@Operation(summary = "Update a Need Deliverable with appropritate values", description = "Update an exsisting need deliverable")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully Updated Need Deliverable", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE)),
@ApiResponse(responseCode = "400", description = "Bad Input"),
@ApiResponse(responseCode = "500", description = "Server Error")}
)
@PutMapping("/need-deliverable/update/{needDeliverableId}")
public ResponseEntity<NeedDeliverable> updateNeedDeliverable(
@PathVariable UUID needDeliverableId,
@RequestBody NeedDeliverableRequest request,
@RequestHeader Map<String, String> headers) {

NeedDeliverable updatedNeedDeliverable = needDeliverableService.updateNeedDeliverable(needDeliverableId, request, headers);
return ResponseEntity.ok(updatedNeedDeliverable);
}

}
Loading

0 comments on commit 4cdff0d

Please sign in to comment.