Skip to content

Commit

Permalink
Merge pull request #35 from prefeiturasp/homolog
Browse files Browse the repository at this point in the history
PROD - Atualizado até 01/09
  • Loading branch information
Zimmerr committed Sep 1, 2021
2 parents b47f956 + 9cedda8 commit fdb0c27
Show file tree
Hide file tree
Showing 28 changed files with 545 additions and 187 deletions.
266 changes: 79 additions & 187 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,206 +1,98 @@
pipeline {
environment {
branchname = env.BRANCH_NAME.toLowerCase()
kubeconfig = getKubeconf(env.branchname)
registryCredential = 'jenkins_registry'
}

agent {
node {
label 'py-uniformes'
}
node { label 'python-36-uniformes' }
}

options {
buildDiscarder(logRotator(numToKeepStr: '5', artifactNumToKeepStr: '5'))
disableConcurrentBuilds()
skipDefaultCheckout()
skipDefaultCheckout()
}

stages {
stage('CheckOut') {
steps {
checkout scm

stage('CheckOut') {
steps { checkout scm }
}
}

stage('Analise codigo') {
when {
branch 'homolog-bypass'
}
steps {
sh 'sonar-scanner \
-Dsonar.projectKey=SME-CadastroInfantil-FrontEnd \
-Dsonar.sources=. \
-Dsonar.host.url=http://sonar.sme.prefeitura.sp.gov.br \
-Dsonar.login=ea6cd1300ded24934a39dae3b104463d17a4f88e'
stage('AnaliseCodigo') {
when { branch 'homolog' }
steps {
withSonarQubeEnv('sonarqube-local'){
sh 'echo "[ INFO ] Iniciando analise Sonar..." && sonar-scanner \
-Dsonar.projectKey=SME-AtualziacaoCadastral-Backend \
-Dsonar.sources=.'
}
}

stage('Deploy DEV') {
when {
branch 'develop'
}
steps {
sh 'echo build docker image desenvolvimento'
// Start JOB para build das imagens Docker e push SME Registry
script {
step([$class: "RundeckNotifier",
includeRundeckLogs: true,
jobId: "2df150ee-ea54-4eb7-ba15-c971acc9e55d",
nodeFilters: "",
//options: """
// PARAM_1=value1
// PARAM_2=value2
// PARAM_3=
// """,
rundeckInstance: "Rundeck-SME",
shouldFailTheBuild: true,
shouldWaitForRundeckJob: true,
tags: "",
tailLog: true])
}




//Start JOB de deploy Kubernetes
sh 'echo Deploy ambiente desenvolvimento'
script {
step([$class: "RundeckNotifier",
includeRundeckLogs: true,
jobId: "638a966c-dc5e-4f9b-a9cd-38f5438e9842",
nodeFilters: "",
//options: """
// PARAM_1=value1
// PARAM_2=value2
// PARAM_3=
// """,
rundeckInstance: "Rundeck-SME",
shouldFailTheBuild: true,
shouldWaitForRundeckJob: true,
tags: "",
tailLog: true])
}
}
}

stage('Deploy homologacao') {
when {
branch 'homolog'
}
steps {
timeout(time: 24, unit: "HOURS") {
// telegramSend("${JOB_NAME}...O Build ${BUILD_DISPLAY_NAME} - Requer uma aprovação para deploy !!!\n Consulte o log para detalhes -> [Job logs](${env.BUILD_URL}console)\n")
input message: 'Deseja realizar o deploy?', ok: 'SIM', submitter: 'anderson_morais, kelwy_oliveira'
}
sh 'echo Deploying ambiente homologacao'

// Start JOB para build das imagens Docker e push SME Registry

script {
step([$class: "RundeckNotifier",
includeRundeckLogs: true,

//JOB DE BUILD
jobId: "6e79d1e0-8848-497b-9b0d-3acf9462af83",
nodeFilters: "",
//options: """
// PARAM_1=value1
// PARAM_2=value2
// PARAM_3=
// """,
rundeckInstance: "Rundeck-SME",
shouldFailTheBuild: true,
shouldWaitForRundeckJob: true,
tags: "",
tailLog: true])
}
//Start JOB deploy Kubernetes

script {
step([$class: "RundeckNotifier",
includeRundeckLogs: true,
jobId: "37bd6a0d-8ae8-4ce0-a3ef-254ba2785c21",
nodeFilters: "",
//options: """
// PARAM_1=value1
// PARAM_2=value2
// PARAM_3=
// """,
rundeckInstance: "Rundeck-SME",
shouldFailTheBuild: true,
shouldWaitForRundeckJob: true,
tags: "",
tailLog: true])
}
}
}

stage('Deploy PROD') {
when {
branch 'master'
}
steps {
timeout(time: 24, unit: "HOURS") {
// telegramSend("${JOB_NAME}...O Build ${BUILD_DISPLAY_NAME} - Requer uma aprovação para deploy !!!\n Consulte o log para detalhes -> [Job logs](${env.BUILD_URL}console)\n")
input message: 'Deseja realizar o deploy?', ok: 'SIM', submitter: 'anderson_morais, kelwy_oliveira'
}
sh 'echo Build image docker Produção'
// Start JOB para build das imagens Docker e push SME Registry

script {
step([$class: "RundeckNotifier",
includeRundeckLogs: true,

//JOB DE BUILD
jobId: "14125608-de29-4601-b239-8cff88acb376",
nodeFilters: "",
//options: """
// PARAM_1=value1
// PARAM_2=value2
// PARAM_3=
// """,
rundeckInstance: "Rundeck-SME",
shouldFailTheBuild: true,
shouldWaitForRundeckJob: true,
tags: "",
tailLog: true])
}
//Start JOB deploy kubernetes

script {
step([$class: "RundeckNotifier",
includeRundeckLogs: true,
jobId: "679ae692-49d3-4f58-a282-b788c51b945a",
nodeFilters: "",
//options: """
// PARAM_1=value1
// PARAM_2=value2
// PARAM_3=
// """,
rundeckInstance: "Rundeck-SME",
shouldFailTheBuild: true,
shouldWaitForRundeckJob: true,
tags: "",
tailLog: true])
stage('Build') {
when { anyOf { branch 'master'; branch 'main'; branch "story/*"; branch 'develop'; branch 'release'; branch 'homolog'; } }
steps {
script {
imagename1 = "registry.sme.prefeitura.sp.gov.br/${env.branchname}/sme-atualizacaocadastral-backend"
//imagename2 = "registry.sme.prefeitura.sp.gov.br/${env.branchname}/sme-outra"
dockerImage1 = docker.build(imagename1, "-f Dockerfile .")
//dockerImage2 = docker.build(imagename2, "-f Dockerfile_outro .")
docker.withRegistry( 'https://registry.sme.prefeitura.sp.gov.br', registryCredential ) {
dockerImage1.push()
//dockerImage2.push()
}
sh "docker rmi $imagename1"
//sh "docker rmi $imagename2"
}
}
}
}
}

post {
always {
echo 'One way or another, I have finished'
}
success {
telegramSend("${JOB_NAME}...O Build ${BUILD_DISPLAY_NAME} - Esta ok !!!\n Consulte o log para detalhes -> [Job logs](${env.BUILD_URL}console)\n\n Uma nova versão da aplicação esta disponivel!!!")
}
unstable {
telegramSend("O Build ${BUILD_DISPLAY_NAME} <${env.BUILD_URL}> - Esta instavel ...\nConsulte o log para detalhes -> [Job logs](${env.BUILD_URL}console)")
}
failure {
telegramSend("${JOB_NAME}...O Build ${BUILD_DISPLAY_NAME} - Quebrou. \nConsulte o log para detalhes -> [Job logs](${env.BUILD_URL}console)")
}
changed {
echo 'Things were different before...'
}
aborted {
telegramSend("O Build ${BUILD_DISPLAY_NAME} - Foi abortado.\nConsulte o log para detalhes -> [Job logs](${env.BUILD_URL}console)")

stage('Deploy'){
when { anyOf { branch 'master'; branch 'main'; branch 'develop'; branch 'release'; branch 'homolog'; } }
steps {
script{
if ( env.branchname == 'main' || env.branchname == 'master' || env.branchname == 'homolog' || env.branchname == 'release' ) {
sendTelegram("🤩 [Deploy ${env.branchname}] Job Name: ${JOB_NAME} \nBuild: ${BUILD_DISPLAY_NAME} \nMe aprove! \nLog: \n${env.BUILD_URL}")
timeout(time: 24, unit: "HOURS") {
input message: 'Deseja realizar o deploy?', ok: 'SIM', submitter: 'luis_zimmermann, kelwy_oliveira, rodolpho_azeredo, anderson_morais'
}
}
withCredentials([file(credentialsId: "${kubeconfig}", variable: 'config')]){
sh('cp $config '+"$home"+'/.kube/config')
sh "kubectl -n sme-atualizacaocadastral rollout restart deploy"
sh('rm -f '+"$home"+'/.kube/config')
}
}
}
}
}

post {
success { sendTelegram("🚀 Job Name: ${JOB_NAME} \nBuild: ${BUILD_DISPLAY_NAME} \nStatus: Success \nLog: \n${env.BUILD_URL}console") }
unstable { sendTelegram("💣 Job Name: ${JOB_NAME} \nBuild: ${BUILD_DISPLAY_NAME} \nStatus: Unstable \nLog: \n${env.BUILD_URL}console") }
failure { sendTelegram("💥 Job Name: ${JOB_NAME} \nBuild: ${BUILD_DISPLAY_NAME} \nStatus: Failure \nLog: \n${env.BUILD_URL}console") }
aborted { sendTelegram ("😥 Job Name: ${JOB_NAME} \nBuild: ${BUILD_DISPLAY_NAME} \nStatus: Aborted \nLog: \n${env.BUILD_URL}console") }
}
}
def sendTelegram(message) {
def encodedMessage = URLEncoder.encode(message, "UTF-8")
withCredentials([string(credentialsId: 'telegramToken', variable: 'TOKEN'),
string(credentialsId: 'telegramChatId', variable: 'CHAT_ID')]) {
response = httpRequest (consoleLogResponseBody: true,
contentType: 'APPLICATION_JSON',
httpMode: 'GET',
url: 'https://api.telegram.org/bot'+"$TOKEN"+'/sendMessage?text='+encodedMessage+'&chat_id='+"$CHAT_ID"+'&disable_web_page_preview=true',
validResponseCodes: '200')
return response
}
}
def getKubeconf(branchName) {
if("main".equals(branchName)) { return "config_prd"; }
else if ("master".equals(branchName)) { return "config_prd"; }
else if ("homolog".equals(branchName)) { return "config_hom"; }
else if ("release".equals(branchName)) { return "config_hom"; }
else if ("develop".equals(branchName)) { return "config_dev"; }
}
1 change: 1 addition & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"sme_atualizacao_cadastral_apps.users.apps.UsersConfig",
"sme_atualizacao_cadastral_apps.core.apps.CoreConfig",
"sme_atualizacao_cadastral_apps.alunos.apps.AlunosConfig",
"sme_atualizacao_cadastral_apps.cadastros.apps.CadastrosConfig",
# Your stuff: custom apps go here
]
# https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
Expand Down
Empty file.
52 changes: 52 additions & 0 deletions sme_atualizacao_cadastral_apps/cadastros/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

from django.contrib import admin, messages
from django.http import HttpResponseRedirect
from django.urls import path

from .models import BaseCadastro, LogProcessamentoPlanilha, PlanilhaSituacao


@admin.register(PlanilhaSituacao)
class PlanilhaSituacaoAdmin(admin.ModelAdmin):
change_list_template = "cadastro_changelist.html"

def get_urls(self):
urls = super().get_urls()
my_urls = [
path('processar_planilha/', self.processar_planilha),
]
return my_urls + urls

def arquivo_nome(self, obj):
return f'{obj.arquivo.name.split("/")[-1]}'

arquivo_nome.short_descriptions = 'arquivo'

def processar_planilha(self, request):
planilha = self.model.objects.last()
if not planilha.extraido:
planilha.processar_planilha()
self.message_user(request, f'Processamento da planilha {planilha.arquivo.name.split("/")[-1]} '
f'foi iniciado. Esta tarefa pode demorar um pouco.')
else:
self.message_user(request, f'A planilha {planilha.arquivo.name.split("/")[-1]} '
f'não pode ser processada novamente.', level=messages.ERROR)
return HttpResponseRedirect("../")

processar_planilha.short_description = 'Processar ultima planilha cadastrada'

list_display = ('arquivo_nome', 'criado_em', 'data_corte_planilha', 'data_corte_lote', 'extraido')
ordering = ('-criado_em',)


@admin.register(BaseCadastro)
class BaseCadastroAdmin(admin.ModelAdmin):
list_display = ('cpf', 'situacao')
readonly_fields = ('cpf', 'situacao')


@admin.register(LogProcessamentoPlanilha)
class LogProcessamentoPlanilhaAdmin(admin.ModelAdmin):

list_display = ('arquivo', 'criado_em', 'status')
readonly_fields = ('arquivo', 'criado_em', 'status', 'msg_retorno')
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from rest_framework import serializers

from ...models import BaseCadastro, PlanilhaSituacao


class BaseCadastroSerializer(serializers.ModelSerializer):
class Meta:
model = BaseCadastro
fields = ('cpf', 'situacao')


class PlanilhaSituacaoSerializer(serializers.ModelSerializer):
class Meta:
model = PlanilhaSituacao
fields = ('data_corte_planilha', 'data_corte_lote')
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from rest_framework import viewsets, mixins
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from ..serializers.situacao_cadastro_serializer import BaseCadastroSerializer, PlanilhaSituacaoSerializer
from ...models.situacao_cadastro import BaseCadastro, PlanilhaSituacao


class BaseCadastroViewSet(mixins.RetrieveModelMixin,
viewsets.GenericViewSet):
permission_classes = [AllowAny]
lookup_field = 'cpf'
queryset = BaseCadastro.objects.all()
serializer_class = BaseCadastroSerializer


class PlanilhaSituacaoViewSet(mixins.ListModelMixin,
viewsets.GenericViewSet):
permission_classes = [AllowAny]
queryset = PlanilhaSituacao.objects.all()
serializer_class = PlanilhaSituacaoSerializer

def list(self, request, **kwargs):
datas = self.get_queryset().filter(extraido=True).last()
return Response(PlanilhaSituacaoSerializer(datas, many=False).data)
5 changes: 5 additions & 0 deletions sme_atualizacao_cadastral_apps/cadastros/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class CadastrosConfig(AppConfig):
name = 'sme_atualizacao_cadastral_apps.cadastros'
Loading

0 comments on commit fdb0c27

Please sign in to comment.