WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit cbc6e03

Browse files
committed
Move public cloud scripts and implement Azure uploads
Signed-off-by: Dimitris Karakasilis <[email protected]>
1 parent d9c6be2 commit cbc6e03

File tree

7 files changed

+283
-2
lines changed

7 files changed

+283
-2
lines changed
File renamed without changes.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
AZURE_GALLERY_NAME="kairos.io"
6+
AZURE_IMAGE_DEFINITION="kairos"
7+
# Use same variables defined in the main upload script
8+
# AZURE_RESOURCE_GROUP and STORAGE_REGION should already be set in the environment
9+
10+
getAllVersions() {
11+
az sig image-version list \
12+
--resource-group "$AZURE_RESOURCE_GROUP" \
13+
--gallery-name "$AZURE_GALLERY_NAME" \
14+
--gallery-image-definition "$AZURE_IMAGE_DEFINITION" \
15+
--query "[].name" \
16+
--output tsv
17+
}
18+
19+
deleteVersion() {
20+
local version=$1
21+
echo "Deleting old image version: $version"
22+
az sig image-version delete \
23+
--resource-group "$AZURE_RESOURCE_GROUP" \
24+
--gallery-name "$AZURE_GALLERY_NAME" \
25+
--gallery-image-definition "$AZURE_IMAGE_DEFINITION" \
26+
--gallery-image-version "$version"
27+
}
28+
29+
cleanupOldVersions() {
30+
echo "Fetching all image versions..."
31+
mapfile -t allVersions < <(getAllVersions)
32+
33+
if (( ${#allVersions[@]} <= 4 )); then
34+
echo "4 or fewer image versions found. No cleanup needed."
35+
return
36+
fi
37+
38+
echo "Sorting versions..."
39+
mapfile -t sortedVersions < <(printf "%s\n" "${allVersions[@]}" | sort -V -r)
40+
41+
echo "Keeping latest 4 versions:" "${sortedVersions[@]:0:4}"
42+
oldVersions=("${sortedVersions[@]:4}")
43+
44+
for version in "${oldVersions[@]}"; do
45+
deleteVersion "$version"
46+
done
47+
}
48+
49+
cleanupOldVersions
File renamed without changes.
File renamed without changes.
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#!/bin/bash
2+
3+
set -e
4+
set -o pipefail
5+
6+
# https://github.com/Azure/login?tab=readme-ov-file#azure-login-action
7+
export AZURE_CORE_OUTPUT=none
8+
9+
checkArguments() {
10+
if [ $# -lt 2 ]; then
11+
echo "Error: You need to specify the cloud image to upload and the Kairos version (to tag resources)."
12+
echo "Usage: $0 <cloud-image> <kairos-version>"
13+
exit 1
14+
fi
15+
16+
local file="$1"
17+
18+
if [ ! -f "$file" ]; then
19+
echo "Error: File '$file' does not exist."
20+
exit 1
21+
fi
22+
}
23+
24+
checkEnvVars() {
25+
if [ -z "$AZURE_RESOURCE_GROUP" ] || [ -z "$AZURE_STORAGE_ACCOUNT" ] || [ -z "$AZURE_CONTAINER_NAME" ]; then
26+
echo "Error: AZURE_RESOURCE_GROUP, AZURE_STORAGE_ACCOUNT and AZURE_CONTAINER_NAME environment variables must be set."
27+
exit 1
28+
fi
29+
}
30+
31+
LOCAL_VHD_PATH=$(readlink -f "$1")
32+
NAME=$(basename "$LOCAL_VHD_PATH" | sed 's/\.raw\.vhd$//') # just the file name without extension or path
33+
VERSION=$2
34+
35+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
36+
# shellcheck source=/dev/null
37+
source "$SCRIPT_DIR/cleanup-old-images-azure.sh"
38+
39+
checkArguments "$@"
40+
checkEnvVars
41+
42+
# === CHECK AZURE LOGIN ===
43+
az account show >/dev/null 2>&1 || { echo "Please run 'az login' first."; exit 1; }
44+
45+
# === GET STORAGE ACCOUNT REGION ===
46+
echo "Fetching storage account region..."
47+
STORAGE_REGION=$(az storage account show --name "$AZURE_STORAGE_ACCOUNT" --query "primaryLocation" --output tsv)
48+
echo "Storage account is in region: $STORAGE_REGION"
49+
50+
# === GET STORAGE ACCOUNT KEY ===
51+
echo "Fetching storage account key..."
52+
STORAGE_KEY=$(az storage account keys list --account-name "$AZURE_STORAGE_ACCOUNT" --query "[0].value" --output tsv)
53+
54+
echo "Uploading VHD file ($LOCAL_VHD_PATH) to Azure Storage..."
55+
az storage blob upload --account-name "$AZURE_STORAGE_ACCOUNT" --container-name "$AZURE_CONTAINER_NAME" --type page \
56+
--name "$NAME" --file "$LOCAL_VHD_PATH" --auth-mode key --account-key "$STORAGE_KEY" --overwrite true
57+
58+
echo "Retrieving uploaded VHD URL..."
59+
VHD_URL=$(az storage blob url --account-name "$AZURE_STORAGE_ACCOUNT" --container-name "$AZURE_CONTAINER_NAME" --name "$NAME" --output tsv)
60+
echo "VHD uploaded successfully: $VHD_URL"
61+
62+
# === Get file size in bytes ===
63+
VHD_SIZE_BYTES=$(stat -c %s "$LOCAL_VHD_PATH")
64+
65+
# === Convert to GB and round up ===
66+
VHD_SIZE_GB=$(( (VHD_SIZE_BYTES + 1073741823) / 1073741824 ))
67+
echo "Calculated disk size: $VHD_SIZE_GB GB"
68+
69+
echo "Creating a managed disk"
70+
az disk create \
71+
--resource-group "$AZURE_RESOURCE_GROUP" \
72+
--name "$NAME" \
73+
--source "$VHD_URL" \
74+
--os-type Linux \
75+
--sku Premium_LRS \
76+
--size-gb "$VHD_SIZE_GB" \
77+
--hyper-v-generation V2
78+
79+
echo "Getting the ID of the managed disk"
80+
DISK_ID=$(az disk show \
81+
--resource-group "$AZURE_RESOURCE_GROUP" \
82+
--name "$NAME" \
83+
--query "id" \
84+
--output tsv)
85+
86+
# === CREATE AZURE IMAGE IN SAME REGION AS STORAGE ACCOUNT ===
87+
echo "Creating Azure image ($NAME) from VHD in region: $STORAGE_REGION..."
88+
az image create \
89+
--resource-group "$AZURE_RESOURCE_GROUP" \
90+
--name "$NAME" \
91+
--os-type "Linux" \
92+
--source "$DISK_ID" \
93+
--hyper-v-generation "V2" \
94+
--location "$STORAGE_REGION"
95+
echo "Image created successfully: $NAME"
96+
97+
echo "Getting the image ID"
98+
IMAGE_ID=$(az image show \
99+
--resource-group "$AZURE_RESOURCE_GROUP" \
100+
--name "$NAME" \
101+
--query "id" \
102+
--output tsv)
103+
104+
# echo "Creating a Shared Image Gallery (one-off)"
105+
# # https://learn.microsoft.com/en-us/azure/virtual-machines/create-gallery?tabs=portal%2Cportaldirect%2Ccli2
106+
# # TODO: Link to some EULA?
107+
# az sig create \
108+
# --gallery-name kairos.io \
109+
# --permissions community \
110+
# --resource-group "$AZURE_RESOURCE_GROUP" \
111+
# --location "$STORAGE_REGION" \
112+
# --publisher-uri kairos.io \
113+
# --publisher-email [email protected] \
114+
# --eula https://github.com/kairos-io/kairos/?tab=Apache-2.0-1-ov-file#readme \
115+
# --public-name-prefix kairos
116+
117+
# echo "Creating an image definition (one-off)"
118+
# az sig image-definition create --resource-group "$AZURE_RESOURCE_GROUP" --gallery-name kairos.io \
119+
# --gallery-image-definition kairos --publisher kairos.io --offer kairos --sku kairos \
120+
# --hyper-v-generation "V2" --os-type Linux
121+
#
122+
# echo "Making the gallery public (one-off)"
123+
# az sig share enable-community --resource-group "$AZURE_RESOURCE_GROUP" --gallery-name kairos.io
124+
125+
echo "Creating a Shared image version"
126+
az sig image-version create --resource-group "$AZURE_RESOURCE_GROUP" --gallery-name kairos.io \
127+
--gallery-image-definition "kairos" --gallery-image-version "${VERSION#v}" \
128+
--managed-image "$IMAGE_ID" --location "$STORAGE_REGION"
129+
130+
echo "Deleting the managed disk"
131+
az disk delete \
132+
--resource-group "$AZURE_RESOURCE_GROUP" \
133+
--name "$NAME" \
134+
--yes
135+
136+
echo "Deleting managed image (no longer needed)"
137+
az image delete \
138+
--resource-group "$AZURE_RESOURCE_GROUP" \
139+
--name "$NAME"
140+
141+
echo "Deleting uploaded VHD blob from Azure Storage..."
142+
az storage blob delete \
143+
--account-name "$AZURE_STORAGE_ACCOUNT" \
144+
--container-name "$AZURE_CONTAINER_NAME" \
145+
--name "$NAME" \
146+
--auth-mode key \
147+
--account-key "$STORAGE_KEY"
File renamed without changes.

.github/workflows/upload-cloud-images.yaml

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ jobs:
9292
qemu-img resize -f raw disk.raw "$ROUNDED_SIZE"G
9393
tar --format=oldgnu -czvf "${file%.*}.tar.gz" disk.raw
9494
95-
.github/upload-image-to-gcp.sh $(ls *.tar.gz) "$latestTag"
95+
.github/public-cloud/upload-image-to-gcp.sh $(ls *.tar.gz) "$latestTag"
9696
9797
# https://docs.github.com/en/actions/security-for-github-actions/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services
9898
upload-aws:
@@ -153,4 +153,89 @@ jobs:
153153
--set "disk.raw=true" \
154154
--set "state_dir=/aurora"
155155
156-
.github/upload-image-to-aws.sh $(ls *.raw) "$latestTag"
156+
.github/public-cloud/upload-image-to-aws.sh $(ls *.raw) "$latestTag"
157+
158+
upload-azure:
159+
permissions:
160+
id-token: write
161+
name: Upload to Azure
162+
runs-on: ubuntu-latest
163+
# https://learn.microsoft.com/en-us/entra/workload-id/workload-identity-federation-create-trust?pivots=identity-wif-apps-methods-azp#github-actions
164+
environment: azure-push
165+
outputs:
166+
shouldBuild: ${{ steps.checkPushed.outputs.shouldBuild }}
167+
steps:
168+
- name: "Checkout code"
169+
uses: actions/checkout@v4
170+
with:
171+
persist-credentials: false
172+
- run: |
173+
git fetch --prune --unshallow
174+
# https://github.com/Azure/login?tab=readme-ov-file#azure-login-action
175+
- name: Azure login
176+
uses: azure/login@v2
177+
with:
178+
client-id: ${{ secrets.AZURE_CLIENT_ID }}
179+
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
180+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
181+
182+
- name: Find latest stable version
183+
run: |
184+
# Azure only allows "stable" version strings. E.g. "v1.2.3" (not "v1.2.3-beta1")
185+
latestTag=$(git tag --list | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n1)
186+
echo $latestTag > LATEST_TAG
187+
188+
- name: Check if already pushed
189+
id: checkPushed
190+
run: |
191+
latestTag=$(cat LATEST_TAG)
192+
193+
echo "Fetching all pushed versions"
194+
mapfile -t kairosVersions < <(az sig image-version list --resource-group kairos-cloud-images --gallery-image-name kairos --gallery-name kairos.io --query '[].name' --output tsv)
195+
196+
echo "Checking if '$latestTag' is already pushed"
197+
echo "Looking among versions: ${kairosVersions[@]}"
198+
for version in "${kairosVersions[@]}"; do
199+
if [[ $version == "${latestTag#v}" ]]; then
200+
stableVersions+=("$version")
201+
alreadyPushed=true
202+
break
203+
fi
204+
done
205+
206+
if [[ "$alreadyPushed" = true && "${{ inputs.force }}" != "true" ]]; then
207+
echo "shouldBuild=false" >> $GITHUB_OUTPUT
208+
echo "Image for $latestTag is already pushed and 'force' wasn't true. Skipping build."
209+
else
210+
echo "shouldBuild=true" >> $GITHUB_OUTPUT
211+
echo "Image for $latestTag is not pushed or 'force' was true. Will build."
212+
fi
213+
214+
- name: Build the image
215+
if: ${{ steps.checkPushed.outputs.shouldBuild == 'true' }}
216+
run: |
217+
latestTag=$(cat LATEST_TAG)
218+
containerImage="quay.io/kairos/ubuntu:24.04-core-amd64-generic-${latestTag}"
219+
docker run -v /var/run/docker.sock:/var/run/docker.sock --net host \
220+
--privileged \
221+
-v $PWD:/aurora --rm quay.io/kairos/auroraboot \
222+
--debug \
223+
--set "disable_http_server=true" \
224+
--set "container_image=docker:${containerImage}" \
225+
--set "disable_netboot=true" \
226+
--set "disk.vhd=true" \
227+
--set "state_dir=/aurora"
228+
229+
- name: Azure CLI script
230+
uses: azure/cli@v2
231+
if: ${{ steps.checkPushed.outputs.shouldBuild == 'true' }}
232+
env:
233+
GCP_PROJECT: palette-kairos
234+
AZURE_RESOURCE_GROUP: "kairos-cloud-images"
235+
AZURE_STORAGE_ACCOUNT: "kairoscloudimages"
236+
AZURE_CONTAINER_NAME: "kairos-cloud-images"
237+
with:
238+
azcliversion: latest
239+
inlineScript: |
240+
latestTag=$(cat LATEST_TAG)
241+
.github/public-cloud/upload-image-to-azure.sh $(ls *.vhd) "$latestTag"

0 commit comments

Comments
 (0)