Created
July 13, 2022 17:35
-
-
Save citizenrich/06d38069496eaae824275ed8cb6d9d5f to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
"""update redmine wiki page with exoscale status | |
How to use this: | |
- `pip install exoscale python-redmine` | |
- set env vars for exoscale, use `exo config show --output-format json | jq -r` to find the existing exo file config | |
- put env vars for redmine user/pass in your shell | |
- determine the redmine site, project, and assign the wiki page to be updated | |
- test it out | |
- add it to cron or systemd | |
Example output in textile format: | |
h3{margin-top:0}. VM Status | |
|_. Name |_. State |_. Type | | |
| | | | | |
| server | Running | Small | | |
| server | Running | Medium | | |
""" | |
import os | |
import exoscale | |
from redminelib import Redmine | |
# exoscale stuff | |
key=os.environ['EXOSCALE_API_KEY'] | |
secret=os.environ['EXOSCALE_API_SECRET'] | |
exo = exoscale.Exoscale(api_key=key, api_secret=secret) | |
zone_gva2 = exo.compute.get_zone("ch-gva-2") | |
stuff = exo.compute.list_instances(zone=zone_gva2) | |
# template for the wiki table | |
vmtable = """ | |
h3{margin-top:0}. VM Status | |
|_. Name |_. State |_. Type | | |
| | | | | |
""" | |
# sort the list by vm name | |
sorted_vmlist = sorted(list(stuff), key=lambda a: a.name, reverse=False) | |
for thing in sorted_vmlist: | |
row = (f"| {thing.name} | {thing.state.capitalize()} | {thing.type.name} |") | |
# print(row) | |
vmtable += row + "\n" | |
print(vmtable) | |
# redmine stuff | |
redmine_username = os.environ['REDMINE_USERNAME'] | |
redmine_password = os.environ['REDMINE_PASSWORD'] | |
redmine_url = os.environ['REDMINE_URL'] | |
redmine = Redmine(url=redmine_url, username=redmine_username, password=redmine_password) | |
# iteration to find the project/page we want | |
# from: https://stackoverflow.com/questions/22166070/python-redmine-wiki-page-text | |
projects = redmine.project.all() | |
for project in projects: | |
print(str(project.id) + ': ' + project.name) | |
proj_id = project.id | |
try: | |
for wiki_page in project.wiki_pages: | |
wiki_page_details = redmine.wiki_page.get(wiki_page.title, project_id=proj_id) | |
print('Title: ' + wiki_page_details.title) | |
except: | |
pass | |
# change the project_id and title as needed | |
redmine.wiki_page.update(resource_id='Uptime', text=vmtable, project_id=8) | |
pagetext = redmine.wiki_page.get(resource_id='Uptime', project_id=8) | |
print(pagetext.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment