AZILS commited on
Commit
3320a80
·
verified ·
1 Parent(s): 0b6caee

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +260 -0
  2. app.py +30 -0
  3. backup.py +63 -0
  4. restore.py +30 -0
Dockerfile ADDED
@@ -0,0 +1,260 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM ubuntu:22.04
2
+
3
+ ENV DEBIAN_FRONTEND=noninteractive
4
+
5
+ # -----------------------------
6
+ # Core tools + Build Essentials
7
+ # -----------------------------
8
+ # Add apt-fast without interactive config
9
+ RUN apt-get update && apt-get install -y software-properties-common \
10
+ && add-apt-repository -y ppa:apt-fast/stable \
11
+ && apt-get update \
12
+ && DEBIAN_FRONTEND=noninteractive \
13
+ apt-get -y install apt-fast \
14
+ && echo "DOWNLOADBEFORE=true" | tee -a /etc/apt-fast.conf \
15
+ && echo "MAXNUM=10" | tee -a /etc/apt-fast.conf \
16
+ && echo "USE_PIGZ=1" | tee -a /etc/apt-fast.conf \
17
+ && echo "APTFAST_PARA_OPTS=(--max-connection-per-server=12 --split=5)" | tee -a /etc/apt-fast.conf
18
+
19
+
20
+ RUN apt-fast update && apt-fast install -y \
21
+ curl wget git git-lfs unzip sudo nano bash apt-fast \
22
+ software-properties-common ca-certificates gnupg \
23
+ build-essential g++ gfortran \
24
+ cmake ninja-build pkg-config \
25
+ llvm clang lld lldb \
26
+ valgrind gdb strace ltrace \
27
+ htop tree jq sqlite3 \
28
+ net-tools iputils-ping \
29
+ rsync git-extras pigz \
30
+ fzf silversearcher-ag ripgrep mingw-w64 \
31
+ && rm -rf /var/lib/apt/lists/*
32
+
33
+ RUN git lfs install
34
+
35
+
36
+ # Add Antigravity repository
37
+ RUN mkdir -p /etc/apt/keyrings && \
38
+ curl -fsSL https://us-central1-apt.pkg.dev/doc/repo-signing-key.gpg | \
39
+ gpg --dearmor --yes -o /etc/apt/keyrings/antigravity-repo-key.gpg && \
40
+ echo "deb [signed-by=/etc/apt/keyrings/antigravity-repo-key.gpg] https://us-central1-apt.pkg.dev/projects/antigravity-auto-updater-dev/ antigravity-debian main" | \
41
+ tee /etc/apt/sources.list.d/antigravity.list > /dev/null
42
+
43
+
44
+ # Install Antigravity
45
+ RUN apt update && \
46
+ apt install -y antigravity
47
+
48
+ # -----------------------------
49
+ # Python 3.12
50
+ # -----------------------------
51
+ RUN add-apt-repository ppa:deadsnakes/ppa -y && apt-fast update
52
+ RUN apt-fast install -y python3.12 python3.12-venv python3.12-dev \
53
+ && curl -sS https://bootstrap.pypa.io/get-pip.py | python3.12
54
+ RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1 \
55
+ && update-alternatives --install /usr/bin/pip pip /usr/local/bin/pip3.12 1
56
+ RUN update-alternatives --set python3 /usr/bin/python3.12
57
+
58
+ # -----------------------------
59
+ # Rust (global)
60
+ # -----------------------------
61
+ RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain stable --no-modify-path \
62
+ && mv /root/.cargo /usr/local/cargo \
63
+ && mv /root/.rustup /usr/local/rustup \
64
+ && ln -s /usr/local/cargo/bin/* /usr/local/bin/ \
65
+ && rm -rf /root/.cargo /root/.rustup
66
+ ENV RUSTUP_HOME=/usr/local/rustup
67
+ ENV CARGO_HOME=/usr/local/cargo
68
+ ENV PATH=/usr/local/cargo/bin:$PATH
69
+
70
+ # -----------------------------
71
+ # Go (latest stable)
72
+ # -----------------------------
73
+ RUN curl -LO https://go.dev/dl/go1.23.0.linux-amd64.tar.gz \
74
+ && tar -C /usr/local -xzf go1.23.0.linux-amd64.tar.gz \
75
+ && rm go1.23.0.linux-amd64.tar.gz
76
+ ENV PATH=/usr/local/go/bin:$PATH
77
+
78
+ # -----------------------------
79
+ # Node.js + Java + .NET
80
+ # -----------------------------
81
+ RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
82
+ && apt-fast install -y nodejs
83
+ RUN apt-fast install -y openjdk-17-jdk
84
+ RUN wget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb \
85
+ && dpkg -i packages-microsoft-prod.deb \
86
+ && rm packages-microsoft-prod.deb \
87
+ && apt-fast update && apt-fast install -y dotnet-sdk-8.0
88
+
89
+ # Yarn + Pnpm build tools
90
+ RUN npm install -g yarn pnpm
91
+
92
+ # -----------------------------
93
+ # Haskell
94
+ # -----------------------------
95
+ RUN apt-fast update && apt-fast install -y ghc cabal-install && rm -rf /var/lib/apt/lists/*
96
+
97
+ # -----------------------------
98
+ # Julia
99
+ # -----------------------------
100
+ RUN JULIA_VERSION=1.11.3 \
101
+ && wget https://julialang-s3.julialang.org/bin/linux/x64/1.11/julia-$JULIA_VERSION-linux-x86_64.tar.gz \
102
+ && tar -xzf julia-$JULIA_VERSION-linux-x86_64.tar.gz -C /opt/ \
103
+ && ln -s /opt/julia-$JULIA_VERSION/bin/julia /usr/local/bin/julia \
104
+ && rm julia-$JULIA_VERSION-linux-x86_64.tar.gz
105
+
106
+ # -----------------------------
107
+ # Scala + sbt
108
+ # -----------------------------
109
+ # Install dependencies
110
+ RUN apt-fast update && apt-fast install -y curl gnupg software-properties-common apt-transport-https && rm -rf /var/lib/apt/lists/*
111
+
112
+ # Add SBT repository and key
113
+ RUN curl -sL "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x99E82A75642AC823" | gpg --dearmor | tee /usr/share/keyrings/sbt.gpg > /dev/null \
114
+ && echo "deb [signed-by=/usr/share/keyrings/sbt.gpg] https://repo.scala-sbt.org/scalasbt/debian all main" | tee /etc/apt/sources.list.d/sbt.list \
115
+ && echo "deb [signed-by=/usr/share/keyrings/sbt.gpg] https://repo.scala-sbt.org/scalasbt/debian /" | tee -a /etc/apt/sources.list.d/sbt.list
116
+
117
+ # Install Scala + SBT
118
+ RUN apt-fast update && apt-fast install -y scala sbt && rm -rf /var/lib/apt/lists/*
119
+
120
+ # -----------------------------
121
+ # PHP + Composer
122
+ # -----------------------------
123
+ RUN apt-fast update && apt-fast install -y php-cli unzip \
124
+ && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && rm -rf /var/lib/apt/lists/*
125
+
126
+ # -----------------------------
127
+ # Ruby
128
+ # -----------------------------
129
+ RUN apt-fast update && apt-fast install -y ruby-full && rm -rf /var/lib/apt/lists/*
130
+
131
+ # -----------------------------
132
+ # code-server
133
+ # -----------------------------
134
+ RUN curl -fsSL https://code-server.dev/install.sh | sh
135
+
136
+ # -----------------------------
137
+ # Workspace + scripts
138
+ # -----------------------------
139
+ WORKDIR /home/vscode
140
+ RUN mkdir -p /home/vscode/workspace
141
+ COPY restore.py /restore.py
142
+ COPY app.py /app.py
143
+ COPY backup.py /home/backup.py
144
+ RUN chmod -R 777 /home
145
+
146
+ # -----------------------------
147
+ # Finishing touches -- you can comment these if not needed
148
+ # -----------------------------
149
+ # pip packages - dev/machine learning/AI
150
+ RUN pip install --upgrade pip setuptools wheel \
151
+ && pip install black flake8 mypy jupyterlab ipython notebook
152
+
153
+ # more pip packages - for Hugging Face Hub
154
+ RUN pip install huggingface_hub huggingface_hub[cli] hf_xet hf_transfer
155
+ ENV HF_HUB_ENABLE_HF_TRANSFER=1
156
+
157
+ # even more pip packages - full-stack ML
158
+ RUN pip install numpy scipy pandas matplotlib seaborn scikit-learn \
159
+ jupyter jupyterlab \
160
+ torch torchvision torchaudio \
161
+ tensorflow keras \
162
+ datasets transformers accelerate
163
+
164
+ # Node tools
165
+ RUN npm install -g typescript ts-node nodemon
166
+
167
+ # Locales to prevent Unicode issues
168
+ RUN apt-fast update && apt-fast install -y locales \
169
+ && locale-gen en_US.UTF-8 \
170
+ && update-locale LANG=en_US.UTF-8
171
+ ENV LANG=en_US.UTF-8
172
+ ENV LANGUAGE=en_US:en
173
+ ENV LC_ALL=en_US.UTF-8
174
+
175
+ # Database Clients
176
+ RUN apt-fast update && apt-fast install -y \
177
+ mysql-client \
178
+ postgresql-client \
179
+ redis-tools \
180
+ gnupg wget ca-certificates \
181
+ && rm -rf /var/lib/apt/lists/*
182
+
183
+ # Install MongoDB Database Tools (mongo, mongodump, etc.)
184
+ RUN wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | gpg --dearmor -o /usr/share/keyrings/mongodb.gpg \
185
+ && echo "deb [signed-by=/usr/share/keyrings/mongodb.gpg] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" \
186
+ | tee /etc/apt/sources.list.d/mongodb-org-7.0.list \
187
+ && apt-fast update && apt-fast install -y mongodb-database-tools \
188
+ && rm -rf /var/lib/apt/lists/*
189
+
190
+ # More build tools
191
+ RUN apt-fast update && apt-fast install -y \
192
+ autoconf automake libtool m4 \
193
+ ninja-build \
194
+ nasm yasm \
195
+ graphviz doxygen && rm -rf /var/lib/apt/lists/*
196
+
197
+ # Productivity + Debug
198
+ RUN apt-fast update && apt-fast install -y \
199
+ tmux screen neovim \
200
+ httpie \
201
+ shellcheck \
202
+ man-db manpages-dev && rm -rf /var/lib/apt/lists/*
203
+
204
+ # Removing cache
205
+ RUN apt-fast clean
206
+ RUN pip cache purge
207
+
208
+ # -----------------------------
209
+ # Disable bash history for all future shells
210
+ # -----------------------------
211
+ RUN echo '\
212
+ unset HISTFILE\n\
213
+ export HISTSIZE=0\n\
214
+ export HISTFILESIZE=0\n\
215
+ export HISTCONTROL=ignoreboth\n\
216
+ export PROMPT_COMMAND="history -r /dev/null"\n\
217
+ ' >> /etc/profile && \
218
+ echo '\
219
+ unset HISTFILE\n\
220
+ export HISTSIZE=0\n\
221
+ export HISTFILESIZE=0\n\
222
+ export HISTCONTROL=ignoreboth\n\
223
+ export PROMPT_COMMAND="history -r /dev/null"\n\
224
+ ' >> /etc/bash.bashrc && \
225
+ rm -f /root/.bash_history && \
226
+ mkdir -p /etc/skel && \
227
+ echo '\
228
+ unset HISTFILE\n\
229
+ export HISTSIZE=0\n\
230
+ export HISTFILESIZE=0\n\
231
+ export HISTCONTROL=ignoreboth\n\
232
+ export PROMPT_COMMAND="history -r /dev/null"\n\
233
+ ' >> /etc/skel/.bashrc
234
+
235
+
236
+ # -----------------------------
237
+ # Create vscode user
238
+ # -----------------------------
239
+ RUN useradd -ms /bin/bash vscode \
240
+ && echo "vscode:vscode" | chpasswd \
241
+ && mkdir -p /home/vscode/.ssh \
242
+ && chown -R vscode:vscode /home/vscode
243
+
244
+ USER vscode
245
+ ENV PATH=$PATH:/home/vscode/.local/bin
246
+
247
+ # -----------------------------
248
+ # Start the restoration step
249
+ # -----------------------------
250
+ RUN mkdir -p /home/vscode/backup
251
+ RUN hf download ThongCoder/vscode-public-data --local-dir /home/vscode --repo-type dataset
252
+ RUN cat /home/vscode/backup/workspace-backup.tar.gz.part-* > /home/vscode/backup/workspace-backup.tar.gz
253
+ RUN tar -xzf /home/vscode/backup/workspace-backup.tar.gz -C /home/vscode/workspace
254
+ RUN rm -rf /home/vscode/backup/*
255
+
256
+ # -----------------------------
257
+ # Entrypoint: restore + code-server
258
+ # -----------------------------
259
+ VOLUME /home/vscode/workspace
260
+ ENTRYPOINT ["python3", "/app.py"]
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+ import threading
4
+
5
+ def start_backup_service():
6
+ proc = subprocess.Popen(
7
+ ["python3", "/backup.py"],
8
+ stdout=subprocess.PIPE,
9
+ stderr=subprocess.STDOUT,
10
+ text=True,
11
+ bufsize=1
12
+ )
13
+ # Stream logs in real-time
14
+ for line in proc.stdout:
15
+ print("[Backup]", line.strip())
16
+
17
+ print("Starting Restorer.")
18
+ subprocess.run(["python3", "/restore.py"], check=True)
19
+ print("Finished.")
20
+
21
+ print("Starting Auto-Backup Service.")
22
+ thr = threading.Thread(target=start_backup_service, daemon=True)
23
+ thr.start()
24
+
25
+ print("Starting Coder server.")
26
+ subprocess.run([
27
+ "code-server",
28
+ "--bind-addr", "0.0.0.0:7860",
29
+ "/home/vscode/workspace"
30
+ ])
backup.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+ import subprocess
4
+ import datetime
5
+
6
+ BACKUP_REPO = os.environ.get("BACKUP_REPO")
7
+ HF_TOKEN = os.environ.get("HF_TOKEN")
8
+
9
+ def run_backup():
10
+ env = os.environ.copy()
11
+ env["HF_HOME"] = "/tmp/hf_cache"
12
+ env["XDG_CACHE_HOME"] = "/tmp/xdg_cache"
13
+ env["TMPDIR"] = "/tmp"
14
+ env["HF_TOKEN"] = HF_TOKEN
15
+
16
+ os.makedirs(env["HF_HOME"], exist_ok=True)
17
+ os.makedirs(env["XDG_CACHE_HOME"], exist_ok=True)
18
+ os.makedirs(env["TMPDIR"], exist_ok=True)
19
+
20
+ local_path = "/home/vscode/workspace"
21
+
22
+ # --- STEP 1: Delete old files ---
23
+ print("[Backup] Deleting old files...")
24
+ cmd = [
25
+ "hf", "repo-files", "delete", BACKUP_REPO, "workspace/",
26
+ "--repo-type", "dataset",
27
+ ]
28
+ process = subprocess.Popen(
29
+ cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
30
+ env=env, text=True, cwd="/tmp"
31
+ )
32
+ for line in iter(process.stdout.readline, ""):
33
+ print("[Backup]", line.strip())
34
+ process.wait()
35
+
36
+ # --- STEP 2: Upload new backup ---
37
+ print("[Backup] Uploading workspace...")
38
+ cmd = [
39
+ "hf", "upload", BACKUP_REPO, local_path, "workspace/",
40
+ "--repo-type", "dataset",
41
+ ]
42
+ process = subprocess.Popen(
43
+ cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
44
+ env=env, text=True, cwd="/tmp"
45
+ )
46
+ for line in iter(process.stdout.readline, ""):
47
+ print("[Backup]", line.strip())
48
+ process.wait()
49
+
50
+ print("[Backup] Completed at", datetime.datetime.utcnow().isoformat(), "UTC")
51
+
52
+ if __name__ == "__main__":
53
+ if not BACKUP_REPO or not HF_TOKEN:
54
+ print("[Backup] BACKUP_REPO or HF_TOKEN not set! Please set it in your Spaces secrets to use the Backup Service.")
55
+ exit(0)
56
+
57
+ while True:
58
+ print("[Backup] Backup started.")
59
+ try:
60
+ run_backup()
61
+ except Exception as e:
62
+ print("[Backup] Failed:", e)
63
+ time.sleep(45 * 60) # 45 minutes
restore.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+ import tempfile
4
+ import shutil
5
+
6
+ BACKUP_REPO = os.environ.get("BACKUP_REPO")
7
+ HF_TOKEN = os.environ.get("HF_TOKEN")
8
+
9
+ if not BACKUP_REPO or not HF_TOKEN:
10
+ print("[Restore] Skipping: BACKUP_REPO or HF_TOKEN not set")
11
+ exit(0)
12
+
13
+ env = os.environ.copy()
14
+ env["HF_HOME"] = "/tmp/hf_cache"
15
+ env["XDG_CACHE_HOME"] = "/tmp/xdg_cache"
16
+ env["TMPDIR"] = "/tmp"
17
+ env["HF_TOKEN"] = HF_TOKEN
18
+
19
+ os.makedirs(env["HF_HOME"], exist_ok=True)
20
+ os.makedirs(env["XDG_CACHE_HOME"], exist_ok=True)
21
+ os.makedirs(env["TMPDIR"], exist_ok=True)
22
+
23
+ subprocess.run(
24
+ ["hf", "download", BACKUP_REPO, "--repo-type", "dataset", "--local-dir", "/home/vscode", "--force", "--exclude", ".gitattributes", "--exclude", "*.md"],
25
+ check=True,
26
+ env=env,
27
+ )
28
+
29
+
30
+ print("[Restore] Completed")