Skip to content

Commit ce4623d

Browse files
committed
Fix the json merge logic
1 parent eb8168d commit ce4623d

File tree

5 files changed

+160
-39
lines changed

5 files changed

+160
-39
lines changed

.github/workflows/docker-image.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ on:
44
push:
55
branches:
66
- master
7+
schedule:
8+
- cron: '0 0 * * *' # Runs daily at midnight
79

810
jobs:
911
build:
@@ -16,6 +18,12 @@ jobs:
1618
- name: Set up Docker Buildx
1719
uses: docker/setup-buildx-action@v1
1820

21+
- name: Get latest dbsync tag
22+
id: get_latest_tag
23+
run: |
24+
latest_tag=$(curl -s https://api.github.com/repos/intersectmbo/cardano-db-sync/releases/latest | jq -r .tag_name)
25+
echo "::set-output name=tag::$latest_tag"
26+
1927
- name: Log in to GitHub Container Registry
2028
uses: docker/login-action@v1
2129
with:
@@ -29,4 +37,6 @@ jobs:
2937
context: .
3038
file: ./Dockerfile
3139
push: true
32-
tags: ghcr.io/${{ github.repository_owner }}/cardano-db-sync:13.6.0.4
40+
tags: ghcr.io/${{ github.repository_owner }}/cardano-db-sync:${{ steps.get_latest_tag.outputs.tag }}
41+
build-args: |
42+
DBSYNC_TAG=${{ steps.get_latest_tag.outputs.tag }}

Dockerfile

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,51 @@
1-
FROM ubuntu:24.04 as config
1+
# Step 1: Build Stage using musl to create a statically linked executable
2+
FROM alpine:latest AS json_update
3+
4+
# Install necessary build dependencies, including autoconf and libtool
5+
RUN apk add --no-cache \
6+
gcc \
7+
musl-dev \
8+
make \
9+
wget \
10+
libc-dev \
11+
zlib-dev \
12+
git \
13+
autoconf \
14+
automake \
15+
libtool
16+
17+
# Install jansson from source
18+
RUN git clone https://github.com/akheron/jansson.git /jansson && \
19+
cd /jansson && \
20+
autoreconf -i && \
21+
./configure && \
22+
make && \
23+
make install && \
24+
cd .. && \
25+
rm -rf /jansson
26+
27+
# Set the working directory inside the container
28+
WORKDIR /app
29+
30+
# Copy the C program into the container
31+
COPY ./json_merge/update_json.c .
32+
33+
# Compile the C program statically using musl-gcc
34+
RUN gcc -o update_json update_json.c -ljansson -static
35+
36+
37+
38+
FROM ubuntu:24.04 AS config
239
RUN apt-get update && apt-get install -y git \
340
&& git clone --depth 1 --filter=blob:none --sparse --no-checkout https://github.com/input-output-hk/cardano-playground.git \
441
&& cd cardano-playground \
542
&& git sparse-checkout set docs/environments \
643
&& git checkout
744

8-
FROM ghcr.io/intersectmbo/cardano-db-sync:13.6.0.4
45+
ARG DBSYNC_TAG=13.6.0.4
46+
47+
FROM ghcr.io/intersectmbo/cardano-db-sync:${DBSYNC_TAG:-13.6.0.4}
48+
COPY --from=json_update /app/update_json /bin/update_json
949
COPY --from=config /cardano-playground/docs/environments /environments
10-
COPY ./entrypoint.sh ./update_json_keys.sh /
50+
COPY ./entrypoint.sh /
1151
ENTRYPOINT ["/bin/bash", "-e", "/entrypoint.sh" ]

entrypoint.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,13 @@ elif [[ -z "${IS_NETWORK_ENV_SET:-}" ]]; then # dbsync config is set and network
8888
else ## both NETWORK and DB_SYNC_CONFIG are set
8989

9090
MERGED_CONFIG="$CONFIG_HOME/$NETWORK/merged-config.json"
91-
# take the keys from config file and replace i the original config.
92-
echo './update_json_keys.sh' "$DB_SYNC_CONFIG" \
91+
92+
# take the keys from config file and insert them into the default config
93+
echo 'update_json' "$DB_SYNC_CONFIG" \
9394
"$CONFIG_HOME/$NETWORK" \
9495
"$MERGED_CONFIG"
9596

96-
./update_json_keys.sh "$DB_SYNC_CONFIG" \
97+
update_json "$DB_SYNC_CONFIG" \
9798
"$DEFAULT_NETWORK_CONFIG" \
9899
"$MERGED_CONFIG"
99100
DB_SYNC_CONFIG="$MERGED_CONFIG"

json_merge/update_json.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <jansson.h>
5+
6+
// Update JSON keys in the destination JSON file with keys from the source JSON file
7+
// This code is chatgpt generated.
8+
void update_json_keys(const char *source_file, const char *dest_file, const char *output_file) {
9+
// Load source JSON file
10+
FILE *source_fp = fopen(source_file, "r");
11+
if (!source_fp) {
12+
perror("Error opening source file");
13+
exit(EXIT_FAILURE);
14+
}
15+
16+
fseek(source_fp, 0, SEEK_END);
17+
long source_size = ftell(source_fp);
18+
fseek(source_fp, 0, SEEK_SET);
19+
20+
char *source_content = (char *)malloc(source_size + 1);
21+
if (!source_content) {
22+
perror("Error allocating memory for source content");
23+
exit(EXIT_FAILURE);
24+
}
25+
26+
fread(source_content, 1, source_size, source_fp);
27+
source_content[source_size] = '\0'; // Null-terminate the string
28+
fclose(source_fp);
29+
30+
json_error_t error;
31+
json_t *source_json = json_loads(source_content, 0, &error);
32+
if (!source_json) {
33+
fprintf(stderr, "Error parsing source JSON: %s\n", error.text);
34+
exit(EXIT_FAILURE);
35+
}
36+
37+
free(source_content); // No longer needed after parsing
38+
39+
// Load destination JSON file
40+
FILE *dest_fp = fopen(dest_file, "r");
41+
if (!dest_fp) {
42+
perror("Error opening destination file");
43+
exit(EXIT_FAILURE);
44+
}
45+
46+
fseek(dest_fp, 0, SEEK_END);
47+
long dest_size = ftell(dest_fp);
48+
fseek(dest_fp, 0, SEEK_SET);
49+
50+
char *dest_content = (char *)malloc(dest_size + 1);
51+
if (!dest_content) {
52+
perror("Error allocating memory for destination content");
53+
exit(EXIT_FAILURE);
54+
}
55+
56+
fread(dest_content, 1, dest_size, dest_fp);
57+
dest_content[dest_size] = '\0'; // Null-terminate the string
58+
fclose(dest_fp);
59+
60+
json_t *dest_json = json_loads(dest_content, 0, &error);
61+
if (!dest_json) {
62+
fprintf(stderr, "Error parsing destination JSON: %s\n", error.text);
63+
exit(EXIT_FAILURE);
64+
}
65+
66+
free(dest_content); // No longer needed after parsing
67+
68+
// Update destination JSON with source JSON keys and values
69+
const char *key;
70+
json_t *value;
71+
json_object_foreach(source_json, key, value) {
72+
json_object_set(dest_json, key, value); // Replace or insert keys
73+
}
74+
75+
// Write updated JSON to output file
76+
FILE *output_fp = fopen(output_file, "w");
77+
if (!output_fp) {
78+
perror("Error opening output file");
79+
exit(EXIT_FAILURE);
80+
}
81+
82+
if (json_dumpf(dest_json, output_fp, JSON_INDENT(4)) < 0) {
83+
fprintf(stderr, "Error writing to output JSON file\n");
84+
exit(EXIT_FAILURE);
85+
}
86+
87+
fclose(output_fp);
88+
json_decref(source_json);
89+
json_decref(dest_json);
90+
91+
printf("Updated JSON saved to %s\n", output_file);
92+
}
93+
94+
int main(int argc, char *argv[]) {
95+
if (argc != 4) {
96+
fprintf(stderr, "Usage: %s <source_json> <destination_json> <output_json>\n", argv[0]);
97+
return EXIT_FAILURE;
98+
}
99+
100+
update_json_keys(argv[1], argv[2], argv[3]);
101+
return EXIT_SUCCESS;
102+
}

update_json_keys.sh

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)