Skip to content

Commit

Permalink
feat : template 배포 controller 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
nookcoder committed Nov 17, 2023
1 parent 4d802b8 commit 47c1928
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 60 deletions.
19 changes: 0 additions & 19 deletions .github/workflows/Prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,6 @@ jobs:
run: |
docker push ${{ secrets.ECR_REGISTRY }}/prod:latest
deploy:
needs: build-and-push
runs-on: ubuntu-latest

steps:
- name: Deploy on Staging Server
uses: appleboy/ssh-action@v0.1.10
with:
host: ${{ secrets.AWS_STAGING }}
username: ${{ secrets.AWS_SSH_USERNAME }}
key: ${{ secrets.AWS_SSH_KEY }}
script_stop: true
script: |
cd /app
password=$(aws ecr get-login-password --region ${{ env.REGION }})
docker login --username AWS --password $password ${{ secrets.ECR_REGISTRY }}
docker-compose pull
docker-compose up -d
# notification:
# needs: deploy
# runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ out/

### VS Code ###
.vscode/
http/*
http/*
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ public class BuildLog {
private String repository;
private String branch;
private Object logs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.io.IOException;
import java.util.List;

import static java.lang.Boolean.parseBoolean;

@Tag(name = "TemplateController", description = "TemplateController")
@RestController
@RequiredArgsConstructor
Expand All @@ -33,7 +35,6 @@ public class TemplateController {
private final AwsTemplateService templateService;
private final AwsBlueprintService awsBlueprintService;
private final Logger logger = LoggerFactory.getLogger(TemplateController.class);
private final MessageProducer sender;

@Operation(
summary = "Template 전체 조회하기",
Expand All @@ -45,6 +46,18 @@ public List<TemplatePreviewDto> getAll(PagingDto page) {
return templateService.getAll(pageable);
}

@Operation(
summary = "template 으로 blueprint 생성하기(static file)",
description = "Requires authentication.",
security = @SecurityRequirement(name = "bearerAuth")
)
@PostMapping("")
@AuthorizationFromToken
public ResultDto createTemplate(@RequestBody AwsBluePrintDto awsBluePrintDto, @RequestParam String name, @RequestParam String provision, String userId) throws IOException {
templateService.deployTemplate(awsBluePrintDto, name, parseBoolean(provision), userId);
return ResultDto.builder().result(true).build();
}

@Operation(
summary = "Kumofactory 에서 만든 Template 조회하기",
description = "example : ?page=0&size=10"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.kumofactory.cloud.blueprint.domain;

import com.kumofactory.cloud.member.domain.Member;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

Expand All @@ -14,6 +12,8 @@
@Setter
@NoArgsConstructor
@Entity
@Builder
@AllArgsConstructor
public class BaseBluePrint {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down Expand Up @@ -42,6 +42,8 @@ public class BaseBluePrint {
@Enumerated(EnumType.STRING)
private BluePrintScope scope; // 블루프린트 공개 범위

private Boolean isTemplate;

@ManyToOne
private Member member;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintListDto;
import com.kumofactory.cloud.global.rabbitmq.domain.CdkMessagePattern;

import java.io.IOException;
import java.util.List;

public interface AwsBlueprintService {
Expand All @@ -17,7 +18,7 @@ public interface AwsBlueprintService {

List<AwsBluePrintListDto> getMyAwsBlueprints(String userId);

void store(AwsBluePrintDto awsBluePrintDto, String provision, CdkMessagePattern pattern, String userId) throws JsonProcessingException;
void store(AwsBluePrintDto awsBluePrintDto, String provision, CdkMessagePattern pattern, String userId) throws IOException;

boolean delete(String uuid); // blueprint uuid 로 모든 정보 삭세

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.kumofactory.cloud.blueprint.dto.ComponentLineDto;
import com.kumofactory.cloud.blueprint.dto.aws.*;

import com.kumofactory.cloud.blueprint.repository.ComponentDotRepository;
import com.kumofactory.cloud.blueprint.repository.ComponentLineRepository;
import com.kumofactory.cloud.blueprint.repository.aws.AwsAreaRepository;
import com.kumofactory.cloud.blueprint.repository.aws.AwsBluePrintRepository;
Expand All @@ -20,19 +19,15 @@
import com.kumofactory.cloud.member.MemberRepository;
import com.kumofactory.cloud.member.domain.Member;
import com.kumofactory.cloud.util.aws.s3.AwsS3Helper;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

import static java.lang.Boolean.parseBoolean;
Expand Down Expand Up @@ -86,7 +81,7 @@ public List<AwsBluePrintListDto> getMyAwsBlueprints(String oauthId) {
}

@Override
public void store(AwsBluePrintDto awsBluePrintDto, String provision, CdkMessagePattern pattern, String userId) throws JsonProcessingException {
public void store(AwsBluePrintDto awsBluePrintDto, String provision, CdkMessagePattern pattern, String userId) throws IOException {
this.delete(awsBluePrintDto.getUuid()); // 기존 BluePrint 삭제

AwsBluePrint savedBlueprint = saveBlueprint(awsBluePrintDto, provision, userId); // BluePrint 저장
Expand Down Expand Up @@ -144,7 +139,7 @@ public ProvisionStatus getProvisionStatus(String uuid, String userId) {
}

// Blueprint 저장
private AwsBluePrint saveBlueprint(AwsBluePrintDto awsBluePrintDto, String provision, String userId) {
private AwsBluePrint saveBlueprint(AwsBluePrintDto awsBluePrintDto, String provision, String userId) throws IOException {
Member member = memberRepository.findMemberByOauthId(userId);
// Provision 여부 설정
ProvisionStatus status;
Expand All @@ -155,7 +150,7 @@ private AwsBluePrint saveBlueprint(AwsBluePrintDto awsBluePrintDto, String provi
}

// thumbnail 저장
String keyname = saveThumbnail(awsBluePrintDto, member);
String keyname = awsS3Helper.saveThumbnail(awsBluePrintDto, member);

// BluePrint 저장
AwsBluePrint awsBluePrint = new AwsBluePrint();
Expand All @@ -167,27 +162,10 @@ private AwsBluePrint saveBlueprint(AwsBluePrintDto awsBluePrintDto, String provi
awsBluePrint.setMember(member);
awsBluePrint.setScope(awsBluePrintDto.getScope() == null ? BluePrintScope.PRIVATE : awsBluePrintDto.getScope());
awsBluePrint.setKeyName(keyname);

awsBluePrint.setIsTemplate(false);
return awsBluePrintRepository.save(awsBluePrint);
}

// thumbnail 저장
private String saveThumbnail(AwsBluePrintDto bluePrint, Member member) {
String objectKey = _getObjectKey(member.getOauthId(), bluePrint.getUuid());
logger.info("thumbnail upload start: {}", objectKey);
try {
byte[] svgContent = Base64.getDecoder().decode(bluePrint.getSvgFile().split(",")[1]);
MultipartFile svgFile = new MockMultipartFile("file", objectKey, "image/svg+xml", svgContent);
awsS3Helper.putS3Object(svgFile, objectKey);
logger.info("thumbnail upload success: {}", objectKey);
logger.info("thumbnail url: {}", awsS3Helper.getPresignedUrl(objectKey));
} catch (Exception e) {
logger.error("thumbnail upload failed: {}", e.getMessage());
}

return objectKey;
}

// ComponentLine 저장
private void saveComponentLines(AwsBluePrint blueprint, List<ComponentLineDto> lines) {
List<ComponentLine> componentLines = new ArrayList<>();
Expand All @@ -207,7 +185,5 @@ private void saveAwsAreas(AwsBluePrint blueprint, List<AwsAreaDto> areas) {
awsAreaRepository.saveAll(awsArea);
}

private String _getObjectKey(String memberId, String blueprintId) {
return memberId + "/" + blueprintId + ".svg";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintDto;
import com.kumofactory.cloud.blueprint.dto.template.TemplatePreviewDto;
import com.kumofactory.cloud.global.dto.ResultDto;
import org.springframework.data.domain.Pageable;
import software.amazon.awssdk.services.s3.model.S3Exception;

import java.io.IOException;
import java.util.List;

public interface AwsTemplateService {
AwsBluePrintDto getAwsBlueprint(String uuid);

// 전체 조회하기
List<TemplatePreviewDto> getAll(Pageable pageable) throws S3Exception;
ResultDto deployTemplate(AwsBluePrintDto dto, String templateName, boolean provision, String userId) throws IOException;

// 전체 조회하기
List<TemplatePreviewDto> getAll(Pageable pageable) throws S3Exception;

// blueprint 이름으로 조회
List<TemplatePreviewDto> searchTemplateFromKumofactory(Pageable pageable) throws S3Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@

import com.kumofactory.cloud.blueprint.domain.BluePrintScope;
import com.kumofactory.cloud.blueprint.domain.ComponentLine;
import com.kumofactory.cloud.blueprint.domain.ProvisionStatus;
import com.kumofactory.cloud.blueprint.domain.aws.AwsArea;
import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint;
import com.kumofactory.cloud.blueprint.domain.aws.AwsComponent;
import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintDto;
import com.kumofactory.cloud.blueprint.dto.aws.AwsCdkDto;
import com.kumofactory.cloud.blueprint.dto.template.TemplatePreviewDto;
import com.kumofactory.cloud.blueprint.repository.ComponentLineRepository;
import com.kumofactory.cloud.blueprint.repository.aws.AwsAreaRepository;
import com.kumofactory.cloud.blueprint.repository.aws.AwsBluePrintRepository;
import com.kumofactory.cloud.blueprint.repository.aws.AwsComponentRepository;
import com.kumofactory.cloud.global.dto.ResultDto;
import com.kumofactory.cloud.global.rabbitmq.MessageProducer;
import com.kumofactory.cloud.global.rabbitmq.domain.CdkMessagePattern;
import com.kumofactory.cloud.member.MemberRepository;
import com.kumofactory.cloud.member.domain.Member;
import com.kumofactory.cloud.util.aws.s3.AwsS3Helper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -21,8 +28,11 @@
import org.springframework.stereotype.Service;
import software.amazon.awssdk.services.s3.model.S3Exception;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
Expand All @@ -32,6 +42,8 @@ public class AwsTemplateServiceImpl implements AwsTemplateService {
private final ComponentLineRepository componentLineRepository;
private final AwsAreaRepository awsAreaRepository;
private final AwsComponentRepository awsComponentRepository;
private final MemberRepository memberRepository;
private final MessageProducer sender;
private final AwsS3Helper s3;
private final Logger logger = LoggerFactory.getLogger(AwsTemplateServiceImpl.class);

Expand All @@ -49,6 +61,35 @@ public AwsBluePrintDto getAwsBlueprint(String uuid) {
return AwsBluePrintDto.build(awsBluePrintById, awsAreas, awsComponents, componentLines);
}

/**
* Thumbnail 저장 -> blueprint 에 저장 -> send message to aws cdk server
*
* @param templateName
* @param userId
* @return
*/
@Override
public ResultDto deployTemplate(AwsBluePrintDto dto, String templateName, boolean provision, String userId) throws IOException {
logger.info("dto {}", dto.toString());
logger.info("USERID {}", userId);
Member member = memberRepository.findMemberByOauthId(userId);
ProvisionStatus status = provision ? ProvisionStatus.PROVISIONING : ProvisionStatus.PENDING; // provision 설정

String keyname = s3.saveThumbnail(dto, member); // thumbnail 저장
// blueprint 저장
saveAwsBluePrint(dto, member, templateName, status, keyname);

if (provision) {
Map<String, Object> options = new HashMap<>();
options.put("templateName", templateName);
AwsCdkDto awsCdkDto = AwsCdkDto.builder().id(dto.getUuid()).options(options).build();
List<AwsCdkDto> list = new ArrayList<>();
list.add(awsCdkDto);
sender.sendAwsCdkOption(CdkMessagePattern.TEMPLATE, list); // send message to aws cdk server
}
return null;
}

@Override
public List<TemplatePreviewDto> getAll(Pageable pageable) throws S3Exception {
List<AwsBluePrint> all = templateRepository.findAllByScopeNot(BluePrintScope.PRIVATE, pageable);
Expand Down Expand Up @@ -82,4 +123,18 @@ private List<TemplatePreviewDto> mapToTemplatePreviewDto(List<AwsBluePrint> all)
throw S3Exception.builder().build();
}
}

private void saveAwsBluePrint(AwsBluePrintDto dto, Member member, String templateName, ProvisionStatus status, String keyname) throws IOException {
AwsBluePrint blueprint = new AwsBluePrint();
blueprint.setUuid(dto.getUuid());
blueprint.setName(templateName);
blueprint.setDescription(dto.getDescription());
blueprint.setDownloadCount(0);
blueprint.setStatus(status);
blueprint.setMember(member);
blueprint.setScope(dto.getScope());
blueprint.setKeyName(keyname);
blueprint.setIsTemplate(true);
templateRepository.save(blueprint);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.kumofactory.cloud.global.rabbitmq.domain;

public enum CdkMessagePattern {
USER, KUMO
USER, KUMO, TEMPLATE
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.kumofactory.cloud.util.aws.s3;

import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintDto;
import com.kumofactory.cloud.member.domain.Member;
import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.services.s3.model.S3Exception;

Expand All @@ -10,4 +12,6 @@ public interface AwsS3Helper {
void putS3Object(MultipartFile svgFile, String keyName) throws S3Exception, IOException;

String getPresignedUrl(String keyName) throws S3Exception;

String saveThumbnail(AwsBluePrintDto bluePrint, Member member) throws S3Exception, IOException;
}
Loading

0 comments on commit 47c1928

Please sign in to comment.