From b58666ead4317df82ca4aa10f291d7dd940d4667 Mon Sep 17 00:00:00 2001 From: Ionel Andrei Cataon Date: Thu, 12 Feb 2026 16:50:19 +0200 Subject: [PATCH] p2 --- project2/famousquotes.py | 93 ++++++++++++++-------------------------- project2/jenkinsfile | 40 ++++++++++++++--- 2 files changed, 66 insertions(+), 67 deletions(-) diff --git a/project2/famousquotes.py b/project2/famousquotes.py index c1a9279..7945c69 100644 --- a/project2/famousquotes.py +++ b/project2/famousquotes.py @@ -1,64 +1,37 @@ -pipeline { - agent any +import requests +import os +import sys - stages { - stage('Build & Lint') { - steps { - script { - dir('project2') { - // 1. Construim imaginea - sh "docker build -t quotes-app ." - - // 2. LINT CHECK: Verificăm sintaxa Python în interiorul containerului - echo "🔍 Verificăm sintaxa codului..." - sh "docker run --rm quotes-app python -m py_compile famousquotes.py" - } - } - } - } +# API stabil pentru a evita erorile de rețea anterioare +API_URL = os.getenv("QUOTE_API_URL", "https://api.adviceslip.com/advice") - stage('Run & Extract') { - steps { - script { - dir('project2') { - try { - // 3. Rulăm cu DNS forțat pentru a evita "NameResolutionError" - sh "docker run --name quotes-worker --network host --dns 8.8.8.8 quotes-app" - - // 4. Extragem rezultatul - sh "docker cp quotes-worker:/app/quote.txt ." - } catch (e) { - echo "Eroare la rulare, dar încercăm notificarea." - } finally { - sh "docker rm -f quotes-worker || true" - } - } - } - } - } - } +def get_quote(): + try: + # Timeout de 20s pentru a preveni blocarea pipeline-ului + response = requests.get(API_URL, timeout=20) + response.raise_for_status() + data = response.json() + + # Extragem mesajul (format AdviceSlip) + if 'slip' in data: + quote = data['slip'].get('advice') + author = "AdviceSlip" + else: + quote = data.get('content') or data.get('q') or "No message found." + author = data.get('author') or data.get('a') or "Unknown" - post { - always { - script { - // !!! Pune Webhook-ul tău aici !!! - def discordUrl = "https://discord.com/api/webhooks/1471492658336891013/T5s6ZKZjJjDMHXc3k3jjZdk6m5EV12bKF1wda9d5I_gZJrsDZQ1m1m078IiLJWK38mqa" - - def quoteText = "Nu am putut citi fișierul." - try { - quoteText = readFile('project2/quote.txt').trim() - } catch (err) { - echo "Fișierul nu a fost găsit." - } + message = f"📜 *\"{quote}\"* \n\n✍️ **{author}**" + + # Salvăm fișierul local în container (/app) + with open("quote.txt", "w", encoding="utf-8") as f: + f.write(message) + + print(f"Succes! Mesaj pregătit.") - def statusEmoji = (currentBuild.result == 'SUCCESS') ? "✅" : "⚠️" - - sh """ - curl -X POST -H 'Content-Type: application/json' \ - -d '{"content": "${statusEmoji} **Pipeline Finalizat (Build #${env.BUILD_NUMBER})**\\n\\n${quoteText}"}' \ - '${discordUrl}' - """ - } - } - } -} \ No newline at end of file + except Exception as e: + with open("quote.txt", "w", encoding="utf-8") as f: + f.write(f"⚠️ Eroare la preluare: {str(e)[:80]}") + sys.exit(0) # Nu oprim pipeline-ul forțat + +if __name__ == "__main__": + get_quote() \ No newline at end of file diff --git a/project2/jenkinsfile b/project2/jenkinsfile index 2c907bd..c1a9279 100644 --- a/project2/jenkinsfile +++ b/project2/jenkinsfile @@ -1,30 +1,56 @@ pipeline { agent any + stages { - stage('Build & Run') { + stage('Build & Lint') { steps { script { dir('project2') { + // 1. Construim imaginea sh "docker build -t quotes-app ." + + // 2. LINT CHECK: Verificăm sintaxa Python în interiorul containerului + echo "🔍 Verificăm sintaxa codului..." + sh "docker run --rm quotes-app python -m py_compile famousquotes.py" + } + } + } + } + + stage('Run & Extract') { + steps { + script { + dir('project2') { try { - // Adăugăm --dns 8.8.8.8 pentru a rezolva problema de rețea - sh "docker run --name quotes-container --network host --dns 8.8.8.8 quotes-app" - sh "docker cp quotes-container:/app/quote.txt ." + // 3. Rulăm cu DNS forțat pentru a evita "NameResolutionError" + sh "docker run --name quotes-worker --network host --dns 8.8.8.8 quotes-app" + + // 4. Extragem rezultatul + sh "docker cp quotes-worker:/app/quote.txt ." } catch (e) { - echo "Eroare la execuție." + echo "Eroare la rulare, dar încercăm notificarea." } finally { - sh "docker rm -f quotes-container || true" + sh "docker rm -f quotes-worker || true" } } } } } } + post { always { script { + // !!! Pune Webhook-ul tău aici !!! def discordUrl = "https://discord.com/api/webhooks/1471492658336891013/T5s6ZKZjJjDMHXc3k3jjZdk6m5EV12bKF1wda9d5I_gZJrsDZQ1m1m078IiLJWK38mqa" - def quoteText = readFile('project2/quote.txt').trim() + + def quoteText = "Nu am putut citi fișierul." + try { + quoteText = readFile('project2/quote.txt').trim() + } catch (err) { + echo "Fișierul nu a fost găsit." + } + def statusEmoji = (currentBuild.result == 'SUCCESS') ? "✅" : "⚠️" sh """