Saturday, May 24, 2025

Bash Scripting

Bash Scripting

Class 30th Jenkins May 24th
What is Script and why it is?
Type of Scripting language
Understand "Shebang(#!)""
Difference Between sh shell.sh,bash shell.sh, &./Shell.sh in Bash scripting.
Working with Bash scripting
Logical operators (AND(&&), OR(||)
File operations
Loops(For,while,Until)
Functions
Working with processes

Shell script: shell script is series of command, Amazon Linux (they combined bash and shell inherited )
so it will support what every give command whether bash/shell

Amazon linux 
[ec2-user@ip-10-0-1-88 ~]$ cd /bin
[ec2-user@ip-10-0-1-88 bin]$ls -lrt 
lrwxrwxrwx 1 root root           4 May 28 02:01 sh -> bash

Debian Linux (separate there)
admin@ip-10-0-0-210:~$ cd /bin
admin@ip-10-0-0-210:/bin$ ls -lrt
-rwxr-xr-x 1 root root 1265648 Mar 29  2024 bash
lrwxrwxrwx 1 root root       4 Jan  5  2023 sh -> dash

Understand shebang(#!)
A shebang(#!)is a special directive at the top of a script that ells the system which interpreter(shell) to use when executing the script.
  #!/bin/bash -->Use Bash shell to execute the script
 #!/bin/sh -->Use sh shell(Could be dash on some systems).
#!/usr/bin/python3-->Use Python 3 interpreter.

Practical 1
Step1:
Amazon Linux 
Example Bash script using shebang
 #!/bin/bash
echo "Hello, Cloud computing!"
[ec2-user@ip-10-0-1-88 ccit]$ sh hello.sh
Hello cloud computing
[ec2-user@ip-10-0-1-88 ccit]$ bash hello.sh
Hello cloud computing
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
-bash: ./hello.sh: Permission denied
Getting error because of it is not execute mode ,give permission using chmod
[ec2-user@ip-10-0-1-88 ccit]$ ls -lrt
-rw-rw-r-- 1 ec2-user ec2-user 41 May 31 09:07 hello.sh
[ec2-user@ip-10-0-1-88 ccit]$ chmod +x hello.sh
[ec2-user@ip-10-0-1-88 ccit]$ ls -lrt
-rwxrwxr-x 1 ec2-user ec2-user 41 May 31 09:07 hello.sh
Now is is allowing because mode was changed 
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Hello cloud computing
 Step2:Changed script in bash to sh ,See here no difference shell , if you are give ./ this and try to execute interpreter will check what shebang used in the script

[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/sh
echo "Hello cloud computing"
[ec2-user@ip-10-0-1-88 ccit]$ sh hello.sh
Hello cloud computing
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Hello cloud computing
[ec2-user@ip-10-0-1-88 ccit]$ bash hello.sh
Hello cloud computing
Step3: Array type script executing amazon linux and debian linux
amazon linux 
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/sh
arr=(one two three)
echo ${arr[0]}
[ec2-user@ip-10-0-1-88 ccit]$ sh hello.sh
one
Debian Linux
Getting error 
admin@ip-10-0-0-210:~/ccit$ cat hello.sh
#!/bin/sh
arr=(one two three)
echo ${arr[0]}
admin@ip-10-0-0-210:~/ccit$ sh hello.sh
hello.sh: 2: Syntax error: "(" unexpected
admin@ip-10-0-0-210:~/ccit$ bash hello.sh
one
admin@ip-10-0-0-210:~/ccit$ ./hello.sh
-bash: ./hello.sh: Permission denied
 After given  permission, after changed script sh to bash , shell no arrey concept only has bash
admin@ip-10-0-0-210:~/ccit$ chmod +x hello.sh
admin@ip-10-0-0-210:~/ccit$ ls -lrt
total 4
-rwxr-xr-x 1 admin admin 48 May 31 09:34 hello.sh
admin@ip-10-0-0-210:~/ccit$ cat hello.sh
#!/bin/bash
arr=(one two three)
echo ${arr[0]}
 See below with bash both working ,if you explicitly run shell sh it will through error  
admin@ip-10-0-0-210:~/ccit$ ./hello.sh
one
admin@ip-10-0-0-210:~/ccit$ bash hello.sh
one
admin@ip-10-0-0-210:~/ccit$ sh hello.sh
hello.sh: 2: Syntax error: "(" unexpected

I have changed back to sh ,see below sh interpreter trying run shell for the array it will not support getting ,previously same script run  with bash.
admin@ip-10-0-0-210:~/ccit$ cat hello.sh
#!/bin/sh
arr=(one two three)
echo ${arr[0]}
admin@ip-10-0-0-210:~/ccit$ ./hello.sh
./hello.sh: 2: Syntax error: "(" unexpected

Practical 2
Inputs reading from the script
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash
echo "Please enter your name"
read uname
echo "The supplied user name is:$uname"
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Please enter your name
Raja
The supplied user name is:Raja
 We can given directly using below script 
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash
read -p "Please enter your name:" $uname
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Please enter your name:Suresh

Practical 3 : Athematic Operations
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash
num1=10
num2=5
echo "Sum of the numbers:"$((num1+$num2))
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Sum of the numbers:15

[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash
num1=10
num2=5
echo "Sum of the numbers:"$((num1+$num2))
echo "Substraction of the numbers:"$((num1-$num2))
echo "Multiplication of the numbers:"$((num1*$num2))
echo "Division numbers:"$((num1/$num2))
echo "Sum of the numbers:"$((num1%$num2))
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Sum of the numbers:15
Substraction of the numbers:5
Multiplication of the numbers:50
Division numbers:2
Sum of the numbers:0
Practical 4 : 
Conditional operations
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash
string1="ccit"
string2="ccit"

if [[ $string1==$string2 ]]; then
echo "both are same "
else
echo "both are not same"
fi
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
both are same

Practical 4: 
if you are passing any value to variable that variable will change automatically
Number 

Equal
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash
string1=5
string2=10

if [[ $string1 -eq $string2 ]]; then
echo "both are same "
else
echo "both are not same"
fi
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
both are not same
Not Equal
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash
string1=5
string2=10

if [[ $string1 -ne $string2 ]]; then
echo "both are not equal "
else
echo "both are same"
fi
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
both are not equal
 Note: 
String ==,!=
num -eq , -ne
less-than -  -lt -le 
Greater than  -gt -ge
and & 
or ||

And operator and OR Operator 

[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash
echo "Enter username"
read uname

echo "Enter password"
read pass

if [[ $uname == "ccit" && $pass == "123" ]]; then
        echo "Valid user"
else
        echo "Invalid user"
fi

[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Enter username
ccit
Enter password
123
Valid user

[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Enter username
ccit
Enter password
345
Invalid user

OR Operator 
if [[ $uname == "ccit" || $pass == "123" ]]; then
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Enter username
ccit
Enter password
345
Valid user
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Enter username
raja
Enter password
123
Valid user
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Enter username
raja
Enter password
34
Invalid user

File exist or not current directory 
[ec2-user@ip-10-0-1-88 ccit]$ cat ./hello.sh
#!/bin/bash

filepath="./file.txt"

if [[ -e $filepath ]]; then
        echo "file exists"
else
        echo "file does not exists"
fi

[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
file does not exists
[ec2-user@ip-10-0-1-88 ccit]$ touch file.txt
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
file exists

Looping statements 

For Loop
for i (1..23);
done
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash

for i in {1..5}; do
        echo "the value is :"$i
done
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
the value is :1
the value is :2
the value is :3
the value is :4
the value is :5

while Loop
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash

count=0

while [[ $count -lt 10 ]]; do
        echo "the values is :"$count

        count=$((count+1))
done

[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
the values is :0
the values is :1
the values is :2
the values is :3
the values is :4
the values is :5
the values is :6
the values is :7
the values is :8
the values is :9

For -le lessthan equal to 
while [[ $count -le10 ]]; do

[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
the values is :0
the values is :1
the values is :2
the values is :3
the values is :4
the values is :5
the values is :6
the values is :7
the values is :8
the values is :9
the values is :10


--Thanks 

Wednesday, May 21, 2025

Jenkins part9

 Jenkins part9

Class 29th Jenkins May 21st

Jenkins part is over final Project below 
->Why is the deployment and why it is ?
->Deployment flow
->Environment Setup
->What precautions should we take before deployment?
->Jenkins End-to-End Project: Practical
We will take one Java Project from Git, deploy into the application (tomcat) container
->Why is the deployment and why it is ?
 Deployment : Deployment refers to the process of installing a web application on a server.
 (simple what every we build .jar/.war application file simple copying to application servers called deployment,but now days no one not do direct copy, we do deployment with tools)
Why Deployment:
 the main Reasons of the deployment are:
 .Installation of a web-application on web-server.
.Adding new features to the application and fixing bugs.
.Enhancing existing features and improving performance.
.Breakdown large applications into microservices for better scalability and maintainability.

Practical

Deployment Flow


Step1:
Need to create two Instances Jenkins , tomcat and nexus

Jekins Server 
[root@ip-10-0-0-81 ~]# yum install -y java-17-amazon-corretto-devel git 
[root@ip-10-0-0-81 ~]# sudo wget -O /etc/yum.repos.d/jenkins.repo \     https://pkg.jenkins.io/redhat-stable/jenkins.repo
[root@ip-10-0-0-81 ~]# sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
[root@ip-10-0-0-81 ~]# sudo yum install jenkins
click y
[root@ip-10-0-0-81 ~]# systemctl start jenkins
Connect jenkins public ip  http://54.195.135.216:8080/
[root@ip-10-0-0-81 jenkins]# cat /var/lib/jenkins/secrets/initialAdminPassword
dd9c30c4b0b2451098c09233c9d60ab9
click initiated create user admin/ required password 

Tomcat server :tomcat-users.xml using for some roles to add, '56d' d means delete 
Take public ip for the server 

tomcat.sh script 
yum install java-17-amazon-corretto -y
wget https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.7/bin/apache-tomcat-11.0.7.tar.gz
(This is not final one you main url check latest version and download)
tar -zxvf apache-tomcat-11.0.7.tar.gz
sed -i '56  a\<role rolename="manager-gui"/>' apache-tomcat-11.0.7/conf/tomcat-users.xml
sed -i '57  a\<role rolename="manager-script"/>' apache-tomcat-11.0.7/conf/tomcat-users.xml
sed -i '58  a\<user username="tomcat" password="ccit123" roles="manager-gui, manager-script"/>' apache-tomcat-11.0.7/conf/tomcat-users.xml

sed -i '59  a\</tomcat-users>' apache-tomcat-11.0.7/conf/tomcat-users.xml
sed -i '56d' apache-tomcat-11.0.7/conf/tomcat-users.xml
sed -i '21d' apache-tomcat-11.0.7/webapps/manager/META-INF/context.xml
sed -i '22d'  apache-tomcat-11.0.7/webapps/manager/META-INF/context.xml
sh apache-tomcat-11.0.7/bin/startup.sh

[ec2-user@ip-10-0-1-93 ~]$ sudo -i
[root@ip-10-0-1-93 ~]# vi tomcat.sh
[root@ip-10-0-1-93 ~]# sh tomcat.sh
Tomcat started.
[root@ip-10-0-1-93 ~]#

Step2: Click Manager app after open the below url ,Login with password ,mentioned above script 
http://3.255.200.138:8080/ 
Below is the web application manager ,for this manager container ,we will hosting our application


Step3:Git project simple html webpage ,We plan deploy this into tomcat 
https://github.com/cloudcomputingintelugu/ccitwebsite-java

We need create one job in Jenkis  Click ok
Jobname: ccitpipe-prj 
Step4: Prior we need configure maven  given any name ccit-mvn click save
Dashboard> Manage Jenkins > Tools
For Containerization we need add one plugin

Dashboard> Manage Jenkins > Plugins >Available plugins 
enter text Deploy to Container click install (this artifact upload)
                Pipeline stage view    click install (This for view good )
                Nexus Artifact uploader click install(if required we can choose s3 artifact uploader)
Step5:add credentials 

Dashboard> Manage Jenkins >Credentials  Click system >Click Global credentials (unrestricted)
and Click add credentials 

Click Create, We can another way also while pipe scripts add there also we can supply the user and password,using pipline syntax script

now Java tomcat server not supported for .jar file to deploy , only support .war or .ear file only

  Or 
Another way to pipeline syntax script  
Simple Step :  Deployment war/ear to container 
Deploy War/Ear files :target/*war
Context patch :ccitwebsite (give any name)
Contaner : Tomcat :take latest version 
Credential: tomat/ccit123 (which is you given script)
Tomcat URL : http://54.185.135.216:8080/ (tomcat url public ip)



Step6: job which added early yar 
Jobname: ccitpipe-prj 
Definition add below script and build 
pipeline {
    agent any
    tools
     {
         maven 'ccit-mvn'
         
     }

    stages {
        stage('git') {
            steps {
                git 'https://github.com/cloudcomputingintelugu/ccitwebsite-java.git'
            }
        }
        stage('build') {
            steps {
                sh 'mvn clean package'
            }
        }
    }
}

Step7: build success 

After build success ,below you jenkins server this you artifacts myweb-0.0.1.war this you need to deploy(upload) in tomcat

[root@ip-10-0-0-81 target]# pwd
/var/lib/jenkins/workspace/ccitpipe-prj/target
[root@ip-10-0-0-81 target]# ls -lrt
total 4
drwxr-xr-x 4 jenkins jenkins   89 May 22 19:25 myweb-0.0.1
drwxr-xr-x 2 jenkins jenkins   28 May 22 19:25 maven-archiver
-rw-r--r-- 1 jenkins jenkins 4069 May 22 19:25 myweb-0.0.1.war

Click Job Pipline Generate syntax script text copy add this into out job pipline stage script

deploy adapters: [tomcat9(alternativeDeploymentContext: '', credentialsId: '732bd861-4be4-49a0-aba2-04a19991b198', path: '', url: 'http://3.255.200.138:8080/')], contextPath: 'ccitwebsite', war: 'target/*.war'

Step8: Click save and build 
pipeline {
    agent any
    tools
     {
         maven 'ccit-mvn'
         
     }

    stages {
        stage('git') {
            steps {
                git 'https://github.com/cloudcomputingintelugu/ccitwebsite-java.git'
            }
        }
        stage('build') {
            steps {
                sh 'mvn clean package'
            }
        }
        stage('tomcat') {
            steps {
              deploy adapters: [tomcat9(alternativeDeploymentContext: '', credentialsId: '732bd861-4be4-49a0-aba2-04a19991b198', path: '', url: 'http://3.255.200.138:8080/')], contextPath: 'ccitwebsite', war: 'target/*.war'
             }
        }
    }
}
Step9: after build tomcat add and build success

Step10: See below screen shot which we have given name ccitwebsite came to tomcat site
We can able host multiple website also using this 
Step11:Website hosted 


   Above sofar done we have placed through build artifacats moved from maven to tomcat  
  Now we plan to go with Nexus move from artifacts nexus to tomcat prior stop this webhost
Now page is open line this ,after stopped 

HTTP Status 404 – Not Found


Type Status Report

Message The requested resource [/ccitwebsite/] is not available

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.


Apache Tomcat/11.0.7


Nexus 
https protocol need run our website need to certificate ssl aws ssl certificate is free

 We need purchase domain or Route 53 
The service in AWS Console "Certificate manager "
>click List Certificate 

Note :- While start nexus with micro amazon Linux facing below issue ,so Nexus minimum small Linux required it is chargeable 

Launch a t2.micro EC2 with Amazon Linux 2 Failed

For Nexus minimum Linux small, with micro free tier facing issue [root@ip-10-0-1-241 bin]# ./nexus run
WARNING: ************************************************************
WARNING: Detected execution as "root" user.  This is NOT recommended!
WARNING: ************************************************************
OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x000000078f550000, 1890254848, 0) failed; error='Not enough space' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 1890254848 bytes. Error detail: committing reserved memory.
# An error report file with more information is saved as:
# /opt/nexus/nexus-3.80.0-06/hs_err_pid7284.log
[root@ip-10-0-1-241 bin]# Connection to 3.249.223.100 closed by remote host.
Connection to 3.249.223.100 closed

Step1: Nexus Launching  
Amazon linux 
Instance type:t2.medium
Storge changed 20 GB

Step2:
Java Installation

[ec2-user@ip-10-0-0-244 ~]$ sudo -i
[root@ip-10-0-0-244 ~]# sudo yum install -y java-17-amazon-corretto-devel

Step3: Need Install Nexus in linux,for software always go with prior url 
https://help.sonatype.com/en/download.html
and Download latest version above url take you latest Unix Nexus repository right click url save copy link address 

[root@ip-10-0-0-244 ~]#wget https://download.sonatype.com/nexus/3/nexus-3.80.0-06-linux-x86_64.tar.gz
[root@ip-10-0-0-244 ~]#tar -zxvf nexus-3.80.0-06-linux-x86_64.tar.gz

Step4:User creation nexus 
[root@ip-10-0-0-244 ~]# useradd nexus
[root@ip-10-0-0-244 ~]# ls -lrt
total 416436
drwxr-xr-x 3 root root        20 May  2 20:55 sonatype-work
-rw-r--r-- 1 root root 426428974 May  6 16:15 nexus-3.80.0-06-linux-x86_64.tar.gz
drwxr-xr-x 6 root root        94 May 23 10:23 nexus-3.80.0-06
[root@ip-10-0-0-244 ~]# chown nexus:nexus nexus-3.80.0-06 sonatype-work
[root@ip-10-0-0-244 ~]# ls -lrt
total 416436
drwxr-xr-x 3 nexus nexus        20 May  2 20:55 sonatype-work
-rw-r--r-- 1 root  root  426428974 May  6 16:15 nexus-3.80.0-06-linux-x86_64.tar.gz
drwxr-xr-x 6 nexus nexus        94 May 23 10:23 nexus-3.80.0-06

Step5: In the nexus file need to change line number 27 run_as_user='' to run_as_user='nexus'
[root@ip-10-0-0-244 bin]# vi nexus
:set number 
(set number command for vi script number display)
 26 # user to execute as; optional but recommended to set
 27 run_as_user='nexus'

[root@ip-10-0-0-244 opt]# mkdir -p /opt/sonatype-work/nexus3

 Getting some permission issue 
[root@ip-10-0-0-244 bin]# ./nexus start
-bash: /root/nexus-3.80.0-06/bin/nexus: Permission denied
[root@ip-10-0-0-244 bin]# mkdir -p /opt/nexus
[root@ip-10-0-0-244 bin]# mv /root/nexus-3.80.0-06 /opt/nexus/
[root@ip-10-0-0-244 bin]# cd /opt/nexus/nexus-3.80.0-06/bin
[root@ip-10-0-0-244 bin]# ./nexus start
Last login: Fri May 23 10:26:39 UTC 2025 on pts/0
mkdir: cannot create directory ‘/opt/nexus/nexus-3.80.0-06/../sonatype-work’: Permission denied
realpath: ‘/opt/nexus/nexus-3.80.0-06/../sonatype-work/nexus3’: No such file or directory
mkdir: cannot create directory ‘/log’: Permission denied
Starting nexus
/opt/nexus/nexus-3.80.0-06/bin/nexus: line 175: /nexus.pid: Permission denied
[root@ip-10-0-0-244 bin]# cd  /opt/nexus/nexus-3.80.0-06
[root@ip-10-0-0-244 nexus-3.80.0-06]# ls
bin  deploy  etc  jdk  NOTICE.txt  OSS-LICENSE.txt
[root@ip-10-0-0-244 opt]# chown -R nexus:nexus /opt/sonatype-work -R
[root@ip-10-0-0-244 opt]# sudo passwd nexus
Changing password for user nexus.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@ip-10-0-0-244 opt]# sudo chown -R nexus:nexus /opt/nexus
[root@ip-10-0-0-244 opt]# export NEXUS_DATA=/opt/nexus/sonatype-work
[root@ip-10-0-0-244 opt]# sudo su - nexus
Last login: Fri May 23 10:33:20 UTC 2025 on pts/0
[nexus@ip-10-0-0-244 ~]$ /opt/nexus/nexus-3.80.0-06/bin/nexus start
Starting nexus
[nexus@ip-10-0-0-244 ~]$ /opt/nexus/nexus-3.80.0-06/bin/nexus stop
Shutting down nexus
Stopping nexus running with pid 9738
Step6: Take the public Ip for the nexus instance default port 8081
Login with user admin password 
[nexus@ip-10-0-0-244 opt]$ cat /opt/nexus/sonatype-work/nexus3/admin.password
88d25272-af46-45b7-adf9-ae42f115d1da

Click Sign in Next > Choose Radio Disable anonymous access >Click next 

Step7: Click repository ,select maven2(hosted)
Step8: Give any repo name in nexus repository and select deployment policy Allow redeploy

Step9:Created one repository in nexus successfully
Step10: Jenkins pipline job, need to added nexus script step,with that take help of pipeline syntax script ,Select below options, Process url past nexus url with out http
 10.1

 10.2 add admin credential of nexus Groupid,version get it from the Git Pom.xml,Repository ccitrepo nexus repo name




















  



10.3: Artifact id ,type of file war , war file location in jenkins server ,click generate pipline script


10.4 Git Project below pom.xml 


Step11: ccitpipelinejob >Configuration > add below script ,nexus script you need replace with your pipline syntax script 

pipeline {

    agent any

    tools

     {

         maven 'ccit-mvn'

    }

    stages {

        stage('git') {

            steps {

                git 'https://github.com/cloudcomputingintelugu/ccitwebsite-java.git'

            }

        }

        stage('build') {

            steps {

                sh 'mvn clean package'

            }

        }

        stage('nexus') {

            steps {

   deploy adapters: [tomcat9(alternativeDeploymentContext: '', credentialsId: '8cf5a8fa-814b-4f44-9834-510bd831b76e', path: '', url: 'http://3.250.85.185:8080/')], contextPath: 'ccitwebsite', war: 'target/*.war'

         }

        }

        stage('tomcat') {

            steps {

deploy adapters: [tomcat9(alternativeDeploymentContext: '', credentialsId: '8cf5a8fa-814b-4f44-9834-510bd831b76e', path: '', url: 'http://3.250.85.185:8080/')], contextPath: 'ccitwebsite1', war: 'target/*.war' }

        }

    }

}

Here is the screen shot script need add click save the job 

Step 12. Save job and Build it is completed Successfully, with stage view you see nexus added file transfer to tomcat


Step13: See here below screen shot , nexus repo .war file deployed successfully, in the nexus pipeline script, for difference first example maven to tomcat, second example maven ->nexus->tomcat  hostname change "ccitwebsite1"


Step14: See below Application path newly came /ccitwebsite1


Step2: After click, web host page opened successfully.


--Thanks 

                              Using S3 Bucket instead of Nexus High-end server paid not free-tier

Step1: Using I am  Service create one user jenkins-s3-user, Add policy "AmazonS3FullAccess" and create


Step2:Add Security key for the user ,download for better

Step3: 
Source :Jenkin Server already Java,Git and Jenkis installed started Jenkin service
Create one simple S3 bucket in aws >  
Name Bucket :ccitnexusbucket
Bucket type :General Purpose
Object ownership : ASLs disabled(recommended) default
Created one bucket

Step4:
Jenkins Dashboard → Manage Jenkins →plugins  >Available plugins 
S3 publisher, (optional Stage viewer)

Step5: After Added Plugins , Need to attach Iam user( i.e 
jenkins-s3-user Download Accesskey ) to S3 profile below navigation 
Jenkins Dashboard → Manage Jenkins >System 
 Profile :aws-profile (given name)
Access key:XXXXXX(which you have download IAM User )
Secret key:XXXXX  
Click Save

Step6: For Generate Pipeline script use job Pipeline syntax 
Sample step: General Build Step
Build Step :Publish Artifacts to S3 Bucket
S3 Profile : aws-profile (it came automatically which you have add profile above step) 
Source :target/*.war (Jenkins sever where you artifacts)
Destination bucket: ccitnexusbucket
Bucket Region : eu-west-1 (where you bucket region)


Click Generate pipeline script ,copy the test 
Step7:Genkins > Create one job pipeline job  Click save and Build 
pipeline {
    agent any
     tools{
         maven 'ccit-mvn'
     }
    stages {
        stage('git') {
            steps {
                git  'https://github.com/cloudcomputingintelugu/ccitwebsite-java.git'
            }
        }
        stage('build') {
            steps {
                sh  'mvn clean package'
            }
        }
        stage('Upload to S3') {
            steps {
             s3Upload consoleLogLevel: 'INFO', dontSetBuildResultOnFailure: false, dontWaitForConcurrentBuildCompletion: false, entries: [[bucket: 'ccitnexusbucket', excludedFile: '', flatten: false, gzipFiles: false, keepForever: false, managedArtifacts: false, noUploadOnFailure: false, selectedRegion: 'eu-west-1', showDirectlyInBrowser: false, sourceFile: 'target/*.war', storageClass: 'STANDARD', uploadFromSlave: false, useServerSideEncryption: false]], pluginFailureResultConstraint: 'FAILURE', profileName: 'aws-profile', userMetadata: []
        
             }
        }
    }
}
Step8: Build success, See below S3 upload also success

Step9:See below s3 ccitnexusbucket artifacts uploaded successfully. 


Step10: 
Create one instance >tomcat Server  >  execute below script root 

tomcat.sh script 
yum install java-17-amazon-corretto -y
wget https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.7/bin/apache-tomcat-11.0.7.tar.gz
(This is not final one you main url check latest version and download)
tar -zxvf apache-tomcat-11.0.7.tar.gz
sed -i '56  a\<role rolename="manager-gui"/>' apache-tomcat-11.0.7/conf/tomcat-users.xml
sed -i '57  a\<role rolename="manager-script"/>' apache-tomcat-11.0.7/conf/tomcat-users.xml
sed -i '58  a\<user username="tomcat" password="ccit123" roles="manager-gui, manager-script"/>' apache-tomcat-11.0.7/conf/tomcat-users.xml

sed -i '59  a\</tomcat-users>' apache-tomcat-11.0.7/conf/tomcat-users.xml
sed -i '56d' apache-tomcat-11.0.7/conf/tomcat-users.xml
sed -i '21d' apache-tomcat-11.0.7/webapps/manager/META-INF/context.xml
sed -i '22d'  apache-tomcat-11.0.7/webapps/manager/META-INF/context.xml
sh apache-tomcat-11.0.7/bin/startup.sh
Step11: login tomcate public ip 

Step12: 
Another way to pipeline syntax script  
Simple Step :  Deployment war/ear to container 
Deploy War/Ear files :target/*war
Context patch :ccitwebsite (give any name)
Contaner : Tomcat :take latest version 
Credential: tomat/ccit123 (which is you given script)
Tomcat URL : http://18.201.159.187:8080/ (tomcat url public ip)
deploy adapters: [tomcat9(alternativeDeploymentContext: '', credentialsId: 'ff93c1a6-d180-4658-a61f-e43c790065a5', path: '', url: 'http://18.201.159.187:8080/')], contextPath: 'ccitwebsite', war: 'target/*.war'


 stage('tomcat') {
            steps {
                deploy adapters: [tomcat9(alternativeDeploymentContext: '', credentialsId: 'ff93c1a6-d180-4658-a61f-e43c790065a5', path: '', url: 'http://18.201.159.187:8080/')], contextPath: 'ccitwebsite', war: 'target/*.war'
            }
        }

Full script : Step7 add above script

Step13: Build Completed Successfully

Step14: Webpage hosted in tomcat

Step15: Web hostname open successfully


--Thanks 

Tuesday, May 20, 2025

Jenkins part8

Jenkins part8

Class 28th Jenkins May 20th

Prometheus and Grafana 

Prometheus and Grafana are two widely used tools for monitoring and Observability in cloud-native environments 

Prometheus main query based ( monitoring cpu memory,network bits it get the data from nodes and transferred to Grafana there we can see visually,graphic format)

Prometheus and Grafana both are open source monitoring and alerting toolkit originally developed by  soundcloud  and is now part of the cloud native computing foundation(CNCF)

Features:

Time-series database: Stores metrics as time-series data.

Pull-based metrics collection:Scrapes metrics from targets (applications, services )over HTTP

Powerful query language (PromQL):Allows flexible querying and data analysis.

Alerting:Integrated with Alert manager to send alerts via email,slack,pagerduty,etc.

Service discovery:Supports Kubernetes,Ec2,consul,and static Configurations.


Cloud Watch( This also monitor the health check of the server)
Cloud trail (This monitor complete account)
Prometheus default port 9090 and Grafana default port  3000 ,Prometheus collect data source of the server information Grafana with that data showing in Graph GUI 
Grafana : Grafana Open source data visualization and monitoring tool used to display metrics from Prometheus and other data sources.
Features : Supports multiple data source:works with Prometheus,InfluxDB,Mysql ,AWS Cloud Watch ,Elasticsearch ,etc.
Rich Visualization Graphs,heatmaps,pie charts and dashboard 
Alerting system :Triggers alerts when thresholds are breached.


Practical
Step1: Need to create the servers 3 one Prometheus ,2 for create worker1,worker2 , for understand two enough i have not create worker3

Created Three servers 

Server1 

[root@ip-10-0-2-7 ~]# hostnamectl set-hostname  prometgrafa 
[root@ip-10-0-2-7 ~]# exit
logout
[ec2-user@ip-10-0-2-7~]$ sudo -i
[root@prometgrafa ~]#

Server2
[ec2-user@ip-10-0-1-114 ~]$ sudo -i
[root@ip-10-0-1-114 ~]# hostnamectl set-hostname worker1
[root@ip-10-0-1-114 ~]# exit
logout
[ec2-user@ip-10-0-1-114 ~]$ sudo -i

Server3
[ec2-user@ip-10-0-0-39 ~]$ sudo -i
[root@ip-10-0-0-39 ~]# hostnamectl set-hostname worker2
[root@ip-10-0-0-39 ~]# exit
logout
[root@worker2~]#

Step2: Copy past prometgrafa server and execute 
https://github.com/cloudcomputingintelugu/ccit-scripts/blob/main/promgraf.sh

[ec2-user@ip-10-0-2-7 ~]$ sudo -i
[root@worker2 ~]# vi pandg.sh
[root@worker2 ~]# sh pandg.sh

Executed Successfully

Step3: for the script promgraf.sh taken from #NODEEXPORTER line 79 upto end copy 
run in worker1 and worker2

[root@worker1 ~]# vi node.sh
[root@worker1 ~]# sh node.sh

[root@worker2 ~]# vi node.sh
[root@worker2 ~]# sh node.sh

Executed Successfully

Step4: Need to check  Prometheus  and Grafana server how to configure using public ip

Select the status go to target: first endpoint state automatically up because it is same server,remaing to down due to it is different servers need to configure the node


Step5:As given script prometgrafa server below path need edit the hosts file add public ip of the two worker servers and save 
example script:
#3.101.56.72  worker-1
#54.193.223.22 worker-2
#sudo vim /etc/hosts
[root@prometgrafa ~]# sudo vim /etc/hosts
[root@prometgrafa ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost6 localhost6.localdomain6
52.212.144.119  worker-1
108.129.132.47  worker-2

After refresh ,worker1,worker2 also went up now.
See here below inbuilt some query there you can select cpu take any and execute ,you see below ouput
get the raw data from three servers ,If you more GUI dashboard level you need send the data 
better view in Grafana.
cpu
 Memory

Above Grafana also included ,if you are facing issue ,Please use below command execute the server 

[root@prometgrafa ~]#wget https://dl.grafana.com/oss/release/grafana-10.3.3-1.x86_64.rpm
[root@prometgrafa ~]#sudo yum install grafana-10.3.3-1.x86_64.rpm -y

[root@prometgrafa ~]#ssudo systemctl daemon-reexec
[root@prometgrafa ~]#ssudo systemctl start grafana-server
[root@prometgrafa ~]#ssudo systemctl enable grafana-server
[root@prometgrafa ~]#ssudo systemctl status grafana-server

Grafana default port 3000, default username and password admin/ admin


Step6: Click add new dashboard add visualization ,need click configure a new data source 


our Source is Prometheus ,See below we have many data source options 

You need give your Prometheus url and click save & test
Step7: Now  configure this to dashboard  if you have knowledge create your own dashboard ,elase we can use some existing templates 1860 import dashboard click load and select you data source prometheus  and click import

 This prometheus  local exist server GUI

 This for Work1 healthchecks
  

--Thanks 

                                              Communicate to Post Build status to slack 
Post Build actions in Jenkins communicate to slack Messager channel
Step1: Launch one instance created



Step2: Create one slack account sign-in with Gmail account Click Create workspace 
Select start with free
https://slack.com/
Give Workspace name and username click next ,if you want add you team members also we can add
as of now not added

Step3: Create new Channel >Blank Channel > radio button Public  give channel name
#ccitbuildstatus
Now Slack channel ready
Step4: Which created Instance install jenkins git and java in the server 
[ec2-user@ip-10-0-3-187 ~]$ sudo -i
[root@ip-10-0-3-187 ~]# yum install -y java-17-amazon-corretto-devel git
[root@ip-10-0-3-187 ~]#sudo wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat-stable/jenkins.repo
[root@ip-10-0-3-187 ~]#
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
[root@ip-10-0-3-187 ~]# sudo yum install jenkins
click y
[root@ip-10-0-3-187  ~]# systemctl status jenkins
[root@ip-10-0-3-187  ~]# systemctl start jenkins
[root@ip-10-0-3-187  ~]# cd /var/lib/jenkins
[root@ip-10-0-3-187 jenkins]# ls -lrt
total 20
drwxr-xr-x 3 jenkins jenkins   21 May 22 09:16 %C
-rw-r--r-- 1 jenkins jenkins   64 May 22 09:16 secret.key
-rw-r--r-- 1 jenkins jenkins    0 May 22 09:16 secret.key.not-so-secret
drwxr-xr-x 2 jenkins jenkins    6 May 22 09:16 plugins
drwxr-xr-x 2 jenkins jenkins    6 May 22 09:16 jobs
-rw-r--r-- 1 jenkins jenkins  156 May 22 09:16 hudson.model.UpdateCenter.xml
-rw-r--r-- 1 jenkins jenkins  171 May 22 09:16 jenkins.telemetry.Correlator.xml
drwxr-xr-x 2 jenkins jenkins   24 May 22 09:16 userContent
-rw-r--r-- 1 jenkins jenkins 1037 May 22 09:16 nodeMonitors.xml
drwxr-xr-x 3 jenkins jenkins   56 May 22 09:16 users
drwx------ 2 jenkins jenkins   91 May 22 09:16 secrets
-rw-r--r-- 1 jenkins jenkins 1660 May 22 09:16 config.xml
drwxr-xr-x 2 jenkins jenkins   67 May 22 09:16 updates
[root@ip-10-0-3-187  ~]# cd /var/lib/jenkins/secrets/
[root@ip-10-0-3-187  secrets]# cat initialAdminPassword
Step4: http://3.249.93.238:8080/ login with initialAdminPassword password give admin password 
create job and build 


Step5:After build Jenkin folder

[root@ip-10-0-3-187 secrets]# cd /var/lib/jenkins/
[root@ip-10-0-3-187 jenkins]# ls -lrt
total 52
drwxr-xr-x  3 jenkins jenkins   21 May 22 09:16 %C
-rw-r--r--  1 jenkins jenkins   64 May 22 09:16 secret.key
-rw-r--r--  1 jenkins jenkins    0 May 22 09:16 secret.key.not-so-secret
-rw-r--r--  1 jenkins jenkins  156 May 22 09:16 hudson.model.UpdateCenter.xml
-rw-r--r--  1 jenkins jenkins  171 May 22 09:16 jenkins.telemetry.Correlator.xml
drwxr-xr-x  2 jenkins jenkins   24 May 22 09:16 userContent
-rw-r--r--  1 jenkins jenkins 1037 May 22 09:16 nodeMonitors.xml
-rw-r--r--  1 jenkins jenkins 1660 May 22 09:16 config.xml
drwxr-xr-x 90 jenkins jenkins 8192 May 22 09:36 plugins
-rw-------  1 jenkins jenkins 1680 May 22 09:36 identity.key.enc
drwxr-xr-x  2 jenkins jenkins  149 May 22 09:36 updates
-rw-r--r--  1 jenkins jenkins  370 May 22 09:36 hudson.plugins.git.GitTool.xml
drwxr-xr-x  2 jenkins jenkins   32 May 22 09:36 logs
drwxr-xr-x  3 jenkins jenkins   56 May 22 09:36 users
-rw-r--r--  1 jenkins jenkins  182 May 22 09:37 jenkins.model.JenkinsLocationConfiguration.xml
-rw-r--r--  1 jenkins jenkins    7 May 22 09:37 jenkins.install.InstallUtil.lastExecVersion
-rw-r--r--  1 jenkins jenkins    7 May 22 09:37 jenkins.install.UpgradeWizard.state
drwxr-xr-x  3 jenkins jenkins   22 May 22 09:37 jobs
drwxr-xr-x  3 jenkins jenkins   22 May 22 09:37 workspace
drwx------  2 jenkins jenkins  237 May 22 09:37 secrets

Step6: We need install the plugin in Jenkins 
Dashboard >manage Jenkins >Plugins >Available Plugins  >type slack 
Choose Slack Notification  click install After installed successfully.

Step7:Need to configure slack 
 Manage Jenkins >System 
 here we need give workspace credential and channel information need to add


Prior to that We need configure Jenkins in slack 


Step8:Type Jenkins CI and the Click add to Slack 

Step9: add which channel to communicate 
Step10: After click Add Jenkins CI Integration ,they will give instructions please do follow


Step11: Team Sub domain name take and configured in jenkins system configuration
 ccit-epl5538  
Click Add credentials  take the Integration token credential ID iLba4X9G2sX2T3AZYPgeLcPR
Step12:
After added secret text select credentials secret text  and then click save ,With out that build will alert will not go so need remember this 

Step13:
Create one New job 
>New item >  name : ccitpipelinejob  >Select Pipleline option  click Ok 
pipeline {
    agent any

    stages {
        stage('Code') {
            steps {
                echo 'This is Code stage'
            }
        }
        stage('Dev') {
            steps {
                echo 'This is Dev stage'
            }
        }
           stage('Test') {
            steps {
                echo 'This is Test stage'
            }
        }
           stage('Deploy') {
            steps {
                echo 'This is Deplyoment stage'
            }
        }
    }
}

Build Success All stage below, but we need setup Post build setup for slack alert
Step14:
Here below click the job and select the option pipline script syntax , given your slack channel name ccitbuildstatus and Click Generate pipeline script, copy generate the text 

Step15:After copy the above text your job pipline script add below script and save and Build click the Job 

pipeline {
    agent any

    stages {
        stage('Code') {
            steps {
                echo 'This is Code stage'
            }
        }
        stage('Dev') {
            steps {
                echo 'This is Dev stage'
            }
        }
           stage('Test') {
            steps {
                echo 'This is Test stage'
            }
        }
           stage('Deploy') {
            steps {
                echo 'This is Deplyoment stage'
            }
        }
       }    
       post {
            success{
              slackSend channel: 'ccitbuildstatus', message: 'Build Success !...', tokenCredentialId: '3d9bccaf-39e3-4455-b1a4-c4e6f620da31'                }
             failure{
               slackSend channel: 'ccitbuildstatus', message: 'Build Failed !...', tokenCredentialId: '3d9bccaf-39e3-4455-b1a4-c4e6f620da31'                }
        }
    
}
Step16:See Alert capture Build success , we will plan to do one some wrong changes make build failed ,will see build failed alert received or not

Build Failed 
Stage code script remove o   ech 'This is Code stage' , so it was failed in code stage alert also received in the slacked Failed Build




 --Thanks