diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..969d00c --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,121 @@ +pipeline { + + agent any +/* + tools { + maven "maven3" + } +*/ + environment { + NEXUS_VERSION = "nexus3" + NEXUS_PROTOCOL = "http" + NEXUS_URL = "172.31.40.209:8081" + NEXUS_REPOSITORY = "vprofile-release" + NEXUS_REPOGRP_ID = "vprofile-grp-repo" + NEXUS_CREDENTIAL_ID = "nexuslogin" + ARTVERSION = "${env.BUILD_ID}" + } + + stages{ + + stage('BUILD'){ + steps { + sh 'mvn clean install -DskipTests' + } + post { + success { + echo 'Now Archiving...' + archiveArtifacts artifacts: '**/target/*.war' + } + } + } + + stage('UNIT TEST'){ + steps { + sh 'mvn test' + } + } + + stage('INTEGRATION TEST'){ + steps { + sh 'mvn verify -DskipUnitTests' + } + } + + stage ('CODE ANALYSIS WITH CHECKSTYLE'){ + steps { + sh 'mvn checkstyle:checkstyle' + } + post { + success { + echo 'Generated Analysis Result' + } + } + } + + stage('CODE ANALYSIS with SONARQUBE') { + + environment { + scannerHome = tool 'sonarscanner4' + } + + steps { + withSonarQubeEnv('sonar-pro') { + sh '''${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=vprofile \ + -Dsonar.projectName=vprofile-repo \ + -Dsonar.projectVersion=1.0 \ + -Dsonar.sources=src/ \ + -Dsonar.java.binaries=target/test-classes/com/visualpathit/account/controllerTest/ \ + -Dsonar.junit.reportsPath=target/surefire-reports/ \ + -Dsonar.jacoco.reportsPath=target/jacoco.exec \ + -Dsonar.java.checkstyle.reportPaths=target/checkstyle-result.xml''' + } + + timeout(time: 10, unit: 'MINUTES') { + waitForQualityGate abortPipeline: true + } + } + } + + stage("Publish to Nexus Repository Manager") { + steps { + script { + pom = readMavenPom file: "pom.xml"; + filesByGlob = findFiles(glob: "target/*.${pom.packaging}"); + echo "${filesByGlob[0].name} ${filesByGlob[0].path} ${filesByGlob[0].directory} ${filesByGlob[0].length} ${filesByGlob[0].lastModified}" + artifactPath = filesByGlob[0].path; + artifactExists = fileExists artifactPath; + if(artifactExists) { + echo "*** File: ${artifactPath}, group: ${pom.groupId}, packaging: ${pom.packaging}, version ${pom.version} ARTVERSION"; + nexusArtifactUploader( + nexusVersion: NEXUS_VERSION, + protocol: NEXUS_PROTOCOL, + nexusUrl: NEXUS_URL, + groupId: NEXUS_REPOGRP_ID, + version: ARTVERSION, + repository: NEXUS_REPOSITORY, + credentialsId: NEXUS_CREDENTIAL_ID, + artifacts: [ + [artifactId: pom.artifactId, + classifier: '', + file: artifactPath, + type: pom.packaging], + [artifactId: pom.artifactId, + classifier: '', + file: "pom.xml", + type: "pom"] + ] + ); + } + else { + error "*** File: ${artifactPath}, could not be found"; + } + } + } + } + + + } + + +} diff --git a/README.md b/README.md index b4548e6..84ce5d0 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,25 @@ -## Prerequisites +#### +### Prerequisites - JDK 1.8 or later - Maven 3 or later - MySQL 5.6 or later -## Technologies +### Technologies - Spring MVC - Spring Security - Spring Data JPA - Maven - JSP - MySQL -## Database +### Database Here,we used Mysql DB MSQL DB Installation Steps for Linux ubuntu 14.04: - $ sudo apt-get update - $ sudo apt-get install mysql-server Then look for the file : -- /src/main/resources/db.sql - -- db.sql file contents all step for DB table creation commands. +- /src/main/resources/accountsdb +- accountsdb.sql file is a mysql dump file.we have to import this dump to mysql db server +- > mysql -u -p accounts < accountsdb.sql diff --git a/ansible/vprofile.yml b/ansible/vprofile.yml new file mode 100644 index 0000000..c8d6de6 --- /dev/null +++ b/ansible/vprofile.yml @@ -0,0 +1,39 @@ +--- +- hosts: tomcatservers + become: yes + gather_facts: False + tasks: + + - name: Install EPEL-release + yum: name=epel-release state=present + + + - name: Install java_1.8 + yum: name=java-1.8.0-openjdk.x86_64 state=present + + - name: Install tomcat + yum: name=tomcat state=present + + + - name: Download latest VProfile.war file + get_url: url=http://{{nexusip}}:8081/nexus/content/repositories/VProfile-repo/{{groupid}}/{{time}}/{{build}}/{{vprofile_version}} dest=/tmp/ mode=755 + + - name: Stop tomcat service + service: name=tomcat state=stopped + + - name: Copy artifact to tomcat folder + shell: cp /tmp/{{vprofile_version}} /var/lib/tomcat/webapps + + - name: Delete link to existing vprofile version + file: path=/var/lib/tomcat/webapps/VProfile state=absent + + - name: Start tomcat service + service: name=tomcat state=started + - wait_for: path=/var/lib/tomcat/webapps/{{time}}-{{build}} + + - name: Link latest vprofile version + file: src=/var/lib/tomcat/webapps/{{time}}-{{build}} dest=/var/lib/tomcat/webapps/VProfile state=link + + - name: Stop iptables + service: name=iptables state=stopped + diff --git a/pom.xml b/pom.xml index da1aeb6..b676478 100644 --- a/pom.xml +++ b/pom.xml @@ -2,9 +2,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 com.visualpathit - VProfile + vprofile war - 1.0 + v2 Visualpathit VProfile Webapp http://maven.apache.org @@ -134,9 +134,14 @@ - org.jacoco - jacoco-maven-plugin - 0.7.2.201409121644 + org.apache.maven.plugins + maven-war-plugin + 3.2.2 + + + org.jacoco + jacoco-maven-plugin + 0.8.4 jacoco-initialize @@ -156,5 +161,10 @@ + + + ${NEXUS-GRP-REPO} + http://${NEXUSIP}:${NEXUSPORT}/repository/${NEXUS-GRP-REPO}/ + + - diff --git a/settings.xml b/settings.xml new file mode 100644 index 0000000..eb788f0 --- /dev/null +++ b/settings.xml @@ -0,0 +1,38 @@ + + + + + + ${SNAP-REPO} + ${NEXUS-USER} + ${NEXUS-PASS} + + + ${RELEASE-REPO} + ${NEXUS-USER} + ${NEXUS-PASS} + + + ${CENTRAL-REPO} + ${NEXUS-USER} + ${NEXUS-PASS} + + + ${NEXUS-GRP-REPO} + ${NEXUS-USER} + ${NEXUS-PASS} + + + + + + ${CENTRAL-REPO} + ${CENTRAL-REPO} + http://${NEXUSIP}:${NEXUSPORT}/repository/${NEXUS-GRP-REPO}/ + * + + + + diff --git a/src/main/resources/accountsdb.sql b/src/main/resources/accountsdb.sql new file mode 100644 index 0000000..d224d81 --- /dev/null +++ b/src/main/resources/accountsdb.sql @@ -0,0 +1,104 @@ +-- MySQL dump 10.13 Distrib 5.7.18, for Linux (x86_64) +-- +-- Host: localhost Database: accounts +-- ------------------------------------------------------ +-- Server version 5.7.18-0ubuntu0.16.10.1 + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +-- +-- Table structure for table `role` +-- + +DROP TABLE IF EXISTS `role`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `role` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(45) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `role` +-- + +LOCK TABLES `role` WRITE; +/*!40000 ALTER TABLE `role` DISABLE KEYS */; +INSERT INTO `role` VALUES (1,'ROLE_USER'); +/*!40000 ALTER TABLE `role` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `user` +-- + +DROP TABLE IF EXISTS `user`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `user` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `username` varchar(255) DEFAULT NULL, + `userEmail` varchar(255) DEFAULT NULL, + `password` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `user` +-- + +LOCK TABLES `user` WRITE; +/*!40000 ALTER TABLE `user` DISABLE KEYS */; +INSERT INTO `user` VALUES (4,'admin_vp','admin@visualpathit.com','$2a$11$DSEIKJNrgPjG.iCYUwErvOkREtC67mqzQ.ogkZbc/KOW1OPOpZfY6'); +/*!40000 ALTER TABLE `user` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `user_role` +-- + +DROP TABLE IF EXISTS `user_role`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `user_role` ( + `user_id` int(11) NOT NULL, + `role_id` int(11) NOT NULL, + PRIMARY KEY (`user_id`,`role_id`), + KEY `fk_user_role_roleid_idx` (`role_id`), + CONSTRAINT `fk_user_role_roleid` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT `fk_user_role_userid` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `user_role` +-- + +LOCK TABLES `user_role` WRITE; +/*!40000 ALTER TABLE `user_role` DISABLE KEYS */; +INSERT INTO `user_role` VALUES (4,1); +/*!40000 ALTER TABLE `user_role` ENABLE KEYS */; +UNLOCK TABLES; +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +-- Dump completed on 2017-08-28 10:50:51 diff --git a/src/main/resources/db.sql b/src/main/resources/db.sql deleted file mode 100644 index 0c073d1..0000000 --- a/src/main/resources/db.sql +++ /dev/null @@ -1,53 +0,0 @@ --- ---create a User with a password --- -CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password'; -GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost'; - -CREATE DATABASE IF NOT EXISTS `accounts`; -USE `accounts`; --- --- Table structure for table `role` --- - -DROP TABLE IF EXISTS `role`; -CREATE TABLE `role` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `name` varchar(45) DEFAULT NULL, - PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; - --- --- Dumping data for table `role` --- - -LOCK TABLES `role` WRITE; -INSERT INTO `role` VALUES (1,'ROLE_USER'); -UNLOCK TABLES; - --- --- Table structure for table `user` --- - -DROP TABLE IF EXISTS `user`; -CREATE TABLE `user` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `username` varchar(255) DEFAULT NULL, - `userEmail` varchar(255) DEFAULT NULL, - `password` varchar(255) DEFAULT NULL, - PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; - --- --- Table structure for table `user_role` --- - -DROP TABLE IF EXISTS `user_role`; -CREATE TABLE `user_role` ( - `user_id` int(11) NOT NULL, - `role_id` int(11) NOT NULL, - PRIMARY KEY (`user_id`,`role_id`), - KEY `fk_user_role_roleid_idx` (`role_id`), - CONSTRAINT `fk_user_role_roleid` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, - CONSTRAINT `fk_user_role_userid` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8; diff --git a/src/test/java/com/visualpathit/account/controllerTest/UserControllerTest.java b/src/test/java/com/visualpathit/account/controllerTest/UserControllerTest.java new file mode 100644 index 0000000..3097f1a --- /dev/null +++ b/src/test/java/com/visualpathit/account/controllerTest/UserControllerTest.java @@ -0,0 +1,109 @@ +package com.visualpathit.account.controllerTest; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import com.visualpathit.account.controller.UserController; +import com.visualpathit.account.model.User; +import com.visualpathit.account.service.UserService; +import com.visualpathit.account.setup.StandaloneMvcTestViewResolver; + + +public class UserControllerTest { + + @Mock + private UserService controllerSer; + @InjectMocks + private UserController controller; + private MockMvc mockMvc; + + @Before + public void setup(){ + MockitoAnnotations.initMocks(this); + + /*InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); + viewResolver.setPrefix("/WEB-INF/views/"); + viewResolver.setSuffix(".jsp"); + */ + mockMvc = MockMvcBuilders.standaloneSetup(controller) + .setViewResolvers(new StandaloneMvcTestViewResolver()).build(); + } + + @Test + public void registrationTestforHappyFlow() throws Exception{ + User user = new User(); + mockMvc.perform(get("/registration")) + .andExpect(status().isOk()) + .andExpect(view().name("registration")) + .andExpect(forwardedUrl("registration")); + + } + @Test + public void registrationTestforNullValueHappyFlow() throws Exception{ + mockMvc.perform(get("/registration")) + .andExpect(status().isOk()) + .andExpect(view().name("registration")) + .andExpect(forwardedUrl("registration")); + + } + /*@Test + public void registrationTestforPostValueHappyFlow() throws Exception{ + String description =new String("Error String"); + UserValidator userValidator; + BindingResult bindingResult; + when(userValidator.validate(new User(),bindingResult)) + .thenThrow(bindingResult.hasErrors()); + mockMvc.perform(post("/registration").contentType(MediaType.APPLICATION_FORM_URLENCODED) + .param("userForm","userForm")) + + .andExpect(status().isOk()); + //.andExpect(view().name("redirect:/welcome")) + //.andExpect(forwardedUrl("redirect:/welcome")); + + }*/ + @Test + public void loginTestHappyFlow() throws Exception{ + String error = "Your username and password is invalid"; + mockMvc.perform(get("/login").param(error, error)) + .andExpect(status().isOk()) + .andExpect(view().name("login")) + .andExpect(forwardedUrl("login")); + + } + @Test + public void welcomeTestHappyFlow() throws Exception{ + mockMvc.perform(get("/welcome")) + .andExpect(status().isOk()) + .andExpect(view().name("welcome")) + .andExpect(forwardedUrl("welcome")); + + } + @Test + public void welcomeAfterDirectLoginTestHappyFlow() throws Exception{ + mockMvc.perform(get("/")) + .andExpect(status().isOk()) + .andExpect(view().name("welcome")) + .andExpect(forwardedUrl("welcome")); + + } + @Test + public void indexTestHappyFlow() throws Exception{ + mockMvc.perform(get("/index")) + .andExpect(status().isOk()) + .andExpect(view().name("index_home")) + .andExpect(forwardedUrl("index_home")); + + } + +} diff --git a/src/test/java/com/visualpathit/account/modelTest/RoleTest.java b/src/test/java/com/visualpathit/account/modelTest/RoleTest.java new file mode 100644 index 0000000..a5ff032 --- /dev/null +++ b/src/test/java/com/visualpathit/account/modelTest/RoleTest.java @@ -0,0 +1,51 @@ +package com.visualpathit.account.modelTest; + +import junit.framework.Assert; + +import java.util.HashSet; +import java.util.Set; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.visualpathit.account.model.Role; +import com.visualpathit.account.model.User; + +/** {@author waheedk} !*/ +public class RoleTest { + + public static final Long EXPECTED_ID = 1L; + public static final String EXPECTED_ROLENAME = "Admin"; + public static final int EXPECTED_SIZE = 1; + private Role role; + @Before + public void setUp() throws Exception { + User user = new User(); + user.setId(1L); + user.setUsername("Wahidkhan74"); + user.setPassword("Wahidkhan74"); + user.setUserEmail("XXXXX@gmail.com"); + + Set users = new HashSet(); + users.add(user); + role = new Role(); + role.setId(1L); + role.setName("Admin"); + role.setUsers(users); + } + + @After + public void tearDown() throws Exception { + System.out.println("Test Completed"); + + } + + @Test + public void testUserDetailsHappyFlow() throws Exception { + Assert.assertEquals(EXPECTED_ID, role.getId()); + Assert.assertEquals(EXPECTED_ROLENAME, role.getName()); + Assert.assertEquals(EXPECTED_SIZE,role.getUsers().size()); + + } +} \ No newline at end of file diff --git a/src/test/java/com/visualpathit/account/modelTest/UserTest.java b/src/test/java/com/visualpathit/account/modelTest/UserTest.java new file mode 100644 index 0000000..df68dc8 --- /dev/null +++ b/src/test/java/com/visualpathit/account/modelTest/UserTest.java @@ -0,0 +1,56 @@ +package com.visualpathit.account.modelTest; + +import junit.framework.Assert; + +import java.util.HashSet; +import java.util.Set; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.visualpathit.account.model.Role; +import com.visualpathit.account.model.User; + +/** {@author waheedk} !*/ +public class UserTest { + + public static final Long EXPECTED_ID = 1L; + public static final int EXPECTED_SIZE = 1; + public static final String EXPECTED_USERNAME = "Wahidkhan74"; + public static final String EXPECTED_PASSWD = "Wahidkhan74"; + public static final String EXPECTED_USEREMAIL = "XXXXX@gmail.com"; + private User user; + @Before + public void setUp() throws Exception { + + Role role = new Role(); + role.setId(1L); + role.setName("Admin"); + Set roles = new HashSet(); + roles.add(role); + + user = new User(); + user.setId(1L); + user.setUsername("Wahidkhan74"); + user.setPassword("Wahidkhan74"); + user.setUserEmail("XXXXX@gmail.com"); + user.setRoles(roles); + } + + @After + public void tearDown() throws Exception { + System.out.println("Test Completed"); + + } + + @Test + public void testUserDetailsHappyFlow() throws Exception { + Assert.assertEquals(EXPECTED_ID, user.getId()); + Assert.assertEquals(EXPECTED_USERNAME, user.getUsername()); + Assert.assertEquals(EXPECTED_PASSWD, user.getPassword()); + Assert.assertEquals(EXPECTED_USEREMAIL, user.getUserEmail()); + Assert.assertEquals(EXPECTED_SIZE,user.getRoles().size()); + + } +} \ No newline at end of file diff --git a/src/test/java/com/visualpathit/account/setup/StandaloneMvcTestViewResolver.java b/src/test/java/com/visualpathit/account/setup/StandaloneMvcTestViewResolver.java new file mode 100644 index 0000000..f282b2a --- /dev/null +++ b/src/test/java/com/visualpathit/account/setup/StandaloneMvcTestViewResolver.java @@ -0,0 +1,20 @@ +package com.visualpathit.account.setup; + +import org.springframework.web.servlet.view.AbstractUrlBasedView; +import org.springframework.web.servlet.view.InternalResourceView; +import org.springframework.web.servlet.view.InternalResourceViewResolver; + +public class StandaloneMvcTestViewResolver extends InternalResourceViewResolver { + + public StandaloneMvcTestViewResolver() { + super(); + } + + @Override + protected AbstractUrlBasedView buildView(final String viewName) throws Exception { + final InternalResourceView view = (InternalResourceView) super.buildView(viewName); + // prevent checking for circular view paths + view.setPreventDispatchLoop(false); + return view; + } +} diff --git a/userdata/jenkins-setup.sh b/userdata/jenkins-setup.sh new file mode 100644 index 0000000..6eb5451 --- /dev/null +++ b/userdata/jenkins-setup.sh @@ -0,0 +1,12 @@ +#!/bin/bash +sudo apt update +sudo apt install openjdk-11-jdk -y +sudo apt install maven -y +curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee \ + /usr/share/keyrings/jenkins-keyring.asc > /dev/null +echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \ + https://pkg.jenkins.io/debian-stable binary/ | sudo tee \ + /etc/apt/sources.list.d/jenkins.list > /dev/null +sudo apt-get update +sudo apt-get install jenkins -y +### diff --git a/userdata/nexus-setup.sh b/userdata/nexus-setup.sh new file mode 100644 index 0000000..f3fadec --- /dev/null +++ b/userdata/nexus-setup.sh @@ -0,0 +1,35 @@ +#!/bin/bash +yum install java-1.8.0-openjdk.x86_64 wget -y +mkdir -p /opt/nexus/ +mkdir -p /tmp/nexus/ +cd /tmp/nexus/ +NEXUSURL="https://download.sonatype.com/nexus/3/latest-unix.tar.gz" +wget $NEXUSURL -O nexus.tar.gz +EXTOUT=`tar xzvf nexus.tar.gz` +NEXUSDIR=`echo $EXTOUT | cut -d '/' -f1` +rm -rf /tmp/nexus/nexus.tar.gz +rsync -avzh /tmp/nexus/ /opt/nexus/ +useradd nexus +chown -R nexus.nexus /opt/nexus +cat <> /etc/systemd/system/nexus.service +[Unit] +Description=nexus service +After=network.target + +[Service] +Type=forking +LimitNOFILE=65536 +ExecStart=/opt/nexus/$NEXUSDIR/bin/nexus start +ExecStop=/opt/nexus/$NEXUSDIR/bin/nexus stop +User=nexus +Restart=on-abort + +[Install] +WantedBy=multi-user.target + +EOT + +echo 'run_as_user="nexus"' > /opt/nexus/$NEXUSDIR/bin/nexus.rc +systemctl daemon-reload +systemctl start nexus +systemctl enable nexus diff --git a/userdata/sonar-analysis-properties b/userdata/sonar-analysis-properties new file mode 100644 index 0000000..8751fe7 --- /dev/null +++ b/userdata/sonar-analysis-properties @@ -0,0 +1,10 @@ +sonar.projectKey=vprofile +sonar.projectName=vprofile-repo +sonar.projectVersion=1.0 +sonar.sources=src/ +sonar.java.binaries=target/test-classes/com/visualpathit/account/controllerTest/ +sonar.junit.reportsPath=target/surefire-reports/ +sonar.jacoco.reportsPath=target/jacoco.exec +sonar.java.checkstyle.reportPaths=target/checkstyle-result.xml + + diff --git a/userdata/sonar-setup.sh b/userdata/sonar-setup.sh new file mode 100644 index 0000000..99a3a78 --- /dev/null +++ b/userdata/sonar-setup.sh @@ -0,0 +1,119 @@ +#!/bin/bash +cp /etc/sysctl.conf /root/sysctl.conf_backup +cat < /etc/sysctl.conf +vm.max_map_count=262144 +fs.file-max=65536 +ulimit -n 65536 +ulimit -u 4096 +EOT +cp /etc/security/limits.conf /root/sec_limit.conf_backup +cat < /etc/security/limits.conf +sonarqube - nofile 65536 +sonarqube - nproc 409 +EOT + +sudo apt-get update -y +sudo apt-get install openjdk-11-jdk -y +sudo update-alternatives --config java + +java -version + +sudo apt update +wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add - + +sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list' +sudo apt install postgresql postgresql-contrib -y +#sudo -u postgres psql -c "SELECT version();" +sudo systemctl enable postgresql.service +sudo systemctl start postgresql.service +sudo echo "postgres:admin123" | chpasswd +runuser -l postgres -c "createuser sonar" +sudo -i -u postgres psql -c "ALTER USER sonar WITH ENCRYPTED PASSWORD 'admin123';" +sudo -i -u postgres psql -c "CREATE DATABASE sonarqube OWNER sonar;" +sudo -i -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE sonarqube to sonar;" +systemctl restart postgresql +#systemctl status -l postgresql +netstat -tulpena | grep postgres +sudo mkdir -p /sonarqube/ +cd /sonarqube/ +sudo curl -O https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-8.3.0.34182.zip +sudo apt-get install zip -y +sudo unzip -o sonarqube-8.3.0.34182.zip -d /opt/ +sudo mv /opt/sonarqube-8.3.0.34182/ /opt/sonarqube +sudo groupadd sonar +sudo useradd -c "SonarQube - User" -d /opt/sonarqube/ -g sonar sonar +sudo chown sonar:sonar /opt/sonarqube/ -R +cp /opt/sonarqube/conf/sonar.properties /root/sonar.properties_backup +cat < /opt/sonarqube/conf/sonar.properties +sonar.jdbc.username=sonar +sonar.jdbc.password=admin123 +sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube +sonar.web.host=0.0.0.0 +sonar.web.port=9000 +sonar.web.javaAdditionalOpts=-server +sonar.search.javaOpts=-Xmx512m -Xms512m -XX:+HeapDumpOnOutOfMemoryError +sonar.log.level=INFO +sonar.path.logs=logs +EOT + +cat < /etc/systemd/system/sonarqube.service +[Unit] +Description=SonarQube service +After=syslog.target network.target + +[Service] +Type=forking + +ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start +ExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stop + +User=sonar +Group=sonar +Restart=always + +LimitNOFILE=65536 +LimitNPROC=4096 + + +[Install] +WantedBy=multi-user.target +EOT + +systemctl daemon-reload +systemctl enable sonarqube.service +#systemctl start sonarqube.service +#systemctl status -l sonarqube.service +apt-get install nginx -y +rm -rf /etc/nginx/sites-enabled/default +rm -rf /etc/nginx/sites-available/default +cat < /etc/nginx/sites-available/sonarqube +server{ + listen 80; + server_name sonarqube.groophy.in; + + access_log /var/log/nginx/sonar.access.log; + error_log /var/log/nginx/sonar.error.log; + + proxy_buffers 16 64k; + proxy_buffer_size 128k; + + location / { + proxy_pass http://127.0.0.1:9000; + proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; + proxy_redirect off; + + proxy_set_header Host \$host; + proxy_set_header X-Real-IP \$remote_addr; + proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto http; + } +} +EOT +ln -s /etc/nginx/sites-available/sonarqube /etc/nginx/sites-enabled/sonarqube +systemctl enable nginx.service +#systemctl restart nginx.service +sudo ufw allow 80,9000,9001/tcp + +echo "System reboot in 30 sec" +sleep 30 +reboot