Bläddra i källkod

Print Monitor in colour

Jimmy Allen 3 månader sedan
förälder
incheckning
8729dc3634
2 ändrade filer med 139 tillägg och 21 borttagningar
  1. 135 17
      docker.ipynb
  2. 4 4
      monitor.py

+ 135 - 17
docker.ipynb

@@ -9,7 +9,7 @@
9 9
   },
10 10
   {
11 11
    "cell_type": "code",
12
-   "execution_count": null,
12
+   "execution_count": 1,
13 13
    "metadata": {
14 14
     "slideshow": {
15 15
      "slide_type": "slide"
@@ -22,7 +22,7 @@
22 22
   },
23 23
   {
24 24
    "cell_type": "code",
25
-   "execution_count": null,
25
+   "execution_count": 2,
26 26
    "metadata": {
27 27
     "slideshow": {
28 28
      "slide_type": "slide"
@@ -35,7 +35,7 @@
35 35
   },
36 36
   {
37 37
    "cell_type": "code",
38
-   "execution_count": null,
38
+   "execution_count": 3,
39 39
    "metadata": {},
40 40
    "outputs": [],
41 41
    "source": [
@@ -43,19 +43,48 @@
43 43
    ]
44 44
   },
45 45
   {
46
-   "cell_type": "markdown",
47
-   "metadata": {
48
-    "scrolled": true
49
-   },
46
+   "cell_type": "code",
47
+   "execution_count": 4,
48
+   "metadata": {},
49
+   "outputs": [
50
+    {
51
+     "name": "stdout",
52
+     "output_type": "stream",
53
+     "text": [
54
+      "\u001b[32mMonitor: A container has been created\n",
55
+      "\u001b[92mMonitor: A container has been started\n",
56
+      "\u001b[91mMonitor: A container has been stopped\n"
57
+     ]
58
+    },
59
+    {
60
+     "data": {
61
+      "text/plain": [
62
+       "b''"
63
+      ]
64
+     },
65
+     "execution_count": 4,
66
+     "metadata": {},
67
+     "output_type": "execute_result"
68
+    }
69
+   ],
50 70
    "source": [
51 71
     "client.containers.run(image=\"ubuntu\", name=\"ubuntu\")"
52 72
    ]
53 73
   },
54 74
   {
55 75
    "cell_type": "code",
56
-   "execution_count": null,
76
+   "execution_count": 5,
57 77
    "metadata": {},
58
-   "outputs": [],
78
+   "outputs": [
79
+    {
80
+     "name": "stdout",
81
+     "output_type": "stream",
82
+     "text": [
83
+      "ubuntu\n",
84
+      "jupyter_jupyter_1\n"
85
+     ]
86
+    }
87
+   ],
59 88
    "source": [
60 89
     "for container in client.containers.list(all=True):\n",
61 90
     "    print(container.name)"
@@ -63,7 +92,7 @@
63 92
   },
64 93
   {
65 94
    "cell_type": "code",
66
-   "execution_count": null,
95
+   "execution_count": 6,
67 96
    "metadata": {},
68 97
    "outputs": [],
69 98
    "source": [
@@ -72,36 +101,104 @@
72 101
   },
73 102
   {
74 103
    "cell_type": "code",
75
-   "execution_count": null,
104
+   "execution_count": 7,
76 105
    "metadata": {},
77
-   "outputs": [],
106
+   "outputs": [
107
+    {
108
+     "name": "stdout",
109
+     "output_type": "stream",
110
+     "text": [
111
+      "exited\n"
112
+     ]
113
+    }
114
+   ],
78 115
    "source": [
79 116
     "print(ubuntu.status)"
80 117
    ]
81 118
   },
82 119
   {
83 120
    "cell_type": "code",
84
-   "execution_count": null,
121
+   "execution_count": 8,
85 122
    "metadata": {},
86
-   "outputs": [],
123
+   "outputs": [
124
+    {
125
+     "name": "stdout",
126
+     "output_type": "stream",
127
+     "text": [
128
+      "\u001b[92mMonitor: A container has been started\n"
129
+     ]
130
+    }
131
+   ],
87 132
    "source": [
88 133
     "ubuntu.start()"
89 134
    ]
90 135
   },
91 136
   {
92 137
    "cell_type": "code",
93
-   "execution_count": null,
138
+   "execution_count": 9,
139
+   "metadata": {},
140
+   "outputs": [
141
+    {
142
+     "name": "stdout",
143
+     "output_type": "stream",
144
+     "text": [
145
+      "\u001b[91mMonitor: A container has been stopped\n"
146
+     ]
147
+    },
148
+    {
149
+     "data": {
150
+      "text/plain": [
151
+       "{'Error': None, 'StatusCode': 0}"
152
+      ]
153
+     },
154
+     "execution_count": 9,
155
+     "metadata": {},
156
+     "output_type": "execute_result"
157
+    }
158
+   ],
159
+   "source": [
160
+    "ubuntu.wait()"
161
+   ]
162
+  },
163
+  {
164
+   "cell_type": "code",
165
+   "execution_count": 10,
94 166
    "metadata": {},
95 167
    "outputs": [],
96 168
    "source": [
169
+    "ubuntu.stop()"
170
+   ]
171
+  },
172
+  {
173
+   "cell_type": "code",
174
+   "execution_count": 11,
175
+   "metadata": {},
176
+   "outputs": [
177
+    {
178
+     "name": "stdout",
179
+     "output_type": "stream",
180
+     "text": [
181
+      "\u001b[31mMonitor: A container has been deleted\n"
182
+     ]
183
+    }
184
+   ],
185
+   "source": [
97 186
     "ubuntu.remove()"
98 187
    ]
99 188
   },
100 189
   {
101 190
    "cell_type": "code",
102
-   "execution_count": null,
191
+   "execution_count": 12,
103 192
    "metadata": {},
104
-   "outputs": [],
193
+   "outputs": [
194
+    {
195
+     "name": "stdout",
196
+     "output_type": "stream",
197
+     "text": [
198
+      "jupyter_jupyter_1\n"
199
+     ]
200
+    }
201
+   ],
105 202
    "source": [
106 203
     "for container in client.containers.list(all=True):\n",
107 204
     "    print(container.name)"
@@ -137,6 +234,27 @@
137 234
   },
138 235
   {
139 236
    "cell_type": "code",
237
+   "execution_count": 14,
238
+   "metadata": {},
239
+   "outputs": [
240
+    {
241
+     "ename": "NameError",
242
+     "evalue": "name 'monitor' is not defined",
243
+     "output_type": "error",
244
+     "traceback": [
245
+      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
246
+      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
247
+      "\u001b[0;32m<ipython-input-14-804e8a6962d2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmonitor\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
248
+      "\u001b[0;31mNameError\u001b[0m: name 'monitor' is not defined"
249
+     ]
250
+    }
251
+   ],
252
+   "source": [
253
+    "monitor.t.stop()"
254
+   ]
255
+  },
256
+  {
257
+   "cell_type": "code",
140 258
    "execution_count": null,
141 259
    "metadata": {},
142 260
    "outputs": [],

+ 4 - 4
monitor.py

@@ -6,13 +6,13 @@ def Monitor():
6 6
     for event in client.events(decode=True):
7 7
         eventType = event["Action"]
8 8
         if eventType == 'create':
9
-            print("Monitor: A container has been created")
9
+            print("\33[32mMonitor: A container has been created")
10 10
         if eventType == 'start':
11
-            print("Monitor: A container has been started")
11
+            print("\33[92mMonitor: A container has been started")
12 12
         if eventType == 'die':
13
-            print("Monitor: A container has been stopped")
13
+            print("\33[91mMonitor: A container has been stopped")
14 14
         if eventType == 'destroy':
15
-            print("Monitor: A container has been deleted")
15
+            print("\33[31mMonitor: A container has been deleted")
16 16
         
17 17
             
18 18
 t = threading.Thread(target=Monitor)