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 ): |
| 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 57 | return main.TRUE |
| 58 | else: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 59 | main.log.error( "Failed to download image from: " + onosRepo +":"+ onosTag ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 60 | 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 zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 68 | def dockerCreateCT( self, onosImage="onosproject/onos:latest", onosNode="onos1" ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 69 | """ |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 70 | Create a Docker container with a specific image |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 71 | """ |
| 72 | try: |
| 73 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 74 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 83 | else: |
| 84 | main.log.info( "Noticed warnings during create" ) |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 85 | return ( main.FALSE, null) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 86 | except Exception: |
| 87 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 88 | main.cleanup() |
| 89 | main.exit() |
| 90 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 91 | def dockerStartCT( self, ctID ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 92 | """ |
| 93 | Start Docker container |
| 94 | """ |
| 95 | try: |
| 96 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 97 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 101 | 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 zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 110 | def dockerStopCT( self, ctName ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 111 | """ |
| 112 | Stop docker container |
| 113 | """ |
| 114 | try: |
| 115 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 116 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 120 | return main.TRUE |
| 121 | else: |
| 122 | main.log.info( "Noticed warnings during stop" ) |
| 123 | return main.FALSE |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 124 | except errors.NotFound: |
| 125 | main.log.info( ctName + " not found! Continue on tests...") |
| 126 | return main.TRUE |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 127 | except Exception: |
| 128 | main.log.exception( self.name + ": Uncaught exception!" ) |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 129 | #main.cleanup() |
| 130 | #main.exit() |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 131 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 132 | def dockerRestartCT( self, ctName ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 133 | """ |
| 134 | Restart Docker container |
| 135 | """ |
| 136 | try: |
| 137 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 138 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 142 | 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 zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 151 | def dockerCheckCTName( self, ctName): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 152 | """ |
| 153 | Check Docker conatiner status |
| 154 | """ |
| 155 | try: |
| 156 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 157 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 162 | return main.TRUE |
| 163 | else: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 164 | main.log.info( "Container " + ctName + " does not exist" ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 165 | return main.FALSE |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 166 | except errors.NotFound: |
| 167 | main.log.warn( ctName + "not found! Continue with the tests...") |
| 168 | return main.FALSE |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 169 | except Exception: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 170 | main.log.exception( self.name + ": Uncaught exception! Continue tests..." ) |
| 171 | #main.cleanup() |
| 172 | #main.exit() |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 173 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 174 | def dockerRemoveCT( self, ctName ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 175 | """ |
| 176 | Remove Docker conatiner |
| 177 | """ |
| 178 | try: |
| 179 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 180 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 184 | return main.TRUE |
| 185 | else: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 186 | main.log.info( "Noticed warnings during Remove " + ctName) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 187 | return main.FALSE |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 188 | 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 192 | except Exception: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 193 | main.log.exception( self.name + ": Uncaught exception! Continuing..." ) |
| 194 | #main.cleanup() |
| 195 | #main.exit() |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 196 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 197 | def dockerRemoveImage( self, image="onosproject/onos:latest" ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 198 | """ |
| 199 | Remove Docker image |
| 200 | """ |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 201 | 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 217 | return main.TRUE |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 218 | except Exception: |
| 219 | main.log.exception( self.name + ": Uncaught exception! Continuing..." ) |
| 220 | #main.cleanup() |
| 221 | #main.exit() |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 222 | |
| 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 zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 239 | 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] | 240 | """ |
| 241 | From ONOS cluster for IP addresses in onosIPs list |
| 242 | """ |
| 243 | try: |
| 244 | onosIPs = " ".join(onosIPs) |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 245 | 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 253 | except Exception: |
| 254 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 255 | main.cleanup() |
| 256 | main.exit() |
| 257 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 258 | def dockerIP( self, ctName ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 259 | """ |
| 260 | Fetch IP address assigned to specified node/container |
| 261 | """ |
| 262 | try: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 263 | output = self.dockerClient.inspect_container( ctName ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 264 | nodeIP = output['NetworkSettings']['IPAddress'] |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 265 | main.log.info( " Docker IP " + str(nodeIP) ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 266 | return str(nodeIP) |
| 267 | |
| 268 | except Exception: |
| 269 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 270 | main.cleanup() |
| 271 | main.exit() |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 272 | |