11const yaml = require ( 'js-yaml' ) ;
22const fs = require ( 'fs' ) ;
3- const { nextTick } = require ( 'process' ) ;
43
54class Compose {
65 constructor ( dockerode ) {
@@ -19,6 +18,8 @@ class Compose {
1918 console . log ( self . recipe ) ;
2019 output . secrets = await self . loadSecrets ( ) ;
2120 output . volumes = await self . loadVolumes ( ) ;
21+ output . configs = await self . loadConfigs ( ) ;
22+ output . networks = await self . loadNetworks ( ) ;
2223 return output ;
2324 } catch ( e ) {
2425 throw e ;
@@ -27,13 +28,13 @@ class Compose {
2728
2829 async loadSecrets ( ) {
2930 var secrets = [ ] ;
30- var secretNames = Object . keys ( this . recipe . secrets ) ;
31+ var secretNames = Object . keys ( this . recipe . secrets || [ ] ) ;
3132 for ( var secretName of secretNames ) {
3233 var secret = this . recipe . secrets [ secretName ] ;
3334 if ( secret . external === true ) continue ;
3435 var opts = {
35- " Name" : this . projectName + '_' + secretName ,
36- " Data" : fs . readFileSync ( secret . file , 'utf8' )
36+ ' Name' : this . projectName + '_' + secretName ,
37+ ' Data' : fs . readFileSync ( secret . file , 'utf8' )
3738 } ;
3839 if ( secret . name !== undefined ) {
3940 opts . Name = secretName ;
@@ -47,15 +48,37 @@ class Compose {
4748 return secrets ;
4849 }
4950
51+ async loadConfigs ( ) {
52+ var configs = [ ] ;
53+ var configNames = Object . keys ( this . recipe . configs || [ ] ) ;
54+ for ( var configName of configNames ) {
55+ var config = this . recipe . configs [ configName ] ;
56+ if ( config . external === true ) continue ;
57+ var opts = {
58+ 'Name' : this . projectName + '_' + configName ,
59+ 'Data' : fs . readFileSync ( config . file , 'utf8' )
60+ } ;
61+ if ( config . name !== undefined ) {
62+ opts . Name = configName ;
63+ }
64+ try {
65+ configs . push ( await this . docker . createConfig ( opts ) ) ;
66+ } catch ( err ) {
67+ throw err ;
68+ }
69+ }
70+ return configs ;
71+ }
72+
5073 async loadVolumes ( ) {
5174 var volumes = [ ] ;
52- var volumeNames = Object . keys ( this . recipe . volumes ) ;
75+ var volumeNames = Object . keys ( this . recipe . volumes || [ ] ) ;
5376 for ( var volumeName of volumeNames ) {
5477 var volume = this . recipe . volumes [ volumeName ] ;
5578 if ( volume . external === true ) continue ;
5679 var opts = {
57- " Name" : this . projectName + '_' + volumeName ,
58- " Driver" : volume . driver ,
80+ ' Name' : this . projectName + '_' + volumeName ,
81+ ' Driver' : volume . driver ,
5982 'DriverOpts' : volume . driver_opts ,
6083 'Labels' : volume . labels
6184 } ;
@@ -70,6 +93,47 @@ class Compose {
7093 }
7194 return volumes ;
7295 }
96+
97+ async loadNetworks ( ) {
98+ var networks = [ ] ;
99+ var networkNames = Object . keys ( this . recipe . networks || [ ] ) ;
100+ for ( var networkName of networkNames ) {
101+ var network = this . recipe . networks [ networkName ] ;
102+ if ( network . external === true ) continue ;
103+ var opts = {
104+ 'Name' : this . projectName + '_' + networkName ,
105+ 'Driver' : network . driver ,
106+ 'DriverOpts' : network . driver_opts ,
107+ 'Labels' : network . labels ,
108+ 'Attachable' : network . attachable ,
109+ 'EnableIPv6' : network . enable_ipv6 ,
110+ 'Internal' : network . internal
111+ } ;
112+ if ( network . name !== undefined ) {
113+ opts . Name = networkName ;
114+ }
115+ if ( network . ipam !== undefined ) {
116+ opts . IPAM = {
117+ 'Driver' : network . ipam . driver ,
118+ 'Options' : network . ipam . options
119+ }
120+ if ( network . ipam . config !== undefined ) {
121+ opts . IPAM [ 'Config' ] = {
122+ 'Subnet' : network . ipam . config . subnet ,
123+ 'IPRange' : network . ipam . config . ip_range ,
124+ 'Gateway' : network . ipam . config . gateway ,
125+ 'AuxAddress' : network . ipam . config . aux_addresses
126+ }
127+ }
128+ }
129+ try {
130+ networks . push ( await this . docker . createNetwork ( opts ) ) ;
131+ } catch ( err ) {
132+ throw err ;
133+ }
134+ }
135+ return networks ;
136+ }
73137}
74138
75139module . exports = Compose ;
0 commit comments