-
-
Notifications
You must be signed in to change notification settings - Fork 33
311 lines (285 loc) · 12.8 KB
/
create-wsl-image.yml
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
name: Build docker image and export for WSL
on:
schedule:
- cron: '0 2 * * *' # Run every day
workflow_dispatch:
jobs:
build-and-export:
runs-on: ubuntu-latest
outputs:
upload_url: ${{ steps.create_release.outputs.upload_url }}
version: ${{ steps.extract_version.outputs.version }}
steps:
# Create WSL export (a tar file) of the minimal Sage install
- name: Prepare Dockerfile
run: |
curl -o Dockerfile https://raw.githubusercontent.com/sagemath/sage-binder-env/master/Dockerfile
sed -i '/COPY notebooks/d' Dockerfile # remove the line from Dockerfile
- name: Extract version from Dockerfile
id: extract_version
run: |
VERSION=$(grep '^FROM ghcr.io/sagemath/sage-binder-env:' Dockerfile | sed 's/^FROM ghcr.io\/sagemath\/sage-binder-env://')
echo "VERSION=$VERSION" >> $GITHUB_ENV
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Build and export Docker image
run: |
docker build -t sagemath-container .
export CONTAINER_ID=$(docker create sagemath-container)
docker export $CONTAINER_ID -o sagemath-$VERSION-wsl.tar
- name: Upload tar file as artifact
id: upload-artifact
uses: actions/upload-artifact@v4
with:
name: sagemath-${{ env.VERSION }}-wsl
path: sagemath-${{ env.VERSION }}-wsl.tar
# Make a release
- name: Get latest release version
id: get_latest_release
run: |
LATEST_VERSION=$(curl -s https://api.github.com/repos/${{ github.repository }}/releases/latest | jq -r '.tag_name // "0.0.0"' | sed 's/v//')
echo "LATEST_VERSION=$LATEST_VERSION" >> $GITHUB_ENV
- name: Zip the tar file
if: ${{ env.VERSION != env.LATEST_VERSION }}
run: |
zip sagemath-${{ env.VERSION }}-wsl.zip sagemath-${{ env.VERSION }}-wsl.tar
- name: Create GitHub Release
if: ${{ env.VERSION != env.LATEST_VERSION }}
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: v${{ env.VERSION }}
release_name: Run SageMath ${{ env.VERSION }} on WSL
body: |
We recommend you check if WSL is enabled before running the installer.
The .ps1 installer downloads the .zip file and imports a minimized SageMath ${{ env.VERSION }} into WSL.
After downloading the installer, right-click it to open the context menu and run it with PowerShell.
You may also open the installer as a text file, and copy and paste all lines into a PowerShell window.
The installer should create two shortcuts on your desktop: one for launching SageMath
and another to start the Jupyter server for SageMath.
The minimized SageMath is the same as the one used in the Binder environment, meaning no bells and whistles.
Notably the Sage build system is not included.
draft: false
prerelease: false
- name: Upload release asset
if: ${{ env.VERSION != env.LATEST_VERSION }}
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./sagemath-${{ env.VERSION }}-wsl.zip
asset_name: sagemath-${{ env.VERSION }}-wsl.zip
asset_content_type: application/zip
# Generate a Windows PowerShell script
- name: Checkout repo
uses: actions/checkout@v3
- name: Generate PowerShell Script
run: |
cat << EOF > download_and_import_sagemath_to_wsl.ps1
# ----------------------------------------------------
# Copy and paste all lines into the Windows PowerShell
# ----------------------------------------------------
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
try {
Write-Host "Checking WSL installation."
wsl --install --no-distribution
if (\$LASTEXITCODE -ne 0) {
Write-Host "WSL installation failed."
Read-Host -Prompt "Press any key to exit"; exit
}
} catch {
Write-Host "Could not check WSL installation, proceeding with fingers crossed."
}
# Check if SageMath already exists in WSL
\$sagemathWSL = "SageMath-$VERSION"
if ((wsl -l -q) -split 'n' -contains \$sagemathWSL) {
# Ask user if they want to reinstall SageMath
\$response = Read-Host "\$sagemathWSL is already installed. Do you want to reinstall it? (y/n)"
if (\$response -eq "y") {
Write-Host "Uninstalling \$sagemathWSL."
# Unregister the existing SageMath WSL distribution
wsl --unregister \$sagemathWSL
} else {
Read-Host "OK. Press any key to exit"; exit
}
}
\$artifactUrl = "https://github.com/sagemath/sage-binder-env/releases/download/v$VERSION/sagemath-$VERSION-wsl.zip"
\$zipFilePath = "\$PWD\\artifact.zip"
\$dataPath = "\$HOME\\AppData\\Local\\SageMath"
\$tarFilePath = "\$dataPath\\sagemath-$VERSION-wsl.tar"
# Ensure the path exists
if (-Not (Test-Path \$dataPath)) { New-Item -Path \$dataPath -ItemType Directory > \$null }
# Skip downloading and extracting if the tar file already exists
if (-Not (Test-Path \$tarFilePath)) {
Write-Host "Downloading \$sagemathWSL..."
Start-BitsTransfer -Source \$artifactUrl -Destination \$zipFilePath
Write-Host "Extracting..."
Expand-Archive -Path \$zipFilePath -DestinationPath \$dataPath -Force
if (Test-Path \$tarFilePath) { Remove-Item \$zipFilePath }
} else {
Write-Host "\$sagemathWSL was already downloaded."
}
# Import the WSL image
Write-Host "Start importing..."
wsl --import SageMath-$VERSION \$dataPath \$tarFilePath
# Check if importing succeeded
if (-Not ((wsl -l -q) -split 'n' -contains \$sagemathWSL)) {
Write-Host "Importing \$sagemathWSL into WSL failed."
Write-Host "If WSL was freshly installed by this installer, try again after rebooting the computer."
Read-Host "Press any key to exit"; exit
}
Write-Host "Creating shortcults at:"
# Download icons for shortcuts
\$iconUrlOrange = "https://raw.githubusercontent.com/sagemath/sage-binder-env/refs/heads/master/.github/icons/orange.ico"
\$iconUrlBlue = "https://raw.githubusercontent.com/sagemath/sage-binder-env/refs/heads/master/.github/icons/blue.ico"
\$iconPathOrange = "\$dataPath\\orange.ico"
\$iconPathBlue = "\$dataPath\\blue.ico"
# Download the icon if it doesn't exist
if (-Not (Test-Path \$iconPathBlue)) {
Start-BitsTransfer -Source \$iconUrlBlue -Destination \$iconPathBlue
}
if (-Not (Test-Path \$iconPathOrange)) {
Start-BitsTransfer -Source \$iconUrlOrange -Destination \$iconPathOrange
}
# Create shortcuts
\$ShortcutFile = "\$([Environment]::GetFolderPath('Desktop'))\\Run Sage.lnk"
\$TargetPath = "C:\\Windows\\System32\\wsl.exe"
\$Arguments = "-d \$sagemathWSL --user user --cd ~ /sage/sage"
\$WScriptShell = New-Object -ComObject WScript.Shell
\$Shortcut = \$WScriptShell.CreateShortcut(\$ShortcutFile)
\$Shortcut.TargetPath = \$TargetPath
\$Shortcut.Arguments = \$Arguments
\$Shortcut.IconLocation = \$iconPathBlue
\$Shortcut.Save()
Write-Host "\$ShortcutFile"
# Create a shortcut that starts Jupyter on Desktop
\$ShortcutFile = "\$([Environment]::GetFolderPath('Desktop'))\\Run Jupyter.lnk"
\$TargetPath = "C:\\Windows\\System32\\wsl.exe"
\$Arguments = "-d \$sagemathWSL --user user --cd ~ jupyter lab --no-browser"
\$WScriptShell = New-Object -ComObject WScript.Shell
\$Shortcut = \$WScriptShell.CreateShortcut(\$ShortcutFile)
\$Shortcut.TargetPath = \$TargetPath
\$Shortcut.Arguments = \$Arguments
\$Shortcut.IconLocation = \$iconPathOrange
\$Shortcut.Save()
Write-Host "\$ShortcutFile"
Read-Host "\`n\$sagemathWSL installation succeeded!\`nPress any key to exit"
EOF
cat << EOF > remove_sagemath_from_wsl.ps1
# ----------------------------------------------------
# Copy and paste all lines into the Windows PowerShell
# ----------------------------------------------------
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
try {
Write-Host "Checking WSL installation."
wsl --install --no-distribution
if (\$LASTEXITCODE -ne 0) {
Write-Host "WSL installation failed."
Read-Host -Prompt "Press any key to exit"; exit
}
} catch {
Write-Host "Could not check WSL installation, proceeding with fingers crossed."
}
# Check if SageMath already exists in WSL
\$sagemathWSL = "SageMath-$VERSION"
\$dataPath = "\$HOME\\AppData\\Local\\SageMath"
\$iconPathOrange = "\$dataPath\\orange.ico"
\$iconPathBlue = "\$dataPath\\blue.ico"
if ((wsl -l -q) -split 'n' -contains \$sagemathWSL) {
# Ask user if they want to reinstall SageMath
\$response = Read-Host "Do you want to uninstall \$(\$sagemathWSL) from WSL? (y/n)"
if (\$response -eq "y") {
Write-Host "Uninstalling \$sagemathWSL..."
try {
wsl --unregister \$sagemathWSL
} catch {
Write-Host "Failed: wsl --unregister \$sagemathWSL"
}
try {
rm -recurse \$dataPath
} catch {
Write-Host "Failed: rm -recurse \$dataPath"
}
try {
\$ShortcutFile = "\$([Environment]::GetFolderPath('Desktop'))\\Run Sage.lnk"
rm \$ShortcutFile
} catch {
Write-Host "Failed: rm \$ShortcutFile"
}
try {
\$ShortcutFile = "\$([Environment]::GetFolderPath('Desktop'))\\Run Jupyter.lnk"
rm \$ShortcutFile
} catch {
Write-Host "Failed: rm \$ShortcutFile"
}
} else {
Read-Host "OK. Press any key to exit"; exit
}
} else {
Write-Host "\$sagemathWSL is not found."
Read-Host -Prompt "Press any key to exit"; exit
}
Read-Host "\`n\$sagemathWSL uninstallation succeeded!\`nPress any key to exit"
EOF
shell: bash
- name: Upload installation script as artifact
uses: actions/upload-artifact@v4
with:
name: download_and_import_sagemath_to_wsl
path: download_and_import_sagemath_to_wsl.ps1
- name: Upload uninstallation script as artifact
uses: actions/upload-artifact@v4
with:
name: remove_sagemath_from_wsl
path: remove_sagemath_from_wsl.ps1
- name: Upload installation script as release asset
if: ${{ env.VERSION != env.LATEST_VERSION }}
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./download_and_import_sagemath_to_wsl.ps1
asset_name: sagemath-${{ env.VERSION }}-wsl-installer.ps1
asset_content_type: application/octet-stream
- name: Upload uninstallation script as release asset
if: ${{ env.VERSION != env.LATEST_VERSION }}
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./remove_sagemath_from_wsl.ps1
asset_name: sagemath-${{ env.VERSION }}-wsl-uninstaller.ps1
asset_content_type: application/octet-stream
# build-exe:
# runs-on: windows-latest
# needs: build-and-export
# if: ${{ contains(needs.build-and-export.outputs.upload_url || '', 'http') }}
# steps:
# - name: Checkout repository
# uses: actions/checkout@v3
#
# - name: Download PowerShell Script artifact
# uses: actions/download-artifact@v4
# with:
# name: download_and_import_sagemath_to_wsl
#
# - name: Install PS2EXE
# run: |
# Install-Module -Name ps2exe -Force
# Import-Module ps2exe
# Invoke-ps2exe -inputFile .\download_and_import_sagemath_to_wsl.ps1 -outputFile .\sagemath_installer.exe
# shell: powershell
#
# - name: Upload EXE to GitHub Release
# uses: actions/upload-release-asset@v1
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# with:
# upload_url: ${{ needs.build-and-export.outputs.upload_url }}
# asset_path: ./sagemath_installer.exe
# asset_name: sagemath-${{ needs.build-and-export.outputs.version }}-wsl-installer.exe
# asset_content_type: application/octet-stream