Skip to content

Commit a508d19

Browse files
committed
mod: wsl-prep,common,registry> cleaning code
1 parent 623c4c6 commit a508d19

11 files changed

Lines changed: 143 additions & 119 deletions

File tree

common/handler.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ handler() {
2020
if _out=$(eval "$_cmd 2>&1"); then
2121
success "[ finished ]: $_description... [${_cmd}]" "${_lvl}"
2222
else
23-
error "[ failed ]: $_description... [${_cmd}] :-{\n\t'${_out}'" "${_lvl}"
23+
error "[ failed ]: $_description... [${_cmd}] :-{\n\t${_out}" "${_lvl}"
2424
return 1
2525
fi
2626
}

dev-prep/git/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Tools for working with git or for git

dev-prep/git/tools.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env sh
2+
set -eu
3+
set +u; [ -n "$ROOT" ] || export ROOT="$(cd -- "$(dirname -- "$0")/.." && pwd)"; set -u;
4+
. "${ROOT}/common/colors.sh"
5+
6+
7+
# rewrite all E-mail of author whole git history (for all commits)
8+
# rewriteAuthorEmail 'old@email' 'new_username' 'new_email'
9+
rewriteAuthorEmail() {
10+
# Validate exactly 3 arguments
11+
case $# in
12+
3) ;;
13+
*) error "Usage: $0 old@email newname new@email" >&2; exit 1 ;;
14+
esac
15+
16+
_OLD_EMAIL="${1}"
17+
_CORRECT_NAME="${2}"
18+
_CORRECT_EMAIL="${3}"
19+
20+
info "Rewriting: ${_OLD_EMAIL}${_CORRECT_NAME} <${_CORRECT_EMAIL}>"
21+
22+
# git filter-branch env-filter (POSIX sh inside)
23+
git filter-branch -f --env-filter "
24+
if [ \"\$GIT_COMMITTER_EMAIL\" = '${_OLD_EMAIL}' ]; then
25+
GIT_COMMITTER_NAME='${_CORRECT_NAME}'
26+
GIT_COMMITTER_EMAIL='${_CORRECT_EMAIL}'
27+
export GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL
28+
fi
29+
if [ \"\$GIT_AUTHOR_EMAIL\" = '${_OLD_EMAIL}' ]; then
30+
GIT_AUTHOR_NAME='${_CORRECT_NAME}'
31+
GIT_AUTHOR_EMAIL='${_CORRECT_EMAIL}'
32+
export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
33+
fi
34+
" --tag-name-filter cat -- --branches --tags
35+
36+
rm -rf .git/refs/original/
37+
git reflog expire --expire=now --all
38+
git gc --prune=now --aggressive
39+
40+
ask 'Local rewrite complete.\nForce push? [y/N]: '
41+
read -r response
42+
case "${response}" in
43+
[yY]|[yY][eE][sS])
44+
git push origin --force --all
45+
git push origin --force --tags
46+
success 'Force pushed'
47+
;;
48+
*) error 'Run: git push origin --force --all --tags' ;;
49+
esac
50+
}

registry/docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ services:
22
# APT Cache (Debian/Ubuntu/Alpine/etc)
33
# https://qmacro.org/blog/posts/2024/09/03/setting-up-a-cache-server-for-apt-packages/
44
registry_apt:
5-
# image: sameersbn/apt-cacher-ng
65
build:
76
context: ./acng
87
dockerfile: Dockerfile
98
args:
109
- PUID=${PUID:-1000}
1110
- PGID=${PGID:-1000}
1211
- TZ=Europe/Moscow
12+
- APT_PROXY_URL=${APT_PROXY_URL}
1313
container_name: registry_apt
1414
restart: unless-stopped
1515
ports:
@@ -116,7 +116,7 @@ volumes:
116116
driver: local
117117
driver_opts:
118118
type: none
119-
device: ${WSL_VLM}/apt_cache
119+
device: ${HST_VLM}/apt_cache
120120
o: bind
121121
verdaccio_storage:
122122
# external: true

wsl-prep/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,19 @@ and then:
1919
```
2020

2121
Each time in the console you can see some warnings for you to do smth... some manual actions (restart WSL or smth else) - pls, do it and go ahead.
22+
23+
## Read it to advance
24+
25+
- https://learn.microsoft.com/ru-ru/windows/wsl/use-custom-distro?source=recommendations
26+
27+
## Hyper-V + WSL Network
28+
29+
- **hyperv-wsl-network.ps1** script to get all current network configuration to reproduce it in another Win-host.
30+
31+
## Structure
32+
33+
- [win](./win/README.md) to works with WSL (add a new WSL machine, clone or del)
34+
- [wsl-files](wsl-files/) configs and some scripts from /etc, /usr, ... to copy-paste into a new system
35+
- [wsl-nat](wsl-nat/) scripts to configure as a NAT your WSL-Ubuntu system
36+
- [getter cur network conf](get-hyperv-wsl-network.ps1) to backup it
37+
- [setter network conf](set-hyperv-wsl-network.ps1) for your win-host to preset an Internet saccess for Hyper-V and WSL

wsl-prep/del/sources.list

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

wsl-prep/del/sources.list.distUpgrade

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

wsl-prep/run.sh

100755100644
Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ set +u; [ -n "$ROOT" ] || export ROOT="$(cd -- "$(dirname -- "$0")/.." && pwd)";
1313
. "${ROOT}/common/handler.sh"
1414

1515
_USERPROFILE=$(wslpath "$(cmd.exe /c "echo %USERPROFILE%" 2>/dev/null)" | tr -d '\r')
16-
16+
_lvl=5
17+
_UID=$(id -u)
1718

1819
fixnetwork() {
1920
ping ya.ru -A -c 3 >/dev/null || { echo 'fixing...'; echo "nameserver 9.9.9.9" | sudo tee /etc/resolv.conf; }
@@ -151,17 +152,24 @@ upgrade() {
151152
reset_wo_reboot
152153
progress_bar 80 "Обновление завершено"
153154

154-
warning "Please reboot this system ('wsl --shutdown' in powershell) \n AND restart this _cmd: `./ubuntu_upgrade.sh upgrade`"
155+
warning "Please reboot this system ('wsl --shutdown' in powershell) \n AND restart this _cmd: './ubuntu_upgrade.sh upgrade'"
155156
fi
156157
}
157158

158159
git_prep() {
159-
info "Check and generate ssh keys for git" 20
160+
# region git_prep() {
161+
info "Check and generate ssh keys for git"
160162
mkdir -p ~/.ssh
161163
. "${ROOT}/secrets/secrets.sh"
162-
([ -d "${ROOT}/secrets" ] && cp -r "${ROOT}/secrets/.ssh/*" ~/.ssh/) || ssh-keygen -t rsa -b 4096 -C "${useremail}" -N "" -f ~/.ssh/github;
164+
([ -d "${ROOT}/secrets" ] && cp -rf "${ROOT}/secrets/.ssh/"* ~/.ssh/) \
165+
|| ( echo 'n' | ssh-keygen -t rsa -b 4096 -C "${useremail}" -N "" -f ~/.ssh/github)
166+
sudo chown -R ${_UID}:${_UID} ~/.ssh
167+
chmod 600 ~/.ssh/github
168+
chmod 644 ~/.ssh/github.pub
169+
chmod 700 ~/.ssh
170+
163171

164-
info "Configuring git Credential Manager" 20
172+
info "Configuring git Credential Manager"
165173
git config --global user.email "${useremail}"
166174
git config --global user.name "${username}"
167175
git config --global credential.credentialStore cache
@@ -170,18 +178,16 @@ git_prep() {
170178
git config --global credential.gitHubAccountFiltering "false"
171179
git config --global credential.gitLabAuthModes "browser"
172180
git config --global init.defaultBranch master
181+
git config --global push.autoSetupRemote true
173182

174-
if [ -z "$(pgrep -a ssh-agent)" ]; then
175-
info "running ssh-agent service..." 20
183+
if ! pgrep -a ssh-agent; then
184+
info "running ssh-agent service..."
176185

177-
if [ -z "$(systemctl --user list-unit-files | grep ssh)" ]; then
178-
info "making ssh-agen.service..." 20;
186+
if ! systemctl --user list-unit-files | grep ssh; then
187+
info "Creating systemd user ssh-agen.service..." 5
179188

180189
mkdir -p ~/.config/systemd/user/
181-
_TMPDIR=$(mktemp -d)
182-
touch "$_TMPDIR/sshass"
183-
trap 'rm -rf "$_TMPDIR"' EXIT
184-
tee "$_TMPDIR/sshass" <<EOF
190+
cat > "${HOME}/.config/systemd/user/ssh-agent.service" <<'EOF'
185191
[Unit]
186192
Description=SSH key agent
187193
Wants=default.target
@@ -194,29 +200,33 @@ ExecStart=/usr/bin/ssh-agent -D -a $SSH_AUTH_SOCK
194200
[Install]
195201
WantedBy=default.target
196202
EOF
197-
tee ~/.config/systemd/user/ssh-agent.service > /dev/null < "${_TMPDIR}/sshass"
198-
systemctl --user enable ssh-agent
199-
systemctl --user start ssh-agent
203+
systemctl --user daemon-reload
204+
systemctl --user enable --now ssh-agent
205+
sleep 1
200206
fi
201207

202208
# Auto SSH-AUTH_SOCK setup
203-
if [ -z "$SSH_AUTH_SOCK" ]; then
204-
info "have no SSH_AUTH_SOCK" 20
209+
if [ -z "${SSH_AUTH_SOCK+isset}" ]; then
210+
info "No SSH_AUTH_SOCK set, configuring..." 5
205211
# Try XDG_RUNTIME_DIR first (systemd user service)
206-
if [ -n "$XDG_RUNTIME_DIR" ] && [ -S "$XDG_RUNTIME_DIR/ssh-agent.socket" ]; then
207-
info 'have no $XDG_RUNTIME_DIR/ssh-agent.socket' 20
208-
export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/ssh-agent.socket"
212+
if [ -n "${XDG_RUNTIME_DIR}" ] && [ -S "${XDG_RUNTIME_DIR}/ssh-agent.socket" ] 2> /dev/null; then
213+
export SSH_AUTH_SOCK="${XDG_RUNTIME_DIR}/ssh-agent.socket"
209214
# Try to find any existing ssh-agent socket
210-
elif sockets=$(find /tmp/ssh-* -user "$USER" -name "agent.*" 2>/dev/null); then
211-
export SSH_AUTH_SOCK="$(echo "$sockets" | head -n1)"
215+
elif sockets=$(find /tmp/ssh-* -maxdepth 2 -user "${USER}" -name "agent.*" -type s 2>/dev/null | head -1); then
216+
export SSH_AUTH_SOCK="$sockets"
217+
info "Using existing socket: $SSH_AUTH_SOCK" 10
212218
# Fallback: start new agent if nothing found
213-
# else
214-
# eval "$(ssh-agent -s)" > /dev/null
219+
else
220+
info "Starting fallback ssh-agent..." 10
221+
eval "$(ssh-agent -s)" > /dev/null
215222
fi
216223
fi
217-
# eval "$(ssh-agent -s)"
218224
fi
219-
eval "$(ssh-agent -s)" && ssh-add ~/.ssh/github
225+
ssh-add ~/.ssh/github 2> /dev/null || true
226+
git fetch || eval "$(ssh-agent -s)"
227+
git fetch
228+
success "GIT configured and ready to use."
229+
# endregion git_prep() {
220230
}
221231

222232
inst_base() {
@@ -227,9 +237,17 @@ inst_base() {
227237
flatpak install flathub git mc screen plocate || { exit 1; }
228238
sudo updatedb&
229239
handler 'git_prep' "Prepare Git..." 10
240+
handler 'inst_registry' 'Install local registry under proxy...' 10
230241
handler 'clean main' "Cleaning after installation" 10
231242
}
232243

244+
inst_registry() {
245+
"${ROOT}/registry/run.sh" conf apply
246+
"${ROOT}/registry/run.sh" conf check || {
247+
error "Need install the Registry: 'wsl -d Ubuntu2 -- /bin/bash -ls \"~/dev/admin/DevSecOps_tools/registry/run.sh install\"'"
248+
}
249+
}
250+
233251
# how to use more isolations for apps [too old?]
234252
tryFlatseal() {
235253
flatpak install flathub com.github.tchx84.Flatseal -y
@@ -393,8 +411,7 @@ case "${1:-}" in
393411
handler 'upgrade' "Upgrade"
394412
info "Next step ->upgrade() and then ->inst()"
395413
;;
396-
inst)
397-
handler 'inst_base' "Install base packages" || { error "Try to use '-> fix()'"; exit 1; };;
414+
inst) handler 'inst_base' "Install base packages" || { error "Try to use '-> fix()'"; exit 1; };;
398415

399416
clean) handler 'clean' "Clean up" ;;
400417

@@ -404,7 +421,27 @@ case "${1:-}" in
404421
nat) handler 'setAsNAT' 'Run [setAsNAT] to preset this system as NAT router' ;;
405422
wslconf) handler 'wslconfiguration' 'Run copying .wslconfig to win-host' ;;
406423
fixnetwork) handler 'fixnetwork' 'Run fix network';;
424+
407425
test) handler "test ${2}" "Run test";;
408426

409-
*) warning 'Undefined parameter';;
427+
*)
428+
error "Usage:";
429+
justSay " $0 full" $_lvl
430+
justSay " $0 prep" $_lvl
431+
justSay " $0 fix" $_lvl
432+
justSay " $0 upgrade" $_lvl
433+
justSay " $0 inst" $_lvl
434+
echo ''
435+
justSay " $0 clean" $_lvl
436+
echo ''
437+
justSay " $0 inst_registry" $_lvl
438+
justSay " $0 inst_code" $_lvl
439+
justSay " $0 gitprep" $_lvl
440+
justSay " $0 nat" $_lvl
441+
justSay " $0 wslconf" $_lvl
442+
justSay " $0 fixnetwork" $_lvl
443+
echo ''
444+
justSay " $0 test [opt]" $_lvl
445+
exit 2
446+
;;
410447
esac
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
22
# [automount]
33
# ldconfig = false
4-
/usr/lib/wsl/lib
4+
/usr/lib/wsl/lib

wsl-prep/wsl-files/etc/resolv.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
nameserver 127.0.0.53
12
nameserver 9.9.9.9
3+
options edns0 trust-ad
24
search .
3-

0 commit comments

Comments
 (0)