Skip to content

Commit

Permalink
Fat JAR and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
assafkamil committed Oct 13, 2019
1 parent 4bf0d3d commit c9056a1
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 60 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ jdk: openjdk8
services:
- docker

before_deploy:
- ./gradlew shadowJar

deploy:
provider: releases
api_key:
Expand Down
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ buildscript {
maven {
url "https://repo.eclipse.org/content/groups/releases/"
}
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:1.0.3.RELEASE"
classpath "com.github.jengelman.gradle.plugins:shadow:2.0.1"
}
}

apply plugin: "io.spring.dependency-management"
apply plugin: 'com.github.johnrengelman.shadow'

dependencyManagement {
imports {
Expand Down
21 changes: 20 additions & 1 deletion examples/flyway-s3.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
AWSTemplateFormatVersion: "2010-09-09"
Description: flyway lambda with migration scripts in S3
Parameters:
VPC:
Type: AWS::EC2::VPC::Id
Subnets:
Type: List<AWS::EC2::Subnet::Id>
S3Bucket:
Expand Down Expand Up @@ -40,7 +42,24 @@ Resources:
- "s3:GetObjectAcl"
- "s3:ListBucket"
Resource:
- !Sub "arn:aws:s3:::${S3Bucket}"
- !Sub "arn:aws:s3:::${S3Bucket}/*"

FlywayLambdaSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Flyway lambda security group to access VPC
VpcId: !Ref VPC

DBFlywaySgRule:
Type: AWS::EC2::SecurityGroupIngress
Properties:
IpProtocol: tcp
FromPort: 5432
ToPort: 5432
SourceSecurityGroupId: !GetAtt FlywayLambdaSecurityGroup.GroupId
GroupId: !Ref RDSSecurityGroup

FlywayLambda:
Type: AWS::Lambda::Function
Properties:
Expand All @@ -59,5 +78,5 @@ Resources:
Timeout: 120
VpcConfig:
SecurityGroupIds:
- !Ref RDSSecurityGroup
- !Ref FlywayLambdaSecurityGroup
SubnetIds: !Ref Subnets
33 changes: 30 additions & 3 deletions examples/flyway-secret-s3.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
AWSTemplateFormatVersion: "2010-09-09"
Description: flyway lambda with migration scripts in S3
Parameters:
VPC:
Type: AWS::EC2::VPC::Id
Subnets:
Type: List<AWS::EC2::Subnet::Id>
S3Bucket:
S3BucketName:
Type: String
RDSSecurityGroup:
Type: AWS::EC2::SecurityGroup::Id
Secret:
Type: String
SecretArn:
Type: String
ConnectionString:
Type: String
Resources:
S3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref S3BucketName
DeletionPolicy: Delete

FlywayLambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
Expand All @@ -36,14 +46,31 @@ Resources:
Action:
- "secretsmanager:DescribeSecret"
- "secretsmanager:GetSecretValue"
Resource: !Sub 'arn:${AWS::Partition}:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:${Secret}'
Resource: !Ref SecretArn
- Effect: "Allow"
Action:
- "s3:GetObject"
- "s3:GetObjectAcl"
- "s3:ListBucket"
Resource:
- !Sub "arn:aws:s3:::${S3Bucket}"
- !Sub "arn:aws:s3:::${S3Bucket}/*"

FlywayLambdaSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Flyway lambda security group to access VPC
VpcId: !Ref VPC

DBFlywaySgRule:
Type: AWS::EC2::SecurityGroupIngress
Properties:
IpProtocol: tcp
FromPort: 5432
ToPort: 5432
SourceSecurityGroupId: !GetAtt FlywayLambdaSecurityGroup.GroupId
GroupId: !Ref RDSSecurityGroup

FlywayLambda:
Type: AWS::Lambda::Function
Properties:
Expand All @@ -61,5 +88,5 @@ Resources:
Timeout: 120
VpcConfig:
SecurityGroupIds:
- !Ref RDSSecurityGroup
- !Ref FlywayLambdaSecurityGroup
SubnetIds: !Ref Subnets
7 changes: 4 additions & 3 deletions examples/rds-secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Parameters:
Default: postgres
InstanceType:
Type: String
Default: db.m4.large
Default: db.t3.small
Resources:
DBSecret:
Type: AWS::SecretsManager::Secret
Expand All @@ -28,14 +28,15 @@ Resources:
AllowMajorVersionUpgrade: true
AutoMinorVersionUpgrade: true
DBInstanceClass: !Ref InstanceType
AllocatedStorage: 10
DBName: !Ref DBName
DBSubnetGroupName:
Ref: DBSubnetGroup
DBSubnetGroupName: !Ref DBSubnetGroup
Engine: postgres
EngineVersion: '11.5'
MasterUsername: !Join ['', ['{{resolve:secretsmanager:', !Ref DBSecret, ':SecretString:username}}' ]]
MasterUserPassword: !Join ['', ['{{resolve:secretsmanager:', !Ref DBSecret, ':SecretString:password}}' ]]
StorageType: gp2
MultiAZ: false
VPCSecurityGroups:
- Ref: DBSecurityGroup
Type: AWS::RDS::DBInstance
Expand Down
7 changes: 4 additions & 3 deletions examples/rds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@ Parameters:
Default: postgres
InstanceType:
Type: String
Default: db.m4.large
Default: db.t3.small
Resources:
DB:
Properties:
AllowMajorVersionUpgrade: true
AutoMinorVersionUpgrade: true
DBInstanceClass: !Ref InstanceType
AllocatedStorage: 10
DBName: !Ref DBName
DBSubnetGroupName:
Ref: DBSubnetGroup
DBSubnetGroupName: !Ref DBSubnetGroup
Engine: postgres
EngineVersion: '11.5'
MasterUserPassword: !Ref Password
MasterUsername: !Ref Username
StorageType: gp2
MultiAZ: false
VPCSecurityGroups:
- Ref: DBSecurityGroup
Type: AWS::RDS::DBInstance
Expand Down
20 changes: 20 additions & 0 deletions examples/vpc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ Resources:
Ref: VPC
Type: AWS::EC2::Subnet

PrivateSubnet2:
Properties:
AvailabilityZone:
Fn::Select:
- 1
- Fn::GetAZs:
Ref: AWS::Region
CidrBlock: !Join [ ".", [ !Select [ 0, !Split [".", !Ref CIDR] ], !Select [ 1, !Split [".", !Ref CIDR] ], '2.0/24' ] ]
VpcId:
Ref: VPC
Type: AWS::EC2::Subnet

PrivateRouteTable:
Properties:
VpcId:
Expand All @@ -72,6 +84,14 @@ Resources:
Ref: PrivateSubnet
Type: AWS::EC2::SubnetRouteTableAssociation

PrivateSubnetRouteTableAssociation2:
Properties:
RouteTableId:
Ref: PrivateRouteTable
SubnetId:
Ref: PrivateSubnet2
Type: AWS::EC2::SubnetRouteTableAssociation

PublicSubnet:
Properties:
AvailabilityZone:
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/com/geekoosh/flyway/FlywayHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.geekoosh.flyway.request.*;
import com.geekoosh.flyway.response.Response;
import com.geekoosh.lambda.MigrationFilesException;
import com.geekoosh.lambda.MigrationFilesService;
import com.geekoosh.lambda.git.GitService;
import com.geekoosh.lambda.s3.S3Service;
import org.apache.commons.io.FileUtils;

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

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -53,7 +49,9 @@ public Response handleRequest(Request input, Context context) {

FlywayService flywayService = new FlywayService(flywayRequest, dbRequest, migrationFilesService);

return new Response(flywayService.call());
Response response = new Response(flywayService.call());
response.log();
return response;
} catch(Exception e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e);
Expand Down
21 changes: 0 additions & 21 deletions src/main/java/com/geekoosh/flyway/Response.java

This file was deleted.

1 change: 0 additions & 1 deletion src/main/java/com/geekoosh/flyway/request/DBRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public static DBRequest build(DBRequest base) {
JSONObject json = ValueManager.latestSecretJson(dbSecret);
base.setUsername(json.getString("username"));
base.setPassword(json.getString("password"));
logger.info(String.format("Using secret git variables: %s, %s", base.getUsername(), base.getPassword()));
} else {
base.setUsername(ValueManager.value(
base.getUsername(), EnvironmentVars.DB_USERNAME
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/geekoosh/flyway/request/GitRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ public Boolean getReuseRepo() {
return reuseRepo;
}

public GitRequest setReuseRepo(boolean reuseRepo) {
this.reuseRepo = reuseRepo;
return this;
}

public String getCommit() {
return commit;
}
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/com/geekoosh/flyway/response/Response.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.geekoosh.flyway.response;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.flywaydb.core.api.MigrationInfoService;

import java.util.Arrays;
import java.util.stream.Collectors;

public class Response {
private static final Logger logger = LogManager.getLogger(Response.class);
private ResponseInfo info;

public Response(MigrationInfoService info) {
this.info = new ResponseInfo(info);
}

public Response() {}

public ResponseInfo getInfo() {
return info;
}

public void setInfo(ResponseInfo info) {
this.info = info;
}

public void log() {
if(info == null) {
logger.warn("No info available for this action");
return;
}
String logInfo = "";
if(info.getCurrent() != null) {
logInfo = String.format("Current version: %s\n",
info.getCurrent().getVersion().getVersion()
);
}
if(info.getApplied() != null) {
logInfo += String.format("Applied migrations: %s, %s\n",
info.getApplied().length,
Arrays.stream(info.getApplied()).map(my -> "(" + my.getVersion().getVersion() + " [" + my.getScript() + "])")
.collect(Collectors.joining(", "))
);
}
if(info.getPending() != null) {
logInfo += String.format("Pending migrations: %s, %s\n",
info.getPending().length,
Arrays.stream(info.getPending()).map(my -> "(" + my.getVersion().getVersion() + " [" + my.getScript() + "])")
.collect(Collectors.joining(", "))
);
}
logger.info(logInfo);
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/geekoosh/flyway/response/ResponseInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.geekoosh.flyway.response;

import org.flywaydb.core.api.MigrationInfo;
import org.flywaydb.core.api.MigrationInfoService;

public class ResponseInfo {
private MigrationInfo current;
private MigrationInfo[] applied;
private MigrationInfo[] pending;

public ResponseInfo() {
}

public ResponseInfo(MigrationInfoService info) {
this.current = info.current();
this.applied = info.applied();
this.pending = info.pending();
}

public MigrationInfo getCurrent() {
return current;
}

public void setCurrent(MigrationInfo current) {
this.current = current;
}

public MigrationInfo[] getApplied() {
return applied;
}

public void setApplied(MigrationInfo[] applied) {
this.applied = applied;
}

public MigrationInfo[] getPending() {
return pending;
}

public void setPending(MigrationInfo[] pending) {
this.pending = pending;
}
}
Loading

0 comments on commit c9056a1

Please sign in to comment.