Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import json |
| 4 | import os |
| 5 | import re |
| 6 | import subprocess |
| 7 | from docker import Client |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 8 | from docker import errors |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 9 | from drivers.common.apidriver import API |
| 10 | |
| 11 | class 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 zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 43 | def dockerPull( self, onosRepo ="onosproject/onos", onosTag="latest" ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 44 | """ |
| 45 | Pulls Docker image from repository |
| 46 | """ |
| 47 | try: |
| 48 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 49 | ": Pulling Docker image " + onosRepo + ":"+ onosTag ) |
| 50 | for line in self.dockerClient.pull( repository = onosRepo, \ |
| 51 | tag = onosTag, stream = True ): |
suibin zhang | 3b067ea | 2015-11-05 13:55:21 -0800 | [diff] [blame] | 52 | print "#", |
| 53 | main.log.info(json.dumps(json.loads(line), indent =4)) |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 54 | |
| 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 58 | return main.TRUE |
| 59 | else: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 60 | main.log.error( "Failed to download image from: " + onosRepo +":"+ onosTag ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 61 | main.log.error( "Error respone: " ) |
suibin zhang | 3b067ea | 2015-11-05 13:55:21 -0800 | [diff] [blame] | 62 | main.log.error( line ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 63 | return main.FALSE |
| 64 | except Exception: |
| 65 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 66 | main.cleanup() |
| 67 | main.exit() |
| 68 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 69 | def dockerCreateCT( self, onosImage="onosproject/onos:latest", onosNode="onos1" ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 70 | """ |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 71 | Create a Docker container with a specific image |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 72 | """ |
| 73 | try: |
| 74 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 75 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 84 | else: |
| 85 | main.log.info( "Noticed warnings during create" ) |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 86 | return ( main.FALSE, null) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 87 | except Exception: |
| 88 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 89 | main.cleanup() |
| 90 | main.exit() |
| 91 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 92 | def dockerStartCT( self, ctID ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 93 | """ |
| 94 | Start Docker container |
| 95 | """ |
| 96 | try: |
| 97 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 98 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 102 | 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 zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 111 | def dockerStopCT( self, ctName ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 112 | """ |
| 113 | Stop docker container |
| 114 | """ |
| 115 | try: |
| 116 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 117 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 121 | return main.TRUE |
| 122 | else: |
| 123 | main.log.info( "Noticed warnings during stop" ) |
| 124 | return main.FALSE |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 125 | except errors.NotFound: |
| 126 | main.log.info( ctName + " not found! Continue on tests...") |
| 127 | return main.TRUE |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 128 | except Exception: |
| 129 | main.log.exception( self.name + ": Uncaught exception!" ) |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 130 | #main.cleanup() |
| 131 | #main.exit() |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 132 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 133 | def dockerRestartCT( self, ctName ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 134 | """ |
| 135 | Restart Docker container |
| 136 | """ |
| 137 | try: |
| 138 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 139 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 143 | 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 zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 152 | def dockerCheckCTName( self, ctName): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 153 | """ |
| 154 | Check Docker conatiner status |
| 155 | """ |
| 156 | try: |
| 157 | main.log.info( self.name + |
Jon Hall | 3dbc7dc | 2015-12-01 12:11:42 -0800 | [diff] [blame] | 158 | ": Checking Docker Status for CT with 'Names' " + ctName ) |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 163 | return main.TRUE |
| 164 | else: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 165 | main.log.info( "Container " + ctName + " does not exist" ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 166 | return main.FALSE |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 167 | except errors.NotFound: |
| 168 | main.log.warn( ctName + "not found! Continue with the tests...") |
| 169 | return main.FALSE |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 170 | except Exception: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 171 | main.log.exception( self.name + ": Uncaught exception! Continue tests..." ) |
| 172 | #main.cleanup() |
| 173 | #main.exit() |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 174 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 175 | def dockerRemoveCT( self, ctName ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 176 | """ |
| 177 | Remove Docker conatiner |
| 178 | """ |
| 179 | try: |
| 180 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 181 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 185 | return main.TRUE |
| 186 | else: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 187 | main.log.info( "Noticed warnings during Remove " + ctName) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 188 | return main.FALSE |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 189 | 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 193 | except Exception: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 194 | main.log.exception( self.name + ": Uncaught exception! Continuing..." ) |
| 195 | #main.cleanup() |
| 196 | #main.exit() |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 197 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 198 | def dockerRemoveImage( self, image="onosproject/onos:latest" ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 199 | """ |
| 200 | Remove Docker image |
| 201 | """ |
suibin zhang | 3b067ea | 2015-11-05 13:55:21 -0800 | [diff] [blame] | 202 | rmResult = main.TRUE |
| 203 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 204 | if self.dockerClient.images() is []: |
suibin zhang | 5ae2533 | 2015-11-04 13:56:28 -0800 | [diff] [blame] | 205 | main.log.info( "No docker image found with RepoTags: " + image ) |
suibin zhang | 3b067ea | 2015-11-05 13:55:21 -0800 | [diff] [blame] | 206 | return rmResult |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 207 | else: |
suibin zhang | 3b067ea | 2015-11-05 13:55:21 -0800 | [diff] [blame] | 208 | 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 228 | |
| 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 zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 245 | def onosFormCluster( self, onosIPs, cmdPath="~/OnosSystemTest/TestON/tests/PLATdockertest/Dependency", user="karaf", passwd="karaf" ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 246 | """ |
| 247 | From ONOS cluster for IP addresses in onosIPs list |
| 248 | """ |
| 249 | try: |
| 250 | onosIPs = " ".join(onosIPs) |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 251 | 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 259 | except Exception: |
| 260 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 261 | main.cleanup() |
| 262 | main.exit() |
| 263 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 264 | def dockerIP( self, ctName ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 265 | """ |
| 266 | Fetch IP address assigned to specified node/container |
| 267 | """ |
| 268 | try: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 269 | output = self.dockerClient.inspect_container( ctName ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 270 | nodeIP = output['NetworkSettings']['IPAddress'] |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 271 | main.log.info( " Docker IP " + str(nodeIP) ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 272 | return str(nodeIP) |
| 273 | |
| 274 | except Exception: |
| 275 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 276 | main.cleanup() |
| 277 | main.exit() |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 278 | |