-
Notifications
You must be signed in to change notification settings - Fork 3
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
DM-42606: Provide self-consistent alert schema version numbers #43
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4e0d3c2
Add class method to populate all schemas in a registry
bsmartradio e6aec2f
Rename syncLatestSchemasToRegistry to syncAllSchemasToRegistry
bsmartradio 64cf278
Update README
bsmartradio bcc9fc0
Update Docker file
bsmartradio ee93394
Change id calculation to new convention and add known_id property
bsmartradio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
python/lsst/alert/packet/bin/syncAllSchemasToRegistry.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
#!/usr/bin/env python | ||
# | ||
# This file is part of alert_packet. | ||
# | ||
# Developed for the LSST Data Management System. | ||
# This product includes software developed by the LSST Project | ||
# (https://www.lsst.org). | ||
# See the COPYRIGHT file at the top-level directory of this distribution | ||
# for details of code ownership. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
import argparse | ||
import json | ||
import fastavro | ||
import requests | ||
|
||
import lsst.alert.packet | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"--schema-registry-url", | ||
type=str, | ||
default="http://alert-schemas.localhost", | ||
help="URL of a Confluent Schema Registry service", | ||
) | ||
parser.add_argument( | ||
"--subject", | ||
type=str, | ||
default="alert-packet", | ||
help="Schema Registry subject name to use", | ||
) | ||
return parser.parse_args() | ||
|
||
|
||
def upload_schemas(registry_url, subject, schema_registry): | ||
"""Parse schema registry and upload all schemas. | ||
""" | ||
for schema_id in schema_registry.known_ids: | ||
schema = schema_registry.get_by_id(schema_id) | ||
normalized_schema = fastavro.schema.to_parsing_canonical_form( | ||
schema.definition) | ||
confluent_schema = {"version": schema_id, | ||
"id": schema_id, "schema": normalized_schema} | ||
payload = json.dumps(confluent_schema) | ||
headers = {"Content-Type": "application/vnd.schemaregistry.v1+json"} | ||
url = f"{registry_url}/subjects/{subject}/versions" | ||
print(f"uploading schema to {url}") | ||
response = requests.post(url=url, data=payload, headers=headers) | ||
response.raise_for_status() | ||
print(f"done, status={response.status_code}") | ||
print(f"response text={response.text}") | ||
|
||
|
||
def clear_schema_registry_for_import(registry_url, subject): | ||
"""Delete schemas in the registry and then remake it in import mode""" | ||
# Define the URLs | ||
url_mode = f"{registry_url}/mode/{subject}" | ||
url_schemas = f"{registry_url}/subjects/{subject}" | ||
url_schema_versions = f"{registry_url}/subjects/{subject}/versions" | ||
response = requests.get(url_schema_versions) | ||
|
||
# Schema registry must be empty to put it in import mode. If it exists, | ||
# remove it and remake the schema. If not, continue. | ||
if response.status_code == 200: | ||
print('The schema will be deleted and remade in import mode.') | ||
response = requests.delete(url_schemas) | ||
print('Status Code:', response.status_code) | ||
print('Response Text:', response.text) | ||
else: | ||
print('The schema does not exist. Creating in import mode.') | ||
|
||
# Switch registry to import mode. | ||
headers = { | ||
'Content-Type': 'application/json' | ||
} | ||
|
||
# Define the data to send | ||
data = { | ||
'mode': 'IMPORT' | ||
} | ||
|
||
# Perform the PUT request | ||
response = requests.put(url_mode, headers=headers, data=json.dumps(data)) | ||
|
||
# Check the status code and response | ||
print('Status Code:', response.status_code) | ||
print('Response Text:', response.text) | ||
|
||
|
||
def close_schema_registry(registry_url, subject): | ||
"""Return the schema registry from import mode to readwrite. | ||
""" | ||
data = { | ||
"mode": "READWRITE" | ||
} | ||
|
||
# Headers to specify the content type | ||
headers = { | ||
'Content-Type': 'application/json' | ||
} | ||
|
||
url_mode = f"{registry_url}/mode/{subject}" | ||
# Send the PUT request | ||
response = requests.put(url_mode, json=data, headers=headers) | ||
print(f'Status Code: {response.status_code}') | ||
print(f'Response Text: {response.text}') | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
clear_schema_registry_for_import(args.schema_registry_url, args.subject) | ||
schema_registry = lsst.alert.packet.schemaRegistry.SchemaRegistry().all_schemas_from_filesystem() | ||
upload_schemas( | ||
args.schema_registry_url, | ||
subject=args.subject, | ||
schema_registry=schema_registry | ||
) | ||
close_schema_registry(args.schema_registry_url, args.subject) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
79 changes: 0 additions & 79 deletions
79
python/lsst/alert/packet/bin/syncLatestSchemaToRegistry.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we should add an
assert(len(numbers) == 2)
here? I'm nervous about having versioning driven by regexp.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added