Skip to content

Commit 1c16905

Browse files
committed
add some utils for common tasks
1 parent e9bb9b2 commit 1c16905

File tree

11 files changed

+141
-26
lines changed

11 files changed

+141
-26
lines changed

Dockerfile.consumer

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ FROM senzing/senzingsdk-runtime:4.0.0
33
USER root
44

55
RUN apt-get update \
6-
&& apt-get -y install --no-install-recommends curl python3 python3-pip python3-boto3 \
6+
&& apt-get -y install --no-install-recommends python3 python3-pip python3-boto3 \
77
&& apt-get -y autoremove \
88
&& apt-get -y clean
99

Dockerfile.exporter

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ FROM senzing/senzingsdk-runtime:4.0.0
33
USER root
44

55
RUN apt-get update \
6-
&& apt-get -y install --no-install-recommends curl python3 python3-pip python3-boto3 \
6+
&& apt-get -y install --no-install-recommends python3 python3-pip python3-boto3 \
77
&& apt-get -y autoremove \
88
&& apt-get -y clean
99

Dockerfile.util

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM senzing/senzingsdk-runtime:4.0.0
2+
3+
USER root
4+
5+
RUN apt-get update \
6+
&& apt-get -y install --no-install-recommends python3 python3-pip python3-boto3 \
7+
&& apt-get -y autoremove \
8+
&& apt-get -y clean
9+
10+
WORKDIR /app
11+
COPY middleware/* .
12+
13+
# Add a new user and switch to it.
14+
RUN useradd -m -u 1001 senzing
15+
USER senzing
16+
17+
ENV PYTHONPATH=/opt/senzing/er/sdk/python:/app
18+
19+
# Flush buffer - helps with print statements.
20+
ENV PYTHONUNBUFFERED=1
21+
22+
ENV AWS_PROFILE=localstack
23+
ENV Q_URL=http://sqs.us-east-1.localhost.localstack.cloud:4566/000000000000/sqs-senzing-local-ingest
24+
ENV S3_BUCKET_NAME=sqs-senzing-local-export
25+
26+
ENTRYPOINT ["python3"]

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,24 @@ sz_command -C add_record \
111111
PEOPLE 1 '{"NAME_FULL":"Robert Smith", "DATE_OF_BIRTH":"7/4/1976", "PHONE_NUMBER":"555-555-2088"}'
112112
```
113113

114+
### Utilities
115+
116+
Load a single record as a simple test:
117+
118+
docker compose run util util_load_record.py
119+
120+
Purge the database:
121+
122+
docker compose run util util_purge_db.py
123+
124+
Copy a file out of the LocalStack S3 bucket into `~/tmp` on your machine (be
125+
sure this folder already exists):
126+
127+
docker compose run util util_s3_retrieve.py hemingway.txt
128+
129+
Purge the LocalStack S3 bucket:
130+
131+
docker compose run util util_purge_s3.py
114132

115133
[awslocal]: https://docs.localstack.cloud/aws/integrations/aws-native-tools/aws-cli/#localstack-aws-cli-awslocal
116134
[localstack]: https://www.localstack.cloud/

docker-compose.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,31 @@ services:
125125
- ~/.aws:/home/senzing/.aws
126126
- ~/tmp:/tmp # Should you wish to write files to host.
127127

128+
util:
129+
build:
130+
context: .
131+
dockerfile: Dockerfile.util
132+
depends_on:
133+
- db
134+
environment:
135+
AWS_ENDPOINT_URL: http://localstack:4566
136+
SENZING_ENGINE_CONFIGURATION_JSON: >-
137+
{
138+
"PIPELINE": {
139+
"CONFIGPATH": "/etc/opt/senzing",
140+
"LICENSESTRINGBASE64": "${SENZING_LICENSE_BASE64_ENCODED}",
141+
"RESOURCEPATH": "/opt/senzing/er/resources",
142+
"SUPPORTPATH": "/opt/senzing/data"
143+
},
144+
"SQL": {
145+
"BACKEND": "SQL",
146+
"CONNECTION": "postgresql://${POSTGRES_USERNAME:-senzing}:${POSTGRES_PASSWORD:-senzing}@db:5432:${POSTGRES_DB:-G2}/?sslmode=disable"
147+
}
148+
}
149+
volumes:
150+
- ~/.aws:/home/senzing/.aws
151+
- ~/tmp:/tmp # Should you wish to write files to host.
152+
128153
volumes:
129154
localstack:
130155
postgres:

middleware/exporter.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,3 @@ def main():
128128

129129
if __name__ == '__main__': main()
130130

131-
#-------------------------------------------------------------------------------
132-
# ad-hoc test funcs - might move later
133-
134-
def _upload_test_file_to_s3():
135-
print("Starting test upload to S3 ...")
136-
s3 = make_s3_client()
137-
print(s3)
138-
fname = 'hemingway.txt'
139-
resp = s3.upload_file(fname, S3_BUCKET_NAME, fname)
140-
print(resp)
141-
print('Upload successful.')
142-
143-
def _get_file_from_s3(key):
144-
'''Get file from S3 and write to /tmp (use docker-compose to map this
145-
to desired directory on host machine).'''
146-
print('Grabbing file...')
147-
s3 = make_s3_client()
148-
resp = s3.download_file(S3_BUCKET_NAME, key, '/tmp/'+key)
149-
print(resp)
150-
print('Done.')

middleware/testutils.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import os
23
import sys
34
import time
45
import boto3
@@ -7,8 +8,9 @@
78
# sample-data/customers.jsonl into SQS.
89

910
# For live AWS, can set these to other values.
10-
AWS_PROFILE_NAME = 'localstack'
11+
AWS_PROFILE = 'localstack'
1112
Q_URL = 'http://sqs.us-east-1.localhost.localstack.cloud:4566/000000000000/sqs-senzing-local-ingest'
13+
S3_BUCKET_NAME = 'sqs-senzing-local-export'
1214

1315
#------------------------------------------------------------------------------
1416

@@ -23,7 +25,9 @@ def make_boto_session(fpath=None):
2325
if fpath:
2426
return boto3.Session(**json.load(open(fpath)))
2527
else:
26-
return boto3.Session(profile_name=AWS_PROFILE_NAME)
28+
# Here we pass in profile_name explicitly since it's not necessarily an env
29+
# var in this context.
30+
return boto3.Session(profile_name=AWS_PROFILE)
2731

2832
def make_sqs_client(boto_session):
2933
return boto_session.client('sqs')
@@ -71,3 +75,25 @@ def _get_1_msg(sqs, q_url):
7175
print(resp)
7276
return resp
7377

78+
#-------------------------------------------------------------------------------
79+
80+
def make_s3_client():
81+
try:
82+
# Here we pass in profile_name explicitly since it's not necessarily an env
83+
# var in this context.
84+
sess = boto3.Session(profile_name=AWS_PROFILE)
85+
if 'AWS_ENDPOINT_URL' in os.environ:
86+
return sess.client('s3', endpoint_url=os.environ['AWS_ENDPOINT_URL'])
87+
else:
88+
return sess.client('s3')
89+
except Exception as e:
90+
print(e)
91+
92+
def upload_test_file_to_s3():
93+
print("Starting test upload to S3 ...")
94+
s3 = make_s3_client()
95+
print(s3)
96+
fname = 'sample-data/hemingway.txt'
97+
resp = s3.upload_file(fname, S3_BUCKET_NAME, fname[fname.rfind('/')+1:])
98+
print(resp)
99+
print('Upload successful.')
File renamed without changes.

middleware/sz_purge.py renamed to middleware/util_purge_db.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
sz_factory = sz_core.SzAbstractFactoryCore("sz_factory_1", senzing_engine_configuration_json)
1717
sz_diagnostic = sz_factory.create_diagnostic()
1818

19-
print('Are you sure you want to purge the repository? If so, type YES:')
19+
print('Are you sure you want to purge the database? If so, type YES:')
2020
ans = input('>')
2121
if ans == 'YES':
2222
sz_diagnostic.purge_repository()
2323
else:
24-
print('Everything left as-is.')
24+
print('Nothing was done. Everything was left as-is.')

middleware/util_purge_s3.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os
2+
import boto3
3+
4+
def purge_s3():
5+
s3 = boto3.resource('s3', endpoint_url=os.environ['AWS_ENDPOINT_URL'])
6+
buck = s3.Bucket(os.environ['S3_BUCKET_NAME'])
7+
print('Purging...')
8+
buck.objects.all().delete()
9+
10+
print('Are you sure you want to purge the S3 bucket (' + os.environ['S3_BUCKET_NAME'] + ')? If so, type YES:')
11+
ans = input('>')
12+
if ans == 'YES':
13+
purge_s3()
14+
print('Done.')
15+
else:
16+
print('Nothing was done. Everything was left as-is.')

0 commit comments

Comments
 (0)