forked from botpress/botpress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho.Tiltfile
More file actions
299 lines (260 loc) · 8.63 KB
/
echo.Tiltfile
File metadata and controls
299 lines (260 loc) · 8.63 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# 0. constants
## 0.1. ports
READINESS_PORT = 8086
CHAT_INTEGRATION_PORT = 8089
ECHO_BOT_PORT = 8090
PUSHPIN_PUBLIC_INNER_PORT = 7999
PUSHPIN_PRIVATE_INNER_PORT = 5561
PUSHPIN_PUBLIC_PORT = 7001
PUSHPIN_PRIVATE_PORT = 6001
DYNAMO_DB_PORT = 8000
## 0.2. strings
AUTH_ENCRYPTION_KEY = "allo"
WORK_DIR = os.path.dirname(__file__)
BOTPRESS_HOME_DIR = "%s/.botpresshome.echo" % WORK_DIR
PUSHPIN_CONFIG_PATH="%s/pushpin.conf" % WORK_DIR
LOCAL_AWS_ACCESS_KEY_ID = "FOO"
LOCAL_AWS_SECRET_ACCESS_KEY = "BAR"
ECHO_BOT_PATH = "%s/bots/echo" % WORK_DIR
CHAT_INT_PATH = "%s/integrations/chat" % WORK_DIR
CONV_FID_STORE = struct(
table_name='chat-integration-conversation-fid-store',
index_name='chat-integration-conversation-id-store',
partition_key='bot_id',
sort_key='fid',
index_sort_key='id',
)
USER_FID_STORE = struct(
table_name='chat-integration-user-fid-store',
index_name='chat-integration-user-id-store',
partition_key='bot_id',
sort_key='fid',
index_sort_key='id',
)
## 0.3. utils
def encode_base64(content):
return str(local(
command=['base64'],
quiet=True,
echo_off=True,
stdin=content,
)).strip()
# 0.4. env
BP_HOME_ENV = struct(key='BP_BOTPRESS_HOME', value=BOTPRESS_HOME_DIR)
# 1. config
config.define_bool('skip-install')
config.define_bool('skip-build')
config.define_string('bp-domain')
config.define_string('bp-token')
config.define_string('bp-workspace-id')
cfg = config.parse()
config_skip_install = cfg.get('skip-install', False)
config_skip_build = cfg.get('skip-build', False)
config_bp_domain = cfg.get('bp-domain', None)
config_bp_token = cfg.get('bp-token', None)
config_bp_workspace_id = cfg.get('bp-workspace-id', None)
if not config_bp_domain:
fail("config 'bp-domain' is required")
if not config_bp_token:
fail("config 'bp-token' is required")
if not config_bp_workspace_id:
fail("config 'bp-workspace-id' is required")
CONFIG = struct(
skip_install=config_skip_install,
skip_build=config_skip_build,
bp_domain=config_bp_domain,
bp_token=config_bp_token,
bp_workspace_id=config_bp_workspace_id,
)
api_webhook_domain = "webhook.%s" % CONFIG.bp_domain
api_api_domain = "api.%s" % CONFIG.bp_domain
api_tunnel_domain = "tunnel.%s" % CONFIG.bp_domain
api_api_url = "https://%s" % api_api_domain
api_tunnel_url = "https://%s" % api_tunnel_domain
API = struct(
bp_webhook_domain = api_webhook_domain,
bp_api_domain = api_api_domain,
bp_tunnel_domain = api_tunnel_domain,
bp_api_url = api_api_url,
bp_tunnel_url = api_tunnel_url,
)
# 2. docker compose
dynamodb_resource = {
"image": "amazon/dynamodb-local",
"ports": ["%s:%s" % (DYNAMO_DB_PORT, DYNAMO_DB_PORT)],
"environment": {
"DYNAMODB_LOCAL_LOG": "DEBUG"
}
}
PUSHPIN_ROUTES = "* %s:443,ssl,insecure,host=%s" % (API.bp_webhook_domain, API.bp_webhook_domain)
PUSHPIN_CONFIG = "%s" % read_file(PUSHPIN_CONFIG_PATH, '')
pushpin_ressource = {
"image": "botpress/pushpin",
"environment": {
"PUSHPIN_ROUTES": PUSHPIN_ROUTES,
"PUSHPIN_CONFIG": PUSHPIN_CONFIG,
},
'ports': ['%s:%s' % (PUSHPIN_PUBLIC_PORT, PUSHPIN_PUBLIC_INNER_PORT), '%s:%s' % (PUSHPIN_PRIVATE_PORT, PUSHPIN_PRIVATE_INNER_PORT)],
'platform': 'linux/amd64',
}
docker_compose(encode_yaml({
'version': '3.5',
'services': {
'run-pushpin': pushpin_ressource,
'run-dynamodb': dynamodb_resource,
},
}))
dc_readiness = [
{ 'type': 'http', 'name': 'run-pushpin', 'url': 'http://localhost:%s' % PUSHPIN_PUBLIC_PORT },
{ 'type': 'dynamodb', 'name': 'run-dynamodb', 'uri': 'http://localhost:%s' % DYNAMO_DB_PORT },
]
# 3. ressources
## 3.1. utils
local_resource(
name='pnpm-install',
cmd='pnpm install' if not CONFIG.skip_install else 'echo skipped',
labels=['utils']
)
local_resource(
name='pnpm-build',
cmd='pnpm build' if not CONFIG.skip_build else 'echo skipped',
labels=['utils'],
resource_deps=['pnpm-install']
)
local_resource(
name='login-botpress',
cmd='pnpm bp login',
env={
BP_HOME_ENV.key: BP_HOME_ENV.value,
'BP_API_URL': API.bp_api_url,
'BP_TOKEN': CONFIG.bp_token,
'BP_WORKSPACE_ID': CONFIG.bp_workspace_id,
},
resource_deps=['pnpm-install', 'pnpm-build'],
labels=['utils'],
)
readiness_deps=[r['name'] for r in dc_readiness] + ['pnpm-install', 'pnpm-build']
local_resource(
name="readiness",
allow_parallel=True,
serve_cmd='pnpm ready',
serve_env={
'PORT': '%s' % READINESS_PORT,
'LOG_LEVEL': 'info',
'CONFIG': encode_json(dc_readiness),
'AWS_REGION': 'localhost',
'AWS_ACCESS_KEY_ID': LOCAL_AWS_ACCESS_KEY_ID,
'AWS_SECRET_ACCESS_KEY': LOCAL_AWS_SECRET_ACCESS_KEY,
},
labels=['utils'],
readiness_probe=probe(http_get=http_get_action(port=READINESS_PORT, path='/ready'), period_secs=2, failure_threshold=20),
resource_deps=readiness_deps,
)
# 3.2. scripts
local_resource(
name='create-conv-fid-store',
cmd='pnpm ts-node -T ./scripts/dynamodb-create-table.ts',
env={
'AWS_REGION': "localhost",
'AWS_ACCESS_KEY_ID': LOCAL_AWS_ACCESS_KEY_ID,
'AWS_SECRET_ACCESS_KEY': LOCAL_AWS_SECRET_ACCESS_KEY,
'endpoint': "http://localhost:%s" % DYNAMO_DB_PORT,
'table_name': CONV_FID_STORE.table_name,
'index_name': CONV_FID_STORE.index_name,
'partition_key': CONV_FID_STORE.partition_key,
'sort_key': CONV_FID_STORE.sort_key,
'index_sort_key': CONV_FID_STORE.index_sort_key,
},
labels=['scripts'],
resource_deps=['readiness'],
)
local_resource(
name='create-user-fid-store',
cmd='pnpm ts-node -T ./scripts/dynamodb-create-table.ts',
env={
'AWS_REGION': "localhost",
'AWS_ACCESS_KEY_ID': LOCAL_AWS_ACCESS_KEY_ID,
'AWS_SECRET_ACCESS_KEY': LOCAL_AWS_SECRET_ACCESS_KEY,
'endpoint': "http://localhost:%s" % DYNAMO_DB_PORT,
'table_name': USER_FID_STORE.table_name,
'index_name': USER_FID_STORE.index_name,
'partition_key': USER_FID_STORE.partition_key,
'sort_key': USER_FID_STORE.sort_key,
'index_sort_key': USER_FID_STORE.index_sort_key,
},
labels=['scripts'],
resource_deps=['readiness'],
)
## 3.3. run
dc_resource(name='run-pushpin', labels=['run'])
dc_resource(name='run-dynamodb', labels=['run'])
local_resource(
name='run-chat-integration',
serve_cmd=" ".join([
"pnpm -F @botpresshub/chat exec bp dev -v",
"--secrets SIGNAL_URL=http://localhost:%s" % PUSHPIN_PRIVATE_PORT,
"--secrets AUTH_ENCRYPTION_KEY=%s" % AUTH_ENCRYPTION_KEY,
'--secrets FID_STORE_CONFIG="%s"' % encode_base64(encode_json({
"strategy": "dynamo-db",
"endpoint": "http://localhost:%s" % DYNAMO_DB_PORT,
"region": "localhost",
"accessKeyId": LOCAL_AWS_ACCESS_KEY_ID,
"secretAccessKey": LOCAL_AWS_SECRET_ACCESS_KEY,
"conversationTable": {
"tableName": CONV_FID_STORE.table_name,
"indexName": CONV_FID_STORE.index_name,
"partitionKey": CONV_FID_STORE.partition_key,
"sortKey": CONV_FID_STORE.sort_key,
"indexSortKey": CONV_FID_STORE.index_sort_key,
},
"userTable": {
"tableName": USER_FID_STORE.table_name,
"indexName": USER_FID_STORE.index_name,
"partitionKey": USER_FID_STORE.partition_key,
"sortKey": USER_FID_STORE.sort_key,
"indexSortKey": USER_FID_STORE.index_sort_key,
}
}))
]),
serve_env={
BP_HOME_ENV.key: BP_HOME_ENV.value,
'BP_CONFIRM': 'true',
'BP_TUNNEL_URL': API.bp_tunnel_url,
'BP_PORT': str(CHAT_INTEGRATION_PORT),
},
resource_deps=['login-botpress', 'readiness', 'run-pushpin', 'create-conv-fid-store', 'create-user-fid-store'],
labels=['run'],
readiness_probe=probe(http_get=http_get_action(port=CHAT_INTEGRATION_PORT, path='/health'), period_secs=1, failure_threshold=10),
)
local_resource(
name='run-echo-bot',
cmd="pnpm -F echo exec bp add --use-dev -y -v",
serve_cmd="pnpm -F echo exec bp dev -y --tunnel-url %s --port %s" % (API.bp_tunnel_url, ECHO_BOT_PORT),
serve_env={
BP_HOME_ENV.key: BP_HOME_ENV.value,
},
resource_deps=['login-botpress', 'run-chat-integration'],
labels=['run'],
readiness_probe=probe(http_get=http_get_action(port=ECHO_BOT_PORT, path='/health'), period_secs=1, failure_threshold=10),
)
## 3.3. test
# TODO: find a way to make this work without bash specific syntax
local_resource(
name='test-chat-integration',
cmd=" ".join([
'wh_id=$(pnpm ts-node -T ./scripts/fetch-chat-wh.ts);',
'API_URL=http://localhost:%s/$wh_id' % PUSHPIN_PUBLIC_PORT,
'pnpm run -F @botpress/chat test:e2e'
]),
env={
BP_HOME_ENV.key: BP_HOME_ENV.value,
'BP_API_URL': API.bp_api_url,
'BP_TOKEN': CONFIG.bp_token,
'BP_WORKSPACE_ID': CONFIG.bp_workspace_id,
'ECHO_PATH': ECHO_BOT_PATH,
'CHAT_PATH': CHAT_INT_PATH,
'ENCRYPTION_KEY': AUTH_ENCRYPTION_KEY,
},
resource_deps=['run-chat-integration', 'run-echo-bot'],
labels=['test'],
)