Created
January 1, 2022 11:24
-
-
Save malys/49baaba5887c6505ea77a765a7494440 to your computer and use it in GitHub Desktop.
[WSL from Clear Linux] script to artillery script to curl commands #wsl #clearlinux #nodejs #docker #kubernetes #desktop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% if docker %} | |
export DOCKER_DIR=/mnt/wsl/shared-docker | |
export DOCKER_SOCK="$DOCKER_DIR/docker.sock" | |
export DOCKER_HOST="unix://$DOCKER_SOCK" | |
#if [ ! -f "$DOCKER_SOCK" ]; then | |
# mkdir -pm o=,ug=rwx "$DOCKER_DIR" | |
# chgrp docker "$DOCKER_DIR" | |
# nohup sudo -b dockerd < /dev/null > $DOCKER_DIR/dockerd.log 2>&1 | |
#fi | |
alias d="docker" | |
alias doc="docker-compose" | |
alias doclean="docker system prune -f" | |
alias dossh="docker exec -i -t" | |
{% endif %} | |
{% if kubernetes %} | |
# Create K8s local cluster https://kind.sigs.k8s.io/docs/user/quick-start/#configuring-your-kind-cluster | |
alias kcreate="kind create cluster --config=/home/{{ username }}/clusterconfig.yml --image kindest/node:v1.23.0" | |
alias kinfo="kubectl cluster-info --context kind-kind" | |
alias kdelete="kind delete cluster" | |
alias k="kubectl" | |
{% endif %} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if [ -n "$BASH_VERSION" ]; then | |
# include .bashrc if it exists | |
if [ -f "$HOME/.bashrc" ]; then | |
. "$HOME/.bashrc" | |
fi | |
fi | |
{% if desktop %} | |
export DISPLAY=$(ipconfig.exe | grep -m 1 IPv4 | awk '{print $14}' | awk 'sub(/\r$/,"")'):0.0 | |
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kind: Cluster | |
apiVersion: kind.x-k8s.io/v1alpha4 | |
nodes: | |
- role: control-plane | |
extraPortMappings: | |
- containerPort: 30000 | |
hostPort: 30000 | |
protocol: TCP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"hosts": ["unix:///mnt/wsl/shared-docker/docker.sock", "tcp://0.0.0.0:2375"], | |
"iptables": true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM clearlinux:latest | |
RUN useradd {{ username }} && \ | |
echo -e -n "{{ password }}\n{{ password }}" | passwd {{ username }} && \ | |
usermod -G wheel -a {{ username }} && \ | |
swupd update && \ | |
# Add sudo commande | |
swupd bundle-add sudo -y | |
ADD wsl.conf /etc/ | |
{% if docker %} | |
RUN swupd bundle-add containers-basic docker-compose -y && \ | |
usermod -aG docker {{ username }} && \ | |
mkdir -p /etc/sudoers.d/ | |
ADD daemon.json /etc/docker/ | |
ADD sudoers /etc/sudoers.d/ | |
RUn chmod 0755 /etc/sudoers.d/sudoers | |
{% endif %} | |
{% if kubernetes %} | |
ADD clusterconfig.yml /home/{{ username }}/ | |
RUN curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.11.1/kind-linux-amd64 && \ | |
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl | |
RUN chmod +x ./kind ./kubectl && \ | |
mv ./kind /usr/bin/kind && \ | |
mv ./kubectl /usr/bin/kubectl && \ | |
chown {{ username }} /usr/bin/kubectl && \ | |
chown {{ username }} /usr/bin/kind | |
{% endif %} | |
{% if desktop %} | |
RUN swupd bundle-add xfce4-desktop vnc-server -y | |
COPY vnc /home/{{ username }}/.vnc | |
RUN chown -R {{ username }} /home/{{ username }}/.vnc && \ | |
chmod +x /home/{{ username }}/.vnc/xstartup | |
{% endif %} | |
ADD .bashrc /home/{{ username }}/ | |
ADD .profile /home/{{ username }}/ | |
RUN chown -R {{ username }} /home/{{ username }}/ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Remove WSL: wsl --unregister test | |
* Connec WSL: wsl -d test | |
* Shutdown WSL: wsl -t test | |
* | |
* ex: node index.mjs --help | |
*/ | |
import nunjucks from 'nunjucks'; | |
import fs from 'fs'; | |
import os from 'os'; | |
import path from 'path'; | |
import { | |
execSync | |
} from 'child_process'; | |
import yargs from 'yargs' | |
import { | |
hideBin | |
} from 'yargs/helpers' | |
const argv = yargs(hideBin(process.argv)) | |
.scriptName("node index.mjs") | |
.usage('$0 [options]') | |
.example('$0 -g -f d:/temp', 'Start installation process (Docker, K8s, XFCE) with default configuration') | |
.example('$0 -p xxx -f D:/backup/docker -i clear -w clear', 'Start installation process') | |
.options({ | |
'username': { | |
alias: 'u', | |
describe: 'Default username to create', | |
default: process.env.USERNAME | |
}, | |
'password': { | |
alias: 'p', | |
describe: 'Default password', | |
default: '123test456', | |
type: 'password' | |
}, | |
'image': { | |
alias: 'i', | |
describe: 'Docker image name', | |
default: 'clear_bt' | |
}, | |
'wsl': { | |
alias: 'w', | |
describe: 'WSL name', | |
default: 'test' | |
}, | |
'folder': { | |
alias: 'f', | |
describe: 'Folder to store wsl disk ex:' + os.tmpdir(), | |
demandOption: true | |
}, | |
'install-docker': { | |
alias: 'd', | |
describe: 'Install Docker', | |
default: true, | |
type: 'boolean' | |
}, | |
'install-kubernetes': { | |
alias: 'k', | |
describe: 'Install Kubernetes', | |
default: true, | |
type: 'boolean' | |
}, | |
'install-desktop': { | |
alias: 'g', | |
describe: 'Install XFCE + VNC :5901. It will geneate heavy image. Start vnc server with "vncserver" and use realvnc viewer', | |
default: false, | |
type: 'boolean' | |
} | |
}) | |
.help() | |
.argv | |
const OUTPUT_DIR = './output' | |
const shell = "nyagos.exe" | |
// Alternative const shell = process.env.ComSpec | |
let processTemplate = (file, conf) => { | |
fs.writeFileSync(path.join(OUTPUT_DIR, file), nunjucks.renderString(fs.readFileSync(file + '.j2').toString(), conf)); | |
} | |
try { | |
fs.rmdirSync(OUTPUT_DIR); | |
} catch (e) { | |
// | |
} | |
try { | |
fs.mkdirSync(OUTPUT_DIR); | |
} catch (e) { | |
// | |
} | |
nunjucks.configure({ | |
autoescape: true | |
}); | |
const CONF = { | |
username: argv.username, | |
password: argv.password, | |
wsl: argv.wsl, | |
folder: argv.folder, | |
image: argv.image, | |
docker: argv.installDocker, | |
kubernetes: argv.installKubernetes, | |
desktop: argv.installDesktop | |
} | |
//WSL | |
processTemplate('wsl.conf', CONF); | |
//Profile | |
processTemplate('.profile', CONF); | |
//Bashrc | |
processTemplate('.bashrc', CONF); | |
// Dockerfile | |
processTemplate('Dockerfile', CONF); | |
// Docker | |
if (CONF.docker) { | |
console.log('Installing Docker'); | |
//daemon.json | |
processTemplate('daemon.json', CONF); | |
processTemplate('sudoers', CONF); | |
} | |
// Kind | |
if (CONF.kubernetes) { | |
console.log('Installing Kubernetes'); | |
processTemplate('clusterconfig.yml', CONF); | |
} | |
// Desktop | |
if (CONF.desktop) { | |
console.log('Installing Desktop'); | |
execSync(`cp -R vnc ${OUTPUT_DIR}`, { | |
stdio: 'inherit' | |
}) | |
} | |
// Build the image && export | |
execSync(`docker build . -t ${CONF.image} --no-cache`, { | |
shell: shell, | |
cwd: OUTPUT_DIR, | |
stdio: 'inherit' | |
}) | |
execSync(`docker run ${CONF.image}`, { | |
shell: shell, | |
cwd: OUTPUT_DIR, | |
stdio: 'inherit' | |
}) | |
let id = execSync(`docker ps -a | grep ${CONF.image} `, { | |
shell: shell, | |
cwd: OUTPUT_DIR | |
}).toString().trim().split(' ')[0] | |
execSync(`docker export --output=${CONF.image}.tar ${id}`, { | |
shell: shell, | |
cwd: OUTPUT_DIR, | |
stdio: 'inherit' | |
}) | |
execSync(`docker rm -f ${id}`, { | |
shell: shell, | |
cwd: OUTPUT_DIR, | |
stdio: 'inherit' | |
}) | |
execSync(`wsl --import ${CONF.wsl} ${CONF.folder} ${CONF.image}.tar`, { | |
shell: shell, | |
cwd: OUTPUT_DIR, | |
stdio: 'inherit' | |
}) | |
execSync(`wsl -l -v`, { | |
shell: shell, | |
cwd: OUTPUT_DIR, | |
stdio: 'inherit' | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "wsl", | |
"version": "1.0.0", | |
"description": "Install WSL on Windows", | |
"main": "index.mjs", | |
"dependencies": { | |
"nunjucks": "^3.2.3", | |
"yargs": "^17.3.1" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% if docker %} | |
{{ username }} ALL = (ALL) NOPASSWD: /bin/dockerd | |
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#To put in vnc folder | |
## Supported server options to pass to vncserver upon invocation can be listed | |
## in this file. See the following manpages for more: vncserver(1) Xvnc(1). | |
## Several common ones are shown below. Uncomment and modify to your liking. | |
## | |
# securitytypes=vncauth,tlsvnc | |
# desktop=sandbox | |
geometry=1920x1080 | |
# localhost | |
# alwaysshared |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
#To put in vnc folder | |
xrdb $HOME/.Xresources | |
startxfce4 & |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[user] | |
default={{ username }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment