Merge remote-tracking branch 'origin/master' into merge-branch
[netvirt.git] / resources / bin / ovsdb-build
1 #!/usr/bin/env python
2 # Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Eclipse Public License v1.0
5 # which accompanies this distribution, and is available at
6 # http://www.eclipse.org/legal/epl-v10.html
7 #
8 # Author: Dave Tucker
9
10 import argparse
11 import os
12 from subprocess import call
13 from sys import exit
14
15 CTRL_GIT = "https://git.opendaylight.org/gerrit/p/controller.git"
16 OVSDB_GIT = "https://git.opendaylight.org/gerrit/p/ovsdb.git"
17
18
19 def main():
20     parser = argparse.ArgumentParser()
21     parser.add_argument('-r', '--reclone',
22                         help='Reclone the Controller/OVSDB repos',
23                         action='store_true')
24     parser.add_argument('-b', '--rebuild',
25                         help='Build the Controller Project',
26                         action='store_true')
27     parser.add_argument('-of13', '--openflow13',
28                         help='Enable OpenFlow 1.3 Support',
29                         action='store_true')
30     parser.add_argument('-v', '--verbose',
31                         help='Verbose output',
32                         action='store_true')
33     parser.add_argument('-g', '--gerrit',
34                         metavar='CHANGE_ID',
35                         help='Gerrit Change ID',
36                         type=int)
37     parser.add_argument('-start', '--start',
38                         help='Start the controller',
39                         default=True,
40                         action='store_true')
41     parser.add_argument('-stop', '--stop',
42                         help='Stop the controller',
43                         action='store_true')
44     parser.add_argument('-d', '--directory',
45                         help='directory',
46                         type=str)
47     parser.add_argument('-t', '--tmux',
48                         help='Create tmux session',
49                         action='store_true')
50     args = parser.parse_args()
51
52     builder = OvsdbBuilder(args.verbose, args.openflow13,
53                            args.directory)
54
55     if args.stop:
56         builder.stop_controller()
57         exit(0)
58
59     if args.reclone:
60         print("---> Recloning Controller")
61         builder.reclone(builder.ctrl_root, CTRL_GIT)
62         print("---> Recloning OVSDB")
63         builder.reclone(builder.ovsdb_root, OVSDB_GIT)
64
65     if args.rebuild:
66         print("---> Building Controller")
67         builder.build(builder.ctrl_build)
68
69     if args.gerrit:
70         print("---> Checking out patch from Gerrit")
71         builder.gerrit(args.gerrit)
72
73     print("---> Building OVSDB")
74     builder.build(builder.ovsdb_root, args.verbose)
75     print("---> Copying OVSDB JARs")
76     builder.copy_jars()
77
78     if args.start:
79         print("---> Running Controller")
80         builder.run_controller()
81
82     if args.tmux:
83         builder.tmux()
84         print("To attach to the session please run: \n\n"
85               "    tmux -2 attach-session -t odl\n\n")
86
87
88 class OvsdbBuilder(object):
89
90     def __init__(self,
91                  verbose=False,
92                  of13=False,
93                  directory=os.path.expanduser('~')):
94         self.root_dir = directory
95         self.ctrl_root = self.root_dir + "/controller"
96         self.ctrl_build = (self.root_dir +
97                            "/opendaylight/distribution/opendaylight")
98         self.ctrl_exec = (self.ctrl_build +
99                           "/target/distribution.opendaylight-osgipackage" +
100                           "/opendaylight")
101         self.ctrl_plugin = self.ctrl_build + "/plugin"
102         self.ovsdb_root = self.root_dir + "/ovsdb"
103         self.verbose = verbose
104
105     def reclone(self, location, repo):
106         try:
107             os.chdir(location)
108             command = "git checkout master"
109             command = "git pull"
110             if not self.verbose:
111                 command += " > /dev/null"
112             call(command, shell=True)
113         except OSError:
114             command = "git clone %s" % repo
115             if not self.verbose:
116                 command += " > /dev/null"
117             call(command, shell=True)
118
119     def gerrit(self, patch):
120         os.chdir(self.ovsdb_root)
121         command = "git review -d %i" % patch
122         call(command, shell=True)
123
124     def build(self, location, verbose):
125         os.chdir(location)
126         command = "mvn clean install"
127         if not verbose:
128             command += " > /dev/null"
129         call(command, shell=True)
130
131     def copy_jars(self):
132         os.chdir(self.ovsdb_root)
133         command = ("find . -name ovsdb*.jar -exec cp {} %s \;"
134                    % self.ctrl_plugin)
135         call(command, shell=True)
136
137     def stop_controller(self):
138         os.chdir(self.root_dir)
139         command = "sh " + self.ctrl_exec + "/run.sh -stop"
140         call(command, shell=True)
141
142     def run_controller(self):
143         os.chdir(self.root_dir)
144         command = "sh " + self.ctrl_exec + "/run.sh -start -debug"
145         if (self.of13):
146             command += " -of13"
147         call(command, shell=True)
148
149     def tmux(self):
150         tmux = ["tmux new-session -d -s odl",
151                 "tmux new-window -t odl:1 -n 'OSGi' 'telnet 127.0.0.1 2400'",
152                 "tmux new-window -t odl:2 -n 'OVSDB Source' 'cd %s'"
153                 % self.ovsdb_root,
154                 "tmux new-window -t odl:3 -n 'Ctrl Source' 'cd %s'"
155                 % self.ctrl_root,
156                 "tmux select-window -t odl:1",
157                 ]
158         call(" ; ".join(tmux), shell=True)
159
160 if __name__ == "__main__":
161     main()
162