Skip to content

Instantly share code, notes, and snippets.

@agrakhov
Last active July 22, 2023 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agrakhov/29f990c7817dd3d71a169eaf03becfa5 to your computer and use it in GitHub Desktop.
Save agrakhov/29f990c7817dd3d71a169eaf03becfa5 to your computer and use it in GitHub Desktop.

GIT

  • Revert commit (not pushed):
git commit -m "Something terribly misguided"
git reset HEAD~
<< edit files as necessary >>
git add .
git commit -c ORIG_HEAD
  • Revert commit (pushed): git revert COMMIT_NUM
  • Checkout file from master to reset (not commited!):
git reset HEAD filename // if added to index already
git checkout -- filename
  • Add tag (to existing commit):
git tag v1.0 ec32d32
git push origin --tags
  • Cherry-pick:
# checkout target to branch
git cherry-pick <hash0> [<hash1>, <hash2>, ...]
# if its a merge request
git cherry-pick -m 1 <hash>
# after resolving conflicts
git cherry-pick --continue
# or
git cherry-pick --abort
  • remove file from added git reset path/to/file or all git reset
  • update existing project remote url: git remote set-url origin git@git.ecd.axway.org:apigov/config-library.git
  • squash commits on a branch:
git reset --soft $(git merge-base master HEAD)
git commit -m "squashed"
git push --force

LINUX

  • get hdd free space df -h
  • get folders size (-d goes for depth) sudo sudo du -h -d 1 /var/nfs/apigov or du -h -d 1 | sort -hr
  • check ports in use sudo netstat -plnt
  • check specific port in use and kill process:
lsof -i:8080
kill $(lsof -t -i:8080) # or kill -9 $(lsof -t -i:8080)
  • tar / untar:
# z=gzip, c=create, x=extract, v=verbose, f=next arg is filename
tar zcvf myfiles.tar.gz mydirectory/
tar zxvf myfiles.tar.gz
  • Infinite curl:
while true; do curl -I -H Host:whoami.docker.localhost localhost:80; sleep 0.5; done
# to output just specific data add %{} params (params may vary, google it)
while true; do curl -s -o /dev/null -w "%{http_code} %{time_total}\n" -H Host:whoami.docker.localhost localhost:80; sleep 0.5; done
  • Check if remote port is open:
# outputs 1 = closed, 0 = open
nc -z -w 1 pg-svc-master 5432 &> /dev/null; echo $?
  • Clear history: cat /dev/null > ~/.bash_history && history -c

  • Install / uninstall JDK on linux

    • install
      sudo add-apt-repository ppa:webupd8team/java
      sudo apt-get update
      sudo apt-get install oracle-java8-installer // or java7, or java9
      
    • remove
      sudo apt-get remove oracle-java8-installer
      sudo apt-get purge oracle-java8-installer
      sudo apt-get autoremove
      
  • Postman

    • install
      # If upgrading, remove first
      # sudo rm -rf /opt/Postman
      wget https://dl.pstmn.io/download/latest/linux64 -O postman.tar.gz
      sudo tar -xzf postman.tar.gz -C /opt
      rm postman.tar.gz
      sudo ln -s /opt/Postman/Postman /usr/bin/postman
    • desktop entity
      cat > ~/.local/share/applications/postman.desktop <<EOL
      [Desktop Entry]
      Encoding=UTF-8
      Name=Postman
      Exec=postman
      Icon=/opt/Postman/app/resources/app/assets/icon.png
      Terminal=false
      Type=Application
      Categories=Development;
      EOL
  • convert image files format: mogrify -format jpg /path/*.png

  • Chrome

    • check repo list: cat /etc/apt/sources.list.d/google-chrome.list
    • update to latest: sudo apt-get --only-upgrade install google-chrome-stable
  • base64 encoding without new line character:

    echo -n "client:secret" | base64
  • generate self-signed cert for localhost:

openssl req -x509 -out localhost.crt -keyout localhost.key \
  -newkey rsa:2048 -nodes -sha256 \
  -subj '/CN=localhost' -extensions EXT -config <( \
   printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")
  • install nfs
sudo apt install nfs-kernel-server
sudo mkdir -p /srv/nfs
sudo chown nobody:nogroup /srv/nfs
sudo chmod 777 /srv/nfs
sudo vi /etc/exports -> add:
/srv/nfs 192.168.0.0/24(rw,sync,no_subtree_check)
sudo exportfs -a
sudo systemctl restart nfs-kernel-server
sudo ufw allow from [clientIP or clientSubnetIP] to any port nfs
  • connect to nfs
sudo apt-get update && sudo apt-get install nfs-common -y
sudo mkdir -p /mnt/nfs_porky
sudo mount 192.168.0.88:/srv/nfs /mnt/nfs_porky/
  • generate random password:
    • date | md5sum
    • openssl rand -base64 18 (number can be different)
  • run as user:
su -l www-data -s /bin/bash
# or
sudo su -l www-data -s /bin/bash
  • CPU load:
# load (run as many time as needed or for each core):
yes > /dev/null &
# stop:
killall yes

Linux SSH and friends

generate new key: ssh-keygen -t rsa -b 4096 -C "agrakhov@gmail.com"

copy key to server ssh-copy-id -i ~/.ssh/id_rsa.pub USER@SERVER

scp using pub key

# -P is optional (if using non-standard 22 port)
scp -P 122 -i PATH/TO/id_rsa.pub FILE_TO_COPY USER@HOST:./

ssh without adding to known_host file:

ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null user@host
//OR
ssh -o UserKnownHostsFile=/dev/null user@host

ssh master check: ssh -O check host.example.com

ssh master stop: ssh -O exit host.example.com

run command without evaluating variables (use \$): ssh user@host echo \$var

tunnel to url from remote host: ssh -L 10000:192.168.0.1:443 sshHost then open in browser https://localhost:10000

Postman uninstall:

sudo rm -rf /opt/Postman
sudo rm -rf ~/.config/Postman
sudo rm ~/.local/share/applications/postman.desktop

APPLE / MAC

generate .pem from .p12:

# create request to CA (from keychain)
# import received x501 cert to keychain
# export cert from keychain (not private key), this will generate p.12 file
openssl pkcs12 -in pushcert.p12 -out pushcert.pem -nodes -clcerts

brew uninstall / install

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Mouse speed:

defaults write -g com.apple.mouse.scaling -float 10.0 # LOGOUT after!
defaults read -g com.apple.mouse.scaling

Postman Uninstall:

sudo rm -rf ~/Applications/Postman.app
sudo rm -rf ~/Library/Application\ Support/Postman
sudo rm ~/Library/Preferences/com.electron.postman.helper.plist
sudo rm ~/Library/Preferences/com.postmanlabs.mac.plist
reboot

Restart stuck process:

# Notifications System
killall -KILL NotificationCenter
# Menubar
killall -KILL SystemUIServer
# Dock or Spaces
killall -KILL Dock
# Finder
killall -KILL Finder

PI3

headless setup:

# SSH:
touch /media/alexey/boot/ssh
# wifi
vi /media/alexey/boot/wpa_supplicant.conf
# add to file:
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="SSID_HERE"
    psk="PWD_HERE"
    key_mgmt=WPA-PSK
}

#static ip
sudo nano /etc/dhcpcd.conf
# add:
interface wlan0
static ip_address=192.168.0.71/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1 8.8.8.8

disable swap file:

#check:
sudo swapon --summary

#disable:
sudo dphys-swapfile swapoff && \
  sudo dphys-swapfile uninstall && \
  sudo update-rc.d dphys-swapfile remove

docker install:

sudo apt update && \
sudo apt-get install apt-transport-https ca-certificates software-properties-common -y && \
curl -sSL get.docker.com | sh && \
sudo usermod pi -aG docker

DOCKER

get list of tags for image: wget -q https://registry.hub.docker.com/v1/repositories/cassandra/tags -O - | sed -e 's/[][]//g' -e 's/"//g' -e 's/ //g' | tr '}' '\n' | awk -F: '{print $3}'

to use in terminal like: dgt node add to .bashrc:

dgt() {
  wget -q https://registry.hub.docker.com/v1/repositories/$1/tags -O - | sed -e 's/[][]//g' -e 's/"//g' -e 's/ //g' | tr '}' '\n' | awk -F: '{print $3}'
}

stop all containers:
docker kill $(docker ps -q)

stop and remove container by name
docker rm $(docker stop $(docker ps -a -q --filter "name=NAME_HERE" --format="{{.ID}}"))

remove all containers
docker rm $(docker ps -a -q)

remove all unused (<none>) images
docker rmi $(docker images --filter "dangling=true" -q --no-trunc)

quick aliases for 2 above

### Aliases
alias dic="docker rmi $(docker images --filter "dangling=true" -q --no-trunc)"
alias dcc="docker rm $(docker ps -a -q)"

remove all docker images
docker rmi $(docker images -q)

remove all docker volumes
docker volume rm $(docker volume ls -qf dangling=true)

delete all images with "engage-yag" in name
docker images | awk '$1 ~ /engage-yag/ { print $3 }' | xargs docker rmi**

update docker
> dpkg -l | grep docker

ii lxc-docker 1.3.2 amd64 Linux container runtime
ii lxc-docker-1.3.2 1.3.2 amd64

Then: apt-get remove --purge lxc-docker lxc-docker-1.3.2 Then install as ususal: sudo apt-get install docker-engine sudo service docker start


AWS

Update CLI: pip install awscli --upgrade --user

Get availability zones: aws ec2 describe-availability-zones --region us-west-2


MAVEN

create jacoco coverage report: mvn jacoco:report then go to target/site/jacoco/index.html

force to update deps: mvn clean install -U


NODE

list global packages: npm ls -g --depth 0

nvm intall latest lts: nvm install --lts

nvm set default by version: nvm alias default 8.12.0

nvm set default lts: nvm alias default lts/*

express.js enable cors:

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', '*');
  // OR 
  // res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  next();
});

get package version in bash: $(node -p "require('./package.json').version")


Bash

ternary command && echo "OK" || echo "NOK" or assign value [ $valid ] && x=1 || x=0


Kubectl

copy secret kubectl get secret gitlab-registry -n=zeta --export -o yaml | kubectl apply -n=alpha -f -


CSS

center content in div vertically:

.container{
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}

body, html{
  height:100%;
}

<div class="container">
    <div>Div to be aligned vertically</div>
</div>

VS code

launch.json for chrome debugging with webpack:

// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "chrome",
      "request": "attach",
      "name": "Attach to Chrome",
      "port": 9222,
      "webRoot": "${workspaceRoot}",
      "sourceMapPathOverrides": {
          "webpack:///./*":   "${webRoot}/*"
      },
      "disableNetworkCache": true
    }
  ]
}

Jenkins

  • cleanup pipeline:
    pipeline {
        // agent { label "master" }
        agent any
        options { skipDefaultCheckout() }
        stages {
            stage('CleanWorkspace') {
                steps {
                    deleteDir()
                }
            }
        }
    }
    
    or as post step
    pipeline {
      agent any
      stages {
          stage('Example') {
              steps {
                  echo 'Hello World'
              }
          }
      }
      post { 
          failure { 
              deleteDir()
          }
      }
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment