Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Adding digital ocean cluster connection setup #9388

Draft
wants to merge 20 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dockerfiles/client/Dockerfile-client-serve-static
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ RUN npm run build-client

RUN npx cross-env ts-node --swc scripts/push-client-to-s3.ts

RUN npx cross-env ts-node --swc scripts/update-cloudfront-function.ts --stage=$STAGE
# RUN npx cross-env ts-node --swc scripts/update-cloudfront-function.ts --stage=$STAGE

RUN rm -r packages/client/public

Expand Down
5 changes: 5 additions & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,10 @@
"trace-unhandled": "2.0.1",
"workbox-core": "^6.5.4"
},
"overrides": {
"koa": {
"cookies": "barankyle/cookies"
}
},
"license": "ISC"
}
3 changes: 2 additions & 1 deletion packages/common/src/schemas/cluster/pods.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export const serverPodInfoSchema = Type.Object(
format: 'uuid'
})
),
currentUsers: Type.Optional(Type.Number())
currentUsers: Type.Optional(Type.Number()),
imagePullSecrets: Type.Optional(Type.Array(Type.Object({ name: Type.String()})))
},
{ $id: 'ServerPodInfo', additionalProperties: false }
)
Expand Down
7 changes: 7 additions & 0 deletions packages/server-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,12 @@
"@types/nodemailer-smtp-transport": "2.7.5",
"@types/pug": "2.0.6"
},
"overrides": {
"@feathersjs/authentication-oauth": {
"cookie-session": {
"cookies": "barankyle/cookies"
}
}
},
"gitHead": "2313453697ca7c6b8d36b3b166b5a6445fe1c851"
}
3 changes: 2 additions & 1 deletion packages/server-core/src/cluster/pods/pods-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ const getServerPodInfo = (item: k8s.V1Pod) => {
name: item.metadata?.name,
status: item.status?.phase,
age: item.status?.startTime?.toString(),
containers: getServerContainerInfo(item.status?.containerStatuses!)
containers: getServerContainerInfo(item.status?.containerStatuses!),
imagePullSecrets: item.spec?.imagePullSecrets
} as ServerPodInfoType
}

Expand Down
73 changes: 73 additions & 0 deletions packages/server-core/src/media/storageprovider/s3-do.storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
CPAL-1.0 License

The contents of this file are subject to the Common Public Attribution License
Version 1.0. (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE.
The License is based on the Mozilla Public License Version 1.1, but Sections 14
and 15 have been added to cover use of software over a computer network and
provide for limited attribution for the Original Developer. In addition,
Exhibit A has been modified to be consistent with Exhibit B.

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
specific language governing rights and limitations under the License.

The Original Code is Ethereal Engine.

The Original Developer is the Initial Developer. The Initial Developer of the
Original Code is the Ethereal Engine team.

All portions of the code written by the Ethereal Engine team are Copyright © 2021-2023
Ethereal Engine. All Rights Reserved.
*/

import { FunctionSummary } from '@aws-sdk/client-cloudfront'

import config from '../../appconfig'
import S3Provider from './s3.storage'

/**
* Storage provide class to communicate with AWS S3 API.
*/
export class S3DOProvider extends S3Provider {
constructor() {
super()
this.bucketAssetURL = `https://${config.aws.cloudfront.domain}`
this.cacheDomain = config.aws.cloudfront.domain
}
/**
* Invalidate items in the S3 storage.
* @param invalidationItems List of keys.
*/
override async createInvalidation(invalidationItems: string[]) {
return undefined
}

override async getOriginURLs(): Promise<string[]> {
return [this.cacheDomain]
}

override async listFunctions(marker: string | null, functions: FunctionSummary[]): Promise<FunctionSummary[]> {
return []
}

async createFunction(functionName: string, routes: string[]) {
return undefined
}

async associateWithFunction(functionARN: string, attempts = 1) {
return undefined
}

async publishFunction(functionName: string) {
return undefined
}

async updateFunction(functionName: string, routes: string[]) {
return undefined
}
}

export default S3DOProvider
28 changes: 19 additions & 9 deletions packages/server-core/src/media/storageprovider/s3.storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Ethereal Engine. All Rights Reserved.
import {
CloudFrontClient,
CreateFunctionCommand,
CreateFunctionCommandOutput,
CreateInvalidationCommand,
DescribeFunctionCommand,
FunctionRuntime,
Expand All @@ -34,8 +35,10 @@ import {
ListFunctionsCommand,
ListFunctionsCommandInput,
PublishFunctionCommand,
PublishFunctionCommandOutput,
UpdateDistributionCommand,
UpdateFunctionCommand
UpdateFunctionCommand,
UpdateFunctionCommandOutput
} from '@aws-sdk/client-cloudfront'
import {
AbortMultipartUploadCommand,
Expand Down Expand Up @@ -163,7 +166,7 @@ export class S3Provider implements StorageProviderInterface {

originURLs = [this.cacheDomain]

private bucketAssetURL =
public bucketAssetURL =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the 's3-do' specific check in this file should be moved to S3DOStorageProvider. The class variable should be updated based on DO in constructor of it.

config.server.storageProvider === 's3'
? config.aws.s3.endpoint
? `${config.aws.s3.endpoint}/${this.bucket}`
Expand Down Expand Up @@ -300,7 +303,6 @@ export class S3Provider implements StorageProviderInterface {
if (!data.Key) return
// key should not contain '/' at the begining
const key = data.Key[0] === '/' ? data.Key.substring(1) : data.Key

const args = params.isDirectory
? {
ACL: ObjectCannedACL.public_read,
Expand Down Expand Up @@ -420,7 +422,11 @@ export class S3Provider implements StorageProviderInterface {
async createInvalidation(invalidationItems: string[]) {
if (!invalidationItems || invalidationItems.length === 0) return
// for non-standard s3 setups, we don't use cloudfront
if (config.server.storageProvider !== 's3' || config.aws.s3.s3DevMode === 'local') return
if (
(config.server.storageProvider !== 's3' && config.server.storageProvider !== 's3-do') ||
config.aws.s3.s3DevMode === 'local'
)
return
const params = {
DistributionId: config.aws.cloudfront.distributionId,
InvalidationBatch: {
Expand All @@ -438,7 +444,11 @@ export class S3Provider implements StorageProviderInterface {
}

async getOriginURLs(): Promise<string[]> {
if (config.server.storageProvider !== 's3' || config.aws.s3.s3DevMode === 'local') return [this.cacheDomain]
if (
(config.server.storageProvider !== 's3-do' && config.server.storageProvider !== 's3') ||
config.aws.s3.s3DevMode === 'local'
)
return [this.cacheDomain]
const getDistributionParams = {
Id: config.aws.cloudfront.distributionId
}
Expand All @@ -449,7 +459,7 @@ export class S3Provider implements StorageProviderInterface {
}

async listFunctions(marker: string | null, functions: FunctionSummary[]): Promise<FunctionSummary[]> {
if (config.server.storageProvider !== 's3') return []
if (config.server.storageProvider !== 's3-do' && config.server.storageProvider !== 's3-do') return []
const params: ListFunctionsCommandInput = {
MaxItems: MAX_ITEMS
}
Expand Down Expand Up @@ -504,7 +514,7 @@ export class S3Provider implements StorageProviderInterface {
)
}

async createFunction(functionName: string, routes: string[]) {
async createFunction(functionName: string, routes: string[]): Promise<CreateFunctionCommandOutput | undefined> {
const code = this.getFunctionCode(routes)
const params = {
Name: functionName,
Expand Down Expand Up @@ -553,7 +563,7 @@ export class S3Provider implements StorageProviderInterface {
}
}

async publishFunction(functionName: string) {
async publishFunction(functionName: string): Promise<PublishFunctionCommandOutput | undefined> {
const functionDetailsParams = {
Name: functionName
}
Expand All @@ -567,7 +577,7 @@ export class S3Provider implements StorageProviderInterface {
return await this.cloudfront.send(command)
}

async updateFunction(functionName: string, routes: string[]) {
async updateFunction(functionName: string, routes: string[]): Promise<UpdateFunctionCommandOutput | undefined> {
const code = this.getFunctionCode(routes)
const functionDetailsParams = {
Name: functionName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Ethereal Engine. All Rights Reserved.
import config from '../../appconfig'
import IPFSStorage from './ipfs.storage'
import LocalStorage from './local.storage'
import S3DOStorage from './s3-do.storage'
import S3Storage from './s3.storage'
import { StorageProviderInterface } from './storageprovider.interface'

Expand Down Expand Up @@ -58,9 +59,18 @@ export const createIPFSStorageProvider = async () => {
}

export const createDefaultStorageProvider = () => {
const StorageProvider =
config.server.storageProvider !== 's3' && config.server.storageProvider !== 'ipfs' ? LocalStorage : S3Storage
let StorageProvider

if (config.server.storageProvider === 's3-do') {
StorageProvider = S3DOStorage
} else if (config.server.storageProvider !== 's3' && config.server.storageProvider !== 'ipfs') {
StorageProvider = LocalStorage
} else {
StorageProvider = S3Storage
}

const provider = createStorageProvider(StorageProvider)
providers['default'] = provider
console.log('provider', provider)
return provider
}
26 changes: 19 additions & 7 deletions packages/server-core/src/projects/project/project-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,9 @@ export async function getProjectUpdateJobBody(
app
)

const image = apiPods.pods[0].containers.find((container) => container.name === 'etherealengine')!.image
const pod = apiPods.pods[0]
const image = pod.containers.find((container) => container.name === 'etherealengine')!.image
const imagePullSecrets = pod.imagePullSecrets

const command = [
'npx',
Expand Down Expand Up @@ -994,6 +996,7 @@ export async function getProjectUpdateJobBody(
}
},
spec: {
imagePullSecrets,
serviceAccountName: `${process.env.RELEASE_NAME}-etherealengine-api`,
containers: [
{
Expand Down Expand Up @@ -1028,7 +1031,9 @@ export async function getProjectPushJobBody(
app
)

const image = apiPods.pods[0].containers.find((container) => container.name === 'etherealengine')!.image
const pod = apiPods.pods[0]
const image = pod.containers.find((container) => container.name === 'etherealengine')!.image
const imagePullSecrets = pod.imagePullSecrets

const command = [
'npx',
Expand Down Expand Up @@ -1074,6 +1079,7 @@ export async function getProjectPushJobBody(
}
},
spec: {
imagePullSecrets,
serviceAccountName: `${process.env.RELEASE_NAME}-etherealengine-api`,
containers: [
{
Expand All @@ -1093,7 +1099,7 @@ export async function getProjectPushJobBody(
}
}

export const getCronJobBody = (project: ProjectType, image: string): object => {
export const getCronJobBody = (project: ProjectType, image: string, imagePullSecrets?): object => {
return {
metadata: {
name: `${process.env.RELEASE_NAME}-${project.name}-auto-update`,
Expand Down Expand Up @@ -1123,6 +1129,7 @@ export const getCronJobBody = (project: ProjectType, image: string): object => {
}
},
spec: {
imagePullSecrets: imagePullSecrets || [],
serviceAccountName: `${process.env.RELEASE_NAME}-etherealengine-api`,
containers: [
{
Expand Down Expand Up @@ -1166,7 +1173,9 @@ export async function getDirectoryArchiveJobBody(
app
)

const image = apiPods.pods[0].containers.find((container) => container.name === 'etherealengine')!.image
const pod = apiPods.pods[0]
const image = pod.containers.find((container) => container.name === 'etherealengine')!.image
const imagePullSecrets = pod.imagePullSecrets

const command = [
'npx',
Expand Down Expand Up @@ -1202,6 +1211,7 @@ export async function getDirectoryArchiveJobBody(
}
},
spec: {
imagePullSecrets,
serviceAccountName: `${process.env.RELEASE_NAME}-etherealengine-api`,
containers: [
{
Expand Down Expand Up @@ -1239,7 +1249,9 @@ export const createOrUpdateProjectUpdateJob = async (app: Application, projectNa
app
)

const image = apiPods.pods[0].containers.find((container) => container.name === 'etherealengine')!.image
const pod = apiPods.pods[0]
const image = pod.containers.find((container) => container.name === 'etherealengine')!.image
const imagePullSecrets = pod.imagePullSecrets

const k8BatchClient = getState(ServerState).k8BatchClient

Expand All @@ -1248,7 +1260,7 @@ export const createOrUpdateProjectUpdateJob = async (app: Application, projectNa
await k8BatchClient.patchNamespacedCronJob(
`${process.env.RELEASE_NAME}-${projectName}-auto-update`,
'default',
getCronJobBody(project, image),
getCronJobBody(project, image, imagePullSecrets),
undefined,
undefined,
undefined,
Expand All @@ -1262,7 +1274,7 @@ export const createOrUpdateProjectUpdateJob = async (app: Application, projectNa
)
} catch (err) {
logger.error('Could not find cronjob %o', err)
await k8BatchClient.createNamespacedCronJob('default', getCronJobBody(project, image))
await k8BatchClient.createNamespacedCronJob('default', getCronJobBody(project, image, imagePullSecrets))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const projectResolver = resolve<ProjectType, HookContext>(
{
// Convert the raw data into a new structure before running property resolvers
converter: async (rawData, context) => {
return projectDbToSchema(rawData)
return rawData ? projectDbToSchema(rawData) : undefined
}
}
)
Expand Down
Loading
Loading