|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "name": "stdout", |
| 10 | + "output_type": "stream", |
| 11 | + "text": [ |
| 12 | + "Requirement already satisfied: onepanel-sdk in /opt/conda/lib/python3.6/site-packages (0.12.0)\n", |
| 13 | + "Requirement already satisfied: python-dateutil in /opt/conda/lib/python3.6/site-packages (from onepanel-sdk) (2.8.1)\n", |
| 14 | + "Requirement already satisfied: certifi in /opt/conda/lib/python3.6/site-packages (from onepanel-sdk) (2020.6.20)\n", |
| 15 | + "Requirement already satisfied: six>=1.10 in /opt/conda/lib/python3.6/site-packages (from onepanel-sdk) (1.15.0)\n", |
| 16 | + "Requirement already satisfied: urllib3>=1.15 in /opt/conda/lib/python3.6/site-packages (from onepanel-sdk) (1.25.10)\n" |
| 17 | + ] |
| 18 | + } |
| 19 | + ], |
| 20 | + "source": [ |
| 21 | + "!pip install onepanel-sdk" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": 2, |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "import os\n", |
| 31 | + "import sys\n", |
| 32 | + "from time import sleep\n", |
| 33 | + "from pprint import pprint\n", |
| 34 | + "\n", |
| 35 | + "import onepanel.core.api\n", |
| 36 | + "from onepanel.core.api.rest import ApiException\n", |
| 37 | + "from onepanel.core.api.models import Parameter" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "code", |
| 42 | + "execution_count": 4, |
| 43 | + "metadata": {}, |
| 44 | + "outputs": [ |
| 45 | + { |
| 46 | + "name": "stdout", |
| 47 | + "output_type": "stream", |
| 48 | + "text": [ |
| 49 | + "{'created_at': '2020-08-26T22:41:51Z',\n", |
| 50 | + " 'finished_at': None,\n", |
| 51 | + " 'labels': None,\n", |
| 52 | + " 'manifest': None,\n", |
| 53 | + " 'metadata': {'url': 'http://app.rush-gcs.onepanel.site/rush/workflows/pytorch-training-qbc6z'},\n", |
| 54 | + " 'name': 'pytorch-training-qbc6z',\n", |
| 55 | + " 'parameters': None,\n", |
| 56 | + " 'phase': None,\n", |
| 57 | + " 'started_at': None,\n", |
| 58 | + " 'uid': 'pytorch-training-qbc6z',\n", |
| 59 | + " 'workflow_template': {'created_at': '2020-08-26T05:25:08Z',\n", |
| 60 | + " 'cron_stats': None,\n", |
| 61 | + " 'is_archived': None,\n", |
| 62 | + " 'is_latest': None,\n", |
| 63 | + " 'labels': None,\n", |
| 64 | + " 'manifest': 'entrypoint: main\\n'\n", |
| 65 | + " 'arguments:\\n'\n", |
| 66 | + " ' parameters:\\n'\n", |
| 67 | + " ' - name: source\\n'\n", |
| 68 | + " ' value: '\n", |
| 69 | + " 'https://github.com/onepanelio/pytorch-examples.git\\n'\n", |
| 70 | + " ' - name: command\\n'\n", |
| 71 | + " ' value: \"python mnist/main.py '\n", |
| 72 | + " '--epochs=1\"\\n'\n", |
| 73 | + " 'volumeClaimTemplates:\\n'\n", |
| 74 | + " ' - metadata:\\n'\n", |
| 75 | + " ' name: data\\n'\n", |
| 76 | + " ' spec:\\n'\n", |
| 77 | + " ' accessModes: [ \"ReadWriteOnce\" ]\\n'\n", |
| 78 | + " ' resources:\\n'\n", |
| 79 | + " ' requests:\\n'\n", |
| 80 | + " ' storage: 2Gi\\n'\n", |
| 81 | + " ' - metadata:\\n'\n", |
| 82 | + " ' name: output\\n'\n", |
| 83 | + " ' spec:\\n'\n", |
| 84 | + " ' accessModes: [ \"ReadWriteOnce\" ]\\n'\n", |
| 85 | + " ' resources:\\n'\n", |
| 86 | + " ' requests:\\n'\n", |
| 87 | + " ' storage: 2Gi\\n'\n", |
| 88 | + " 'templates:\\n'\n", |
| 89 | + " ' - name: main\\n'\n", |
| 90 | + " ' dag:\\n'\n", |
| 91 | + " ' tasks:\\n'\n", |
| 92 | + " ' - name: train-model\\n'\n", |
| 93 | + " ' template: pytorch\\n'\n", |
| 94 | + " '# Uncomment section below to send metrics '\n", |
| 95 | + " 'to Slack\\n'\n", |
| 96 | + " '# - name: notify-in-slack\\n'\n", |
| 97 | + " '# dependencies: [train-model]\\n'\n", |
| 98 | + " '# template: slack-notify-success\\n'\n", |
| 99 | + " '# arguments:\\n'\n", |
| 100 | + " '# parameters:\\n'\n", |
| 101 | + " '# - name: status\\n'\n", |
| 102 | + " '# value: '\n", |
| 103 | + " '\"{{tasks.train-model.status}}\"\\n'\n", |
| 104 | + " '# artifacts:\\n'\n", |
| 105 | + " '# - name: metrics\\n'\n", |
| 106 | + " '# from: '\n", |
| 107 | + " '\"{{tasks.train-model.outputs.artifacts.sys-metrics}}\"\\n'\n", |
| 108 | + " ' - name: pytorch\\n'\n", |
| 109 | + " ' inputs:\\n'\n", |
| 110 | + " ' artifacts:\\n'\n", |
| 111 | + " ' - name: src\\n'\n", |
| 112 | + " ' path: /mnt/src\\n'\n", |
| 113 | + " ' git:\\n'\n", |
| 114 | + " ' repo: '\n", |
| 115 | + " '\"{{workflow.parameters.source}}\"\\n'\n", |
| 116 | + " ' outputs:\\n'\n", |
| 117 | + " ' artifacts:\\n'\n", |
| 118 | + " ' - name: model\\n'\n", |
| 119 | + " ' path: /mnt/output\\n'\n", |
| 120 | + " ' optional: true\\n'\n", |
| 121 | + " ' archive:\\n'\n", |
| 122 | + " ' none: {}\\n'\n", |
| 123 | + " ' container:\\n'\n", |
| 124 | + " ' image: pytorch/pytorch:latest\\n'\n", |
| 125 | + " ' command: [sh,-c]\\n'\n", |
| 126 | + " ' args: '\n", |
| 127 | + " '[\"{{workflow.parameters.command}}\"]\\n'\n", |
| 128 | + " ' workingDir: /mnt/src\\n'\n", |
| 129 | + " ' volumeMounts:\\n'\n", |
| 130 | + " ' - name: data\\n'\n", |
| 131 | + " ' mountPath: /mnt/data\\n'\n", |
| 132 | + " ' - name: output\\n'\n", |
| 133 | + " ' mountPath: /mnt/output\\n'\n", |
| 134 | + " ' - name: slack-notify-success\\n'\n", |
| 135 | + " ' container:\\n'\n", |
| 136 | + " ' image: technosophos/slack-notify\\n'\n", |
| 137 | + " ' command: [sh,-c]\\n'\n", |
| 138 | + " \" args: ['SLACK_USERNAME=Worker \"\n", |
| 139 | + " 'SLACK_TITLE=\"{{workflow.name}} '\n", |
| 140 | + " '{{inputs.parameters.status}}\" '\n", |
| 141 | + " 'SLACK_ICON=https://www.gravatar.com/avatar/5c4478592fe00878f62f0027be59c1bd '\n", |
| 142 | + " 'SLACK_MESSAGE=$(cat /tmp/metrics.json)} '\n", |
| 143 | + " \"./slack-notify']\\n\"\n", |
| 144 | + " ' inputs:\\n'\n", |
| 145 | + " ' parameters:\\n'\n", |
| 146 | + " ' - name: status\\n'\n", |
| 147 | + " ' artifacts:\\n'\n", |
| 148 | + " ' - name: metrics\\n'\n", |
| 149 | + " ' path: /tmp/metrics.json\\n'\n", |
| 150 | + " ' optional: true\\n',\n", |
| 151 | + " 'modified_at': '2020-08-26T05:25:08Z',\n", |
| 152 | + " 'name': 'PyTorch Training',\n", |
| 153 | + " 'parameters': [{'display_name': None,\n", |
| 154 | + " 'hint': None,\n", |
| 155 | + " 'name': 'source',\n", |
| 156 | + " 'options': None,\n", |
| 157 | + " 'required': None,\n", |
| 158 | + " 'type': None,\n", |
| 159 | + " 'value': 'https://github.com/onepanelio/pytorch-examples.git',\n", |
| 160 | + " 'visibility': 'public'},\n", |
| 161 | + " {'display_name': None,\n", |
| 162 | + " 'hint': None,\n", |
| 163 | + " 'name': 'command',\n", |
| 164 | + " 'options': None,\n", |
| 165 | + " 'required': None,\n", |
| 166 | + " 'type': None,\n", |
| 167 | + " 'value': 'python mnist/main.py '\n", |
| 168 | + " '--epochs=1',\n", |
| 169 | + " 'visibility': 'public'}],\n", |
| 170 | + " 'stats': {'completed': None,\n", |
| 171 | + " 'failed': None,\n", |
| 172 | + " 'last_executed': '0001-01-01T00:00:00Z',\n", |
| 173 | + " 'running': None,\n", |
| 174 | + " 'terminated': None,\n", |
| 175 | + " 'total': None},\n", |
| 176 | + " 'uid': 'pytorch-training',\n", |
| 177 | + " 'version': '1598419508627499422',\n", |
| 178 | + " 'versions': None}}\n" |
| 179 | + ] |
| 180 | + } |
| 181 | + ], |
| 182 | + "source": [ |
| 183 | + "# If outside of Onepanel\n", |
| 184 | + "# token = '<auth-token>'\n", |
| 185 | + "# Set environment variables for:\n", |
| 186 | + "# ONEPANEL_API_URL, example: https://app.domain.com/api\n", |
| 187 | + "# ONEPANEL_RESOURCE_NAMESPACE, example: default\n", |
| 188 | + "\n", |
| 189 | + "# If inside of Onepanel, get mounted service account token to use as API Key\n", |
| 190 | + "with open('/var/run/secrets/kubernetes.io/serviceaccount/token') as f:\n", |
| 191 | + " token = f.read()\n", |
| 192 | + "\n", |
| 193 | + "# Configure API key authorization: Bearer\n", |
| 194 | + "configuration = onepanel.core.api.Configuration(\n", |
| 195 | + " host=os.getenv('ONEPANEL_API_URL'),\n", |
| 196 | + " api_key={\n", |
| 197 | + " 'Bearer': token\n", |
| 198 | + " }\n", |
| 199 | + ")\n", |
| 200 | + "configuration.api_key_prefix['Bearer'] = 'Bearer'\n", |
| 201 | + "\n", |
| 202 | + "# Enter a context with an instance of the API client\n", |
| 203 | + "with onepanel.core.api.ApiClient(configuration) as api_client:\n", |
| 204 | + " # Create an instance of the API class\n", |
| 205 | + " api_instance = onepanel.core.api.WorkflowServiceApi(api_client)\n", |
| 206 | + "\n", |
| 207 | + " namespace = os.getenv('ONEPANEL_RESOURCE_NAMESPACE')\n", |
| 208 | + " params = []\n", |
| 209 | + " params.append(Parameter(name='source', value='https://github.com/onepanelio/pytorch-examples.git'))\n", |
| 210 | + " params.append(Parameter(name='command', value='python mnist/main.py --epochs=1'))\n", |
| 211 | + " body = onepanel.core.api.CreateWorkflowExecutionBody(\n", |
| 212 | + " parameters=params,\n", |
| 213 | + " workflow_template_uid='pytorch-training',\n", |
| 214 | + " )\n", |
| 215 | + " try:\n", |
| 216 | + " workflow = api_instance.create_workflow_execution(namespace, body)\n", |
| 217 | + " pprint(workflow)\n", |
| 218 | + " except ApiException as e:\n", |
| 219 | + " print('Exception when calling WorkflowServiceApi->create_workflow_execution: %s\\n' % e)" |
| 220 | + ] |
| 221 | + }, |
| 222 | + { |
| 223 | + "cell_type": "code", |
| 224 | + "execution_count": null, |
| 225 | + "metadata": {}, |
| 226 | + "outputs": [], |
| 227 | + "source": [] |
| 228 | + } |
| 229 | + ], |
| 230 | + "metadata": { |
| 231 | + "kernelspec": { |
| 232 | + "display_name": "Python 3", |
| 233 | + "language": "python", |
| 234 | + "name": "python3" |
| 235 | + }, |
| 236 | + "language_info": { |
| 237 | + "codemirror_mode": { |
| 238 | + "name": "ipython", |
| 239 | + "version": 3 |
| 240 | + }, |
| 241 | + "file_extension": ".py", |
| 242 | + "mimetype": "text/x-python", |
| 243 | + "name": "python", |
| 244 | + "nbconvert_exporter": "python", |
| 245 | + "pygments_lexer": "ipython3", |
| 246 | + "version": "3.6.11" |
| 247 | + } |
| 248 | + }, |
| 249 | + "nbformat": 4, |
| 250 | + "nbformat_minor": 4 |
| 251 | +} |
0 commit comments