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 |
| 8 | from drivers.common.apidriver import API |
| 9 | |
| 10 | class DockerApiDriver( API ): |
| 11 | |
| 12 | def __init__( self ): |
| 13 | """ |
| 14 | Initialize client |
| 15 | """ |
| 16 | self.name = None |
| 17 | self.home = None |
| 18 | self.handle = None |
| 19 | super( API, self ).__init__() |
| 20 | |
| 21 | def connect( self, **connectargs ): |
| 22 | """ |
| 23 | Create Client handle to connnect to Docker server |
| 24 | """ |
| 25 | try: |
| 26 | for key in connectargs: |
| 27 | vars( self )[ key ] = connectargs[ key ] |
| 28 | self.name = self.options[ 'name' ] |
| 29 | for key in self.options: |
| 30 | if key == "home": |
| 31 | self.home = self.options[ 'home' ] |
| 32 | break |
| 33 | if self.home is None or self.home == "": |
| 34 | self.home = "/var/tmp" |
| 35 | |
| 36 | self.handle = super( DockerApiDriver, self ).connect() |
| 37 | self.dockerClient = Client(base_url='unix://var/run/docker.sock') |
| 38 | return self.handle |
| 39 | except Exception as e: |
| 40 | main.log.exception( e ) |
| 41 | |
| 42 | def dockerPull( self, image="onosproject/onos", tag="latest" ): |
| 43 | """ |
| 44 | Pulls Docker image from repository |
| 45 | """ |
| 46 | try: |
| 47 | main.log.info( self.name + |
| 48 | ": Pulling Docker image " + image + ":"+tag ) |
| 49 | for line in self.dockerClient.pull( repository=image, \ |
| 50 | tag=tag, stream=True ): |
| 51 | response = json.dumps( json.loads( line ), indent=4 ) |
| 52 | if( re.search( "Downloaded", response ) ): |
| 53 | return main.TRUE |
| 54 | else: |
| 55 | main.log.error( "Failed to download image: " + image +":"+ tag ) |
| 56 | main.log.error( "Error respone: " ) |
| 57 | main.log.error( response ) |
| 58 | return main.FALSE |
| 59 | except Exception: |
| 60 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 61 | main.cleanup() |
| 62 | main.exit() |
| 63 | |
| 64 | def dockerRun( self, image="onosproject/onos", node="onos1" ): |
| 65 | """ |
| 66 | Run specified Docker container |
| 67 | """ |
| 68 | try: |
| 69 | main.log.info( self.name + |
| 70 | ": Creating Docker conatiner for node " + node ) |
| 71 | reponse = self.dockerClient.create_container( image=image, \ |
| 72 | tty=True, hostname=node, detach=True ) |
| 73 | if( reponse['Warnings'] == 'None' ): |
| 74 | main.log.info( "Created container for node: " + node ) |
| 75 | return main.TRUE |
| 76 | else: |
| 77 | main.log.info( "Noticed warnings during create" ) |
| 78 | return main.FALSE |
| 79 | except Exception: |
| 80 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 81 | main.cleanup() |
| 82 | main.exit() |
| 83 | |
| 84 | def dockerStart( self, node="onos1" ): |
| 85 | """ |
| 86 | Start Docker container |
| 87 | """ |
| 88 | try: |
| 89 | main.log.info( self.name + |
| 90 | ": Starting Docker conatiner for node " + node ) |
| 91 | reponse = self.dockerClient.start( node ) |
| 92 | if( reponse == 'None' ): |
| 93 | main.log.info( "Started container for node: " + node ) |
| 94 | return main.TRUE |
| 95 | else: |
| 96 | main.log.info( "Noticed warnings during start" ) |
| 97 | return main.FALSE |
| 98 | except Exception: |
| 99 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 100 | main.cleanup() |
| 101 | main.exit() |
| 102 | |
| 103 | def dockerStop( self, node="onos1" ): |
| 104 | """ |
| 105 | Stop docker container |
| 106 | """ |
| 107 | try: |
| 108 | main.log.info( self.name + |
| 109 | ": Stopping Docker conatiner for node " + node ) |
| 110 | reponse = self.dockerClient.stop( node ) |
| 111 | if( reponse == 'None' ): |
| 112 | main.log.info( "Stopped container for node: " + node ) |
| 113 | return main.TRUE |
| 114 | else: |
| 115 | main.log.info( "Noticed warnings during stop" ) |
| 116 | return main.FALSE |
| 117 | except Exception: |
| 118 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 119 | main.cleanup() |
| 120 | main.exit() |
| 121 | |
| 122 | def dockerRestart( self, node="onos1" ): |
| 123 | """ |
| 124 | Restart Docker container |
| 125 | """ |
| 126 | try: |
| 127 | main.log.info( self.name + |
| 128 | ": Restarting Docker conatiner for node " + node ) |
| 129 | reponse = self.dockerClient.restart( node ) |
| 130 | if( reponse == 'None' ): |
| 131 | main.log.info( "Restarted container for node: " + node ) |
| 132 | return main.TRUE |
| 133 | else: |
| 134 | main.log.info( "Noticed warnings during Restart" ) |
| 135 | return main.FALSE |
| 136 | except Exception: |
| 137 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 138 | main.cleanup() |
| 139 | main.exit() |
| 140 | |
| 141 | def dockerStatus( self, node="onos1" ): |
| 142 | """ |
| 143 | Check Docker conatiner status |
| 144 | """ |
| 145 | try: |
| 146 | main.log.info( self.name + |
| 147 | ": Checking Docker Status for node " + node ) |
| 148 | reponse = self.dockerClient.inspect_container( node ) |
| 149 | if( reponse == 'True' ): |
| 150 | main.log.info( "Container " + node + " is running" ) |
| 151 | return main.TRUE |
| 152 | else: |
| 153 | main.log.info( "Container " + node + " is not running" ) |
| 154 | return main.FALSE |
| 155 | except Exception: |
| 156 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 157 | main.cleanup() |
| 158 | main.exit() |
| 159 | |
| 160 | def dockerRemove( self, node="onos1" ): |
| 161 | """ |
| 162 | Remove Docker conatiner |
| 163 | """ |
| 164 | try: |
| 165 | main.log.info( self.name + |
| 166 | ": Removing Docker container for node " + node ) |
| 167 | reponse = self.dockerClient.remove_container( node ) |
| 168 | if( reponse == 'None' ): |
| 169 | main.log.info( "Removed container for node: " + node ) |
| 170 | return main.TRUE |
| 171 | else: |
| 172 | main.log.info( "Noticed warnings during Remove " + node ) |
| 173 | return main.FALSE |
| 174 | except Exception: |
| 175 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 176 | main.cleanup() |
| 177 | main.exit() |
| 178 | |
| 179 | def dockerImageRemove( self, image="onosproject/onos" ): |
| 180 | """ |
| 181 | Remove Docker image |
| 182 | """ |
| 183 | try: |
| 184 | main.log.info( self.name + |
| 185 | ": Removing Docker container for node " + node ) |
| 186 | reponse = self.dockerClient.remove_image( image ) |
| 187 | if( reponse == 'None' ): |
| 188 | main.log.info( "Removed Docker image: " + image ) |
| 189 | return main.TRUE |
| 190 | else: |
| 191 | main.log.info( "Noticed warnings during Remove " + image ) |
| 192 | return main.FALSE |
| 193 | except Exception: |
| 194 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 195 | main.cleanup() |
| 196 | main.exit() |
| 197 | |
| 198 | def fetchLatestClusterFile( self, branch="master" ): |
| 199 | """ |
| 200 | Fetch onos-form-cluster file from a particular branch |
| 201 | """ |
| 202 | try: |
| 203 | command = "wget -N https://raw.githubusercontent.com/opennetworkinglab/\ |
| 204 | onos/" + branch + "/tools/package/bin/onos-form-cluster" |
| 205 | subprocess.call( command ) # output checks are missing for now |
| 206 | command = "chmod u+x " + "onos-form-cluster" |
| 207 | subprocess.call( command ) |
| 208 | return main.TRUE |
| 209 | except Exception: |
| 210 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 211 | main.cleanup() |
| 212 | main.exit() |
| 213 | |
| 214 | def onosFormCluster( self, onosIPs, user="onos", passwd="rocks" ): |
| 215 | """ |
| 216 | From ONOS cluster for IP addresses in onosIPs list |
| 217 | """ |
| 218 | try: |
| 219 | onosIPs = " ".join(onosIPs) |
| 220 | command = "./onos-form-cluster -u " + user + " -p " + passwd + \ |
| 221 | " `" + onosIPs + "`" |
| 222 | subprocess.call( command ) # output checks are missing for now |
| 223 | return main.TRUE # FALSE condition will be verified from output |
| 224 | except Exception: |
| 225 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 226 | main.cleanup() |
| 227 | main.exit() |
| 228 | |
| 229 | def dockerIP( self, node='onos1' ): |
| 230 | """ |
| 231 | Fetch IP address assigned to specified node/container |
| 232 | """ |
| 233 | try: |
| 234 | output = self.dockerClient.inspect_container(node) |
| 235 | nodeIP = output['NetworkSettings']['IPAddress'] |
| 236 | #main.log.info( " Docker IP " + str(nodeIP) ) |
| 237 | return str(nodeIP) |
| 238 | |
| 239 | except Exception: |
| 240 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 241 | main.cleanup() |
| 242 | main.exit() |