Repository navigation
Expand file tree
/
Copy pathupload.sh
More file actions
executable file
·185 lines (164 loc) · 7.16 KB
/
Copy pathupload.sh
File metadata and controls
executable file
·185 lines (164 loc) · 7.16 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
#!/bin/bash
echo "Build and Push jasonacox/pypowerwall-server to Docker Hub"
echo "Usage: $0 [beta_number]"
echo " If beta_number is not provided, auto-increments from last beta version"
echo ""
# Start timing
BUILD_START=$(date +%s)
last_path=$(basename $PWD)
if [ "$last_path" == "pypowerwall-server" ]; then
# Determine version
SERVER_VERSION=`grep "SERVER_VERSION = " app/config.py | cut -d\" -f2`
# Ask if this is a beta release
echo "Release Type:"
echo " 1) Beta release (adds -betaX suffix)"
echo " 2) Production release (version ${SERVER_VERSION})"
echo ""
read -p "Select release type [1-2]: " RELEASE_TYPE
if [ "$RELEASE_TYPE" == "2" ]; then
# Production release - just use version number
VER="${SERVER_VERSION}"
CONTAINER_NAME="jasonacox/pypowerwall-server:${VER}"
echo ""
echo "Production release: ${CONTAINER_NAME}"
else
# Beta release - handle beta numbering
BETA_FILE=".beta_version"
if [ -n "$1" ]; then
# Use provided beta number
BETA_NUM="$1"
echo "$BETA_NUM" > "$BETA_FILE"
else
# Auto-increment beta number
if [ -f "$BETA_FILE" ]; then
BETA_NUM=$(cat "$BETA_FILE")
BETA_NUM=$((BETA_NUM + 1))
else
BETA_NUM=1
fi
echo "$BETA_NUM" > "$BETA_FILE"
fi
VER="${SERVER_VERSION}-beta${BETA_NUM}"
CONTAINER_NAME="jasonacox/pypowerwall-server:${VER}"
echo ""
echo "Beta release: ${CONTAINER_NAME}"
echo "Beta version: ${BETA_NUM} (stored in ${BETA_FILE})"
fi
# Check with user before proceeding
echo ""
read -p "Build and push to Docker Hub? Press [Enter] to continue or Ctrl-C to cancel..."
# Ask if no-cache build is desired
echo ""
read -p "Use --no-cache for build? [y/N]: " NO_CACHE_RESPONSE
NO_CACHE_FLAG=""
if [ "$NO_CACHE_RESPONSE" == "y" ] || [ "$NO_CACHE_RESPONSE" == "Y" ]; then
NO_CACHE_FLAG="--no-cache"
echo "Building with --no-cache"
else
echo "Building with cache"
fi
# Select Dockerfile and determine whether to tag :latest
if [ "$RELEASE_TYPE" == "2" ]; then
DOCKERFILE="Dockerfile"
LATEST_TAG="-t jasonacox/pypowerwall-server:latest"
PYPW_DEREFFED=false
else
DOCKERFILE="Dockerfile.beta"
LATEST_TAG="" # Beta builds do not overwrite :latest
# Preflight: Dockerfile.beta COPYs pypowerwall/ into the image; fail fast if missing.
if [ ! -d "pypowerwall" ] && [ ! -L "pypowerwall" ]; then
echo "ERROR: Beta builds require a 'pypowerwall/' directory or symlink in the project root."
echo " Dockerfile.beta will COPY ./pypowerwall into the image."
echo " Create it (e.g. ln -s /path/to/pypowerwall/pypowerwall pypowerwall) and re-run."
exit 1
fi
# Docker BuildKit does not follow symlinks that point outside the build context.
# If pypowerwall/ is a symlink (e.g. to ../pypowerwall/pypowerwall), dereference it
# into a real directory so the COPY step in Dockerfile.beta works correctly.
PYPW_DEREFFED=false
if [ -L "pypowerwall" ]; then
echo "* Dereferencing pypowerwall symlink for Docker build context..."
cp -rL pypowerwall pypowerwall_real
mv pypowerwall pypowerwall_symlink
mv pypowerwall_real pypowerwall
PYPW_DEREFFED=true
# Ensure symlink is always restored even if the build fails.
# Use -L (is a symlink) not -d (is a directory) so the check works even
# when the symlink target is temporarily missing or broken.
trap 'if [ "$PYPW_DEREFFED" = true ] && [ -L pypowerwall_symlink ]; then rm -rf pypowerwall; mv pypowerwall_symlink pypowerwall; fi' EXIT
fi
fi
# Build jasonacox/pypowerwall-server:x.y.z
echo "* BUILD ${CONTAINER_NAME} (using ${DOCKERFILE})"
docker buildx build -f ${DOCKERFILE} ${NO_CACHE_FLAG} --platform linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v8 --push -t ${CONTAINER_NAME} ${LATEST_TAG} .
echo ""
# Verify
echo "* VERIFY ${CONTAINER_NAME}"
docker buildx imagetools inspect ${CONTAINER_NAME} | grep Platform
echo ""
if [ "$RELEASE_TYPE" == "2" ]; then
echo "* VERIFY jasonacox/pypowerwall-server:latest"
docker buildx imagetools inspect jasonacox/pypowerwall-server:latest | grep Platform
echo ""
fi
# Restore pypowerwall symlink if it was dereferenced for the beta build
if [ "$PYPW_DEREFFED" = true ]; then
echo "* Restoring pypowerwall symlink..."
rm -rf pypowerwall
mv pypowerwall_symlink pypowerwall
fi
# Calculate build time
BUILD_END=$(date +%s)
BUILD_TIME=$((BUILD_END - BUILD_START))
BUILD_MIN=$((BUILD_TIME / 60))
BUILD_SEC=$((BUILD_TIME % 60))
# Get container sizes for each architecture
echo "* Fetching container sizes..."
# Get the digest for each platform
AMD64_DIGEST=$(docker buildx imagetools inspect ${CONTAINER_NAME} --raw 2>/dev/null | jq -r '.manifests[] | select(.platform.architecture=="amd64") | .digest' 2>/dev/null)
ARM64_DIGEST=$(docker buildx imagetools inspect ${CONTAINER_NAME} --raw 2>/dev/null | jq -r '.manifests[] | select(.platform.architecture=="arm64") | .digest' 2>/dev/null)
ARMV7_DIGEST=$(docker buildx imagetools inspect ${CONTAINER_NAME} --raw 2>/dev/null | jq -r '.manifests[] | select(.platform.architecture=="arm" and .platform.variant=="v7") | .digest' 2>/dev/null)
ARMV8_DIGEST=$(docker buildx imagetools inspect ${CONTAINER_NAME} --raw 2>/dev/null | jq -r '.manifests[] | select(.platform.architecture=="arm" and .platform.variant=="v8") | .digest' 2>/dev/null)
# Get actual image sizes by inspecting each platform's manifest
if [ -n "$AMD64_DIGEST" ]; then
SIZE_AMD64=$(docker buildx imagetools inspect ${CONTAINER_NAME}@${AMD64_DIGEST} --raw 2>/dev/null | jq '[.layers[].size] | add' 2>/dev/null)
SIZE_AMD64_MB=$((SIZE_AMD64 / 1024 / 1024))
else
SIZE_AMD64_MB="N/A"
fi
if [ -n "$ARM64_DIGEST" ]; then
SIZE_ARM64=$(docker buildx imagetools inspect ${CONTAINER_NAME}@${ARM64_DIGEST} --raw 2>/dev/null | jq '[.layers[].size] | add' 2>/dev/null)
SIZE_ARM64_MB=$((SIZE_ARM64 / 1024 / 1024))
else
SIZE_ARM64_MB="N/A"
fi
if [ -n "$ARMV7_DIGEST" ]; then
SIZE_ARMV7=$(docker buildx imagetools inspect ${CONTAINER_NAME}@${ARMV7_DIGEST} --raw 2>/dev/null | jq '[.layers[].size] | add' 2>/dev/null)
SIZE_ARMV7_MB=$((SIZE_ARMV7 / 1024 / 1024))
else
SIZE_ARMV7_MB="N/A"
fi
if [ -n "$ARMV8_DIGEST" ]; then
SIZE_ARMV8=$(docker buildx imagetools inspect ${CONTAINER_NAME}@${ARMV8_DIGEST} --raw 2>/dev/null | jq '[.layers[].size] | add' 2>/dev/null)
SIZE_ARMV8_MB=$((SIZE_ARMV8 / 1024 / 1024))
else
SIZE_ARMV8_MB="N/A"
fi
# Print summary
echo "=========================================="
echo " BUILD SUMMARY"
echo "=========================================="
echo "Build Time: ${BUILD_MIN}m ${BUILD_SEC}s"
echo "Container Sizes:"
echo " - amd64: ${SIZE_AMD64_MB} MB"
echo " - arm64: ${SIZE_ARM64_MB} MB"
echo " - arm/v7: ${SIZE_ARMV7_MB} MB"
echo " - arm/v8: ${SIZE_ARMV8_MB} MB"
echo "Container Name: ${CONTAINER_NAME}"
echo "Docker Hub: https://hub.docker.com/r/jasonacox/pypowerwall-server"
echo "=========================================="
else
# Exit script if last_path is not "server"
echo "Current directory is not 'server'."
exit 0
fi