-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
191 lines (165 loc) · 6.27 KB
/
Makefile
File metadata and controls
191 lines (165 loc) · 6.27 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
SHELL := /bin/bash
COMPOSE ?= docker compose
# Compose service names (do NOT rely on container_name)
APP_SERVICE ?= app
EMULATOR_SERVICE ?= gcs-emulator
# Storage endpoint override (inside the compose network by default).
# For real GCP, set STORAGE_ENDPOINT empty: STORAGE_ENDPOINT=
STORAGE_ENDPOINT ?= http://gcs-emulator:4443
# Parameters
GCS_BUCKET ?= my-bucket
GCS_PREFIX ?= uploads/
SOURCE_DIR ?= .
PROJECT_ID ?= local-project
PYTHON ?= python
SCRIPT ?= upload_and_move.py
# A tiny curl container for health checks (no host deps)
CURL_IMAGE ?= curlimages/curl:8.6.0
.PHONY: help build up down restart logs ps shell emulator \
wait-endpoint init \
py-version run run-dry \
list-buckets list-objects clean-bucket \
clean
help:
@echo "Targets:"
@echo " build Build images"
@echo " up Start environment (detached)"
@echo " down Stop environment"
@echo " restart Restart environment"
@echo " logs Follow docker compose logs"
@echo " ps List services/containers"
@echo " shell Open a shell in the app service container"
@echo " emulator Open a shell in the emulator service container"
@echo " wait-endpoint Wait until the Storage endpoint is reachable (no host deps)"
@echo " init Start and initialize (wait + ensure bucket)"
@echo " py-version Show the Python version inside the app container"
@echo " run Run uploader (GCS_BUCKET=..., GCS_PREFIX=..., SOURCE_DIR=..., STORAGE_ENDPOINT=...)"
@echo " run-dry Dry-run uploader (no upload)"
@echo " list-buckets List buckets"
@echo " list-objects List objects in a bucket (GCS_BUCKET=...)"
@echo " clean-bucket Delete all objects in the bucket (GCS_BUCKET=...)"
@echo " clean Stop and remove environment + volumes"
build:
$(COMPOSE) build
up:
$(COMPOSE) up -d
down:
$(COMPOSE) down
restart: down up
logs:
$(COMPOSE) logs -f --tail=200
ps:
$(COMPOSE) ps
shell:
$(COMPOSE) exec -it $(APP_SERVICE) bash
emulator:
$(COMPOSE) exec -it $(EMULATOR_SERVICE) sh
# No host curl. We run a curl container on the same compose network.
# If STORAGE_ENDPOINT is empty (real GCP), this target is not needed.
wait-endpoint:
@if [ -z "$(STORAGE_ENDPOINT)" ]; then \
echo "STORAGE_ENDPOINT is empty; using default Google endpoints. Skipping wait-endpoint."; \
exit 0; \
fi
@echo "Waiting for Storage endpoint: $(STORAGE_ENDPOINT) ..."
@until $(COMPOSE) run --rm --no-deps --entrypoint "" \
$(APP_SERVICE) bash -lc "curl -fsS '$(STORAGE_ENDPOINT)/storage/v1/b' >/dev/null"; do \
sleep 1; \
done
@echo "Storage endpoint OK"
# Initialize environment + ensure bucket exists (by running the script in dry-run with create-bucket)
init: up wait-endpoint
@echo "Ensuring bucket exists..."
$(COMPOSE) exec -it $(APP_SERVICE) bash -lc '\
GOOGLE_CLOUD_PROJECT=$(PROJECT_ID) \
STORAGE_ENDPOINT_URL=$(STORAGE_ENDPOINT) \
$(PYTHON) $(SCRIPT) \
--bucket $(GCS_BUCKET) \
--prefix $(GCS_PREFIX) \
--source-dir $(SOURCE_DIR) \
$(if $(STORAGE_ENDPOINT),--storage-endpoint $(STORAGE_ENDPOINT),) \
--create-bucket \
--dry-run \
'
py-version:
$(COMPOSE) exec -it $(APP_SERVICE) $(PYTHON) --version
run:
$(COMPOSE) exec -it $(APP_SERVICE) bash -lc '\
GOOGLE_CLOUD_PROJECT=$(PROJECT_ID) \
STORAGE_ENDPOINT_URL=$(STORAGE_ENDPOINT) \
$(PYTHON) $(SCRIPT) \
--bucket $(GCS_BUCKET) \
--prefix $(GCS_PREFIX) \
--source-dir $(SOURCE_DIR) \
$(if $(STORAGE_ENDPOINT),--storage-endpoint $(STORAGE_ENDPOINT),) \
--create-bucket \
'
run-dry:
$(COMPOSE) exec -it $(APP_SERVICE) bash -lc '\
GOOGLE_CLOUD_PROJECT=$(PROJECT_ID) \
STORAGE_ENDPOINT_URL=$(STORAGE_ENDPOINT) \
$(PYTHON) $(SCRIPT) \
--bucket $(GCS_BUCKET) \
--prefix $(GCS_PREFIX) \
--source-dir $(SOURCE_DIR) \
$(if $(STORAGE_ENDPOINT),--storage-endpoint $(STORAGE_ENDPOINT),) \
--dry-run \
'
create-bucket:
$(COMPOSE) exec -it $(APP_SERVICE) bash -lc '\
GOOGLE_CLOUD_PROJECT=$(PROJECT_ID) \
STORAGE_ENDPOINT_URL=$(STORAGE_ENDPOINT) \
$(PYTHON) -c "import os; \
from google.cloud import storage; \
ep=os.getenv(\"STORAGE_ENDPOINT_URL\"); \
os.environ[\"STORAGE_EMULATOR_HOST\"]=ep if ep else os.environ.get(\"STORAGE_EMULATOR_HOST\", \"\"); \
bucket_name=\"$(GCS_BUCKET)\"; \
project=os.getenv(\"GOOGLE_CLOUD_PROJECT\"); \
client=storage.Client(project=project); \
bucket=client.lookup_bucket(bucket_name); \
print(f\"Bucket already exists: {bucket_name}\") if bucket else (client.create_bucket(bucket_name) or print(f\"Bucket created: {bucket_name}\"))" \
'
list-buckets:
$(COMPOSE) exec -it $(APP_SERVICE) bash -lc '\
GOOGLE_CLOUD_PROJECT=$(PROJECT_ID) \
STORAGE_ENDPOINT_URL=$(STORAGE_ENDPOINT) \
$(PYTHON) -c "import os; \
from google.cloud import storage; \
ep=os.getenv(\"STORAGE_ENDPOINT_URL\"); \
os.environ[\"STORAGE_EMULATOR_HOST\"]=ep if ep else os.environ.get(\"STORAGE_EMULATOR_HOST\", \"\"); \
project=os.getenv(\"GOOGLE_CLOUD_PROJECT\"); \
client=storage.Client(project=project); \
[print(b.name) for b in client.list_buckets()]" \
'
list-objects:
$(COMPOSE) exec -it $(APP_SERVICE) bash -lc '\
GOOGLE_CLOUD_PROJECT=$(PROJECT_ID) \
STORAGE_ENDPOINT_URL=$(STORAGE_ENDPOINT) \
$(PYTHON) -c "import os; \
from google.cloud import storage; \
ep=os.getenv(\"STORAGE_ENDPOINT_URL\"); \
os.environ[\"STORAGE_EMULATOR_HOST\"]=ep if ep else os.environ.get(\"STORAGE_EMULATOR_HOST\", \"\"); \
bucket_name=\"$(GCS_BUCKET)\"; \
project=os.getenv(\"GOOGLE_CLOUD_PROJECT\"); \
client=storage.Client(project=project); \
bucket=client.bucket(bucket_name); \
[print(blob.name) for blob in bucket.list_blobs()]" \
'
clean-bucket:
$(COMPOSE) exec -it $(APP_SERVICE) bash -lc '\
GOOGLE_CLOUD_PROJECT=$(PROJECT_ID) \
STORAGE_ENDPOINT_URL=$(STORAGE_ENDPOINT) \
$(PYTHON) -c "import os; \
from google.cloud import storage; \
ep=os.getenv(\"STORAGE_ENDPOINT_URL\"); \
os.environ[\"STORAGE_EMULATOR_HOST\"]=ep if ep else os.environ.get(\"STORAGE_EMULATOR_HOST\", \"\"); \
bucket_name=\"$(GCS_BUCKET)\"; \
project=os.getenv(\"GOOGLE_CLOUD_PROJECT\"); \
client=storage.Client(project=project); \
bucket=client.bucket(bucket_name); \
blobs=list(bucket.list_blobs()); \
[blob.delete() for blob in blobs]; \
print(f\"Deleted {len(blobs)} objects from gs://{bucket_name}\")" \
'
clean:
$(COMPOSE) down -v