Advertisement
bittertea

Devops2

May 20th, 2025 (edited)
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.28 KB | None | 0 0
  1. ######### LAB 2 ########
  2.  
  3. mvn --version
  4.  
  5. mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.5 -DinteractiveMode=false
  6.  
  7. cd my-app
  8.  
  9. mvn compile
  10.  
  11. mvn package
  12.  
  13. java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
  14.  
  15.  
  16.  
  17.  
  18. ######### LAB 3 #########
  19.  
  20. gradle -v
  21.  
  22. mkdir gradle_project
  23.  
  24. cd gradle_project
  25.  
  26. gradle init --type java-application
  27.  
  28. // if error after above command:
  29. JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64/
  30. PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
  31. export JAVA_HOME
  32.  
  33. gradle build
  34.  
  35. gradle run
  36.  
  37. //Open build.gradle and add below code:
  38.  
  39. task hello {
  40. doLast {
  41. println 'Hello Gradle!'
  42. }
  43. }
  44.  
  45. gradle hello
  46.  
  47.  
  48.  
  49.  
  50. ######### LAB 4 #########
  51.  
  52. mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.5 -DinteractiveMode=false
  53.  
  54. cd my-app
  55.  
  56. mvn compile
  57.  
  58. mvn package
  59.  
  60. // if error in above commands:
  61.  
  62. // if pom.xml file does not have property tag then type this code BELOW url tag and ABOVE dependencies tag:
  63. <properties>
  64. <maven.compiler.source>1.6</maven.compiler.source>
  65. <maven.compiler.targer>1.6</maven.compiler.target>
  66. </properties>
  67.  
  68. // OR if it has property tag, then change 17 to 11 in property tag
  69. // run mvn compile and mvn package commands again and the error should be resolved
  70.  
  71. gradle init --type pom
  72.  
  73. // if error after above command then run below 3 commands:
  74. JAVA_HOME=/usr/bin/jvm/java-11-openjdk-amd64
  75. export PATH=$JAVA_HOME/bin:$PATH
  76. export JAVA_HOME
  77.  
  78. // in build.gradle file:
  79. //add line:
  80. apply plugin: 'application'
  81. //change 1.5 to 1.6 such that:
  82. sourceCompatibility = 1.6
  83. targetCompatibility = 1.6
  84. //at the end add below code:
  85. mainClassName = 'com.mycompany.app.App'
  86.  
  87. gradle build
  88.  
  89. gradle run
  90.  
  91.  
  92.  
  93.  
  94. ######### LAB 6 #########
  95.  
  96.  
  97. mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.5 -DinteractiveMode=false
  98.  
  99. cd my-app/
  100.  
  101. mvn package
  102.  
  103. // login to github account, click on Create Repository
  104. // Give the repo a name (lab-6-devops), click the "Add ReadMe file" and confirm
  105.  
  106. git config --global user.email "[email protected]"
  107. git config --global user.name "stelister"
  108. git init
  109. git add pom.xml
  110. git commit -m "commit maven file"
  111. ssh-keygen -t ed25519 -C "[email protected]"
  112. cat /home/student/.ssh/id_ed25519.pub
  113.  
  114. // go to github Settings > SSH & GPG Keys and click on "New ssh key"
  115. // give a title and paste the output of the cat command under 'Key', then click on 'Add SSH key'
  116.  
  117. git remote add origin [email protected]:stelister/lab-6-devops
  118. git config --global push.autoSetupRemote true
  119. git push origin main
  120.  
  121. // if error after above command:
  122. git pull --rebase origin main
  123.  
  124. // now retry:
  125. git push origin main
  126.  
  127. sudo systemctl start jenkins
  128. sudo systemctl status jenkins
  129.  
  130. // open jenkins by navigating to 'localhost:8080' in browser
  131. // login: username: admin
  132. sudo cat var/lib/jenkins/secrets/initialAdminPassword
  133. // paste password
  134.  
  135. // in dashboard, click on "New Item", enter Item name and select "Pipelin", click "Ok"
  136. // scroll down and type the following script:
  137.  
  138. pipeline
  139. {
  140. agent any
  141. stages
  142. {
  143. stage('Checkout')
  144. {
  145. steps
  146. {
  147. git branch: 'main', url: 'https://github.com/stelister/lab-6-devops.git';
  148. }
  149. }
  150.  
  151. stage('Build')
  152. {
  153. steps
  154. {
  155. sh 'mvn clean package'
  156. }
  157. }
  158.  
  159. stage('Test')
  160. {
  161. steps
  162. {
  163. sh 'mvn test'
  164. }
  165. }
  166. }
  167. }
  168.  
  169. // click on 'Save', from the dashboard click on 'Build Now'
  170. // then click on the build and go to 'Console Output'
  171.  
  172.  
  173.  
  174.  
  175.  
  176. ######### LAB - 7 #########
  177.  
  178. make a folder called "lab7" in the home folder
  179.  
  180. open this folder
  181.  
  182. create these new files using gedit in this folder-
  183. hosts.ini
  184. playbook.yml
  185. playbook2.yml
  186.  
  187. create another folder in this folder called "library"
  188.  
  189. go to the "library" folder and create a new file in the "library" folder called-
  190. hello_world.py
  191.  
  192. ---------
  193.  
  194. In "hosts.ini" file paste below code using gedit-
  195. [local]
  196. localhost ansible_connection=local
  197.  
  198. ---------
  199.  
  200. In "playbook.yml" file paste below code using gedit-
  201. - name: 1st Playbook
  202. hosts: local
  203. connection: local
  204. tasks:
  205. - name: Print simple message
  206. debug:
  207. msg: "Hello, welcome to Ansible..."
  208.  
  209. ---------
  210.  
  211. In "playbook2.yml" file paste below code using gedit-
  212. - name: Test custom module
  213. hosts: localhost
  214. gather_facts: no
  215. tasks:
  216. - name: Say Hello
  217. hello_world:
  218. name: "Ansible"
  219. register: result
  220. - name: Show message
  221. debug:
  222. msg: "{{result.message}}"
  223.  
  224. ---------
  225.  
  226. In the "hello_world.py" file paste the below code using gedit-
  227. #!/usr/bin/python
  228. from ansible.module_utils.basic import AnsibleModule
  229.  
  230. def run_module():
  231. module_args=dict(name=dict(type='str',required=True))
  232. module=AnsibleModule(argument_spec=module_args)
  233. result={"changed":False, "message":f"Hello, {module.params['name']}!"}
  234. module.exit_json(**result)
  235.  
  236. if __name__=="__main__":
  237. run_module()
  238.  
  239. ---------
  240.  
  241.  
  242. Now, right-click open terminal inside "lab7" folder and paste and enter below commands one-by-one for output:
  243.  
  244. $ ansible all -i hosts.ini --list-hosts
  245.  
  246. $ ansible all -i hosts.ini -m ping
  247.  
  248. $ ansible-playbook -i hosts.ini playbook.yml
  249.  
  250. $ chmod +x library/hello_world.py
  251.  
  252. $ ansible-playbook -i hosts.ini playbook2.yml
  253.  
  254.  
  255.  
  256.  
  257.  
  258. ######### LAB 8 #########
  259.  
  260.  
  261.  
  262. // Repeat the following steps from Lab-6:
  263.  
  264. mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.5 -DinteractiveMode=false
  265.  
  266. cd my-app/
  267.  
  268. mvn package
  269.  
  270. // login to github account, click on Create Repository
  271. // Give the repo a name (devops-lab-10), click the "Add ReadMe file" and confirm
  272.  
  273. git config --global user.email "[email protected]"
  274. git config --global user.name "stelister"
  275. git init
  276. git add pom.xml
  277. git commit -m "commit maven file"
  278. ssh-keygen -t ed25519 -C "[email protected]"
  279. cat /home/student/.ssh/id_ed25519.pub
  280.  
  281. // go to github Settings > SSH & GPG Keys and click on "New ssh key"
  282. // give a title and paste the output of the cat command under 'Key', then click on 'Add SSH key'
  283.  
  284. git remote add origin [email protected]:stelister/devops-lab-10
  285. git config --global push.autoSetupRemote true
  286. git push origin main
  287.  
  288. // if error after above command:
  289. git branch -M main
  290. // repeat
  291. git push origin main
  292. // if error still persists:
  293. git pull --rebase origin main
  294.  
  295. // now retry:
  296. git push origin main
  297.  
  298.  
  299. // NOW, create an empty jar file:
  300. gedit t.jar
  301.  
  302.  
  303. sudo systemctl start jenkins
  304. sudo systemctl status jenkins
  305.  
  306. // open jenkins by navigating to 'localhost:8080' in browser
  307. // login: username: admin
  308. sudo cat /var/lib/jenkins/secrets/initialAdminPassword
  309. // paste password
  310.  
  311. // in dashboard, click on "New Item", enter Item name and select "Pipeline", click "Ok"
  312. // scroll down and type the following script:
  313.  
  314. pipeline
  315. {
  316. agent any
  317. stages
  318. {
  319. stage("Checkout")
  320. {
  321. steps
  322. {
  323. git branch: 'main', url : 'https://github.com/stelister/devops-lab-10.git'
  324. }
  325. }
  326.  
  327. stage("Build")
  328. {
  329. steps
  330. {
  331. sh 'mvn clean package'
  332. }
  333. }
  334.  
  335. stage("Test")
  336. {
  337. steps
  338. {
  339. sh 'mvn test'
  340. }
  341. }
  342.  
  343. stage("Archive Artifacts")
  344. {
  345. steps
  346. {
  347. archiveArtifacts artifacts: '**/target/*.jar', allowEmptyArchive: true
  348. }
  349. }
  350.  
  351. stage("Deploy")
  352. {
  353. steps
  354. {
  355. sh """
  356. export ANSIBLE_HOST_KEY_CHECKING=False
  357.  
  358. ansible-playbook -i hosts.ini mydeploy.yml --extra-vars='ansible_become_pass=exam@cse'
  359.  
  360. """
  361. }
  362. }
  363. }
  364. }
  365.  
  366. // click on "Save"
  367.  
  368. // Click on "Build Now"
  369. // !! You must click build now, and let your 1st build fail purposely to proceed further
  370.  
  371.  
  372.  
  373.  
  374. //create hosts.ini:
  375. sudo nano /var/lib/jenkins/workspace/<Your pipeline name>/hosts.ini
  376.  
  377. //code inside hosts.ini:
  378. [local]
  379. localhost ansible_connection=local
  380.  
  381. // Save and exit nano::: Ctrl + o > Press enter > Ctrl + x
  382.  
  383. //Create a playbook mydeploy.yml by executing the following command:
  384. sudo nano /var/lib/jenkins/workspace/<Your pipeline name>/mydeploy.yml
  385.  
  386. //Paste below code inside mydeploy.yml:
  387. ---
  388. - name: Deploy Artifact to Localhost
  389. hosts: localhost
  390. tasks:
  391. - name: Copy the artifact to the target location
  392. become: true
  393. become_user: student
  394. become_method: su
  395. copy:
  396. src: "/var/lib/jenkins/workspace/< Your pipeline name >/target/my-app-1.0-SNAPSHOT.jar"
  397. dest: "/home/student/t.jar"
  398.  
  399.  
  400.  
  401.  
  402. // Save and exit nano::: Ctrl + o > Press enter > Ctrl + x
  403.  
  404. // go to jenkins and click "Build Now" again
  405. // Click on “Stages” in the Jenkins Dashboard to see the final result showing all the 5 stages in green tick.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement