Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mats16 committed Dec 27, 2022
1 parent b682e0f commit de9e476
Show file tree
Hide file tree
Showing 5 changed files with 1,074 additions and 235 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ This repo includes a template of starting Supabase stack on AWS via CloudFormati
| 2xlarge | 8192 | 16384 |
| 4xlarge | 16384 | 32768 |

#### IAM Policy
#### IAM Policy to create CloudFormation Stack

```json
{
Expand Down
29 changes: 21 additions & 8 deletions src/aws-amplify-hosting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class AmplifyHosting extends Construct {

const repository = new Repository(this, 'Repo', { repositoryName, description: `${this.node.path}/Repo` });

const repoImportJob = repository.importFromUrl('main', sourceRepo, sourceBranch);
const repoImportJob = repository.importFromUrl(sourceRepo, sourceBranch);

const amplifySSRLoggingRole = new iam.Role(this, 'AmplifySSRLoggingRole', {
description: 'The service role that will be used by AWS Amplify for SSR app logging.',
Expand Down Expand Up @@ -62,6 +62,10 @@ export class AmplifyHosting extends Construct {
},
postBuild: {
commands: [
'shopt -s dotglob',
`if [ -d .next/standalone/node_modules ]; then mv -n .next/standalone/node_modules/* .next/standalone/${appRoot}/node_modules/.; fi`,
'rm -rf .next/standalone/node_modules',
`mv -f .next/standalone/${appRoot}/* .next/standalone/.`,
'cp .env .env.production .next/standalone/',
],
},
Expand All @@ -88,6 +92,9 @@ export class AmplifyHosting extends Construct {
});
(this.app.node.defaultChild as cdk.CfnResource).addPropertyOverride('Platform', 'WEB_COMPUTE');

const outputFileTracingRoot = appRoot.split('/').map(x => x = '..').join('/') + '/';
this.app.addEnvironment('NEXT_PRIVATE_OUTPUT_TRACE_ROOT', outputFileTracingRoot);

this.app.addEnvironment('AMPLIFY_MONOREPO_APP_ROOT', appRoot);
this.app.addEnvironment('AMPLIFY_DIFF_DEPLOY', 'false');
this.app.addEnvironment('_LIVE_UPDATES', JSON.stringify(liveUpdates));
Expand Down Expand Up @@ -136,13 +143,14 @@ export class AmplifyHosting extends Construct {
}

export class Repository extends codecommit.Repository {
readonly importFunction: lambda.Function;
readonly importProvider: cr.Provider;

constructor(scope: Construct, id: string, props: codecommit.RepositoryProps) {
super(scope, id, props);

const importFunction = new lambda.Function(this, 'ImportFunction', {
description: 'Copy Git Repository',
this.importFunction = new lambda.Function(this, 'ImportFunction', {
description: 'Clone to CodeCommit from remote repo (You can execute this function manually.)',
runtime: lambda.Runtime.PYTHON_3_9,
code: lambda.Code.fromAsset('./src/functions/copy-git-repo', {
bundling: {
Expand All @@ -167,21 +175,26 @@ export class Repository extends codecommit.Repository {
memorySize: 2048,
ephemeralStorageSize: cdk.Size.gibibytes(2),
timeout: cdk.Duration.minutes(3),
environment: {
TARGET_REPO: this.repositoryCloneUrlGrc,
},
});
this.grantPullPush(importFunction);
this.grantPullPush(this.importFunction);

this.importProvider = new cr.Provider(this, 'ImportProvider', { onEventHandler: importFunction });
this.importProvider = new cr.Provider(this, 'ImportProvider', { onEventHandler: this.importFunction });
}

importFromUrl(targetBranch: string, sourceRepoUrlHttp: string, sourceBranch: string) {
importFromUrl(sourceRepoUrlHttp: string, sourceBranch: string, targetBranch: string = 'main') {
this.importFunction.addEnvironment('SOURCE_REPO', sourceRepoUrlHttp);
this.importFunction.addEnvironment('SOURCE_BRANCH', sourceBranch);
this.importFunction.addEnvironment('TARGET_BRANCH', targetBranch);

return new cdk.CustomResource(this, targetBranch, {
resourceType: 'Custom::RepoImportJob',
serviceToken: this.importProvider.serviceToken,
properties: {
SourceRepo: sourceRepoUrlHttp,
SourceBranch: sourceBranch,
TargetRepo: this.repositoryCloneUrlGrc,
TargetBranch: targetBranch,
},
});
}
Expand Down
18 changes: 11 additions & 7 deletions src/functions/copy-git-repo/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

CLONE_DIR = '/tmp/src-repo'

TARGET_REPO = os.environ['TARGET_REPO']
TARGET_BRANCH = os.environ['TARGET_BRANCH']
SOURCE_REPO = os.environ['SOURCE_REPO']
SOURCE_BRANCH = os.environ['SOURCE_BRANCH']

PATH = os.environ['PATH']
LD_LIBRARY_PATH = os.environ['LD_LIBRARY_PATH']
AWS_REGION = os.environ['AWS_REGION']
Expand Down Expand Up @@ -43,16 +48,15 @@ def exec(command: List[str], cwd: str = '/tmp') -> str:
return stdout

def handler(event: dict, context: Any) -> dict:
request_type: str = event['RequestType']
source_repo: str = event['ResourceProperties']['SourceRepo']
source_branch: str = event['ResourceProperties'].get('SourceBranch', 'main')
target_repo: str = event['ResourceProperties']['TargetRepo']
target_branch: str = event['ResourceProperties'].get('TargetBranch', 'main')
props: dict = event.get('ResourceProperties', {})
source_repo: str = props.get('SourceRepo', SOURCE_REPO)
source_branch: str = props.get('SourceBranch', SOURCE_BRANCH)
request_type: str = event.get('RequestType', 'ManualExecution')
if request_type != 'Delete':
exec(['rm', '-rf', CLONE_DIR])
exec(['git', 'clone', '--depth', '1', '-b', source_branch, source_repo, CLONE_DIR])
exec(['git', 'fetch', '--unshallow'], CLONE_DIR)
exec(['git', 'checkout', '-b', 'local_tmp'], CLONE_DIR)
exec(['git', 'remote', 'add', 'dest', target_repo], CLONE_DIR)
exec(['git', 'push', '--force', 'dest', f'local_tmp:{target_branch}'], CLONE_DIR)
exec(['git', 'remote', 'add', 'dest', TARGET_REPO], CLONE_DIR)
exec(['git', 'push', '--force', 'dest', f'local_tmp:{TARGET_BRANCH}'], CLONE_DIR)
return {}
6 changes: 5 additions & 1 deletion src/supabase-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ export class SupabaseStack extends FargateStack {
const studioBranch = new cdk.CfnParameter(this, 'StudioBranch', {
type: 'String',
default: '0.22.08',
description: 'https://github.com/supabase/supabase/tags',
description: 'Branch or tag - https://github.com/supabase/supabase/tags',
});

new AmplifyHosting(this, 'Studio', {
Expand All @@ -458,7 +458,10 @@ export class SupabaseStack extends FargateStack {
environmentVariables: {
STUDIO_PG_META_URL: `${apiExternalUrl}/pg`,
POSTGRES_PASSWORD: db.secret.secretValueFromJson('password').toString(),
//DEFAULT_ORGANIZATION: 'Default Organization',
//DEFAULT_PROJECT: 'Default Project',
SUPABASE_URL: `${apiExternalUrl}`,
//SUPABASE_PUBLIC_URL: `${apiExternalUrl}`,
SUPABASE_REST_URL: `${apiExternalUrl}/rest/v1/`,
SUPABASE_ANON_KEY: anonKey.value,
SUPABASE_SERVICE_KEY: serviceRoleKey.value,
Expand Down Expand Up @@ -508,6 +511,7 @@ export class SupabaseStack extends FargateStack {
restImageUri.logicalId,
realtimeImageUri.logicalId,
storageImageUri.logicalId,
imgproxyImageUri.logicalId,
postgresMetaImageUri.logicalId,
studioBranch.logicalId,
],
Expand Down
Loading

0 comments on commit de9e476

Please sign in to comment.