diff options
author | Johannes Garm Nielsen <jgn@nordu.net> | 2018-11-02 14:51:26 +0100 |
---|---|---|
committer | Johannes Garm Nielsen <jgn@nordu.net> | 2018-11-02 14:51:26 +0100 |
commit | 0fd73c56adbeb4a040de1ff2dad1b36e50c41425 (patch) | |
tree | 70bfd58af8f3ce91ac8d85d434fe23d2660ba7f1 |
initial commit
-rw-r--r-- | uploadDoc.py | 133 | ||||
-rw-r--r-- | wipeDocs.py | 21 |
2 files changed, 154 insertions, 0 deletions
diff --git a/uploadDoc.py b/uploadDoc.py new file mode 100644 index 0000000..a9a384e --- /dev/null +++ b/uploadDoc.py @@ -0,0 +1,133 @@ +import subprocess, fileinput, pprint, re, os, sys, json, pypandoc, yaml +from atlassian import Confluence +from json2html import * + +def prepareMarkdown(sourcePath): + fileContent = pypandoc.convert_file(sourcePath, "html") + + # Alter document to confluence standard so we can tell if the page needs updating + # Strip trailing whitespace + fileContent = fileContent.strip() + + # Remove ids since confluence file comparison fails with them + fileContent = re.sub(r" id=\"[\w-]*\"", "", fileContent) + + # Replace new lines with space + fileContent = re.sub(r"\n", " ", fileContent) + + # Remove markdown comments + fileContent = re.sub(r"<!--[ \w:]*-->", "", fileContent) + + # Custom character handling + fileContent = fileContent.replace('ó', u'ó') + fileContent = fileContent.replace('’', '’') + + return fileContent + +def prepareJSON(jsonFileContent): + jsonDict = json.loads(jsonFileContent) + htmlTable = json2html.convert(jsonDict) + htmlTable = htmlTable.strip() + + # Remove ids since confluence file comparison fails with them + htmlTable = re.sub(r" id=\"[\w-]*\"", "", htmlTable) + + # Replace new lines with space + htmlTable = re.sub(r"\n", " ", htmlTable) + + # Remove markdown comments + htmlTable = re.sub(r"<!--[ \w:]*-->", "", htmlTable) + + # Custom character handling + htmlTable = htmlTable.replace('ó', u'ó') + htmlTable = htmlTable.replace('’', '’') + + return htmlTable + +pp = pprint.PrettyPrinter(indent=2) + +configuration = yaml.load(open("docUploadConfig.yml", "r").read()) +print(configuration) + +confluence = Confluence( + url=configuration.get('confluenceUrl'), + username=configuration.get('confluenceUser'), + password=configuration.get('confluencePass')) + +hostsDir = sys.argv[1] + "/hosts" +puppetDir = sys.argv[1] + "/puppet/modules/ndn" +docsDir = sys.argv[1] + "/docs" + +errors = "" + +# Create hosts page +hostsPageId = confluence.get_page_id(configuration.get('spaceName'), "hosts") +if hostsPageId == None: + print("Creating hosts page") + hostsPageId = confluence.create_page(configuration.get('spaceName'), "hosts", '').get("id") + +# Create manifests pages +for dirname in os.listdir(hostsDir): + print(dirname) + # Create individual host page and manifests + dirPath = hostsDir + "/" + dirname + landingContent = "" + if os.path.isfile(dirPath + "/manifest.json"): + landingContent += prepareJSON(open(dirPath + "/manifest.json", "r").read()) + if os.path.isfile(dirPath + "/README.md"): + landingContent += prepareMarkdown(dirPath + "/README.md") + + dirPageReturn = confluence.update_or_create(hostsPageId, dirname, landingContent) + dirPageId = confluence.get_page_id(configuration.get('spaceName'), dirname) + manifestsPageReturn = confluence.update_or_create(dirPageId, "manifests - " + dirname, 'placeholder') + + for filename in os.listdir(dirPath + "/manifests"): + if filename.endswith(".pp"): + try: + subprocess.run(["puppet", "strings", "generate", "--format", "markdown", dirPath + "/" + filename, "--out", "temp.md"]) + fileContent = prepareMarkdown("temp.md") + pageReturn = confluence.update_or_create(manifestsPageReturn.get("id"), filename + " - " + dirname, fileContent) + except Exception as e: + print("Error processing manifest file: " + dirPath + "/" + filename) + errors += dirPath + "/manifests/" + filename + " MESSAGE: " + str(e) + "\n" + + +dirPageId = confluence.get_page_id(configuration.get('spaceName'), "ndn") +if dirPageId == None: + dirPageId = confluence.create_page(configuration.get('spaceName'), "ndn", '').get("id") + +modulesPageReturn = confluence.update_or_create(dirPageId, "puppet/modules/ndn", "") + +manifestsDir = puppetDir + "/manifests" + +if os.path.isdir(manifestsDir): + for manifestFile in os.listdir(manifestsDir): + try: + if manifestFile.endswith(".pp"): + subprocess.run(["puppet", "strings", "generate", "--format", "markdown", manifestsDir + "/" + manifestFile, "--out", "temp.md"]) + fileContent = prepareMarkdown("temp.md") + pp.pprint("page: " + manifestFile + " - " + dirname) + pageReturn = confluence.update_or_create(modulesPageReturn.get("id"), manifestFile + " - " + dirname, fileContent) + except Exception as e: + print("Error processing puppet manifest: " + dirname + "/" + manifestFile) + errors += manifestsDir + "/" + manifestFile + " MESSAGE: " + str(e) + "\n" + +docsPageId = confluence.get_page_id(configuration.get('spaceName'), "docs") +if docsPageId == None: + print("Creating docs page") + docsPageId = confluence.create_page(configuration.get('spaceName'), "docs", '').get("id") + +for docsName in os.listdir(docsDir): + try: + fileContent = prepareMarkdown(docsDir + "/" + docsName) + pp.pprint("page: " + docsDir + " - " + docsName) + pageReturn = confluence.update_or_create(docsPageId, docsName + " - docs", fileContent) + except Exception as e: + print("Error processing docs file: " + docsDir + "/" + docsName) + errors += docsDir + "/" + docsName + " MESSAGE: " + str(e) + "\n" + +if errors != "": + print("Errors encountered while handling following docs: ") + print(errors) + +print("------- DONE -------")
\ No newline at end of file diff --git a/wipeDocs.py b/wipeDocs.py new file mode 100644 index 0000000..390f167 --- /dev/null +++ b/wipeDocs.py @@ -0,0 +1,21 @@ +import pprint, json +from atlassian import Confluence +from base64 import b64encode + +pp = pprint.PrettyPrinter(indent=2) + +confluence = Confluence( + url='http://localhost:8090', + username='apiuser', + password='apiuserpass') + +spaceName = sys.argv[1] + +pages = confluence.get_all_pages_from_space(spaceName) + +# jsonPages = json.loads(pages) + +for page in pages: + print(pp.pprint(page)) + resp = confluence.remove_page(page['id']) + print(pp.pprint(resp))
\ No newline at end of file |