|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "name": "stdout", |
| 10 | + "output_type": "stream", |
| 11 | + "text": [ |
| 12 | + "Waiting for repo sync tasks to finish...\n", |
| 13 | + "Repo Sync completed waiting after 0 seconds\n", |
| 14 | + "> \u001b[0;32m<ipython-input-1-015ab9615730>\u001b[0m(105)\u001b[0;36mmain\u001b[0;34m()\u001b[0m\n", |
| 15 | + "\u001b[0;32m 103 \u001b[0;31m \u001b[0mset_trace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 16 | + "\u001b[0m\u001b[0;32m 104 \u001b[0;31m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 17 | + "\u001b[0m\u001b[0;32m--> 105 \u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 18 | + "\u001b[0m\u001b[0;32m 106 \u001b[0;31m \u001b[0moutput\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdisplay_task_results\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mSAT_API\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'foreman_tasks/api/tasks/%s'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'SyncID'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 19 | + "\u001b[0m\u001b[0;32m 107 \u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0moutput\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 20 | + "\u001b[0m\n" |
| 21 | + ] |
| 22 | + } |
| 23 | + ], |
| 24 | + "source": [ |
| 25 | + "import json\n", |
| 26 | + "import sys\n", |
| 27 | + "import time\n", |
| 28 | + "import urllib2\n", |
| 29 | + "import subprocess\n", |
| 30 | + "from IPython.core.debugger import set_trace\n", |
| 31 | + "\n", |
| 32 | + "try:\n", |
| 33 | + " import requests\n", |
| 34 | + "except ImportError:\n", |
| 35 | + " print \"Please install the python-requests module.\"\n", |
| 36 | + " sys.exit(-1)\n", |
| 37 | + "\n", |
| 38 | + "from requests.packages.urllib3.exceptions import InsecureRequestWarning\n", |
| 39 | + "requests.packages.urllib3.disable_warnings(InsecureRequestWarning)\n", |
| 40 | + "\n", |
| 41 | + "URL = 'https://vm255-149.gsslab.pnq2.redhat.com/'\n", |
| 42 | + "SAT_API = 'https://vm255-149.gsslab.pnq2.redhat.com/'\n", |
| 43 | + "\n", |
| 44 | + "USERNAME = \"admin\"\n", |
| 45 | + "PASSWORD = \"redhat\"\n", |
| 46 | + "\n", |
| 47 | + "SSL_VERIFY = False # Ignore SSL for now\n", |
| 48 | + "def get_json(url):\n", |
| 49 | + " # Performs a GET using the passed URL location\n", |
| 50 | + " r = requests.get(url, auth=(USERNAME, PASSWORD), verify=SSL_VERIFY)\n", |
| 51 | + " return r.json()\n", |
| 52 | + "\n", |
| 53 | + "def get_results(url):\n", |
| 54 | + " jsn = get_json(url)\n", |
| 55 | + " if jsn.get('error'):\n", |
| 56 | + " print \"Error: \" + jsn['error']['message']\n", |
| 57 | + " else:\n", |
| 58 | + " if jsn.get('results'):\n", |
| 59 | + " return jsn['results']\n", |
| 60 | + " elif 'results' not in jsn:\n", |
| 61 | + " return jsn\n", |
| 62 | + " else:\n", |
| 63 | + " print \"No results found\"\n", |
| 64 | + " return None\n", |
| 65 | + "\n", |
| 66 | + "def post_json(location):\n", |
| 67 | + " \"\"\"\n", |
| 68 | + " Performs a POST and passes the data to the URL location\n", |
| 69 | + " \"\"\"\n", |
| 70 | + " POST_HEADERS = {'content-type': 'application/json'}\n", |
| 71 | + " result = requests.post(\n", |
| 72 | + " location,\n", |
| 73 | + " auth=(USERNAME, PASSWORD),\n", |
| 74 | + " verify=SSL_VERIFY,\n", |
| 75 | + " headers=POST_HEADERS)\n", |
| 76 | + "\n", |
| 77 | + " return result.json()\n", |
| 78 | + "\n", |
| 79 | + "def wait_for_sync(seconds):\n", |
| 80 | + "# repo_sync_tasks = \"foreman_tasks/api/tasks?search=utf8=%E2%9C%93&search=label+%3D+Actions%3A%3AKatello%3A%3ARepository%3A%3ASync+and+state+%3D+running\"\n", |
| 81 | + " repo_sync_tasks = \"foreman_tasks/api/tasks?utf8=.&search=Actions%3A%3AKatello%3A%3ARepository%3A%3ASync+and+state+%3D+running\"\n", |
| 82 | + " \"\"\"\n", |
| 83 | + " Wait for all repo sync tasks to terminate. Search string is:\n", |
| 84 | + " label = Actions::Katello::Repository::Sync and state = running\n", |
| 85 | + " \"\"\"\n", |
| 86 | + "\n", |
| 87 | + " count = 0\n", |
| 88 | + " print \"Waiting for repo sync tasks to finish...\"\n", |
| 89 | + "\n", |
| 90 | + " # Make sure that repo sync tasks gets the chance to appear before looking for them\n", |
| 91 | + " time.sleep(2)\n", |
| 92 | + "\n", |
| 93 | + " while get_json(URL + repo_sync_tasks)[\"total\"] != 0:\n", |
| 94 | + " time.sleep(seconds)\n", |
| 95 | + " count += 1\n", |
| 96 | + "\n", |
| 97 | + " print \"Repo Sync completed waiting after \" + str(seconds * count) + \" seconds\"\n", |
| 98 | + "\n", |
| 99 | + "def display_task_results(url):\n", |
| 100 | + " results = get_results(url)\n", |
| 101 | + " if results:\n", |
| 102 | + " #print results['output']\n", |
| 103 | + " out = results['humanized']['output']\n", |
| 104 | + " return out\n", |
| 105 | + "\n", |
| 106 | + "\n", |
| 107 | + "def main():\n", |
| 108 | + " date=(time.strftime(\"%d-%m-%Y\"))\n", |
| 109 | + "\n", |
| 110 | + " url=SAT_API + '/katello/api/products/248/repositories?organization_id=1'\n", |
| 111 | + " results=get_results(url)\n", |
| 112 | + " length=0\n", |
| 113 | + " for result in results:\n", |
| 114 | + " length=length+1\n", |
| 115 | + " data = [{'RepoID' : 'repo_id', 'RepoName' : 'Name', 'SyncID' : 'sync_id', 'Output' : 'out'} for k in range(length)]\n", |
| 116 | + " i=0\n", |
| 117 | + " for result in results:\n", |
| 118 | + " sync_url=SAT_API + '/katello/api/repositories/%d/sync' % result['id']\n", |
| 119 | + " sync_out=post_json(sync_url)\n", |
| 120 | + " data[i]['RepoID'] = result['id']\n", |
| 121 | + " data[i]['SyncID'] = sync_out['id']\n", |
| 122 | + " data[i]['RepoName'] = result['name']\n", |
| 123 | + " time.sleep(600)\n", |
| 124 | + " i=i+1\n", |
| 125 | + "\n", |
| 126 | + " wait_for_sync(10)\n", |
| 127 | + " set_trace()\n", |
| 128 | + "\n", |
| 129 | + " for k in range(0,i):\n", |
| 130 | + " output=display_task_results(SAT_API + 'foreman_tasks/api/tasks/%s' % data[k]['SyncID'])\n", |
| 131 | + " print output\n", |
| 132 | + "\n", |
| 133 | + "if __name__ == \"__main__\":\n", |
| 134 | + " main()" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "code", |
| 139 | + "execution_count": null, |
| 140 | + "metadata": {}, |
| 141 | + "outputs": [], |
| 142 | + "source": [] |
| 143 | + } |
| 144 | + ], |
| 145 | + "metadata": { |
| 146 | + "kernelspec": { |
| 147 | + "display_name": "Python 2", |
| 148 | + "language": "python", |
| 149 | + "name": "python2" |
| 150 | + }, |
| 151 | + "language_info": { |
| 152 | + "codemirror_mode": { |
| 153 | + "name": "ipython", |
| 154 | + "version": 2 |
| 155 | + }, |
| 156 | + "file_extension": ".py", |
| 157 | + "mimetype": "text/x-python", |
| 158 | + "name": "python", |
| 159 | + "nbconvert_exporter": "python", |
| 160 | + "pygments_lexer": "ipython2", |
| 161 | + "version": "2.7.5" |
| 162 | + } |
| 163 | + }, |
| 164 | + "nbformat": 4, |
| 165 | + "nbformat_minor": 2 |
| 166 | +} |
0 commit comments