blob: fc96682798749a22566182a482013095436de88d [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 ):
52 print(json.dumps(json.loads(line), indent =4))
53
54 #response = json.dumps( json.load( pullResult ), indent=4 )
55 if re.search( "for onosproject/onos:latest", line ):
56 main.log.info( "latest onos docker image pulled is: " + line )
Hari Krishna6031b7c2015-09-28 17:46:29 -070057 return main.TRUE
58 else:
suibin zhangfd266fd2015-10-27 17:06:33 -070059 main.log.error( "Failed to download image from: " + onosRepo +":"+ onosTag )
Hari Krishna6031b7c2015-09-28 17:46:29 -070060 main.log.error( "Error respone: " )
61 main.log.error( response )
62 return main.FALSE
63 except Exception:
64 main.log.exception( self.name + ": Uncaught exception!" )
65 main.cleanup()
66 main.exit()
67
suibin zhangfd266fd2015-10-27 17:06:33 -070068 def dockerCreateCT( self, onosImage="onosproject/onos:latest", onosNode="onos1" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -070069 """
suibin zhangfd266fd2015-10-27 17:06:33 -070070 Create a Docker container with a specific image
Hari Krishna6031b7c2015-09-28 17:46:29 -070071 """
72 try:
73 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -070074 ": Creating Docker container for node: " + onosNode )
75 response = self.dockerClient.create_container( image=onosImage, \
76 tty=True, name=onosNode, detach=True )
77 #print response
78 #print response.get("Id")
79 #print response.get("Warnings")
80 if( str( response.get("Warnings") ) == 'None' ):
81 main.log.info( "Created container for node: " + onosNode + "; container id is: " + response.get("Id") )
82 return ( main.TRUE, response.get("Id") )
Hari Krishna6031b7c2015-09-28 17:46:29 -070083 else:
84 main.log.info( "Noticed warnings during create" )
suibin zhangfd266fd2015-10-27 17:06:33 -070085 return ( main.FALSE, null)
Hari Krishna6031b7c2015-09-28 17:46:29 -070086 except Exception:
87 main.log.exception( self.name + ": Uncaught exception!" )
88 main.cleanup()
89 main.exit()
90
suibin zhangfd266fd2015-10-27 17:06:33 -070091 def dockerStartCT( self, ctID ):
Hari Krishna6031b7c2015-09-28 17:46:29 -070092 """
93 Start Docker container
94 """
95 try:
96 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -070097 ": Starting Docker conatiner Id " + ctID )
98 response = self.dockerClient.start( container = ctID )
99 if response is None:
100 main.log.info( "Started container for Id: " + ctID )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700101 return main.TRUE
102 else:
103 main.log.info( "Noticed warnings during start" )
104 return main.FALSE
105 except Exception:
106 main.log.exception( self.name + ": Uncaught exception!" )
107 main.cleanup()
108 main.exit()
109
suibin zhangfd266fd2015-10-27 17:06:33 -0700110 def dockerStopCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700111 """
112 Stop docker container
113 """
114 try:
115 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700116 ": Stopping Docker conatiner for node " + ctName )
117 response = self.dockerClient.stop( ctName )
118 if response is None:
119 main.log.info( "Stopped container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700120 return main.TRUE
121 else:
122 main.log.info( "Noticed warnings during stop" )
123 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700124 except errors.NotFound:
125 main.log.info( ctName + " not found! Continue on tests...")
126 return main.TRUE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700127 except Exception:
128 main.log.exception( self.name + ": Uncaught exception!" )
suibin zhangfd266fd2015-10-27 17:06:33 -0700129 #main.cleanup()
130 #main.exit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700131
suibin zhangfd266fd2015-10-27 17:06:33 -0700132 def dockerRestartCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700133 """
134 Restart Docker container
135 """
136 try:
137 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700138 ": Restarting Docker conatiner for node " + ctName )
139 response = self.dockerClient.restart( ctName )
140 if response is None:
141 main.log.info( "Restarted container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700142 return main.TRUE
143 else:
144 main.log.info( "Noticed warnings during Restart" )
145 return main.FALSE
146 except Exception:
147 main.log.exception( self.name + ": Uncaught exception!" )
148 main.cleanup()
149 main.exit()
150
suibin zhangfd266fd2015-10-27 17:06:33 -0700151 def dockerCheckCTName( self, ctName):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700152 """
153 Check Docker conatiner status
154 """
155 try:
156 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700157 ": Checkeng Docker Status for CT with 'Names' " + ctName )
158 namelist = [response["Names"] for response in self.dockerClient.containers(all=True) if not []]
159 main.log.info("Name list is: " + str(namelist) )
160 if( [ctName] in namelist):
161 main.log.info( "Container " + ctName + " exists" )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700162 return main.TRUE
163 else:
suibin zhangfd266fd2015-10-27 17:06:33 -0700164 main.log.info( "Container " + ctName + " does not exist" )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700165 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700166 except errors.NotFound:
167 main.log.warn( ctName + "not found! Continue with the tests...")
168 return main.FALSE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700169 except Exception:
suibin zhangfd266fd2015-10-27 17:06:33 -0700170 main.log.exception( self.name + ": Uncaught exception! Continue tests..." )
171 #main.cleanup()
172 #main.exit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700173
suibin zhangfd266fd2015-10-27 17:06:33 -0700174 def dockerRemoveCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700175 """
176 Remove Docker conatiner
177 """
178 try:
179 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700180 ": Removing Docker container for node " + ctName )
181 response = self.dockerClient.remove_container( ctName, force=True )
182 if response is None:
183 main.log.info( "Removed container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700184 return main.TRUE
185 else:
suibin zhangfd266fd2015-10-27 17:06:33 -0700186 main.log.info( "Noticed warnings during Remove " + ctName)
Hari Krishna6031b7c2015-09-28 17:46:29 -0700187 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700188 main.log.exception(self.name + ": not found, continuing...")
189 except errors.NotFound:
190 main.log.warn( ctName + "not found! Continue with the tests...")
191 return main.TRUE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700192 except Exception:
suibin zhangfd266fd2015-10-27 17:06:33 -0700193 main.log.exception( self.name + ": Uncaught exception! Continuing..." )
194 #main.cleanup()
195 #main.exit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700196
suibin zhangfd266fd2015-10-27 17:06:33 -0700197 def dockerRemoveImage( self, image="onosproject/onos:latest" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700198 """
199 Remove Docker image
200 """
suibin zhangfd266fd2015-10-27 17:06:33 -0700201 if self.dockerClient.images() is []:
202 main.log.info( "no image found with RepoTags: " + image )
203 return main.TRUE
204 else:
205 try:
206 main.log.info( self.name +
207 ": Removing Docker image " + image )
208 response = self.dockerClient.remove_image( image, force = True)
209 if response is None:
210 main.log.info( "Removed Docker image: " + image )
211 return main.TRUE
212 else:
213 main.log.info( "Noticed warnings during Remove " + image )
214 return main.FALSE
215 except errors.NotFound:
216 main.log.warn( image + "not found! Continue with the tests...")
Hari Krishna6031b7c2015-09-28 17:46:29 -0700217 return main.TRUE
suibin zhangfd266fd2015-10-27 17:06:33 -0700218 except Exception:
219 main.log.exception( self.name + ": Uncaught exception! Continuing..." )
220 #main.cleanup()
221 #main.exit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700222
223 def fetchLatestClusterFile( self, branch="master" ):
224 """
225 Fetch onos-form-cluster file from a particular branch
226 """
227 try:
228 command = "wget -N https://raw.githubusercontent.com/opennetworkinglab/\
229 onos/" + branch + "/tools/package/bin/onos-form-cluster"
230 subprocess.call( command ) # output checks are missing for now
231 command = "chmod u+x " + "onos-form-cluster"
232 subprocess.call( command )
233 return main.TRUE
234 except Exception:
235 main.log.exception( self.name + ": Uncaught exception!" )
236 main.cleanup()
237 main.exit()
238
suibin zhangfd266fd2015-10-27 17:06:33 -0700239 def onosFormCluster( self, onosIPs, cmdPath="~/OnosSystemTest/TestON/tests/PLATdockertest/Dependency", user="karaf", passwd="karaf" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700240 """
241 From ONOS cluster for IP addresses in onosIPs list
242 """
243 try:
244 onosIPs = " ".join(onosIPs)
suibin zhangfd266fd2015-10-27 17:06:33 -0700245 command = cmdPath + "/onos-form-cluster -u " + user + " -p " + passwd + \
246 " " + onosIPs
247 result = subprocess.call( command, shell=True )
248 if result == 0:
249 return main.TRUE
250 else:
251 main.log.info("Something is not right in forming cluster>")
252 return main.FALSE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700253 except Exception:
254 main.log.exception( self.name + ": Uncaught exception!" )
255 main.cleanup()
256 main.exit()
257
suibin zhangfd266fd2015-10-27 17:06:33 -0700258 def dockerIP( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700259 """
260 Fetch IP address assigned to specified node/container
261 """
262 try:
suibin zhangfd266fd2015-10-27 17:06:33 -0700263 output = self.dockerClient.inspect_container( ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700264 nodeIP = output['NetworkSettings']['IPAddress']
suibin zhangfd266fd2015-10-27 17:06:33 -0700265 main.log.info( " Docker IP " + str(nodeIP) )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700266 return str(nodeIP)
267
268 except Exception:
269 main.log.exception( self.name + ": Uncaught exception!" )
270 main.cleanup()
271 main.exit()
suibin zhangfd266fd2015-10-27 17:06:33 -0700272