blob: 04446655e849dc5360678baf7e8df0a71e2cdedb [file] [log] [blame]
Hari Krishna6031b7c2015-09-28 17:46:29 -07001#!/usr/bin/env python
2
3import json
4import os
5import re
6import subprocess
7from docker import Client
suibin zhangfd266fd2015-10-27 17:06:33 -07008from docker import errors
Hari Krishna6031b7c2015-09-28 17:46:29 -07009from drivers.common.apidriver import API
10
11class DockerApiDriver( API ):
12
13 def __init__( self ):
14 """
15 Initialize client
16 """
17 self.name = None
18 self.home = None
19 self.handle = None
20 super( API, self ).__init__()
21
22 def connect( self, **connectargs ):
23 """
24 Create Client handle to connnect to Docker server
25 """
26 try:
27 for key in connectargs:
28 vars( self )[ key ] = connectargs[ key ]
29 self.name = self.options[ 'name' ]
30 for key in self.options:
31 if key == "home":
32 self.home = self.options[ 'home' ]
33 break
34 if self.home is None or self.home == "":
35 self.home = "/var/tmp"
36
37 self.handle = super( DockerApiDriver, self ).connect()
38 self.dockerClient = Client(base_url='unix://var/run/docker.sock')
39 return self.handle
40 except Exception as e:
41 main.log.exception( e )
42
suibin zhangfd266fd2015-10-27 17:06:33 -070043 def dockerPull( self, onosRepo ="onosproject/onos", onosTag="latest" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -070044 """
45 Pulls Docker image from repository
46 """
47 try:
48 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -070049 ": Pulling Docker image " + onosRepo + ":"+ onosTag )
50 for line in self.dockerClient.pull( repository = onosRepo, \
51 tag = onosTag, stream = True ):
suibin zhang3b067ea2015-11-05 13:55:21 -080052 print "#",
53 main.log.info(json.dumps(json.loads(line), indent =4))
suibin zhangfd266fd2015-10-27 17:06:33 -070054
55 #response = json.dumps( json.load( pullResult ), indent=4 )
56 if re.search( "for onosproject/onos:latest", line ):
57 main.log.info( "latest onos docker image pulled is: " + line )
Hari Krishna6031b7c2015-09-28 17:46:29 -070058 return main.TRUE
59 else:
suibin zhangfd266fd2015-10-27 17:06:33 -070060 main.log.error( "Failed to download image from: " + onosRepo +":"+ onosTag )
Hari Krishna6031b7c2015-09-28 17:46:29 -070061 main.log.error( "Error respone: " )
suibin zhang3b067ea2015-11-05 13:55:21 -080062 main.log.error( line )
Hari Krishna6031b7c2015-09-28 17:46:29 -070063 return main.FALSE
64 except Exception:
65 main.log.exception( self.name + ": Uncaught exception!" )
66 main.cleanup()
67 main.exit()
68
suibin zhangfd266fd2015-10-27 17:06:33 -070069 def dockerCreateCT( self, onosImage="onosproject/onos:latest", onosNode="onos1" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -070070 """
suibin zhangfd266fd2015-10-27 17:06:33 -070071 Create a Docker container with a specific image
Hari Krishna6031b7c2015-09-28 17:46:29 -070072 """
73 try:
74 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -070075 ": Creating Docker container for node: " + onosNode )
76 response = self.dockerClient.create_container( image=onosImage, \
77 tty=True, name=onosNode, detach=True )
78 #print response
79 #print response.get("Id")
80 #print response.get("Warnings")
81 if( str( response.get("Warnings") ) == 'None' ):
82 main.log.info( "Created container for node: " + onosNode + "; container id is: " + response.get("Id") )
83 return ( main.TRUE, response.get("Id") )
Hari Krishna6031b7c2015-09-28 17:46:29 -070084 else:
85 main.log.info( "Noticed warnings during create" )
suibin zhangfd266fd2015-10-27 17:06:33 -070086 return ( main.FALSE, null)
Hari Krishna6031b7c2015-09-28 17:46:29 -070087 except Exception:
88 main.log.exception( self.name + ": Uncaught exception!" )
89 main.cleanup()
90 main.exit()
91
suibin zhangfd266fd2015-10-27 17:06:33 -070092 def dockerStartCT( self, ctID ):
Hari Krishna6031b7c2015-09-28 17:46:29 -070093 """
94 Start Docker container
95 """
96 try:
97 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -070098 ": Starting Docker conatiner Id " + ctID )
99 response = self.dockerClient.start( container = ctID )
100 if response is None:
101 main.log.info( "Started container for Id: " + ctID )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700102 return main.TRUE
103 else:
104 main.log.info( "Noticed warnings during start" )
105 return main.FALSE
106 except Exception:
107 main.log.exception( self.name + ": Uncaught exception!" )
108 main.cleanup()
109 main.exit()
110
suibin zhangfd266fd2015-10-27 17:06:33 -0700111 def dockerStopCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700112 """
113 Stop docker container
114 """
115 try:
116 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700117 ": Stopping Docker conatiner for node " + ctName )
118 response = self.dockerClient.stop( ctName )
119 if response is None:
120 main.log.info( "Stopped container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700121 return main.TRUE
122 else:
123 main.log.info( "Noticed warnings during stop" )
124 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700125 except errors.NotFound:
126 main.log.info( ctName + " not found! Continue on tests...")
127 return main.TRUE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700128 except Exception:
129 main.log.exception( self.name + ": Uncaught exception!" )
suibin zhangfd266fd2015-10-27 17:06:33 -0700130 #main.cleanup()
131 #main.exit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700132
suibin zhangfd266fd2015-10-27 17:06:33 -0700133 def dockerRestartCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700134 """
135 Restart Docker container
136 """
137 try:
138 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700139 ": Restarting Docker conatiner for node " + ctName )
140 response = self.dockerClient.restart( ctName )
141 if response is None:
142 main.log.info( "Restarted container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700143 return main.TRUE
144 else:
145 main.log.info( "Noticed warnings during Restart" )
146 return main.FALSE
147 except Exception:
148 main.log.exception( self.name + ": Uncaught exception!" )
149 main.cleanup()
150 main.exit()
151
suibin zhangfd266fd2015-10-27 17:06:33 -0700152 def dockerCheckCTName( self, ctName):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700153 """
154 Check Docker conatiner status
155 """
156 try:
157 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700158 ": Checkeng Docker Status for CT with 'Names' " + ctName )
159 namelist = [response["Names"] for response in self.dockerClient.containers(all=True) if not []]
160 main.log.info("Name list is: " + str(namelist) )
161 if( [ctName] in namelist):
162 main.log.info( "Container " + ctName + " exists" )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700163 return main.TRUE
164 else:
suibin zhangfd266fd2015-10-27 17:06:33 -0700165 main.log.info( "Container " + ctName + " does not exist" )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700166 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700167 except errors.NotFound:
168 main.log.warn( ctName + "not found! Continue with the tests...")
169 return main.FALSE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700170 except Exception:
suibin zhangfd266fd2015-10-27 17:06:33 -0700171 main.log.exception( self.name + ": Uncaught exception! Continue tests..." )
172 #main.cleanup()
173 #main.exit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700174
suibin zhangfd266fd2015-10-27 17:06:33 -0700175 def dockerRemoveCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700176 """
177 Remove Docker conatiner
178 """
179 try:
180 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700181 ": Removing Docker container for node " + ctName )
182 response = self.dockerClient.remove_container( ctName, force=True )
183 if response is None:
184 main.log.info( "Removed container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700185 return main.TRUE
186 else:
suibin zhangfd266fd2015-10-27 17:06:33 -0700187 main.log.info( "Noticed warnings during Remove " + ctName)
Hari Krishna6031b7c2015-09-28 17:46:29 -0700188 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700189 main.log.exception(self.name + ": not found, continuing...")
190 except errors.NotFound:
191 main.log.warn( ctName + "not found! Continue with the tests...")
192 return main.TRUE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700193 except Exception:
suibin zhangfd266fd2015-10-27 17:06:33 -0700194 main.log.exception( self.name + ": Uncaught exception! Continuing..." )
195 #main.cleanup()
196 #main.exit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700197
suibin zhangfd266fd2015-10-27 17:06:33 -0700198 def dockerRemoveImage( self, image="onosproject/onos:latest" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700199 """
200 Remove Docker image
201 """
suibin zhang3b067ea2015-11-05 13:55:21 -0800202 rmResult = main.TRUE
203
suibin zhangfd266fd2015-10-27 17:06:33 -0700204 if self.dockerClient.images() is []:
suibin zhang5ae25332015-11-04 13:56:28 -0800205 main.log.info( "No docker image found with RepoTags: " + image )
suibin zhang3b067ea2015-11-05 13:55:21 -0800206 return rmResult
suibin zhangfd266fd2015-10-27 17:06:33 -0700207 else:
suibin zhang3b067ea2015-11-05 13:55:21 -0800208 list = [ image["Id"] for image in self.dockerClient.images() if image["RepoTags"][0] == '<none>:<none>' ]
209 for id in list:
210 try:
211 main.log.info( self.name + ": Removing Docker image " + id )
212 response = self.dockerClient.remove_image(id, force = True)
213 if response is None:
214 main.log.info( "Removed Docker image: " + id )
215 rmResult = rmResult and main.TRUE
216 else:
217 main.log.info( "Noticed warnings during Remove " + id )
218 rmResult = rmResult and main.FALSE
219 except errors.NotFound:
220 main.log.warn( image + "not found! Continue with the tests...")
221 rmResult = rmResult and main.TRUE
222 except Exception:
223 main.log.exception( self.name + ": Uncaught exception! Continuing..." )
224 rmResult = rmResult and main.FALSE
225 #main.cleanup()
226 #main.exit()
227 return rmResult
Hari Krishna6031b7c2015-09-28 17:46:29 -0700228
229 def fetchLatestClusterFile( self, branch="master" ):
230 """
231 Fetch onos-form-cluster file from a particular branch
232 """
233 try:
234 command = "wget -N https://raw.githubusercontent.com/opennetworkinglab/\
235 onos/" + branch + "/tools/package/bin/onos-form-cluster"
236 subprocess.call( command ) # output checks are missing for now
237 command = "chmod u+x " + "onos-form-cluster"
238 subprocess.call( command )
239 return main.TRUE
240 except Exception:
241 main.log.exception( self.name + ": Uncaught exception!" )
242 main.cleanup()
243 main.exit()
244
suibin zhangfd266fd2015-10-27 17:06:33 -0700245 def onosFormCluster( self, onosIPs, cmdPath="~/OnosSystemTest/TestON/tests/PLATdockertest/Dependency", user="karaf", passwd="karaf" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700246 """
247 From ONOS cluster for IP addresses in onosIPs list
248 """
249 try:
250 onosIPs = " ".join(onosIPs)
suibin zhangfd266fd2015-10-27 17:06:33 -0700251 command = cmdPath + "/onos-form-cluster -u " + user + " -p " + passwd + \
252 " " + onosIPs
253 result = subprocess.call( command, shell=True )
254 if result == 0:
255 return main.TRUE
256 else:
257 main.log.info("Something is not right in forming cluster>")
258 return main.FALSE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700259 except Exception:
260 main.log.exception( self.name + ": Uncaught exception!" )
261 main.cleanup()
262 main.exit()
263
suibin zhangfd266fd2015-10-27 17:06:33 -0700264 def dockerIP( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700265 """
266 Fetch IP address assigned to specified node/container
267 """
268 try:
suibin zhangfd266fd2015-10-27 17:06:33 -0700269 output = self.dockerClient.inspect_container( ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700270 nodeIP = output['NetworkSettings']['IPAddress']
suibin zhangfd266fd2015-10-27 17:06:33 -0700271 main.log.info( " Docker IP " + str(nodeIP) )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700272 return str(nodeIP)
273
274 except Exception:
275 main.log.exception( self.name + ": Uncaught exception!" )
276 main.cleanup()
277 main.exit()
suibin zhangfd266fd2015-10-27 17:06:33 -0700278