blob: 99cfaf4c071f6c413c96e5b14be34249d34ecabb [file] [log] [blame]
Dave Pigott281203e2014-09-17 23:45:02 +01001#! /usr/bin/python
2
Steve McIntyre719e3772018-01-31 17:16:53 +00003# Copyright 2014-2018 Linaro Limited
Steve McIntyre94ef65e2015-09-25 01:08:14 +01004# Authors: Dave Pigott <dave.pigot@linaro.org>,
5# Steve McIntyre <steve.mcintyre@linaro.org>
Dave Pigott281203e2014-09-17 23:45:02 +01006#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20# MA 02110-1301, USA.
21
22# First of all, create the vland user
23# Next - create the vland database
24
Steve McIntyre719e3772018-01-31 17:16:53 +000025# Create the switch, port, vlan, trunk and state tables
Dave Pigott281203e2014-09-17 23:45:02 +010026
Steve McIntyre719e3772018-01-31 17:16:53 +000027import datetime
Dave Pigott281203e2014-09-17 23:45:02 +010028from psycopg2 import connect
29
Steve McIntyred7c87682018-01-31 17:28:55 +000030DATABASE_SCHEMA_VERSION = 1
31
Dave Pigott281203e2014-09-17 23:45:02 +010032conn = connect(database="postgres", user="postgres", password="postgres")
33
34cur = conn.cursor()
35cur.execute("CREATE USER vland WITH SUPERUSER")
36cur.execute("CREATE DATABASE vland WITH OWNER = vland PASSWORD 'vland'")
37conn.close()
38
39conn = connect(database="vland", user="vland", password="vland")
40cur = conn.cursor()
41
Steve McIntyre87e1adb2014-11-27 16:04:45 +000042cur.execute("CREATE TABLE switch (switch_id SERIAL, name VARCHAR(64))")
Steve McIntyreea753972015-08-05 13:52:48 +010043cur.execute("CREATE TABLE port (port_id SERIAL, name VARCHAR(64),"
Steve McIntyre87e1adb2014-11-27 16:04:45 +000044 "switch_id INTEGER, is_locked BOOLEAN,"
45 "is_trunk BOOLEAN, base_vlan_id INTEGER,"
Steve McIntyrec4890132015-08-07 15:19:11 +010046 "current_vlan_id INTEGER, number INTEGER, trunk_id INTEGER)")
Steve McIntyre87e1adb2014-11-27 16:04:45 +000047cur.execute("CREATE TABLE vlan (vlan_id SERIAL, name VARCHAR(32),"
Steve McIntyre36e8e802014-12-16 18:18:50 +000048 "tag INTEGER, is_base_vlan BOOLEAN, creation_time TIMESTAMP)")
Steve McIntyrec4890132015-08-07 15:19:11 +010049cur.execute("CREATE TABLE trunk (trunk_id SERIAL,"
50 "creation_time TIMESTAMP)")
Steve McIntyre719e3772018-01-31 17:16:53 +000051cur.execute("CREATE TABLE state (last_modified TIMESTAMP, schema_version INTEGER)")
Steve McIntyred7c87682018-01-31 17:28:55 +000052cur.execute("INSERT INTO state (last_modified, schema_version) VALUES (%s, %s)" % (datetime.datetime.now(), DATABASE_SCHEMA_VERSION))
Steve McIntyre87e1adb2014-11-27 16:04:45 +000053cur.execute("COMMIT;")
54
Steve McIntyrec9a20462018-01-31 17:35:26 +000055# Do not make any more changes here - the database code will cope with upgrades
56# from this V1 database as they're needed.