@@ -24,43 +24,48 @@ import { mergeObjects } from "~/utils/object";
2424export async function create ( formData : FormData ) {
2525 const data = NewServiceSchema . parse ( getData ( formData ) ) ;
2626 await db . transaction ( async tx => {
27- const service = await createService ( tx , data ) ;
28- if ( data . kind === "database" ) {
29- const envEntries : [ string , string ] [ ] = [ ] ;
30- const portEntries : [ string , string ] [ ] = [ ] ;
31- if ( data . repo === "postgres" ) {
32- envEntries . push ( [ "POSTGRES_PASSWORD" , crypto . randomUUID ( ) ] ) ;
33- // portEntries.push(["5432", "5432"]);
34- const volume = await createVolume ( tx , { containerPath : "/var/lib/postgresql/data" , serviceId : service . id } ) ;
35- await createDockerVolume ( volume . id ) ;
36- }
37- if ( data . repo === "mysql" ) {
38- envEntries . push ( [ "MYSQL_ROOT_PASSWORD" , "example" ] ) ;
39- portEntries . push ( [ "3306" , "3306" ] ) ;
40- const volume = await createVolume ( tx , { containerPath : "/var/lib/mysql" , serviceId : service . id } ) ;
41- await createDockerVolume ( volume . id ) ;
42- }
43- if ( data . repo === "mongo" ) {
44- envEntries . push ( [ "MONGO_INITDB_ROOT_USERNAME" , "root" ] ) ;
45- envEntries . push ( [ "MONGO_INITDB_ROOT_PASSWORD" , "example" ] ) ;
46- portEntries . push ( [ "27017" , "27017" ] ) ;
47- const volume = await createVolume ( tx , { containerPath : "/data/db" , serviceId : service . id } ) ;
48- await createDockerVolume ( volume . id ) ;
49- }
50- if ( data . repo === "redis" ) {
51- envEntries . push ( [ "REDIS_ARGS" , "--appendonly yes" ] ) ;
52- // portEntries.push(["6379", "6379"]);
53- const volume = await createVolume ( tx , { containerPath : "/data" , serviceId : service . id } ) ;
54- await createDockerVolume ( volume . id ) ;
55- }
56- if ( envEntries . length ) {
57- await syncEnvironmentVariables ( tx , service . id , Object . fromEntries ( envEntries ) ) ;
58- }
59- if ( portEntries . length ) {
60- await syncPorts ( tx , service . id , Object . fromEntries ( portEntries ) ) ;
27+ try {
28+ const service = await createService ( tx , data ) ;
29+ if ( data . kind === "database" ) {
30+ const envEntries : [ string , string ] [ ] = [ ] ;
31+ const portEntries : [ string , string ] [ ] = [ ] ;
32+ if ( data . repo === "postgres" ) {
33+ envEntries . push ( [ "POSTGRES_PASSWORD" , crypto . randomUUID ( ) ] ) ;
34+ // portEntries.push(["5432", "5432"]);
35+ const volume = await createVolume ( tx , { containerPath : "/var/lib/postgresql/data" , serviceId : service . id } ) ;
36+ await createDockerVolume ( volume . id ) ;
37+ }
38+ if ( data . repo === "mysql" ) {
39+ envEntries . push ( [ "MYSQL_ROOT_PASSWORD" , "example" ] ) ;
40+ portEntries . push ( [ "3306" , "3306" ] ) ;
41+ const volume = await createVolume ( tx , { containerPath : "/var/lib/mysql" , serviceId : service . id } ) ;
42+ await createDockerVolume ( volume . id ) ;
43+ }
44+ if ( data . repo === "mongo" ) {
45+ envEntries . push ( [ "MONGO_INITDB_ROOT_USERNAME" , "root" ] ) ;
46+ envEntries . push ( [ "MONGO_INITDB_ROOT_PASSWORD" , "example" ] ) ;
47+ portEntries . push ( [ "27017" , "27017" ] ) ;
48+ const volume = await createVolume ( tx , { containerPath : "/data/db" , serviceId : service . id } ) ;
49+ await createDockerVolume ( volume . id ) ;
50+ }
51+ if ( data . repo === "redis" ) {
52+ envEntries . push ( [ "REDIS_ARGS" , "--appendonly yes" ] ) ;
53+ // portEntries.push(["6379", "6379"]);
54+ const volume = await createVolume ( tx , { containerPath : "/data" , serviceId : service . id } ) ;
55+ await createDockerVolume ( volume . id ) ;
56+ }
57+ if ( envEntries . length ) {
58+ await syncEnvironmentVariables ( tx , service . id , Object . fromEntries ( envEntries ) ) ;
59+ }
60+ if ( portEntries . length ) {
61+ await syncPorts ( tx , service . id , Object . fromEntries ( portEntries ) ) ;
62+ }
63+ const network = await createNetwork ( tx , service . id , "provider" ) ;
64+ await createDockerNetwork ( network . id ) ;
6165 }
62- const network = await createNetwork ( tx , service . id , "provider" ) ;
63- await createDockerNetwork ( network . id ) ;
66+ } catch ( error ) {
67+ console . error ( error ) ;
68+ throw error ;
6469 }
6570 } ) ;
6671 redirect ( "/services" ) ;
@@ -81,9 +86,14 @@ export async function update(id: string, _: unknown, formData: FormData): Promis
8186 patch . status = "idle" ;
8287 }
8388 await db . transaction ( async tx => {
84- await updateService ( tx , id , patch ) ;
85- await syncEnvironmentVariables ( tx , id , env ) ;
86- await syncPorts ( tx , id , ports ) ;
89+ try {
90+ await updateService ( tx , id , patch ) ;
91+ await syncEnvironmentVariables ( tx , id , env ) ;
92+ await syncPorts ( tx , id , ports ) ;
93+ } catch ( error ) {
94+ console . error ( error ) ;
95+ throw error ;
96+ }
8797 } ) ;
8898 redirect ( "/services" ) ;
8999}
0 commit comments