Рубрики
Bash Linux

Bash

Shortcuts:
Ctrl+A: to the beginning of the line (like Home)
Ctrl+C: cancel the program
Ctrl+E: to the end of the line (like End)
Ctrl+K: Delete a string from a where cursor located now to the end of the string.
Ctrl+Y: paste just deleted text
Ctrl+Z: stop a program (stop PID). can be restored with fg PID or bg PID

Alt+B: move the cursor one word backward
Alt+F: move the cursor one word forward
Alt+D: delete the word, the cursor must be before the word
Alt+T: switch two previous words
Crtl+T: switch two previous strings

####################################################

Vi:
i — insert mode
i i — replace mode
Esc — exit from edit mode

in view mode:
H — move the cursor to the right
L — move the cursor to the left
J — move the cursor one string down
K — move the cursor one string up
X — Delete a symbol
D D — delete a string
P — paste deleted string
U — undo all
: — switch to command mode

####################################################

list file in the directory:
ls
-t : by time
-S : by size
-X : by extension
-r : reverse
-l : more info, like owner etc.
-h : human-readable size
-R : recursively with sub-catalogs
-F : show file type
/ — catalog
* — executable
@ — symlink
— — symbol device
+ — block device
= — Pipe, FIFO

combine ls and less to list though long output:
ls -lR | less

####################################################

While true loops:

This script makes log rotate to execute every 20 minutes and writes status to file.
while true;
do sleep 1200;
logrotate -s /var/log/logstatus /etc/logrotate.conf;
done

jq — makes pretty print for json. («.» after is mandatory for filtering, without «.« the file will not be written)
while true;
do sleep 1;
curl -v -u login:passwd http://ip:port/api/nodes | jq . >> /home/user/curl.20200512.log;
done

To delete all commented lines from file use next script:
./stripcomments.sh file.name
#!/bin/sh
grep -v ^[[:space:]]*\# $1 | grep -v ^[[:space:]]*\; | grep -v ^$

Copy all files in catalog with «.bak» extensions to «.bak~»
for i in *.bak;
do cp $i $i~;
done

Save logs for all pods without sidecars in k8s default namespace to separate files:
now=$(date "+%Y%d%m-%H%M"); for item in $(kubectl get pods | awk '{print $1}'); do kubectl logs ${item} > ${item}.$now.logs; done;

More advanced script for logs from k8s:

#!/bin/bash now=$(date "+%Y%d%m-%H%M");
mkdir "${now}_logs";
for item in $(kubectl get pods | awk '{print $1}')
do if [[ ${item} = NAME ]] || [[ ${item} = *"cpro"* ]] || [[ ${item} = *"snmp-webhook"* ]] || [[ ${item} = *"reloader"* ]] || [[ ${item} = *"coap-adapter"* ]]
then
echo "Pod ${item} has multiply containers or there is some error or it may be filtered!" else
kubectl logs "${item}" > "${now}_logs/${item}.${now}.logs" fi
done
tar -zcvf "logs-${now}.tar.gz" "${now}_logs" echo echo echo "Done!"

Copy SSH identity without password prompt and fingerprint confirmation:
sshpass -f password.txt ssh-copy-id 10.241.45.73 -o StrictHostKeyChecking=no;

Change user password in one string:
echo -e "userpassword\nuserpassword" | passwd username

Create a file in each directory and all subdirectories recursively:
find . -type d -exec touch .gitkeep {} \;

Execute command in each subdirectory:
find . -type d -print0 | xargs -0 -L1 sh -c 'cd "$0" && pwd && ./packer.sh unpack'

xargs flags:
-0 — input is terminated (split) by end of the line and not with white space.
-L1 — max 1 not blank line per argument

Generate ssh key in one string to the default file:
ssh-keygen -q -t rsa -N '' -f ~/.ssh/id_rsa <<<y 2>&1 >/dev/null

Turn off swap and comment it in fstab:
swapoff -a
sed -i 's/^\/swap/#\/swap/' /etc/fstab

Рубрики
Docker

Docker

Save an image to a tar.gz file using gzip:
docker save myimage:latest | gzip > myimage_latest.tar.gz

Check mounted volumes for a specific docker container:
docker inspect -f '{{ .Mounts }}' containerid

Рубрики
k8s

K8S

Sort pods by creation time:
kubectl get pods --sort-by=.metadata.creationTimestamp


Enable kubelet to log level 5:
sed -i -e 's/--v=1/--v=5/g' /etc/systemd/system/kubelet.service; systemctl daemon-reload; systemctl restart kubelet

Check pod-eviction-timeout:
grep pod-eviction-timeout /etc/kubernetes/manifests/kube-controller-manager.yml

Change pod-eviction-timeout:
sed -i 's/- --pod-eviction-timeout=120s/- --pod-eviction-timeout=5m/g' /etc/kubernetes/manifests/kube-controller-manager.yml

Get pod information in yaml format:
kubectl get pod POD_NAME -o yaml

Рубрики
NTP

NTP

Install NTP:
yum install -y ntp


firewalld open service:
firewall-cmd —add-service=ntp —permanent
firewall-cmd —reload


Start and enable:
systemctl start ntpd;
systemctl enable ntpd;
systemctl status ntpd;


Synchronize with NTP server and check date:
ntpq -p
date -R


Synchronize with the special server:
ntpdate -q ntp-server.url

Complete flow in one string:

yum install -y ntp; firewall-cmd --add-service=ntp --permanent; firewall-cmd --reload; systemctl start ntpd; systemctl enable ntpd; systemctl status ntpd; ntpq -p; date -R

Рубрики
Git

Git

Initialize a new git repository:
in target folder execute:
git init


How to add a local ssh key to GitHub:
generate ssh key on Linux VM:
ssh-keygen

copy the public key from target folder, by default /home/username/.id_rsa.pub
cat /home/username/.id_rsa.pub

paste key to GitHub:
user settings —> SSH and GPG keys —> New ssh key



Add remote repository:
git remote add origin git@github.com:username/repository_name.git



Check on what branch you are:
git branch

Check what local and remote branches git knows about:
git branch -a

Switch to the existing branch:
git checkout "existing-branch-name"

Create a new branch:
git checkout -b "new-branch-name"

Push the new branch to remote:
git push -u origin "new-branch-name"

Create an existing branch and move HEAD there:
git checkout -b "new-branch-name" "existing-branch-name"

Fetch all info about remote branches:
git fetch --all

Diff one branch against another:
git diff master..staging

Rename git branch:
git branch -m "existing-branch-name" "new-branch-name"

Help and man:
git help branch
git branch --help
man git-branch

Delete a branch:
git branch -d "branch-name-to-delete"


Forcefully delete a branch (think twice before using it):
git branch -d --force "branch-name-to-delete"

Full new feature flow:
# Start a new feature
git checkout -b new-feature master

# Edit some files
git add <file>
git commit -m "Start a feature"


# Edit some files
git add <file>
git commit -m "Finish a feature"


# Merge in the new-feature branch
git checkout master
git merge new-feature
git branch -d new-feature

Рубрики
Jenkins

Jenkins notes

How to reboot Jenkins from UI:

Safe reboot (Jenkins will wait for all jobs to finish):
{Jenkins_url}/safeRestart

Reboot (Jenkins will reboot immediately):
{Jenkins_url}/restart



####################################################

Jenkins update:

Find where Jenkins war file is located:
find / -name *.war

Rename original «jenkins.war» file in «/usr/lib/jenkins» to something like jenkins.war.20200426.bak

Download new «jenkins.war» file and put it in «/usr/lib/jenkins»

Restart Jenkins from UI or CLI:
systemctl restart jenkins



####################################################

Jenkins remote CLI:
jar can be downloaded from:
{Jenkins_url}/jnlpJars/jenkins-cli.jar
or
from web UI:
{Jenkins_url}/cli/



####################################################

Jenkins environment variables for CLI:

Linux:
Auth:
export JENKINS_USER_ID=username
export JENKINS_API_TOKEN=312312312312-token


Powershell:
Auth:
$env:JENKINS_USER_ID-«username«
$env:JENKINS_API_TOKEN-«312312312312-token«


####################################################

Jenkins build job on GitHub commit.

Install plugin in Jenkins named:
«GitHub plugin»

Configure job:
Check option
General —> GitHub project
and provide link ssh or https to your project.

and check the option:
Build Triggers —> GitHub hook trigger for GITScm polling

apply and save