Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ART-9026 add new api for check schedule #135

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
re_path('test', views.test),
re_path('rpms_images_fetcher', views.rpms_images_fetcher_view),
re_path('login', views.login_view, name='login'),
re_path('check_auth', views.check_auth, name='check_auth')
re_path('check_auth', views.check_auth, name='check_auth'),
re_path('advisory_activites', views.get_advisory_activities, name='advisory_activities'),
re_path('release_schedule', views.get_release_schedule, name='release_schedule'),
re_path('release_status', views.get_release_status, name='release_status')
]
85 changes: 85 additions & 0 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@
from . import request_dispatcher
from .serializer import BuildSerializer
import django_filters
import requests
import base64
import yaml
import json
import re
import os
import jwt
from datetime import datetime, timedelta
from build_interface.settings import SECRET_KEY, SESSION_COOKIE_DOMAIN, JWTAuthentication
from django.http import JsonResponse
from lib.errata.errata_requests import get_advisory_status_activities, get_advisory_schedule, get_feature_freeze_schedule, get_ga_schedule


class BuildDataFilter(django_filters.FilterSet):
Expand Down Expand Up @@ -169,6 +174,86 @@ def branch_data(request):
return response


@api_view(["GET"])
def get_advisory_activities(request):
request_id = request.query_params.get("advisory", None)

if request_id is None:
return JsonResponse({"status": "error", "message": "Missing \"advisory\" params in the url."})
else:
return JsonResponse(get_advisory_status_activities(request_id))


@api_view(["GET"])
def get_release_schedule(request):
request_type = request.query_params.get("type", None)
branch_version = request.query_params.get("branch_version", None)

if request_type is None:
return JsonResponse({"status": "error", "message": "Missing \"type\" params in the url."})
if request_type not in ["ga", "release", "feature_freeze"]:
return JsonResponse({"status": "error", "message": "Invalid \"type\" params in the url. It sould be in ga,release,feature_freeze"})
if branch_version is None:
return JsonResponse({"status": "error", "message": "Missing \"branch_version\" params in the url."})
if request_type == "ga":
return JsonResponse(get_ga_schedule(branch_version), safe=False)
elif request_type == "release":
return JsonResponse(get_advisory_schedule(branch_version), safe=False)
elif request_type == "feature_freeze":
return JsonResponse(get_feature_freeze_schedule(branch_version), safe=False)


shipped_advisory = []

@api_view(["GET"])
def get_release_status(request):
ga_version = get_ga_version()
major, minor = int(ga_version.split('.')[0]), int(ga_version.split('.')[1])
status = {"message":[], "alert":[], "unshipped": []}
headers = {"Authorization": f"token {os.environ['GITHUB_PERSONAL_ACCESS_TOKEN']}"}
for r in range(0, 4):
version = minor - r
advisory_schedule = get_advisory_schedule(f"{major}.{version}")['all_ga_tasks']
for release in advisory_schedule:
if datetime.strptime(release['date_finish'],"%Y-%m-%d") < datetime.now():
release_date, release_name = release['date_finish'], release['name']
else:
break
if "GA" in release_name:
assembly = re.search(r'\d+\.\d+', release_name).group()+".0"
else:
assembly = re.search(r'\d+\.\d+.\d+', release_name).group()
status['message'].append({"release":f"{major}.{version}", "status": f"{assembly} release date is {release_date} and {release['name']} release date is {release['date_finish']}"})
res = requests.get(f"https://api.github.com/repos/openshift/ocp-build-data/contents/releases.yml?ref=openshift-{major}.{version}", headers=headers)
release_assembly = yaml.safe_load(base64.b64decode(res.json()['content']))['releases'][assembly]['assembly']
if "group" in release_assembly.keys():
if 'advisories!' in release_assembly['group'].keys():
advisories = release_assembly['group']['advisories!']
elif 'advisories' in release_assembly['group'].keys():
advisories = release_assembly['group']['advisories']
else:
advisories = {}
for ad in advisories:
if datetime.strptime(release_date,"%Y-%m-%d").strftime("%Y-%m-%d") == datetime.now().strftime("%Y-%m-%d"):
if advisories[ad] in shipped_advisory:
status['alert'].append({"release":f"{major}.{version}", "status": f"{assembly} <https://errata.devel.redhat.com/advisory/{advisories[ad]}|{ad}> advisory is shipped live"})
else:
errata_activity = get_advisory_status_activities(advisories[ad])['data']
if len(errata_activity) > 0:
errata_state = errata_activity[-1]['attributes']['added']
else:
errata_state = "NEW_FILES"
if errata_state == "SHIPPED_LIVE":
shipped_advisory.append(advisories[ad])
status['alert'].append({"release":f"{major}.{version}", "status": f"{assembly} <https://errata.devel.redhat.com/advisory/{advisories[ad]}|{ad}> advisory is shipped live"})
elif errata_state == "DROPPED_NO_SHIP":
status['alert'].append({"release":f"{major}.{version}", "status": f"{assembly} <https://errata.devel.redhat.com/advisory/{advisories[ad]}|{ad}> advisory is dropped"})
else:
status['alert'].append({"release":f"{major}.{version}", "status": f"{assembly} <https://errata.devel.redhat.com/advisory/{advisories[ad]}|{ad}> advisory is {errata_state}, release date is today"})
status['unshipped'].append({"advisory": advisories[ad], "note": f"{assembly} {ad} advisory"})
return JsonResponse(status)


@api_view(["GET"])
def test(request):
return Response({
Expand Down
3 changes: 3 additions & 0 deletions cron_tasks/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM registry.redhat.io/ubi9/ubi-minimal:latest
RUN microdnf install -y python pip && python3 -m pip install --upgrade pip && pip3 install slack_sdk requests
COPY check_schedule.py /check_schedule.py
40 changes: 40 additions & 0 deletions cron_tasks/check_schedule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I know why this scheduled job is here and not in aos-cd-jobs along with the other scheduled jobs? Unless there is a good reason, I would really like it to be in aos-cd-jobs

https://github.com/openshift-eng/aos-cd-jobs/tree/master/scheduled-jobs/scanning

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Different from the Jenkins job, this is a script that will build into the image. I used to think that moving this part to art-tools/art-cluster/pipelines, may be the right place along with task and job config. But consider we may set up a trigger to the image build when a new commit happens in the docker file repo. I think the art-tools repo updates more frequently than the art-dashboard-server repo and the script is more bonded with the server API so put it here. When the API is changed, it can be in the same PR and commit and the cluster only will rebuild once.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I understand from the code, the new API endpoint serves the data from errata unmodified, so we do not need to redeploy art-dash-server as well. The purpose of the Dockerfile is only to run this script right? This can be achieved with Jenkins, by setting up a periodic job. That is also where similar jobs are running.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if I didn't miss anything, I don't see any need for a Dockerfile here, and we can achieve the result that you intended by separating out the check_schedule.py code to aos-cd-jobs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true and false, the dockerfile only for run the script is true and we need to redeploy the server to provide service for the API. I would like to try the cron job in art cluster instead of using the jenkins scheduled job. the art-dash-server plays a role has permission to connect errata and response filtered output to the requester, which makes the "job" side very light and shaped with efficiency without extra init step.

import requests
from typing import Optional
import time
from slack_sdk import WebClient

slack_token = os.environ.get('SLACK_TOKEN', None)
API_ENDPOINT = "https://art-dash-server-art-dashboard-server.apps.artc2023.pc3z.p1.openshiftapps.com/api/v1"

def post_slack_message(message: str, thread_ts: Optional[str] = None,):
response = WebClient(token=slack_token).chat_postMessage(
channel="#forum-ocp-release",
text=message,
thread_ts=thread_ts, username="art-release-bot", link_names=True, attachments=[], icon_emoji=":dancing_robot:", reply_broadcast=False)
return response

release_status = requests.get(f"{API_ENDPOINT}/release_status").json()
if release_status['alert'] != []:
response = post_slack_message(' \n'.join([msg['status'] for msg in release_status['alert']]))
print(f"message posted in https://redhat-internal.slack.com/archives/{response['channel']}/p{response['ts'].replace('.', '')}")
if release_status['unshipped'] != []:
post_slack_message("start monitoring advisory not in shipped live status, interval set to 1 hour ...", thread_ts=response['ts'])
while release_status['unshipped'] != []:
for item in release_status['unshipped']:
# check ad status
advisory_status_response = requests.get(f"{API_ENDPOINT}/advisory_activites/?advisory={item['advisory']}").json()
errata_activity = advisory_status_response['data']
if len(errata_activity) > 0:
advisory_status = errata_activity[-1]['attributes']['added']
else:
advisory_status = "NEW_FILES"
if advisory_status == "SHIPPED_LIVE" or advisory_status == "DROPPED_NO_SHIP":
release_status['unshipped'].remove(item)
post_slack_message(f"{item['note']} status changed to {advisory_status}", thread_ts=response['ts'])
# sleep 1 hours
print(f"sleeping 1 hours due to {release_status['unshipped']}")
time.sleep(3600)
post_slack_message("All advisory now in shipped live status, stop monitoring", thread_ts=response['ts'])
else:
print("No alert", [msg['status'] for msg in release_status['message']])
36 changes: 36 additions & 0 deletions lib/errata/errata_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from requests_gssapi import HTTPSPNEGOAuth
import ssl

PP_SERVER = "https://pp.engineering.redhat.com/api/v7/releases"

@update_keytab
def get_advisory_data(advisory_id):
Expand Down Expand Up @@ -54,6 +55,41 @@ def get_user_data(user_id):
return None


def catch_request_result(url):
try:
print(url)
response = requests.get(url, verify=ssl.get_default_verify_paths().openssl_cafile, auth=HTTPSPNEGOAuth(), headers={'Content-Type': 'application/json'})
return json.loads(response.text)
except Exception as e:
print(e)
raise e


@update_keytab
def get_advisory_status_activities(advisory_id):
"""
This method returns advisory activities for a given id.
:param advisory_id: The id of the advisory to get data for.
:return: Dict, advisory data.
"""
return catch_request_result(f"https://errata.devel.redhat.com/api/v1/erratum/{advisory_id}/activities?filter[what]=status")


@update_keytab
def get_advisory_schedule(branch_version):
return catch_request_result(f"{PP_SERVER}/openshift-{branch_version}.z/?fields=all_ga_tasks")


@update_keytab
def get_feature_freeze_schedule(branch_version):
return catch_request_result(f"{PP_SERVER}/openshift-{branch_version}/schedule-tasks/?name__regex=Feature+Development+for")


@update_keytab
def get_ga_schedule(branch_version):
return catch_request_result(f"{PP_SERVER}/openshift-{branch_version}/schedule-tasks/?name=OpenShift+Container+Platform+GA+Release+Schedule")


def format_user_data(user_data):
return user_data

Expand Down
Loading