From 09bb787c28a48871ebd8b3465fa84d111443db9a Mon Sep 17 00:00:00 2001
From: shishirgoenka <shishir.goenka@aricent.com>
Date: Thu, 30 Aug 2018 14:02:07 -0600
Subject: [PATCH 1/7] Single nic support

---
 network_config.py                             | 45 ++++++++++++++++++
 snaps_openstack/utilities/network_utils.py    | 47 +++++++++++++++++++
 .../playbooks/viface_clean_play.yaml          | 19 ++++++++
 .../utilities/playbooks/viface_post_play.yaml | 32 +++++++++++++
 .../utilities/playbooks/viface_pre_play.yaml  | 21 +++++++++
 snaps_openstack/utilities/var.yaml            | 13 +++++
 6 files changed, 177 insertions(+)
 create mode 100755 snaps_openstack/utilities/playbooks/viface_clean_play.yaml
 create mode 100755 snaps_openstack/utilities/playbooks/viface_post_play.yaml
 create mode 100755 snaps_openstack/utilities/playbooks/viface_pre_play.yaml

diff --git a/network_config.py b/network_config.py
index 0cd1ea9..b4d9638 100644
--- a/network_config.py
+++ b/network_config.py
@@ -83,6 +83,42 @@ def __main(arguments):
                         logger.info('Completed operation successfully')
                     else:
                         logger.info('Error while performing operation')
+        if arguments.preNic is not ARG_NOT_SET:
+            for task in config.get("TASKS"):
+                if task.get("name") == "pre-post":
+                    logger.info("Performing Task " + task.get(
+                        'name') + " with arguments")
+                    logger.info("--pre-post")
+                    network_utils.add_ansible_hosts(task.get("HOSTS"))
+                    ret = network_utils.preNic(task)
+                    if ret == 0:
+                        logger.info('Completed operation successfully')
+                    else:
+                        logger.info('Error while performing operation')
+        if arguments.postNic is not ARG_NOT_SET:
+            for task in config.get("TASKS"):
+                if task.get("name") == "pre-post":
+                    logger.info("Performing Task " + task.get(
+                        'name') + " with arguments")
+                    logger.info("--pre-post")
+                    network_utils.add_ansible_hosts(task.get("HOSTS"))
+                    ret = network_utils.postNic(task)
+                    if ret == 0:
+                        logger.info('Completed operation successfully')
+                    else:
+                        logger.info('Error while performing operation')
+        if arguments.cleanNic is not ARG_NOT_SET:
+            for task in config.get("TASKS"):
+                if task.get("name") == "pre-post":
+                    logger.info("Performing Task " + task.get(
+                        'name') + " with arguments")
+                    logger.info("--pre-post")
+                    network_utils.add_ansible_hosts(task.get("HOSTS"))
+                    ret = network_utils.cleanNic(task)
+                    if ret == 0:
+                        logger.info('Completed operation successfully')
+                    else:
+                        logger.info('Error while performing operation')
 
     except Exception as e:
         logger.error(
@@ -117,6 +153,15 @@ def __main(arguments):
     parser.add_argument(
         '-mtu', '--mtu', dest='mtu', nargs='?', default=ARG_NOT_SET,
         help='When used, sets the mtu size on nic')
+    parser.add_argument(
+        '-preNic', '--preNic', dest='preNic', nargs='?', default=ARG_NOT_SET,
+        help='When used, sets the pre play for single nic support')
+    parser.add_argument(
+        '-postNic', '--postNic', dest='postNic', nargs='?', default=ARG_NOT_SET,
+        help='When used, sets the post play for single nic support')
+    parser.add_argument(
+        '-cleanNic', '--cleanNic', dest='cleanNic', nargs='?', default=ARG_NOT_SET,
+        help='When used, cleanup of single nic support')
     args = parser.parse_args()
 
     if (args.tenant_vlan is ARG_NOT_SET
diff --git a/snaps_openstack/utilities/network_utils.py b/snaps_openstack/utilities/network_utils.py
index dd2a6c9..c7bd27e 100755
--- a/snaps_openstack/utilities/network_utils.py
+++ b/snaps_openstack/utilities/network_utils.py
@@ -177,3 +177,50 @@ def mtu(task):
         ret = os.system(ansible_command_restart)
 
     return ret
+def preNic(task):
+    ret = None
+    for host in task.get("HOSTS"):
+        ip = host.get("ip")
+        primary_interface = host.get("primary_interface")
+        target_interface = host.get("target_interface")
+        pre_pb = pkg_resources.resource_filename(
+                'snaps_openstack.utilities.playbooks',
+                'viface_pre_play.yaml')
+        ansible_command = "ansible-playbook " + pre_pb \
+                              + " --extra-vars=\'{\"target\": \"" \
+                              + ip + "\",\"target_interface\": \"" + target_interface + "\"}\'"
+        logger.info(ansible_command)
+        ret = os.system(ansible_command)
+    return ret
+def postNic(task):
+    ret = None
+    for host in task.get("HOSTS"):
+        ip = host.get("ip")
+        primary_interface = host.get("primary_interface")
+        target_interface = host.get("target_interface")
+        node_type= host.get("node_type")
+        post_pb = pkg_resources.resource_filename(
+                'snaps_openstack.utilities.playbooks',
+                'viface_post_play.yaml')
+        ansible_command = "ansible-playbook " + post_pb \
+                              + " --extra-vars=\'{\"target\": \"" \
+                              + ip + "\",\"node_type\":\"" +node_type+"\",\"interface\": \"" + primary_interface + "\"}\'"
+        logger.info(ansible_command)
+        ret = os.system(ansible_command)
+    return ret
+def cleanNic(task):
+    ret = None
+    for host in task.get("HOSTS"):
+        ip = host.get("ip")
+        primary_interface = host.get("primary_interface")
+        target_interface = host.get("target_interface")
+        node_type=host.get("node_type")
+        clean_pb = pkg_resources.resource_filename(
+                'snaps_openstack.utilities.playbooks',
+                'viface_clean_play.yaml')
+        ansible_command = "ansible-playbook " + clean_pb \
+                              + " --extra-vars=\'{\"target\": \"" \
+                              + ip +"\",\"node_type\":\"" +node_type+"\",\"interface\": \"" + primary_interface + "\"}\'"
+        logger.info(ansible_command)
+        ret = os.system(ansible_command)
+    return ret
diff --git a/snaps_openstack/utilities/playbooks/viface_clean_play.yaml b/snaps_openstack/utilities/playbooks/viface_clean_play.yaml
new file mode 100755
index 0000000..4ea9b2f
--- /dev/null
+++ b/snaps_openstack/utilities/playbooks/viface_clean_play.yaml
@@ -0,0 +1,19 @@
+---
+- hosts: "{{ target }}"
+  tasks:
+   - name: Remove Script file
+     shell: rm /etc/network/veth.sh
+     ignore_errors: True
+   - name: Edit interface file
+     lineinfile:
+       dest: /etc/network/interfaces
+       state: present
+       regexp: '^post-up'
+       line: ''      
+   - name: Removing port from linux bridge
+     shell: brctl delif br-ex '{{ interface }}';systemctl restart networking
+     when: node_type == "controller"
+   - name: Removing port from OVS bridge
+     shell: docker exec  $(docker ps -a| grep switchd| awk {'print $1'}) ovs-vsctl del-port br-ex  '{{ interface }}';ifconfig br-ex 0.0.0.0; systemctl restart networking
+     when: node_type == "compute"
+
diff --git a/snaps_openstack/utilities/playbooks/viface_post_play.yaml b/snaps_openstack/utilities/playbooks/viface_post_play.yaml
new file mode 100755
index 0000000..fce5c27
--- /dev/null
+++ b/snaps_openstack/utilities/playbooks/viface_post_play.yaml
@@ -0,0 +1,32 @@
+---
+- hosts: '{{ target }}'
+  become: yes
+  become_user: root
+# vars_files:
+#  vars:
+  tasks:
+   - name: get the default gateway of machine
+     shell: ip route show | grep default|awk '{print $3}'
+     register: gateway
+   - debug:
+       var: gateway.stdout_lines[0]
+   - name: Editing script file for controller
+     lineinfile:
+        dest: /etc/network/veth.sh
+        line: 'brctl addif uplinkbridge "{{ interface }}";ip addr add "{{ target }}"/24 dev uplinkbridge;ip addr del "{{ target }}"/24 dev "{{ interface }}";route add default gw "{{ gateway.stdout_lines[0] }}" uplinkbridge;ifconfig "{{ interface }}" "{{ target }}"/24;'
+     when:  node_type  == "controller"
+   - name: Editing script file for compute
+     lineinfile:
+        dest: /etc/network/veth.sh
+        line: "(sleep 90;echo 'starting ovs config' ip a a '{{ target }}'/24 dev br-ex;docker exec -i $(docker ps -a| grep switchd| awk {'print $1'}) ovs-vsctl add-port br-ex '{{ interface }}';ifconfig br-ex  up;route add default gw '{{ gateway.stdout_lines[0] }}' br-ex;ifconfig '{{ interface }}' 0.0.0.0;echo 'ending ovs config')& "
+     when:  node_type == "compute"
+   
+   - name: "Reboot if required"
+     shell: sleep 1 && shutdown -r now 'Reboot required'
+     become: true
+     async: 1
+     poll: 0
+     ignore_errors: true
+   - name: waiting for server to come back
+     local_action: wait_for host={{ target }} port=22 state=started delay=10 
+
diff --git a/snaps_openstack/utilities/playbooks/viface_pre_play.yaml b/snaps_openstack/utilities/playbooks/viface_pre_play.yaml
new file mode 100755
index 0000000..191c08a
--- /dev/null
+++ b/snaps_openstack/utilities/playbooks/viface_pre_play.yaml
@@ -0,0 +1,21 @@
+---
+- hosts: "{{ target }}"
+  tasks:
+   - name: install bridge-utils
+     shell: sudo apt-get install bridge-utils;
+     ignore_errors: True
+   - name: creating veth script file
+     file:
+       path: /etc/network/veth.sh
+       state: touch
+       mode: 0777
+   - lineinfile:
+        dest: /etc/network/veth.sh
+       # line: 'ip link add type "{{ veth_name }}";ip link set dev "{{ veth_name }}"0 up;ip link set dev "{{ veth_name }}"1 up;brctl addbr uplinkbridge;brctl addif uplinkbridge "{{ veth_name }}"0;ip link set dev uplinkbridge up;'
+        line: 'ip link add veth0 type veth peer name "{{ target_interface }}";ip link set dev veth0 up;ip link set dev "{{ target_interface }}" up;brctl addbr uplinkbridge;brctl addif uplinkbridge veth0;ip link set dev uplinkbridge up;'
+   - name: Editing interface file
+     lineinfile:
+        dest: /etc/network/interfaces
+        line: 'post-up /etc/network/veth.sh'
+   - name: restarting networks
+     command: systemctl restart networking
diff --git a/snaps_openstack/utilities/var.yaml b/snaps_openstack/utilities/var.yaml
index f9b4403..0974b82 100755
--- a/snaps_openstack/utilities/var.yaml
+++ b/snaps_openstack/utilities/var.yaml
@@ -60,3 +60,16 @@ TASKS:
           -
             port_name: 
             size: ''
+  -
+   name: pre-post
+   HOSTS:
+     - 
+       ip: ""
+       node_type: "controller"
+       primary_interface: ""
+       target_interface: "veth1"
+     - 
+       ip: ""
+       primary_interface: ""
+       target_interface: "veth1"
+       node_type: "compute"

From bc24d236bfba0348fa843acbe1c39f6ef336a43b Mon Sep 17 00:00:00 2001
From: shishirgoenka <36440470+shishirgoenka@users.noreply.github.com>
Date: Fri, 31 Aug 2018 12:25:56 +0530
Subject: [PATCH 2/7] Update install.md

---
 doc/source/install/install.md | 88 +++++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)

diff --git a/doc/source/install/install.md b/doc/source/install/install.md
index d584aba..832cf6a 100644
--- a/doc/source/install/install.md
+++ b/doc/source/install/install.md
@@ -547,6 +547,57 @@ below.
   </tr>
 </table>
 
+### 3.4 var.yaml (Single NIC settings)
+
+Configuration file used by SNAPS-OpenStack for single NIC deployemnt.
+
+#### TASKS
+
+Parameters defined in this section allows user to specify pre and post deployment tasks
+(Single NIC). Configuration parameters defined in this section are explained
+below.
+
+<table>
+  <tr>
+    <th colspan="3">Parameter</th>
+    <th>Optionality</th>
+    <th>Description</th>
+  </tr>
+  <tr>
+    <td colspan="3">name</td>
+    <td>N</td>
+    <td>Should be <code>pre-post</code>.</td>
+  </tr>
+  <tr>
+    <td colspan="4">host</td>
+    <td>Define this set of parameters for each host machine (A separate host section should be defined for each host machine).</td>
+  </tr>
+  <tr>
+    <td/>
+    <td colspan="2">ip</td>
+    <td>Y</td>
+    <td>IP of Management network.</td>
+  </tr>
+  <tr>
+    <td/>
+    <td colspan="2">node_type</td>
+    <td>N</td>
+    <td>User can choose node type controller/compute.</td>
+  </tr>
+  <tr>
+    <td/>
+    <td colspan="2">primary_interface</td>
+    <td>N</td>
+    <td>Name of primary interfaces.</td>
+  </tr>
+  <tr>
+    <td/>
+    <td colspan="2">target_interface</td>
+    <td>N</td>
+    <td>Name of the veth pairs to be created.</td>
+  </tr>
+</table>
+
 ## 4 Installation Steps
 
 ### 4.1 Fresh OpenStack Installation
@@ -612,6 +663,34 @@ Run `network_config.py` as shown below:
 sudo python <repo_dir>/network_config.py -f <repo_dir>/snaps_openstack/utilities/var.yaml -mtu
 ```
 
+### 4.4 Settings for Single NIC deployment
+
+#### Step 1
+
+Go to `<repo_dir>/snaps_openstack/utilities/` directory.
+
+Define Single NIC configurations for all hosts in `var.yaml` file under pre-post task.
+
+#### Step 2
+
+Run `network_config.py` as shown below:
+
+```
+sudo python <repo_dir>/network_config.py -f <repo_dir>/snaps_openstack/utilities/var.yaml -preNic
+```
+
+#### Step 3
+
+Install fresh openstack using physical interface and veth0 as management and data interfaces respectively. (Please Refer 4.1)
+
+#### Step 2
+
+Run `network_config.py` as shown below:
+
+```
+sudo python <repo_dir>/network_config.py -f <repo_dir>/snaps_openstack/utilities/var.yaml -postNic
+```
+
 ## 5 Cleanup and Troubleshooting
 
 ### 5.1 Fresh Deployment with Existing Docker Repository
@@ -716,3 +795,12 @@ Exit from mariadb container interactive mode:
 ```
 exit
 ```
+#### 5.3.2 Single NIC Cleanup
+
+Before cleaning the opesntack setup user needs to clean the single NIC configurations.
+
+Run `network_config.py` as shown below:
+
+```
+sudo python <repo_dir>/network_config.py -f <repo_dir>/snaps_openstack/utilities/var.yaml -cleanNic
+```

From 641ba295c4a47ebdce5b8fbcb0c968d84db693f9 Mon Sep 17 00:00:00 2001
From: shishirgoenka <36440470+shishirgoenka@users.noreply.github.com>
Date: Fri, 31 Aug 2018 12:35:50 +0530
Subject: [PATCH 3/7] Update network_utils.py

---
 snaps_openstack/utilities/network_utils.py | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/snaps_openstack/utilities/network_utils.py b/snaps_openstack/utilities/network_utils.py
index c7bd27e..7468bf3 100755
--- a/snaps_openstack/utilities/network_utils.py
+++ b/snaps_openstack/utilities/network_utils.py
@@ -198,13 +198,13 @@ def postNic(task):
         ip = host.get("ip")
         primary_interface = host.get("primary_interface")
         target_interface = host.get("target_interface")
-        node_type= host.get("node_type")
+        node_type = host.get("node_type")
         post_pb = pkg_resources.resource_filename(
                 'snaps_openstack.utilities.playbooks',
                 'viface_post_play.yaml')
         ansible_command = "ansible-playbook " + post_pb \
                               + " --extra-vars=\'{\"target\": \"" \
-                              + ip + "\",\"node_type\":\"" +node_type+"\",\"interface\": \"" + primary_interface + "\"}\'"
+                              + ip + "\",\"node_type\":\"" + node_type +"\",\"interface\": \"" + primary_interface + "\"}\'"
         logger.info(ansible_command)
         ret = os.system(ansible_command)
     return ret
@@ -214,13 +214,13 @@ def cleanNic(task):
         ip = host.get("ip")
         primary_interface = host.get("primary_interface")
         target_interface = host.get("target_interface")
-        node_type=host.get("node_type")
+        node_type = host.get("node_type")
         clean_pb = pkg_resources.resource_filename(
                 'snaps_openstack.utilities.playbooks',
                 'viface_clean_play.yaml')
         ansible_command = "ansible-playbook " + clean_pb \
                               + " --extra-vars=\'{\"target\": \"" \
-                              + ip +"\",\"node_type\":\"" +node_type+"\",\"interface\": \"" + primary_interface + "\"}\'"
+                              + ip +"\",\"node_type\":\"" + node_type +"\",\"interface\": \"" + primary_interface + "\"}\'"
         logger.info(ansible_command)
         ret = os.system(ansible_command)
     return ret

From 098eb9157d3a156f592cff19de1770b86e2cb0f0 Mon Sep 17 00:00:00 2001
From: shishirgoenka <36440470+shishirgoenka@users.noreply.github.com>
Date: Mon, 3 Sep 2018 11:26:08 +0530
Subject: [PATCH 4/7] Update network_utils.py

---
 snaps_openstack/utilities/network_utils.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/snaps_openstack/utilities/network_utils.py b/snaps_openstack/utilities/network_utils.py
index 7468bf3..947f045 100755
--- a/snaps_openstack/utilities/network_utils.py
+++ b/snaps_openstack/utilities/network_utils.py
@@ -204,7 +204,9 @@ def postNic(task):
                 'viface_post_play.yaml')
         ansible_command = "ansible-playbook " + post_pb \
                               + " --extra-vars=\'{\"target\": \"" \
-                              + ip + "\",\"node_type\":\"" + node_type +"\",\"interface\": \"" + primary_interface + "\"}\'"
+                              + ip + "\",\"node_type\":\"" + node_type \
+                              +"\",\"interface\": \"" + primary_interface \
+                              + "\"}\'"
         logger.info(ansible_command)
         ret = os.system(ansible_command)
     return ret
@@ -220,7 +222,9 @@ def cleanNic(task):
                 'viface_clean_play.yaml')
         ansible_command = "ansible-playbook " + clean_pb \
                               + " --extra-vars=\'{\"target\": \"" \
-                              + ip +"\",\"node_type\":\"" + node_type +"\",\"interface\": \"" + primary_interface + "\"}\'"
+                              + ip +"\",\"node_type\":\"" + node_type \
+                              +"\",\"interface\": \"" + primary_interface \
+                              + "\"}\'"
         logger.info(ansible_command)
         ret = os.system(ansible_command)
     return ret

From a612a8268044d37a07107f8e0140a316dda16725 Mon Sep 17 00:00:00 2001
From: shishirgoenka <36440470+shishirgoenka@users.noreply.github.com>
Date: Tue, 4 Sep 2018 11:32:06 +0530
Subject: [PATCH 5/7] Update install.md

---
 doc/source/install/install.md | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/doc/source/install/install.md b/doc/source/install/install.md
index 832cf6a..12645e7 100644
--- a/doc/source/install/install.md
+++ b/doc/source/install/install.md
@@ -549,7 +549,7 @@ below.
 
 ### 3.4 var.yaml (Single NIC settings)
 
-Configuration file used by SNAPS-OpenStack for single NIC deployemnt.
+Configuration file used by SNAPS-OpenStack for single NIC deployment.
 
 #### TASKS
 
@@ -681,7 +681,7 @@ sudo python <repo_dir>/network_config.py -f <repo_dir>/snaps_openstack/utilities
 
 #### Step 3
 
-Install fresh openstack using physical interface and veth0 as management and data interfaces respectively. (Please Refer 4.1)
+Install fresh openstack using physical interface and veth0 as management and data interfaces respectively. (Please refer to 4.1)
 
 #### Step 2
 
@@ -797,7 +797,7 @@ exit
 ```
 #### 5.3.2 Single NIC Cleanup
 
-Before cleaning the opesntack setup user needs to clean the single NIC configurations.
+Before cleaning the opesntack setup user needs to clean up the single NIC configurations.
 
 Run `network_config.py` as shown below:
 

From 9abbda7ab84fd780a4a9d074e08dbd11bd73c2f7 Mon Sep 17 00:00:00 2001
From: shishirgoenka <36440470+shishirgoenka@users.noreply.github.com>
Date: Tue, 4 Sep 2018 11:33:29 +0530
Subject: [PATCH 6/7] Update network_utils.py

---
 snaps_openstack/utilities/network_utils.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/snaps_openstack/utilities/network_utils.py b/snaps_openstack/utilities/network_utils.py
index 947f045..39f4694 100755
--- a/snaps_openstack/utilities/network_utils.py
+++ b/snaps_openstack/utilities/network_utils.py
@@ -205,7 +205,7 @@ def postNic(task):
         ansible_command = "ansible-playbook " + post_pb \
                               + " --extra-vars=\'{\"target\": \"" \
                               + ip + "\",\"node_type\":\"" + node_type \
-                              +"\",\"interface\": \"" + primary_interface \
+                              + "\",\"interface\": \"" + primary_interface \
                               + "\"}\'"
         logger.info(ansible_command)
         ret = os.system(ansible_command)
@@ -222,7 +222,7 @@ def cleanNic(task):
                 'viface_clean_play.yaml')
         ansible_command = "ansible-playbook " + clean_pb \
                               + " --extra-vars=\'{\"target\": \"" \
-                              + ip +"\",\"node_type\":\"" + node_type \
+                              + ip + "\",\"node_type\":\"" + node_type \
                               +"\",\"interface\": \"" + primary_interface \
                               + "\"}\'"
         logger.info(ansible_command)

From 8d71f7a2c089c7d55274a77a40f40ee5c3f43996 Mon Sep 17 00:00:00 2001
From: shishirgoenka <36440470+shishirgoenka@users.noreply.github.com>
Date: Tue, 4 Sep 2018 11:34:40 +0530
Subject: [PATCH 7/7] Update var.yaml

---
 snaps_openstack/utilities/var.yaml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/snaps_openstack/utilities/var.yaml b/snaps_openstack/utilities/var.yaml
index 0974b82..5e0216f 100755
--- a/snaps_openstack/utilities/var.yaml
+++ b/snaps_openstack/utilities/var.yaml
@@ -70,6 +70,6 @@ TASKS:
        target_interface: "veth1"
      - 
        ip: ""
+       node_type: "compute"
        primary_interface: ""
        target_interface: "veth1"
-       node_type: "compute"
