-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfigurator.ol
172 lines (156 loc) · 4.92 KB
/
configurator.ol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Copyright (C) 2024 Valentino Picotti
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
from console import Console
from file import File
from mustache import Mustache
from string-utils import StringUtils
type SlicerConfig: undefined
type DeploymentConfig: undefined
/* More spefic config types (pseudo-jolie syntax):
type SlicerConfig {
?"<service-name>": {
params: undefined
ports*: int( ranges([0,65535]) ) | string( enum(["internal"]) )
}
}
type DeploymentConfig {
simulator: bool
?"<service-name>": {
params: undefined
locations*: string
}
}
*/
type DeploymentConfigRequest {
slicerConfig: SlicerConfig
simulate: bool
}
type DockerfileRequest {
slicerConfig: SlicerConfig
}
type Dockerfiles: undefined
/*
type Dockerfiles {
?"<service-name>": string
}
*/
type ComposefileRequest {
slicerConfig: SlicerConfig
}
type Composefile: string
interface ConfiguratorIface {
RequestResponse:
produceDeploymentConfig( DeploymentConfigRequest )( DeploymentConfig ),
produceDockerfiles( DockerfileRequest )( Dockerfiles ),
produceComposefile( ComposefileRequest )( Composefile )
}
constants {
LOCAL = "local://",
SOCKET = "socket://",
INTERNAL = "internal",
BASE_PORT = 10000,
TEMPLATES_DIR = "/templates/",
DOCKER_TEMPLATE ="Dockerfile.mustache",
COMPOSE_TEMPLATE ="docker-compose.mustache",
JOLIE_EXTENSION = ".ol",
CONFIG_FILENAME = "config.json"
}
service Configurator {
execution: sequential
embed Console as console
embed File as file
embed Mustache as mst
embed StringUtils as str
inputPort in {
location: "local"
interfaces: ConfiguratorIface
}
main {
[ produceDeploymentConfig( request )( response ){
// produce deployment config from slicer.json
config -> request.slicerConfig
deployment << config
deployment.simulator = request.simulate || false
foreach( service : config ) {
undef( deployment.( service ).ports )
toLowerCase@str( service )( host )
i = 0
for( port in config.(service).ports ) {
serviceLocation -> deployment.( service ).locations._[i]
isLocal = request.simulate && port == INTERNAL
serviceLocation = if ( isLocal ) LOCAL else SOCKET
serviceLocation += host
if( !isLocal ) {
serviceLocation += ":"
}
if( request.simulate && is_int( port ) ) {
serviceLocation += port
} else {
serviceLocation += BASE_PORT + i
}
++i
}
}
response -> deployment
} ]
[ produceDockerfiles( request )( response ){
config -> request.slicerConfig
// Which one to use??
getRealServiceDirectory@file()( rdir )
// getServiceDirectory@file()( dir )
// getServiceParentPath@file()( pdir )
readFile@file( { filename = rdir + TEMPLATES_DIR + DOCKER_TEMPLATE } )( render.template )
with( render.data ){
.jolie_version = "1.12.1"
.config_file = CONFIG_FILENAME
}
foreach( service: config ) {
render.data.service_file = service + JOLIE_EXTENSION
for( i = 0, i < #config.( service ).ports, i++ ) {
render.data.ports[#render.data.ports] = BASE_PORT + i++
}
render@mst( render )( response.( service ) )
}
} ]
[ produceComposefile( request )( response ){
config -> request.slicerConfig
// Which one to use??
getRealServiceDirectory@file()( rdir )
// getServiceDirectory@file()( dir )
// getServiceParentPath@file()( pdir )
readFile@file( { filename = rdir + TEMPLATES_DIR + COMPOSE_TEMPLATE } )( render.template )
serviceIdx = 0
foreach( service: config ) {
serviceData -> render.data.services[serviceIdx]
serviceData.name = toLowerCase@str( service )
for( portIdx = 0, portIdx < #config.( service ).ports, portIdx++ ) {
port -> config.( service ).ports[portIdx]
if( is_int( port ) ){
serviceData.ports[#serviceData.ports] << {
exposed -> port
internal = BASE_PORT + portIdx }
}
}
serviceData.has_ports = is_defined( serviceData.ports )
serviceIdx += 1
}
render@mst( render )( response )
} ]
}
}