Popular Posts

Search This Blog

Saturday, 4 June 2022

Create a Docker Image


mkdir demo && cd demo

 cat Dockerfile

FROM ubuntu

RUN apt update

RUN apt install -y nginx

COPY index_nginx_debian.html /var/www/html

CMD nginx -g 'daemon off;'


cat index_nginx_debian.html

 *** WELCOME TO NGINX ***



sudo docker build .

Sending build context to Docker daemon  3.072kB

Step 1/5 : FROM ubuntu

 ---> d2e4e1f51132

<...>


Step 2/5 : RUN apt update

 ---> Using cache

 ---> 8ceb245edcdd

<...>


Step 3/5 : RUN apt install -y nginx

 ---> Using cache

 ---> e034c1ac1b38

<...>


Step 4/5 : COPY index_nginx_debian.html /var/www/html

 ---> Using cache

 ---> d279aabf79b0

Step 5/5 : CMD nginx -g 'daemon off;'

 ---> Using cache

 ---> aaadaadbd170

Successfully built aaadaadbd170



sudo docker image ls

REPOSITORY   TAG       IMAGE ID       CREATED          SIZE

<none>       <none>    aaadaadbd170   31 seconds ago   168MB

ubuntu       latest    d2e4e1f51132   5 weeks ago      77.8MB



sudo docker run --publish 80:80 aaadaadbd170


curl http://192.168.182.130/index_nginx_debian.html

 *** WELCOME TO NGINX *** 









Configure Docker Daemon to Start on Boot


#to enable Docker Daemon on the boot

sudo systemctl enable docker

Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable docker


#to disable Docker Daemon on the boot
sudo systemctl disable docker
Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable docker
Removed /etc/systemd/system/multi-user.target.wants/docker.service.


#to reload Docker Daemon
systemctl daemon-reload


#to disable Docker start on the system boot
echo manual | sudo tee /etc/init/docker.override
manual


# to control the disk space used for Docker images, containers, and volumes by moving it to a separate partition
cat /etc/docker/daemon.json
{
    "data-root": "/mnt/docker-data",
    "storage-driver": "overlay2"
}


#After reboot
sudo systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; disabled; vendor preset: enabled)
     Active: inactive (dead)
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com


docs.docker.com


Sunday, 29 May 2022

Reset Jenkins Admin Password


Make a backup copy of a Jenkins config file (If need to restore the previous settings)

cp -r /var/lib/jenkins/config.xml /var/lib/jenkins/config.xml.back


Disable the security

vim /var/lib/jenkins/config.xml 

<...>

<useSecurity>false</useSecurity>

<...>


Restart the Jenkins service

systemctl restart jenkins


Go to the Jenkins UI (No credentials this time) and reset the admin password


Navigate to "Manage Jenkins" 

In Security "Configure Global Security"

In Security Realm Select "Jenkins’ own user database" and  Save


Go to "People" From the Dashboard 

Select  username and "Configure"  Enter a new password in the "Password" and "Confirm password" fields and Save


Once the admin password is reset, restore the /var/lib/jenkins/config.xml file and restart Jenkins

 mv /var/lib/jenkins/config.xml.back /var/lib/jenkins/config.xml

 systemctl restart jenkins


Ansible Playbook | Trigger Jenkins Job using token and copy .war to Apache tomcat path

 cat trigger_jenkins.yml 
---
 - name: Deployment of .war file
   hosts: localhost
   become: yes

   tasks:

      - name: Trigger Jenkins Job
        shell: curl -v -X POST http://localhost:8080/job/Project_name/build --user jenkinsxyz:110d44acb43a8f57c8e54fc6360bf3e5ab

      - name: Wait until the file .war is present before continuing
        wait_for:
          path: /var/lib/jenkins/workspace/Project_name/target/Name.war

     - name: copy .war file to tomcat
        copy:
          src: /var/lib/jenkins/workspace/Project_name/target/Name.war
          dest: /opt/tomcat10/apache-tomcat-10.0.21/webapps/
          remote_src: yes
          directory_mode: yes

Ansible Playbook | Tomcat10 | Ubuntu 20.04 | Systemd | Port Confuguration


cat tomcat_install.yml 
---
 - name: Install Tomcat10 and Configure
   hosts: localhost
   become: yes
   vars: 
     tomcat_port: 8081

   tasks:

      - name: Update the System Packages
        apt:
          update_cache: yes

      - name: Create a Tomcat User
        user:
          name: tomcat

      - name: Create a Tomcat Group
        group:
          name: tomcat

      - name: Create a Tomcat Directory
        file:
          path: /opt/tomcat10
          owner: tomcat
          group: tomcat
          mode: 755
          recurse: yes

      - name: download tomcat server packages
        get_url:
          url: https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.21/bin/apache-tomcat-10.0.21.tar.gz
          dest: /opt/tomcat10

      - name: extract tomcat packages
        unarchive:
          src: /opt/tomcat10/apache-tomcat-10.0.21.tar.gz
          dest: /opt/tomcat10
          remote_src: yes

      - name: Configure tomcat port as 8081
        template: 
          src: server.xml.j2
          dest: /opt/tomcat10/apache-tomcat-10.0.21/conf/server.xml

      - name: Change ownership of tomcat directory
        file:
          path: /opt/tomcat10
          owner: tomcat
          group: tomcat
          mode: "u+rwx,g+rx,o=rx"
          recurse: yes
          state: directory

      - name: Copy Tomcat service from local to remote
        copy:
          src: tomcat.service.j2
          dest: /etc/systemd/system/tomcat.service
          mode: 0755
         
      - name: Start Tomcat service
        systemd:
          name: tomcat
          state: started
          daemon_reload: true

server.xml.j2
<...>
<Connector port="{{ tomcat_port }}" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
<...>

 cat tomcat.service.j2 

[Unit]
Description=Apache Tomcat Web Application Container
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
Environment=CATALINA_PID=/opt/tomcat10/apache-tomcat-10.0.21/temp
Environment=CATALINA_HOME=/opt/tomcat10/apache-tomcat-10.0.21
Environment=CATALINA_BASE=/opt/tomcat10/apache-tomcat-10.0.21
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'
ExecStart=/opt/tomcat10/apache-tomcat-10.0.21/bin/startup.sh
ExecStop=/opt/tomcat10/apache-tomcat-10.0.21/bin/shutdown.sh

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

Sunday, 22 May 2022

Install WordPress on Ubuntu using Ansible Playbook


ansible-playbook wordpress.yml


cat wordpress.yml 

---

- hosts: all

  become: yes

  vars_files:

    - vars.yml


  tasks:

    - name: Install prerequisites

      apt: 

        name: aptitude 

        update_cache: yes 

        state: latest 


    - name: Install Apache MySQL PHP Packages

      apt: 

        name: "{{ item }}" 

        update_cache: yes 

        state: latest

      loop: [ 'apache2', 'mysql-server', 'python3-pymysql', 'php', 'php-mysql', 'libapache2-mod-php' ]


    - name: Install PHP Modules

      apt: 

        name: "{{ item }}"

        update_cache: yes 

        state: latest

      loop: [ 'php-curl', 'php-gd', 'php-mbstring', 'php-xml', 'php-xmlrpc', 'php-soap', 'php-intl', 'php-zip' ]


   # Configure Apache

    - name: Create document root

      file:

        path: "/var/www/{{ http_host }}"

        state: directory

        owner: "www-data"

        group: "www-data"

        mode: '0755'


    - name: Set up Apache VirtualHost

      template:

        src: "apacheconf.j2"

        dest: "/etc/apache2/sites-available/{{ http_conf }}"

      notify: Reload Apache


    - name: Enable rewrite module

      shell: /usr/sbin/a2enmod rewrite

      notify: Reload Apache


    - name: Enable new site

      shell: /usr/sbin/a2ensite {{ http_conf }}

      notify: Reload Apache


    - name: Disable default Apache site

      shell: /usr/sbin/a2dissite 000-default.conf

      notify: Restart Apache


  # Configure MySQL

      

    - name: Set the root password

      mysql_user:

        name: root

        password: "{{ mysql_root_password }}"

        login_unix_socket: /var/run/mysqld/mysqld.sock


    - name: Creates database for WordPress

      mysql_db:

        name: "{{ mysql_db }}"

        state: present

        login_user: root

        login_password: "{{ mysql_root_password }}"


    - name: Create MySQL user for WordPress

      mysql_user:

        name: "{{ mysql_user }}"

        password: "{{ mysql_password }}"

        priv: "{{ mysql_db }}.*:ALL"

        state: present

        login_user: root

        login_password: "{{ mysql_root_password }}"


 # Configure WordPress

    - name: Download and unpack latest WordPress

      unarchive:

        src: https://wordpress.org/latest.tar.gz

        dest: "/var/www/{{ http_host }}"

        remote_src: yes

        creates: "/var/www/{{ http_host }}/wordpress"


    - name: Change ownership

      file:

        path: "/var/www/{{ http_host }}"

        state: directory

        recurse: yes

        owner: www-data

        group: www-data


    - name: Change permissions for directories

      shell: "/usr/bin/find /var/www/{{ http_host }}/wordpress/ -type d -exec chmod 750 {} \\;"


    - name: Change permissions for files

      shell: "/usr/bin/find /var/www/{{ http_host }}/wordpress/ -type f -exec chmod 640 {} \\;"


    - name: Copy wp-config

      template:

        src: "wpconf.j2"

        dest: "/var/www/{{ http_host }}/wordpress/wp-config.php"

        owner: www-data

        group: www-data


  handlers:

         

    - name: Reload Apache

      service:

        name: apache2

        state: reloaded


    - name: Restart Apache

      service:

        name: apache2

        state: restarted


cat vars.yml 

---

#MySQL credentials

mysql_root_password: "mysql_root_password"

mysql_db: "mysql_db_name"

mysql_user: "mysql_wp_user"

mysql_password: "mysql_wp_password"


#HTTP info

http_host: "domain_name"

http_conf: "domain.conf"

http_port: "80"



cat wpconf.j2 

<?php

define( 'DB_NAME', '{{ mysql_db }}' );

define( 'DB_USER', '{{ mysql_user }}' );

define( 'DB_PASSWORD', '{{ mysql_password }}' );

define( 'DB_HOST', 'localhost' );

define( 'DB_CHARSET', 'utf8' );

define( 'DB_COLLATE', '' );

define('FS_METHOD', 'direct');

define( 'AUTH_KEY',         '{{ lookup('password', '/dev/null chars=ascii_letters length=64') }}' );

define( 'SECURE_AUTH_KEY',  '{{ lookup('password', '/dev/null chars=ascii_letters length=64') }}' );

define( 'LOGGED_IN_KEY',    '{{ lookup('password', '/dev/null chars=ascii_letters length=64') }}' );

define( 'NONCE_KEY',        '{{ lookup('password', '/dev/null chars=ascii_letters length=64') }}' );

define( 'AUTH_SALT',        '{{ lookup('password', '/dev/null chars=ascii_letters length=64') }}' );

define( 'SECURE_AUTH_SALT', '{{ lookup('password', '/dev/null chars=ascii_letters length=64') }}' );

define( 'LOGGED_IN_SALT',   '{{ lookup('password', '/dev/null chars=ascii_letters length=64') }}' );

define( 'NONCE_SALT',       '{{ lookup('password', '/dev/null chars=ascii_letters length=64') }}' );

$table_prefix = 'wp_';

define( 'WP_DEBUG', false );

if ( ! defined( 'ABSPATH' ) ) {

        define( 'ABSPATH', dirname( __FILE__ ) . '/' );

}

require_once( ABSPATH . 'wp-settings.php' );


Friday, 29 April 2022

Install Docker Container using Ansible YAML File on Ubuntu 22.04


cat ansible_docker.yml
---
 - name: Setup Docker 
   hosts: localhost
   become: yes
   tasks:
     - name: Install docker dependencies
       include: docker_dependency.yml

     - name: update apt
       apt:
         update_cache: yes

     - name: Install docker
       apt:
         name: docker-ce
         state: latest
         update_cache: yes

     - name: Service status - docker
       service:
         name: docker
         state: started

     - name: docker ps -a
       shell: sudo docker ps -a
       register: docker_ps

     - debug:
         var: docker_ps.stdout_lines

     - name: Run Hello World
       shell: sudo docker run hello-world
       register: docker_run

     - debug:
         var: docker_run.stdout_lines

     - name: docker ps -a run
       shell: sudo docker ps -a
       register: docker_ps_run

     - debug:
         var: docker_ps_run.stdout_lines


cat docker_dependency.yml
---
- name: Ensure old versions of Docker are not installed.
  package:
    name:
      - docker
      - docker-engine
    state: absent

- name: Ensure dependencies are installed.
  apt:
    name:
      - apt-transport-https
      - ca-certificates
      - curl
      - software-properties-common
      - gnupg
      - lsb-release
    state: present

- name: Add Docker GPG apt Key
  apt_key:
    url: https://download.docker.com/linux/ubuntu/gpg
    state: present

- name: Add Docker Repository
  apt_repository:
    repo: deb https://download.docker.com/linux/ubuntu focal stable
    state: present

ansible-playbook ansible_docker.yml

Wednesday, 27 April 2022

Install and Configure Wordpress LAMP on Ubuntu 22.04

 


git clone https://github.com/jpmolekunnel/Wordpress-LAMP-on-Ubuntu-22.04.git

vim vars/default.yml

sudo apt update

ansible-playbook playbook.yml


http://<IP/URL/localhost>/wp-admin/


tags

ansible-playbook playbook.yml --tags=wordpress -v

ansible-playbook playbook.yml --tags=system -v

ansible-playbook playbook.yml --tags=mysql -v

ansible-playbook playbook.yml --tags=mysql-root -v


Install Ansible on Ubuntu 22.04 and Configure in the user/current Directory


sudo apt install -f

sudo apt install software-properties-common

sudo apt-add-repository ppa:ansible/ansible

sudo apt update

sudo apt install ansible


ansible --version

ansible [core 2.12.4]

  config file = /etc/ansible/ansible.cfg


Configure ansible in the current directory /home/ansible

cat ansible.cfg

[defaults]

inventory - /home/ansible/inventory


cat inventory

controller


ansible --version

ansible [core 2.12.4]

  config file = /home/ansible/ansible.cfg


hostname -i

sudo hostnamectl set-hostname controller

exec bash

hostname


sudo echo "192.168.182.131 controller" | sudo tee -a /etc/hosts

192.168.182.131 controller

cat /etc/hosts


ansible all --list

  hosts (1):

    controller


#run "sudo apt install" if  Failed sudo: a password is required

#hosts: localhost, in playbook 

ansible-playbook install.yml


Reset Mysql root Password Ubuntu 22.04


systemctl status mysql.service

systemctl stop mysql.service

ps -eaf|grep mysql


If mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists

sudo mkdir -p /var/run/mysqld

sudo chown mysql:mysql /var/run/mysqld


sudo mysqld_safe --skip-grant-tables &

[1] 28088

 2022-05-09T18:18:53.504935Z mysqld_safe Logging to '/var/log/mysql/error.log'.

2022-05-09T18:18:53.533285Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.29-0ubuntu0.22.04.2 (Ubuntu)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

mysql> UPDATE mysql.user SET authentication_string=null WHERE User='root';

Query OK, 1 row affected (0.01 sec)

Rows matched: 1  Changed: 1  Warnings: 0


mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)


mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root_password';

Query OK, 0 rows affected (0.02 sec)


mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)


mysql> exit

Bye


sudo systemctl restart mysql.service 


mysql --version

mysql  Ver 8.0.29-0ubuntu0.22.04.2 for Linux on x86_64 ((Ubuntu))


mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.


Error

sudo mysqld_safe --skip-grant-tables &

[1] 110591

2022-04-28T03:23:04.569926Z mysqld_safe Logging to '/var/log/mysql/error.log'.

2022-04-28T03:23:04.583095Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.


[1]+  Exit 1                  sudo mysqld_safe --skip-grant-tables


Solved:

sudo mkdir -p /var/run/mysqld

sudo chown mysql:mysql /var/run/mysqld

sudo mysqld_safe --skip-grant-tables &

mysql -u root


Tuesday, 26 April 2022

Git checkouts using ansible.builtin.git module: example


cat git.yml 

---

 - name: git module demo

   hosts: all

   become: yes

   tasks: 

     - name: ensure git pkg installed

       ansible.builtin.apt:

         name: git

         state: present

     - name: checkout git repo

       ansible.builtin.git:

         repo: https://<URL> 

         dest: /Path/to/Dir 


ansible-playbook git.yml


ansible.builtin.git module

Saturday, 16 April 2022

How to List Packages Added/Installed in Ubuntu Debian-based System


dpkg -l

dpkg -l pattern

dpkg --list pattern

dpkg -l |awk '/^[hi]i/{print $2}'

dpkg -l | grep '^ii '


dpkg-query -l | less

dpkg-query -l | grep ansible


dpkg --get-selections

dpkg --get-selections | grep -v deinstall

dpkg --get-selections | grep ansible

dpkg --get-selections | grep -w "install" 

dpkg --get-selections | grep -w "install" | cut -f1 


apt list

apt list pattern

apt list -a sudo


apt list --installed

apt list --installed pattern

apt list --installed | grep ansible

apt list --installed | awk '{split($0, a, "/"); print a[1]}'


apt list --manual-installed=true

apt-mark showmanual

apt show '~i' -a


apt-cache pkgnames

apt-cache policy <package_name>

apt-cache policy ansible


zgrep " installed " /var/log/dpkg.log*

grep " install " /var/log/apt/history.log


cat /var/lib/apt/lists/ppa.launchpad.net_*_Packages | grep '^Package:'

awk '/^Package: / {print $2}' /var/lib/apt/lists/ppa.launchpad.net_*_Packages | sort -u


grep Package /var/lib/apt/lists/ppa.launchpad.net_*_Packages

grep -h -P -o "^Package: \K.*" /var/lib/apt/lists/ppa.launchpad.net_*_Packages | sort -u


grep ^Package /var/lib/apt/lists/ppa.launchpad.net_*_Packages | awk '{print $2}' | sort | uniq

grep ^Package /var/lib/apt/lists/ppa.launchpad.net_*_Packages | awk '{print $2}' | sort -u


grep ' installed ' /var/log/dpkg.log /var/log/dpkg.log.1 | awk '{print $5}' | sort -u


#Intentionally Installed

(zcat $(ls -tr /var/log/apt/history.log*.gz); cat /var/log/apt/history.log) 2>/dev/null | egrep '^(Start-Date:|Commandline:)' | grep -v aptdaemon | egrep '^Commandline:'

(zcat $(ls -tr /var/log/apt/history.log*.gz); cat /var/log/apt/history.log) 2>/dev/null | egrep '^(Start-Date:|Commandline:)' | grep -v aptdaemon | egrep -B1 '^Commandline:'

zgrep -hE '^(Start-Date:|Commandline:)' $(ls -tr /var/log/apt/history.log ) | egrep -v 'aptdaemon|upgrade' | egrep -B1 '^Commandline:'


#sudo apt install dctrl-tools

grep-dctrl -sPackage . /var/lib/apt/lists/ppa.launchpad.net_*_Packages

grep-status -FStatus -sPackage -n   "install ok installed"


#sudo apt install aptitude

aptitude search '~i!~M'

aptitude search -F "%p %V %v" "?narrow(?installed,?not(?archive(stable)))" | grep ansible


#To exclude just stable you need to anchor the regex pattern:

aptitude search -F "%p %V %v %t" '?any-version(?installed ?not(?archive("^stable$")))'


#To exclude multiple repositories:

aptitude search -F "%p %V %v %t" '?any-version(?installed ?not(?archive("^(xenial|xenial-updates)$")))'


#count

sudo dpkg-query -f '${binary:Package}\n' -W | wc -l


#ppa-purge

apt install ppa-purge

Then get ppa list by tab-completion

ppa-purge -o (hit Tab key twice)


How to List Added/All Repositories and PPA's in Ubuntu Debian-based System


apt policy

apt-cache policy


#saved repos

cat /etc/apt/sources.list.save


#added repos

ls /etc/apt/sources.list.d/

#example

cat /etc/apt/sources.list.d/ansible-ubuntu-ansible-focal.list

apt-cache policy | grep ansible

apt policy | grep ansible


#List all repos

sudo grep -rhE ^deb /etc/apt/sources.list* 

grep ^ /etc/apt/sources.list /etc/apt/sources.list.d/*


grep -r --include '*.list' '^deb ' /etc/apt/sources.list*

grep -r --include '*.list' '^deb ' /etc/apt/sources.list /etc/apt/sources.list.d/


grep "^[^#]" /etc/apt/sources.list /etc/apt/sources.list.d/*


grep -r --include '*.list' '^deb ' /etc/apt/ | sed -re 's/^\/etc\/apt\/sources\.list((\.d\/)?|(:)?)//' -e 's/(.*\.list):/\[\1\] /' -e 's/deb http:\/\/ppa.launchpad.net\/(.*?)\/ubuntu .*/ppa:\1/'

grep -hE '^deb\s' /etc/apt/sources.list /etc/apt/sources.list.d/*.list | sed '/ppa/ s/deb //g' | sed -re 's#http://ppa\.launchpad\.net/([^/]+)/([^/]+)(.*?)$#ppa:\1/\2#g'


apt-cache policy | grep http | awk '{print $2" "$3}' | sort -u

sudo apt update > /dev/null 2>&1 && sudo apt-cache policy | grep http | awk '{print $2 $3}' | sort -u

find /etc/apt/sources.list* -type f -iname "*.list" -exec grep -viE '(^#|^$)' {} \; -print | column -tx

sed -r -e '/^deb /!d' -e 's/^([^#]*).*/\1/' -e 's/deb http:\/\/ppa.launchpad.net\/(.+)\/ubuntu .*/ppa:\1/' -e "s/.*/sudo add-apt-repository '&'/" /etc/apt/sources.list /etc/apt/sources.list.d/*


#Examine the origin tag (such as o=Debian) for each of your current repositories

apt-cache policy | sed -n 's/.*o=\([^,]\+\).*/\1/p' | uniq


Friday, 8 April 2022

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock


ISSUE:-

ubuntu@ubuntu:~/docker-work$ sudo docker rm $(docker ps -qa)

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json?all=1": dial unix /var/run/docker.sock: connect: permission denied

"docker rm" requires at least 1 argument.

See 'docker rm --help'.

Usage:  docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers


Solution:-

sudo chmod 666 /var/run/docker.sock


How to Change Docker cgroup Driver to systemd


docker info | grep -i driver

 Storage Driver: overlay2

 Logging Driver: json-file

 Cgroup Driver: cgroupfs


vim /etc/docker/daemon.json

{

  "exec-opts": ["native.cgroupdriver=systemd"]

}


sudo systemctl restart docker


docker info | grep -i driver

Storage Driver: overlay2

Logging Driver: json-file

Cgroup Driver: systemd


sudo systemctl status docker


Verify that Docker Engine is installed correctly

sudo docker run hello-world


Wednesday, 6 April 2022

SSH access to AWS EC2 Instance Node Using .pem key


Convert File_name.ppk file to File_name.pem

sudo apt-get install putty-tools

puttygen File_name.ppk -O private-openssh -o File_name.pem
chmod 400 File_name.pem

ssh -i
File_name.pem username@IP

! [remote rejected] main -> main (refusing to allow a Personal Access Token to create or update workflow `.github/workflows/maven.yml` without `workflow` scope) error: failed to push some refs to 'https://github.com/path/filename.git'

  

Issue

! [remote rejected] main -> main (refusing to allow a Personal Access Token to create or update workflow `.github/workflows/maven.yml` without `workflow` scope)

error: failed to push some refs to 'https://github.com/path/filename.git'


Solution

Login GitHub URL 

Go to Settings ➡ Developer settingsPersonal access tokens


✔workflow Update GitHub Action workflows


Docker Compose to set up and run a simple Django/PostgreSQL app


docker --version
Docker version 20.10.12, build e91ed57

Create a Dockerfile

sudo mkdir Docker-Django

cd Docker-Django

cat Dockerfile

# syntax=docker/dockerfile:1
FROM python:3
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

cat requirements.txt
Django>=3.0,<4.0
psycopg2>=2.8

cat docker-compose.yml
version: "3.9"
   
services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    environment:
      - POSTGRES_NAME=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    depends_on:
      - db 

sudo docker-compose run web django-admin startproject composeexample .


Django-admin created are owned by root. Change the ownership of the new files.

sudo chown -R ubuntu:ubuntu

ls -l


Connect the database

Edit the composeexample/settings.py file, Replace the DATABASES = ... with the following.

sudo vim composeexample/settings.py
# settings.py
   
import os
   
[...]
   
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.environ.get('POSTGRES_NAME'),
        'USER': os.environ.get('POSTGRES_USER'),
        'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),
        'HOST': 'db',
        'PORT': 5432,
    }
}

sudo docker-compose up

sudo docker ps

http://localhost:8001/


Ref: https://docs.docker.com/samples/django/






Sunday, 27 March 2022

Install Java with Apt on Ubuntu 20.04


sudo apt update

java -version

Command 'java' not found, but can be installed with:

sudo apt install default-jre              # version 2:1.11-72, or

sudo apt install openjdk-11-jre-headless  # version 11.0.14+9-0ubuntu2~20.04

sudo apt install openjdk-16-jre-headless  # version 16.0.1+9-1~20.04

sudo apt install openjdk-17-jre-headless  # version 17.0.2+8-1~20.04

sudo apt install openjdk-8-jre-headless   # version 8u312-b07-0ubuntu1~20.04

sudo apt install openjdk-13-jre-headless  # version 13.0.7+5-0ubuntu1~20.04


sudo apt install default-jre

<...>

java -version

openjdk version "11.0.14" 2022-01-18

OpenJDK Runtime Environment (build 11.0.14+9-Ubuntu-0ubuntu2.20.04)

OpenJDK 64-Bit Server VM (build 11.0.14+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)


sudo apt install default-jdk

<...>

javac -version

javac 11.0.14


Tuesday, 22 March 2022

Install Docker on Ubuntu 20.04


sudo apt update

sudo apt-get install ca-certificates curl gnupg lsb-release


sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg


 sudo echo  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

sudo docker --version

Docker version 20.10.13, build a224086


sudo systemctl status docker

● docker.service - Docker Application Container Engine

     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)

     Active: active (running) since Tue 2022-03-22 16:21:57 PDT; 2min 37s ago

...


#check Docker is working correctly

sudo docker run hello-world

#list complete list of available subcommands

docker

#for more information on a command

docker COMMAND --help


#view system-wide information

sudo docker info

#crawl Docker Hub and return a listing of all images 

sudo docker search ubuntu

#see the images that have been downloaded

sudo docker images


#view the active containers

sudo docker ps

#view all containers — active and inactive

sudo docker ps -a

#view the latest container

sudo docker ps -l


#start container with the ID/NAME

sudo docker start container_ID/container_NAME

#stop container with the ID/NAME

sudo docker stop container_ID/container_NAME

#delete container

sudo docker rm container_ID/container_NAME

Ref: docs.docker.com


Thursday, 17 March 2022

Ansible Vault Simple Examples


#encrypt a file

ansible-vault encrypt file_name

New Vault password:

Confirm New Vault password:

Encryption successful


#view encrypted files

ansible-vault view file_name

Vault password:


#edit encrypted files

ansible-vault edit file_name

Vault password:


#change password of encrypted files

ansible-vault rekey --ask-vault-pass file_name

Vault password:

New Vault password:

Confirm New Vault password:

Rekey successful


#decrypt files

ansible-vault decrypt --ask-vault-pass file_name

Vault password:

Decryption successful


#use vault password file

echo "password" > password_file

ansible-vault encrypt file_name --vault-password-file=password_file

Encryption successful


TASK [Deploy tomcat config] fatal: [localhost]: FAILED! => {"msg": "an error occurred while trying to read the file '/path/to/file_name': [Errno 13] Permission denied: b'/path/to/file_name'.

 

Issue

TASK [Deploy tomcat config] *****************************************************************************************************************************************************************

fatal: [localhost]: FAILED! => {"msg": "an error occurred while trying to read the file '/path/to/file_name': [Errno 13] Permission denied: b'/path/to/file_name'. [Errno 13] Permission denied: b'/path/to/file_name'"}


Troubleshoot

809755 -rw-r-----  1 root   root   7598 Mar 16 18:33 file_name

sudo chown -R user:user file_name

809755 -rw-r-----  1 user user 7598 Mar 16 18:33 file_name


Wednesday, 16 March 2022

Ansible ping: Check connectivity status of target remote hosts


mkdir ansible

cd ansible


vim inventory.ini

[webservers]

IP


[all:vars]

ansible_connection=ssh

ansible_user=user_name

ansible_ssh_pass=password

ansible_port=42006

ansible_ssh_common_args='-o StrictHostKeyChecking=no'


#ansible -m ping -i inventory.ini all

ansible -m ping -i inventory.ini webservers

localhost | SUCCESS => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python3"

    },

    "changed": false,

    "ping": "pong"

}


Check connectivity between ansible and the target.

ping module checks connectivity using inventory file.

the group can be specified in the inventory file.


ansible all -m ping -v

#Using /etc/ansible/ansible.cfg as config file

localhost | SUCCESS => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python3"

    },

    "changed": false,

    "ping": "pong"

}


ansible webservers -m ping


#to check connectivity manually

ssh -p 42006 username@IP


#check ssh port

sudo netstat -tnlup | grep ssh

tcp        0      0 0.0.0.0:42006           0.0.0.0:*               LISTEN      1006/sshd: /usr/sbi 

tcp6       0      0 :::42006                :::*                    LISTEN      1006/sshd: /usr/sbi


| UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host localhost port 22: Connection refused",


 Issue


ansible -m ping -i inventory.ini webservers

localhost | UNREACHABLE! => {

    "changed": false,

    "msg": "Failed to connect to the host via ssh: ssh: connect to host localhost port 22: Connection refused",

    "unreachable": true

}


Troubleshoot


#specify port number

ansible_port=42006


| UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: Invalid multiplex command.", "unreachable": true


Issue


ansible -m ping -i inventory.ini webservers

localhost | UNREACHABLE! => {

    "changed": false,

    "msg": "Failed to connect to the host via ssh: Invalid multiplex command.",

    "unreachable": true

}


Troubleshoot


vim devops.ini

ansible_ssh_common_args='-O StrictHostKeyChecking=no'

#change to

ansible_ssh_common_args='-o StrictHostKeyChecking=no'


Result


ansible -m ping -i devops.ini webservers

localhost | SUCCESS => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python3"

    },

    "changed": false,

    "ping": "pong"

}


Monday, 14 March 2022

ansible-playbook fatal: [localhost]: FAILED! => {"changed": false, "msg": "Unable to start service nginx: Job for nginx.service failed because the control process exited with error code.

 

Issue


ansible-playbook demo_playbook.yml -i demo_ansible.ini --user=user-name --extra-vars "ansible_sudo_pass=password"


PLAY [This demo playbook] *******************************************************************************************************************************************************************


TASK [Gathering Facts] **********************************************************************************************************************************************************************

ok: [localhost]


TASK [This is to install nginx software] ****************************************************************************************************************************************************

changed: [localhost]


TASK [This is restart the nginx service] ****************************************************************************************************************************************************

fatal: [localhost]: FAILED! => {"changed": false, "msg": "Unable to start service nginx: Job for nginx.service failed because the control process exited with error code.\nSee \"systemctl status nginx.service\" and \"journalctl -xe\" for details.\n"}


PLAY RECAP **********************************************************************************************************************************************************************************

localhost                  : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0


systemctl status nginx.service

● nginx.service - A high performance web server and a reverse proxy server

     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)

     Active: failed (Result: exit-code) since Sun 2022-03-13 05:03:15 PDT; 2min 13s ago

       Docs: man:nginx(8)

    Process: 9296 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)

    Process: 9297 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)


Mar 13 05:03:13 ubuntu nginx[9297]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

Mar 13 05:03:13 ubuntu nginx[9297]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)

Mar 13 05:03:14 ubuntu nginx[9297]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

Mar 13 05:03:14 ubuntu nginx[9297]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)

Mar 13 05:03:14 ubuntu nginx[9297]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

Mar 13 05:03:14 ubuntu nginx[9297]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)

Mar 13 05:03:15 ubuntu nginx[9297]: nginx: [emerg] still could not bind()

Mar 13 05:03:15 ubuntu systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE

Mar 13 05:03:15 ubuntu systemd[1]: nginx.service: Failed with result 'exit-code'.

Mar 13 05:03:15 ubuntu systemd[1]: Failed to start A high performance web server and a reverse proxy server.


cat /var/log/nginx/error.log

2022/03/13 05:03:12 [emerg] 9297#9297: bind() to 0.0.0.0:80 failed (98: Address already in use)

2022/03/13 05:03:12 [emerg] 9297#9297: bind() to [::]:80 failed (98: Address already in use)

2022/03/13 05:03:12 [emerg] 9297#9297: bind() to 0.0.0.0:80 failed (98: Address already in use)

2022/03/13 05:03:12 [emerg] 9297#9297: bind() to [::]:80 failed (98: Address already in use)

2022/03/13 05:03:12 [emerg] 9297#9297: bind() to 0.0.0.0:80 failed (98: Address already in use)

2022/03/13 05:03:12 [emerg] 9297#9297: bind() to [::]:80 failed (98: Address already in use)

2022/03/13 05:03:12 [emerg] 9297#9297: bind() to 0.0.0.0:80 failed (98: Address already in use)

2022/03/13 05:03:12 [emerg] 9297#9297: bind() to [::]:80 failed (98: Address already in use)

2022/03/13 05:03:12 [emerg] 9297#9297: bind() to 0.0.0.0:80 failed (98: Address already in use)

2022/03/13 05:03:12 [emerg] 9297#9297: bind() to [::]:80 failed (98: Address already in use)

2022/03/13 05:03:12 [emerg] 9297#9297: still could not bind()


Troubleshoot


vim /etc/nginx/sites-enabled/default

server {

        listen 80 default_server;

        listen [::]:80 default_server;

#change to

server {

        listen 5601 default_server;

        listen [::]:5601 default_server;


ansible-playbook fatal: [localhost]: FAILED! => {"msg": "Missing sudo password"}


 Issue

ansible-playbook demo_playbook.yml -i inventory.ini

PLAY [This demo playbook] *******************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************

fatal: [localhost]: FAILED! => {"msg": "Missing sudo password"}

PLAY RECAP **********************************************************************************************************************************************************************************

localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0


Troubleshoot 

#execute from command line
ansible-playbook demo_playbook.yml -i demo.ini --user=user_name --extra-vars "ansible_sudo_pass=password"

OR

#add the following line in inventory.ini
[all:vars]
ansible_sudo_pass="password"

ansible-playbook RequestsDependencyWarning: urllib3 (1.26.8) or chardet (3.0.4) doesn't match a supported version


Issue


ubuntu@ubuntu:~/ansible-work$ ansible-playbook demo_playbook.yml -i ansible.ini

/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.8) or chardet (3.0.4) doesn't match a supported version!

  warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "


PLAY [This demo playbook] *******************************************************************************************************************************************************************


Troubleshoot

pip3 install --upgrade requests


Installation of Terraform using zip file on Ubuntu 20.04


wget https://releases.hashicorp.com/terraform/1.1.7/terraform_1.1.7_linux_amd64.zip

unzip terraform_1.1.7_linux_amd64.zip

mv terraform /usr/local/bin/


which terraform

/usr/local/bin/terraform


terraform -version

Terraform v1.1.7

on linux_amd64

+ provider registry.terraform.io/hashicorp/aws v4.5.0


Friday, 11 March 2022

Downloading ChromeDriver, Selenium TestNG Installation and Integration in Eclipse


Tools required: Selenium, ChromeDriver

Pre-requisites: Ubuntu, Java, Eclipse


Downloading ChromeDriver


Chrome ➡ Settings ➡ about chrome ➡ version

Version 99.0.4844.51 (Official Build) (64-bit)

chromedriver.chromium.org

OR

wget https://chromedriver.storage.googleapis.com/99.0.4844.51/chromedriver_linux64.zip

unzip chromedriver_linux64.zip


Selenium Installation


locate selenium

# for locate, sudo apt install mlocate

sudo pip install selenium

OR

Download the Latest stable version from selenium.dev


Integrating Selenium to Eclipse


Open the Eclipse. Create a new Java project. 

Right-click on the project and create a package. 

Create a new class inside the package.

Configure the build path by right-clicking on the created project.

Java  Build Path ➡ Libraries(select class) ➡ Add External JARs(Add Selenium JAR) ➡ 

Apply and Close

See the referenced .jar files in the project to confirm.


Install TestNG


wget http://www.java2s.com/Code/JarDownload/testng/testng-6.8.7.jar.zip

unzip testng-6.8.7.jar.zip


Setting up TestNG in Eclipse


Configure the build path by right-clicking on the created project.

Java  Build Path ➡ Libraries(select class) ➡ Add External JARs(Add Selenium JAR) ➡ 

Apply and Close

See the referenced .jar files in the project to confirm.

Eclipse ➡ Help ➡ Eclipse Marketplace… ➡ Search(testng) ➡ Go ➡ TestNG for Eclipse ➡ Install ➡ TestNG (required) ➡ confirm ➡ I accept the terms of the license agreement ➡ Finish.


Tuesday, 8 March 2022

Jenkins: Remote Triggering of a Parameterized Build


Configure a parameterized build in Jenkins


#Install Plugin - Build With parameters

Jenkins ➡ Manage Jenkins ➡ Manage Plugins ➡ Available ➡ Build With Parameters

Jenkins ➡ New Item ➡ Enter an item name ➡ [ParameterizedDemo] ➡ Freestyle project ➡ OK

General ➡ This project is parameterized ➡ String parameter

Name

my_param

Default Value

Hello


Build Triggers ➡ Trigger builds remotely ➡ 

Authentication Token

Token_987


Build ➡ dd build step ➡ Execute shell

Command

echo $my_param

Apply-Save


Triggering a parameterized build remotely


#Open the terminal

#curl -X GET <YourJenkinsJobUrl>/buildWithParameters?token=TOKEN_NAME

curl -X GET http://localhost:8080/job/ParameterizedDemo/buildWithParameters?token=Token_987


#Open the job in the Jenkins UI

Build with Parameters ➡ Build ➡ Build history ➡ Console Output


Console Output


Started by user admin

Running as SYSTEM

Building in workspace /var/lib/jenkins/workspace/ParameterizedDemo

[ParameterizedDemo] $ /bin/sh -xe /tmp/jenkins17901601731840577267.sh

+ echo Hello

Hello

Finished: SUCCESS


Install Ant plugin and integrate it with Jenkins


Tools: Git, GitHub, and Jenkins


1. Create a GitHub repository 

Create a new repository ➡ Repository Name ➡ Public ➡ Add a README file ➡ create repository

Code ➡ HTTPS ➡ https://<url> ➡ copy


2. Add build.xml file to the repository

#open terminal

git clone https://<url>

cd Demo-Ant/

vim build.xml

<?xml version="1.0"?>

<project name="Hello World Project" default="info">

<target name="info">

<echo> Hello World - Demo Ant</echo>

</target>

</project>


git init

git add build.xml

git commit -m "Add build file"

git remote -v

git push -u origin main


3. Set the Global Tool Configuration


Jenkins ➡ Manage Jenkins ➡ Global Tool Configuration


Ant-

Ant installations  

Add Ant

Ant

Name

[FirstAnt]

Install automatically

Install from Apache

Version

List of Ant installations on this system

Apply ➡ Save


4. Integrate Ant with Jenkins

Jenkins ➡ New Item ➡ Enter an item name ➡ Freestyle project ➡ OK

Source Code Management ➡ Git ➡ Repository URL[https://<url>] ➡ Branch Specifier[*/main]

Build ➡ Add build step ➡ Invoke Ant ➡ Ant Version[FirstAnt]

Apply ➡ Save


Build Now ➡ Build History ➡ Console Output


Monday, 7 March 2022

How To Install Ansible on Ubuntu 20.04

 

sudo apt install -f

#find dependencies


sudo apt install software-properties-common

#update package repositories and get the latest package information


sudo apt-add-repository ppa:ansible/ansible

#list of available software and install ansible

#It also pulls down Ansible PPA's signing key and adds it to the system


sudo apt update

sudo apt install ansible

ansible --version

ansible [core 2.12.2]

  config file = /etc/ansible/ansible.cfg

  configured module search path = ['/home/ubuntu/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']

  ansible python module location = /usr/lib/python3/dist-packages/ansible

  ansible collection location = /home/ubuntu/.ansible/collections:/usr/share/ansible/collections

  executable location = /usr/bin/ansible

  python version = 3.8.10 (default, Nov 26 2021, 20:14:08) [GCC 9.3.0]

  jinja version = 2.10.1

  libyaml = True



Sunday, 6 March 2022

Get Absolute Path of a File in Linux


readlink -f filename

realpath filename

ls "`pwd`/filename"


echo $(pwd)/filename

echo $(pwd)$/$(ls filename)

find $PWD -type f -name filename


locate filename

find $PWD -type f | grep "filename"


Friday, 4 March 2022

Reset Jenkins Admin Password


 sudo vi /var/lib/jenkins/config.xml


 <useSecurity>true</useSecurity>

#modify to

 <useSecurity>false</useSecurity>


 sudo systemctl restart jenkins


Navigate to the web console

Notice that you were not prompted for a username or password


do the following.


Click on People on the left-hand navigation menu.

Click on the Admin

Delete the user account


Navigate to Manage Jenkins

Click on Configure Global Security

Under Security Realm, select Jenkins’ own user database


In the Authorization section, select Logged-in users can do anything

Unselect Allow anonymous read access

Save changes


It will redirect to a page where a new Admin user can be created


#try by specifying the right path

http://localhost:8080/securityRealm/firstUser


#not working? repeat the above steps after the following

sudo mv /var/lib/jenkins/users/* /tmp/jenkins


Ref: - serverlab.ca


Thursday, 3 March 2022

Install Eclipse IDE on Ubuntu 20.04

 

sudo apt update

sudo apt install default-jre

java --version

wget https://mirrors.ustc.edu.cn/eclipse/oomph/epp/2021-12/R/eclipse-inst-linux64.tar.gz

sudo tar -xf eclipse-inst-linux64.tar.gz -C /opt

ls /opt/eclipse-installer/ -1

cd /opt/eclipse-installer/

./eclipse-inst

choose your favorite package IDE.


Eclipse can be downloaded via eclipse.org

Ref:- inoxide.com


install Google Chrome in Ubuntu 20.04 LTS


sudo apt update

sudo apt upgrade

wget --version

#if not get wget

sudo apt install wget


#download and install

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

ls google-chrome-stable_current_amd64.deb

sudo dpkg -i google-chrome-stable_current_amd64.deb


#launch chrome in terminal

google-chrome


Ref:- linuxways.net

Monday, 28 February 2022

Git Stash Save Changes Temporarily


Git Stash sample workflow

1. Modify a file

2. Stage file

3. Stash it

4. View stash list

5. Confirm no pending changes through status

6. Apply with pop

7. View list to confirm changes


# Modify file and add a file

git add .

git stash save "Saving changes"

git stash list

git status

git stash pop

git stash list

git status


Example

#update the branch files if necessary

#make staging area clean and commit changes

git status

git add -A

#add a newfile

git status

git stash

Saved working directory and index state WIP on new_branch: e436353 updated main

git status

On branch stash_branch

nothing to commit, working tree clean

git stash list

stash@{0}: WIP on new_branch: e436353 updated main


#can do many stash, the stash will hide the files

git stash 

git stash show


#can create a new branch

git stash branch stash_branch stash@{0}


#drop

git stash drop stash@{1}

#drop only  latest

git stash drop -q


#recover step by step

git stash pop stash@{0}

git stash apply stash@{0}


git stash apply 

#recover all changes, good to use if not too many stashes available

#recover not specifying stash number


#clean

#show untracked files

git clean -n

#remove  all untracked files

git clean -f


#date range, git stash list –before and –after options

git stash list --before 5.days.ago

#summary of changes for each element

git stash list --stat


#diff of changes for each stash

git stash list -p

#difference between a stash and your local Git working tree

git stash show -p stash@


# difference between stash and HEAD on the main branch

git diff stash@ main

#-p option (patch) to view the full diff of a stash

git stash show -p


#add a description to the stash

git stash save <description>


#push the stash entry created via git stash create to the stash reflog

git stash create "sample stash"

63a711cd3c7f8047662007490723e26ae9d4acf9

git stash store -m "sample stash testing.." "63a711cd3c7f8047662007490723e26ae9d4acf9"

git stash list

stash @{0}: sample stash testing..


Saturday, 26 February 2022

Git:- Creating Tags


Git uses two main types of tags: lightweight and annotated.


Annotated Tags:

$ git tag -a v2.1.0 -m "message"

Lightweight Tags:

$ git tag v2.1.0


git log --oneline

#use SHA-1 while creating a tag, for example

git tag -a v1.1 e436353

git show v1.1

git push --tags


#verify created tags

Github⇒Repo⇒Branch⇒Tags


Add/change some file then commit and create a new tag v1.2 by repeating the above steps

Tags and branches are completely unrelated


tag create a bundle of .zip and tar.gz of branch

It won't be in sync with each branch in the remote.


#delete a tag

git tag --list

git tag -d v1.1

#delete a tag from remote

git push origin --delete v1.1


#copy tag as a branch

git checkout -b new_branch v1.2


git log --pretty

 

Useful specifiers for git log --pretty=format lists some of the more useful specifiers that format

takes.


Specifier    Description of Output

    %H        Commit hash

    %h        Abbreviated commit hash

    %T        Tree hash

    %t         Abbreviated tree hash

    %P         Parent hashes

    %p         Abbreviated parent hashes

    %an        Author name

    %ae        Author email

    %ad        Author date (format respects the --date=option)

    %ar        Author date, relative

    %cn        Committer name

    %ce        Committer email

    %cd        Committer date

    %cr        Committer date, relative

    %s         Subject


Examples

#SHA-1, author, message
git log --pretty=format:"%h - %an, %ar : %s"

git log --oneline --decorate --graph --all

git log --pretty="%h - %s" --author='Name' --since="2022-01-01"   --before="2022-26-02" --no-merges

#SHA-1, Message,Date
git log --pretty=reference

#SHA-1, author, day,month,date,time,year, message
git log --pretty=format:"%h%x09%an%x09%ad%x09%s"
git log --pretty=format:"%h%x09%an%x09%ai%x09%B"

#day,month,date,time,year, author, message
git log --pretty=" %C(reset)%ad %C(Cyan)%an: %C(reset)%s"

#SHA-1, day,month,date,time,year,hours ago, author, message
git log --pretty="%C(Yellow)%h  %C(reset)%ad (%C(Green)%cr%C(reset))%x09 %C(Cyan)%an: %C(reset)%s" -9

#SHA-1, time ago, message, author
git log --pretty=format:"%C(yellow)%h %ar %C(auto)%d %Creset %s , %Cblue%cn" --graph --all

create alias, add this line to your ~/.gitconfig:
[alias]
list = log --pretty=format:\"%C(yellow)%h %ar %C(auto)%d %Creset %s, %Cblue%cn\" --graph --all
git list

#SHA-1, branch & origin
git show-ref --head --dereference

#SHA-1, head Ref, action, message, date
git log -g --abbrev-commit --pretty=oneline
git reflog show

Friday, 25 February 2022

gitignore - Specifies intentionally untracked files to ignore


#create .gitignore file in repository path

.getignore file


*.xml

*.doc

package/*.*


git status

git add -A

git commit -m "git ignore"


create .doc .xml and package folder with index.html


git status

git add -A

git commit -M "commit"


do change in .xml or .doc files and it will be ignored with,

git status


More geeksforgeeks.org    linuxize.com


Tuesday, 22 February 2022

Git reset example for soft mixed hard modes


 --soft: uncommit changes, changes are left staged (index).

 git log --oneline

c6e3374 (HEAD -> main) Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

echo "new file" > new.txt

git add -A

git commit -m new.txt

[main 7ae912c] new.txt

 1 file changed, 1 insertion(+)

 create mode 100644 new.txt

git log --oneline

7ae912c (HEAD -> main) new.txt

c6e3374 Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

git reset --soft c6e3374

git log --oneline

c6e3374 (HEAD -> main) Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

git status

On branch main

Your branch is ahead of 'origin/main' by 3 commits.

  (use "git push" to publish your local commits)

Changes to be committed:

  (use "git restore --staged <file>..." to unstage)

        new file:   new.txt

--mixed (default): uncommit + unstage changes, changes are left in working tree.

git commit -m "new file for --mixed"

[main 16ca1a9] new file for --mixed

 1 file changed, 1 insertion(+)

 create mode 100644 new.txt

emailid@ip-172-3X-21-XX7:~/gitdemo_feb$ git status

On branch main

Your branch is ahead of 'origin/main' by 4 commits.

  (use "git push" to publish your local commits)

nothing to commit, working tree clean

git log --oneline

16ca1a9 (HEAD -> main) new file for --mixed

c6e3374 Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

git reset c6e3374

    OR

git reset --mixed c6e3374

git log --oneline

c6e3374 (HEAD -> main) Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

emailid@ip-172-3X-21-XX7:~/gitdemo_feb$ git status

On branch main

Your branch is ahead of 'origin/main' by 3 commits.

  (use "git push" to publish your local commits)

Untracked files:

  (use "git add <file>..." to include in what will be committed)

        new.txt

nothing added to commit but untracked files present (use "git add" to track)


--hard: uncommit + unstage + delete changes, nothing left.

git add -A

git commit -m "commit for --hard"

[main 9022f76] commit for --hard

 1 file changed, 1 insertion(+)

 create mode 100644 new.txt

git log --oneline

9022f76 (HEAD -> main) commit for --hard

c6e3374 Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

ls

file1  file2  new.txt  

git reset --hard c6e3374

HEAD is now at c6e3374 Revert "ticket 93"

ls

file1  file2

git log --oneline

c6e3374 (HEAD -> main) Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit


Git example for revert show switch reset


#git revert 

git clone <https://url>

git log --oneline

9f58820 (HEAD -> main, origin/main, origin/HEAD) new file

2c76a55 first commit

echo "New demo file" > demo.txt

git add -A demo.txt

git commit -m "ticket 92"

git log --oneline

3ab8b74 (HEAD -> main) ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

echo "adding more line" >> demo.txt

git commit -am "ticket 93"

git log --oneline

0f70f4f (HEAD -> main) ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit


git show

OR

#using head

git show 0f70f4f

commit 0f70f4f177301f314ba83e409228bab6987156b4 (HEAD -> main)

Author: name <emailid@email.com>

Date:   Wed Feb 23 01:25:47 2022 +0000


    ticket 93


diff --git a/demo.txt b/demo.txt

index 3ad4fc4..746e199 100644

--- a/demo.txt

+++ b/demo.txt

@@ -1 +1,2 @@

 New demo file

+adding more line


#git revert (always record a new copy)

git log --oneline

0f70f4f (HEAD -> main) ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

git revert 0f70f4f

[main c6e3374] Revert "ticket 93"

 1 file changed, 1 deletion(-)

git status

On branch main

Your branch is ahead of 'origin/main' by 3 commits.

  (use "git push" to publish your local commits)

nothing to commit, working tree clean


#redo the changes

git log --oneline

c6e3374 (HEAD -> main) Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

cat demo.txt

New demo file

git revert c6e3374

[main 7e7621e] Revert "Revert "ticket 93""

 1 file changed, 1 insertion(+)

cat demo.txt

New demo file

adding more line

git log --oneline

7e7621e (HEAD -> main) Revert "Revert "ticket 93""

c6e3374 Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit


#switching head using git checkout 

git checkout 3ab8b74

Note: switching to '3ab8b74'.

...

git log --oneline

3ab8b74 (HEAD) ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

git checkout main

Previous HEAD position was 3ab8b74 ticket 92

Switched to branch 'main'

Your branch is ahead of 'origin/main' by 3 commits.

  (use "git push" to publish your local commits)

git log --oneline

7e7621e (HEAD -> main) Revert "Revert "ticket 93""

c6e3374 Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

git log --oneline --graph

#graph view

* 7e7621e (HEAD -> main) Revert "Revert "ticket 93""

* c6e3374 Revert "ticket 93"

* 0f70f4f ticket 93

* 3ab8b74 ticket 92

* 9f58820 (origin/main, origin/HEAD) new file

* 2c76a55 first commit


#git reset

git log --oneline

7e7621e (HEAD -> main) Revert "Revert "ticket 93""

c6e3374 Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

echo "This is foo.txt" > foo.txt

git add -A

git commit -m "version 1"

cat foo.txt

This is foo.txt

git log --oneline

b717ad4 (HEAD -> main) version 1

7e7621e Revert "Revert "ticket 93""

c6e3374 Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

git reset HEAD~1

OR

git reset 7e7621e

#example for mixed

git log --oneline

7e7621e (HEAD -> main) Revert "Revert "ticket 93""

c6e3374 Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

git status

On branch main

Your branch is ahead of 'origin/main' by 5 commits.

  (use "git push" to publish your local commits)

Untracked files:

  (use "git add <file>..." to include in what will be committed)

        foo.txt


git reset command In the simplest terms:

--soft: uncommit changes, changes are left staged (index).

--mixed (default): uncommit + un stage changes, changes are left in the working tree.

--hard: uncommit + un stage + delete changes, nothing left.


Friday, 18 February 2022

Merging branches in Git and Push files to branches using SSH Key

 

#at terminal

mkdir demo-merge

cd demo-merge/

ls

echo "hello DevOps" > index.html

git add -A

git init

git add -A

git commit -m "First Commit"

git branch

#change branch name to main

git branch -m main

git branch

git remote add origin <HTTPS URL>

git remote -v

git push -u origin main


#Now do the setting for Push files using SSH key

#Remove HTTPS URL

git remote remove origin

git remote -v


#ssh push and pull

ssh-keygen

#add password if necessary

|/home/ubuntu/.ssh/id_rsa

|/home/ubuntu/.ssh/id_rsa.pub 

#share .pub key with git-hub


Github.com⇒Settings⇒SSH and GPG keys⇒New SSH key

Title

#lab_sys

Key

#Copy /home/ubuntu/.ssh/id_rsa.pub key and paste

Add SSH key

confirm access


git remote add origin <SSH URL>   

git remote -v


git push -u origin main

#yes ask for the first time

#verify the push remote


#merging example

git checkout -b JIRA

echo "adding new feature" > view.java

git add *

git commit -m "done JIRA"

git push -u origin JIRA

git branch

* JIRA

  develop

  main

ls

demo.java  index.html  view.java

git checkout develop

Switched to branch 'develop'

Your branch is up to date with 'origin/develop'.

ls

demo.java  index.html

#to merge JIRA branch to develop switch to develop branch

git merge JIRA

Updating b50ca2a..4a62f5e

Fast-forward

 view.java | 1 +

 1 file changed, 1 insertion(+)

 create mode 100644 view.java

ls

demo.java  index.html  view.java

git push -u origin develop

#verify the push in Github remote, develop branch


Wednesday, 16 February 2022

Git Branch :- create, switching, rename, delete, push, status, merge


#Create a GitHub repository

Go to github.com, and log in to your account.

Click on the New button to create a new repository, enter a repository name and click on Create Repository button.


#Clone the GitHub repository

git clone <https URL>

cd Newbranchrepo

git branch

#creating a new branch

git branch new_branch


#Rename from branch

git branch -m new_branch

#Rename from another branch

git branch -m new_branch rename_branch


#Delete branch locally

git branch -d rename_branch

#Delete branch remotely

git push origin --delete rename_branch

#enter Username and Personal Access Token


#Switch to the new branch

git checkout new_branch

#Switch to the new branch by creating

git checkout -b branch_name

git branch


#Create a file and commit the changes

vim index.html

git add index.html

git commit -m "commit index file"


#push a new branch

git remote -v

git push -u origin new_branc

#enter Username and Personal Access Token


#Check the status of the new branch

git status


#Switch back to the main branch

git checkout main

git branch


More  Git-branch 


#Git merge

git merge branch_name


3-way merge

#Start a new feature

git checkout -b new-feature main

# Edit some files

git add <file>

git commit -m "Start a feature"

# Edit some files

git add <file>

git commit -m "Finish a feature"

# Develop the main branch

git checkout main

# Edit some files

git add <file>

git commit -m "Make some super-stable changes to main"

# Merge in the new-feature branch

git merge new-feature

git branch -d new-feature


Learn Git


Tuesday, 15 February 2022

Push file to GitHub Repository

 

Go to github.com, and log in to your account.

Click on the New button to create a new repository, enter a repository name and click on Create Repository button.


Create a repository on the local machine

 mkdir createnewproject

 cd createnewproject

 echo "# create new file for my project" >> README.md

 git init

 git add README.md

 git commit -m "first commit"

 git branch -M main


Push the changes in the local repository to GitHub

 git remote add origin <Your HTTPS_URL>

 git push -u origin main

 git status

Go to github.com, and check the remote repository


Monday, 14 February 2022

Create a Pull Request in GitHub


Create a Fork

Login to GitHub and from the same browser navigate to the following repository(owner) 

https://github.com/SharedRepo

In the top-right corner of the page, click the Fork button to enable Fork


Clone your Fork in terminal

mkdir Git-PR

cd Git-PR

On GitHub, click on the code button on your repository

Copy the URL from under Clone with HTTPS option

git clone [the copied HTTPS URL]

#clone command creates a local git repository from your remote fork on GitHub.


Sync fork with the original repository

cd SharedRepo

git remote -v


|git config --global --list

|#add user details if required

|git config --global user.name "Your Name"

|git config --global user.email "yourgitemailid@gmail.com


#commit

echo "my first files" > foo.txt

git status

git add foo.txt

git commit -m "first file"


|#see log

|git log #q to quit

|git log --oneline


Push changes

git remote -v

git push -u origin main 

#Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.

GithubSettingsDeveloper SettingsPersonal access tokensGenerate new token

give userid and personal access token using keys shift+insert(*Enter)

#verify the commit in your GitHub account repository


Create a Pull Request

Your GitHub forked repositoryPull requestsNew pull requestCreate pull request

#Choose two branches to see what’s changed or to start a new pull request.

#check "base repository" and "head repository (your GitHub) is correct.

#share this URL or owner can see in their GitHub Pull requests tab.

#now the owner can see the pull request and from where it comes

The owner can select the Pull requestMerge pull requestconfirm mergecomment(close with).


Friday, 11 February 2022

Add JDK Maven and Git Plugins with Jenkins

Add JDK Maven and Git Plugins with Jenkins in local

Login Jenkins⇒Manage Jenkins⇒Manage Plugins⇒available(search maven)⇒Maven Integration(Install without restart)

http://localhost:8080/updateCenter/    #to view


#in terminal

sudo apt install maven

mvn -version

Apache Maven 3.6.3

Maven home: /usr/share/maven

Java version: 11.0.13, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64

Default locale: en_US, platform encoding: UTF-8

OS name: "linux", version: "5.13.0-28-generic", arch: "amd64", family: "unix"


Add JDK

Login Jenkins⇒Manage Jenkins⇒Global Tool Configuration⇒add JDK

#Name

local_java

#JAVA_HOME

/usr/lib/jvm/java-11-openjdk-amd64

#uncheck Install automatically

save


Add maven

Login Jenkins⇒Manage Jenkins⇒Global Tool Configuration⇒add maven

#Name

local_maven

#MAVEN_HOME

/usr/share/maven

#uncheck Install automatically

save


Add Git

git --version

git version 2.25.1

Login Jenkins⇒Manage Jenkins⇒Global Tool Configuration⇒add maven

#Name

localGit

#Path to Git executable

/bin/git

#uncheck Install automatically

save



Install Jenkins on Ubuntu 20.04

 To Install Jenkins on Ubuntu 20.04


#java

java --version

openjdk 11.0.13 2021-10-19


#add jenkins public key

wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -

OK


#add jenkins repository 

sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'


sudo apt update

sudo apt install jenkins


#service status

sudo service jenkins status

Ctrl+C #exit

sudo systemctl start jenkins

sudo service jenkins status


#In the browser

http://localhost:8080/


#to get Administrator password

sudo cat /var/lib/jenkins/secrets/initialAdminPassword


#Follow the pages to complete

Install suggested plugins 

Create Frist Admin User

Instance Configuration

Jenkins is Ready!


Jenkins⇒Manage Jenkins⇒Jenkins CLI⇒download "jenkins-cli.jar"

#or go to http://localhost:8080/cli/ 


sudo java -jar jenkins-cli.jar -s http://localhost:8080/ -version

Version: 2.319.3


Add JDK Maven and Git Plugins with Jenkins


Ref:- how-to-install-jenkins-on-ubuntu-20-04