Skip to content

Commit

Permalink
feat(do): added DigitalOcean Database Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Hajdas authored and Piotr Hajdas committed Dec 24, 2024
1 parent feb2389 commit 9f90f5a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 89 deletions.
85 changes: 0 additions & 85 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions src/config/digitalocean/database-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
interface DatabaseConfig {
engine: string;
versions: string[];
description: string;
}

interface DigitalOceanDatabaseConfig {
databases: {
[key: string]: DatabaseConfig;
};
}

export const digitalOceanDatabaseConfig: DigitalOceanDatabaseConfig = {
databases: {
'docker.io/library/mysql': {
engine: 'MYSQL',
versions: ['8'],
description: 'MySQL database service - requires managed database service due to TCP protocol'
},
'docker.io/library/mariadb': {
engine: 'MYSQL',
versions: ['8'],
description: 'MariaDB database service - maps to MySQL managed database due to compatibility'
},
'docker.io/library/postgres': {
engine: 'PG',
versions: ['13', '14', '15'],
description: 'PostgreSQL database service - requires managed database service due to TCP protocol'
},
'docker.io/library/redis': {
engine: 'REDIS',
versions: ['6', '7'],
description: 'Redis database service - requires managed database service due to TCP protocol'
},
'docker.io/library/mongodb': {
engine: 'MONGODB',
versions: ['6.0', '7.0'],
description: 'MongoDB database service - requires managed database service due to TCP protocol'
}
}
};

export type { DatabaseConfig, DigitalOceanDatabaseConfig };
19 changes: 15 additions & 4 deletions src/parsers/digitalocean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { parseCommand } from '../utils/parseCommand';
import { digitalOceanParserServiceName } from '../utils/digitalOceanParserServiceName';
import { parseEnvironmentVariables } from '../utils/parseEnvironmentVariables';
import { constructImageString } from '../utils/constructImageString';
import { getDigitalOceanDatabaseType } from '../utils/getDigitalOceanDatabaseType';

const defaultParserConfig: DefaultParserConfig = {
templateFormat: TemplateFormat.yaml,
Expand All @@ -13,9 +14,6 @@ const defaultParserConfig: DefaultParserConfig = {
subscriptionName: 'basic-xxs'
};

/**
* Gets the registry type for DigitalOcean format based on Docker image info
*/
function getRegistryType(dockerImageInfo: DockerImageInfo): string {
switch (dockerImageInfo.registry_type) {
case 'GHCR':
Expand All @@ -29,9 +27,22 @@ function getRegistryType(dockerImageInfo: DockerImageInfo): string {
class DigitalOceanParser extends BaseParser {
parse(config: ApplicationConfig, templateFormat: TemplateFormat = defaultParserConfig.templateFormat): any {
const services: Array<any> = [];
const databases: Array<any> = [];
let isFirstService = true;

for (const [serviceName, serviceConfig] of Object.entries(config.services)) {
const databaseInfo = getDigitalOceanDatabaseType(serviceConfig.image);

if (databaseInfo) {
databases.push({
name: serviceName,
engine: databaseInfo.engine,
version: databaseInfo.version,
production: false
});
continue;
}

const ports = new Set<number>();

if (serviceConfig.ports) {
Expand All @@ -47,7 +58,6 @@ class DigitalOceanParser extends BaseParser {
});
}

// Use the image info directly since it's already a DockerImageInfo object
const dockerImageInfo = serviceConfig.image;
const imageString = constructImageString(dockerImageInfo);
const [repository, tag] = imageString.split(':');
Expand Down Expand Up @@ -83,6 +93,7 @@ class DigitalOceanParser extends BaseParser {
spec: {
name: 'deploystack',
region: defaultParserConfig.region,
...(databases.length > 0 && { databases }),
services: services.map(service => ({
name: service.name,
image: service.image,
Expand Down
24 changes: 24 additions & 0 deletions src/utils/getDigitalOceanDatabaseType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { DockerImageInfo } from '../parsers/base-parser';
import { getImageUrl } from './getImageUrl';
import { constructImageString } from './constructImageString';
import { digitalOceanDatabaseConfig } from '../config/digitalocean/database-types';

export interface DatabaseInfo {
engine: string;
version: string;
}

export function getDigitalOceanDatabaseType(image: DockerImageInfo): DatabaseInfo | null {
const normalizedImage = getImageUrl(constructImageString(image));

for (const [configImage, dbConfig] of Object.entries(digitalOceanDatabaseConfig.databases)) {
if (normalizedImage.includes(configImage)) {
return {
engine: dbConfig.engine,
version: dbConfig.versions[0]
};
}
}

return null;
}

0 comments on commit 9f90f5a

Please sign in to comment.