Skip to content

Instantly share code, notes, and snippets.

@paulwongx
Last active October 18, 2022 02:28
Show Gist options
  • Save paulwongx/adf9dc48443ea11bb4400cedc189f349 to your computer and use it in GitHub Desktop.
Save paulwongx/adf9dc48443ea11bb4400cedc189f349 to your computer and use it in GitHub Desktop.
devops

Devops

Source: https://www.youtube.com/watch?v=Wvf0mBNGjXY

Linux commands

$ echo $SHELL
>> shellvar

$ pwd
>> /home/my_dir1

$ cd new_dir; mkdir www; pwd
>> /home/new_dir/

$ mkdir -p /tmp/asia/india/bangalore

$ rm -r /tmp/my_dir1

$ cp -r my_dir1 /tmp/my_dir1

# Add text to a file. Ctrl+D to save and exit
$ cat > new_file.txt

# view contents of a file
$ cat new_file.txt

$ whoami
>> matthew
$ id
>> uid=1001(matthew) gid=1001(matthew) groups=1001(matthew)
$ su aparna
>> Password: 

$ ssh aparna@192.168.1.2

# download file
$ curl https://example.com/text.txt -O
$ wget https://example.com/text.txt -O file.txt

# view os data
ls /etc/*release*
cat /etc/*release*

yum repolist
yum list ansible
yum remove ansible
yum --showduplicates list anisble

Services

service httpd start
systemctl start httpd
systemctl stop httpd
systemctl status httpd
systemctl enable httpd
systemctl disable httpd
# Create a system file that runs on $ systemctl start my_app
cd /etc/systemd/system
touch my_app.service

my_app.service

[Unit]
Description=My python web application

[Service]
ExecStart=/usr/bin/python3 /opt/code/my_app.py
ExecStartPre=/opt/code/configure_db.sh
ExecStartPost=/opt/code/email_status.sh
Restart=always 

[Install]
WantedBy=multi-user.target
$ systemctl daemon-reload
$ systemctl start my_app
$ systemctl stop my_app
$ systemctl enable my_app

Editors

Vi

$ vi index.html

esc - command mode i - insert mode KHJL - up, down, left, right delete - x delete whole line - dd copy - yy paste - p scroll - ctrl+up / ctrl+down : - command :w - save :q - quit :wq - save and quit / - find (/of), n to move to next word

Networking

$ ip addr show
$ ssh root@192.168.57.3

Network Address Translation (NAT)

Vagrant

Helps you instantiate many VMs with code

  • Works for virtualbox, vmware, hyper-v, docker
$ vagrant init centos/7
$ ls
$ vagrant up
$ vagrant ssh

Vagrantfile

Vagrant.configure("2") do |config|

  config.vm.box = "centos/7"
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.synced_folder "../data", "/vagrant_data"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
  end
  config.vm.provision "shell", inline: <<-SHELL
    apt-get update
    apt-get install -y apache2
  SHELL
end

DNS

$ ping db
$ cat >> /etc/hosts
192.168.1.11        db
192.168.1.11        www.google.com
$ ping db

Configure it centrally in a DNS resolver server

  • 8.8.8.8 by google points to many popular sites
$ cat /etc/resolv.conf
nameserver          192.168.1.100
search              mycompany.com   prod.mycompany.com
Forward All to 8.8.8.8

Record Types A - web-server - 192.168.1.1 AAAA - web-server - 2001:0db8:85a3:0000:0000:8a2e:0370:7334 CNAME - food.web-server - eat.web-server, hungry.web-server

Nig doesn't look at etc/resolv file

nslookup www.google.com
dig www.google.com

Network Switches

  • Network switch connects multiple devices
  • Router connects multiple network switches
ip link
ip addr add 192.168.1.10/24 dev eth0
ip addr add 192.168.1.11/24 dev eth0

route
ip route add 192.168.2.0/24 via 192.168.1.1

Allowing packets to be sent back from another device

$ cat /proc/sys/net/ipv4/ip_forward
>> 0

$ echo 1 > /proc/sys/net/ipv4/ip_forward
1

Must change in in /etc/sysctl.conf to persist b/w reboots

net.ipv4.ip_forward = 1

Lookback address

  • localhost is 127.0.0.1 means myself
ip addr show

YAML Key value pair

Fruit: Apple
Vegetable: Carrot
Liquid: Water

Array/Lists

Fruits:
-   Orange
-   Apple
-   Banana

Dictionary/Map

Banana:
    Calories: 105
    Fat: 0.4 g
    Carbs: 27 g

Git

Source: https://learngitbranching.js.org/

Introduction

  • Checkout a branch before committing to commit the branch and not just the parent head
  • Latest command is git switch instead of git checkout
  • Create new branch and checkout with git checkout -b <yourbranchname>
  • Rebasing moves the current branch as a child of the one you're rebasing to as one linear commit
# committing
git commit 

# branching/ checkout
git branch newImage
git checkout newImage; git commit
git switch 
git checkout -b <yourbranchname>

# merging
git merge bugFix
git checkout bugFix; git merge main

#rebasing
git rebase main
git rebase bugFix
git rebase main bugFix

Ramping up

  • Checking out the hash points the HEAD to that node
  • Use relative refs to commit hashes
  • Move up with ^ and up multiple times with -<num>
  • Move up a number of parents with ~
  • Use -f to force reassigning a branch to a commit
  • revert creates a commit prime (for pushed commits), while reset can be used to remove commits locally
# relative refs
git commit <hash>
git log
git checkout main^
git checkout C3; git checkout HEAD^
git checkout HEAD~4
git branch -f main HEAD~3

# reversing changes
git reset
git revert
git reset HEAD~1
git revert HEAD

Moving around

  • Rebase with the interactive -i flag
# cherry-pick
git cherry-pick <commit1> <commit2>
git cherry-pick C2 C4

# rebase interactive
git rebase -i HEAD~4

Mixed bag

  • Tags are used to mark milestones in the project
  • Describe used to show where you are relative to the closest anchor
git commit -amend
git tag v1 c1
git tag v1
git describe <ref>
>> <tag>_<numCommits>_g<hash>

Advanced

  • ^ also accepts which parent path to choose
git checkout main^2
git checkout HEAD~^2~2

Remote commands

  • Remote branches have this naming convention <remote name>/<branch name>, usually origin/main
  • git fetch downloads and commits remote branches and updates where the branches point. Simply downloads all the commits
  • git pull is a shortform of fetching remote changes and merging them
  • git push -u is for upstream
  • git pull --rebase is a shorthand for fetching and rebasing
git clone
git checkout origin/main; git commit
git fetch

# pulling
git cherry-pick origin/main
git rebase origin/main
git merge origin/main

git fetch; git merge origin/main
git pull

# pushing
git push

# syncing then push
git fetch; git rebase o/main; git push
git pull --rebase; git push
git fetch; git merge o/main; git push
git pull; git push

git reset --hard o/main
git checkout -b feature c2
git push origin feature

Remote tracking

  • Create a new branch that tracks origin/main
  • The <place> is where the commits will come from and go to. Since we told everything, it ignores where HEAD is at
  • <source>:<destination> is called a refspec
  • When pulling, <source> is a remote location and <destination> is local
  • You can specify nothing for <source>
# tracking o/main
git checkout -b totallyNotMain origin/main
git checkout -b foo origin/main; git pull
git checkout -b foo origin/main; git commit; git push
git branch -u origin/main foo; git commit; git push

# push
git push <remote> <place>
git push origin main

git push origin <source>:<destination>
git push origin foo^:main

# fetch
git fetch origin foo
git fetch origin <source>:<destination>

# deleting branches
git push origin :side

# creates a new branch locally
git fetch origin :bugFix

# equivalent commands
git pull origin foo
git fetch origin foo; git merge o/foo

git pull origin bar~1:bugFix
git fetch origin bar~1:bugFix; git merge bugFix

Remove a file from git history but keep in local filesystem

git rm --cached file1.txt
git commit -m "remove file1.txt"
git push origin branch_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment