initial commit

Change-Id: I5063800f2ddaf90a350325a9186479c25f90f8e1
diff --git a/roles/compute-node/files/i40e-1.4.25.tar.gz b/roles/compute-node/files/i40e-1.4.25.tar.gz
new file mode 100644
index 0000000..6a30a04
--- /dev/null
+++ b/roles/compute-node/files/i40e-1.4.25.tar.gz
Binary files differ
diff --git a/roles/compute-node/files/rename_ifaces.sh b/roles/compute-node/files/rename_ifaces.sh
new file mode 100755
index 0000000..be8cb72
--- /dev/null
+++ b/roles/compute-node/files/rename_ifaces.sh
@@ -0,0 +1,180 @@
+#!/bin/bash
+
+function ip2int {
+    local a b c d
+    { IFS=. read a b c d; } <<< $1
+    echo $(((((((a << 8) | b) << 8) | c) << 8) | d))
+}
+
+function int2ip {
+    local ui32=$1; shift
+    local ip n
+    for n in 1 2 3 4; do
+        ip=$((ui32 & 0xff))${ip:+.}$ip
+        ui32=$((ui32 >> 8))
+    done
+    echo $ip
+}
+
+function netmask {
+    local mask=$((0xffffffff << (32 - $1))); shift
+    int2ip $mask
+}
+
+function broadcast {
+    local addr=$(ip2int $1); shift
+    local mask=$((0xffffffff << (32 -$1))); shift
+    int2ip $((addr | ~mask))
+}
+
+function network {
+    local addr=$(ip2int $1); shift
+    local mask=$((0xffffffff << (32 -$1))); shift
+    int2ip $((addr & mask))
+}
+
+function first {
+    local addr=$(ip2int $1)
+    addr=`expr $addr + 1`
+    int2ip $addr
+}
+
+function guess_type {
+    local CNT=$(echo "$1" | sed -e 's/[:.]/ /g' | wc -w)
+    if [ $CNT -ne 1 ]; then
+        # drop all sub and vlan interfaces
+        echo "DNC"
+        return
+    fi
+    local DRIVER=$(ethtool -i $1 2>/dev/null | grep driver | awk '{print $2}')
+    local RESULT="DNC"
+    case $DRIVER in
+        i40e)
+            RESULT="I40G"
+            ;;
+        igb)
+            RESULT="ETH"
+            ;;
+        *) ;;
+    esac
+    echo $RESULT
+}
+
+function get_mac {
+  echo $(ifconfig $1 | grep HWaddr | awk '{print $5}')
+}
+
+function generate_persistent_names {
+    local OUT=$NAMES_FILE
+#"70-persistent-net.rules"
+    rm -rf $OUT
+
+    IDX=0
+    for i in $(cat $1 | sort); do
+        echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$i\", ATTR{dev_id}==\"0x0\", ATTR{type}==\"1\", KERNEL==\"eth*\", NAME=\"eth$IDX\"" >> $OUT
+        IDX=$(expr $IDX + 1)
+    done
+
+    for i in $(cat $2 | sort); do
+        echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$i\", NAME=\"eth$IDX\"" >> $OUT
+        IDX=$(expr $IDX + 1)
+    done
+}
+
+function generate_interfaces {
+    OUT=$IFACES_FILE
+    rm -rf $OUT
+    echo "# This file describes the network interfaces available on your system" >> $OUT
+    echo "# and how to activate them. For more information, see interfaces(5)." >> $OUT
+    echo "" >> $OUT
+    echo "# The loopback network interface" >> $OUT
+    echo "auto lo" >> $OUT
+    echo "iface lo inet loopback" >> $OUT
+    echo "" >> $OUT
+
+    IDX=0
+    FIRST=1
+    for i in $(cat $1); do
+        if [ $FIRST -eq 1 ]; then
+            echo "auto eth$IDX" >> $OUT
+            echo "iface eth$IDX inet static" >> $OUT
+            echo "    address $IP" >> $OUT
+            echo "    network $NETWORK" >> $OUT
+            echo "    netmask $NETMASK" >> $OUT
+            FIRST=0
+        else
+            echo "iface eth$IDX inet manual" >> $OUT
+        fi
+        echo "" >> $OUT
+        IDX=$(expr $IDX + 1)
+    done
+
+    FIRST=1
+    for i in $(cat $2); do
+        if [ $FIRST -eq 1 ]; then
+            echo "auto eth$IDX" >> $OUT
+            echo "iface eth$IDX inet dhcp" >> $OUT
+            FIRST=0
+        else
+            echo "iface eth$IDX inet manual" >> $OUT
+        fi
+        echo "" >> $OUT
+        IDX=$(expr $IDX + 1)
+    done
+}
+
+ADDR=$1
+IP=$(echo $ADDR | cut -d/ -f1)
+MASKBITS=$(echo $ADDR | cut -d/ -f2)
+NETWORK=$(network $IP $MASKBITS)
+NETMASK=$(netmask $MASKBITS)
+
+LIST_ETH=$(mktemp -u)
+LIST_40G=$(mktemp -u)
+IFACES_FILE=$(mktemp -u)
+NAMES_FILE=$(mktemp -u)
+
+IFACES=$(ifconfig -a | grep "^[a-z]" | awk '{print $1}')
+
+for i in $IFACES; do
+    TYPE=$(guess_type $i)
+    case $TYPE in
+        ETH)
+            echo "$(get_mac $i)" >> $LIST_ETH
+            ;;
+        I40G)
+            echo "$(get_mac $i)" >> $LIST_40G
+            ;;
+        *) ;;
+    esac
+done
+
+RESULT="false"
+
+generate_interfaces $LIST_40G $LIST_ETH
+diff /etc/network/interfaces $IFACES_FILE 2>&1 > /dev/null
+if [ $? -ne 0 ]; then
+  RESULT="true"
+  cp /etc/network/interfaces /etc/network/interfaces.1
+  cp $IFACES_FILE /etc/network/interfaces
+fi
+
+generate_persistent_names $LIST_40G $LIST_ETH
+if [ -r /etc/udev/rules.d/70-persistent-net.rules ]; then
+  diff /etc/udev/rules.d/70-persistent-net.rules $NAMES_FILE 2>&1 > /dev/null
+  if [ $? -ne 0 ]; then
+    RESULT="true"
+    cp /etc/udev/rules.d/70-persistent-net.rules /etc/udev/rules.d/70-persistent-net.rules.1
+    cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules
+  fi
+else
+  RESULT="true"
+  cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules
+fi
+
+rm -rf $IFACES_FILE
+rm -rf $NAMES_FILE
+rm -rf $LIST_ETH
+rm -rf $LIST_40G
+
+echo -n $RESULT
diff --git a/roles/compute-node/files/rename_ifaces.sh.back b/roles/compute-node/files/rename_ifaces.sh.back
new file mode 100755
index 0000000..76056ff
--- /dev/null
+++ b/roles/compute-node/files/rename_ifaces.sh.back
@@ -0,0 +1,110 @@
+#!/bin/bash
+
+BASE="10.4"
+LEAF="1"
+SERVER="1"
+
+function guess_type {
+    local CNT=$(echo "$1" | sed -e 's/[:.]/ /g' | wc -w)
+    if [ $CNT -ne 1 ]; then
+        # drop all sub and vlan interfaces
+        echo "DNC"
+        return
+    fi
+    local DRIVER=$(ethtool -i $1 2>/dev/null | grep driver | awk '{print $2}')
+    local RESULT="DNC"
+    case $DRIVER in
+        i40e)
+            RESULT="I40G"
+            ;;
+        igb)
+            RESULT="ETH"
+            ;;
+        *) ;;
+    esac
+    echo $RESULT
+}
+
+function get_mac {
+  echo $(ifconfig $1 | grep HWaddr | awk '{print $5}')
+}
+
+function generate_persistent_names {
+    local OUT="70-persistent-net.rules"
+    rm -rf $OUT
+
+    IDX=0
+    for i in $(cat $1 | sort); do
+        echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$i\", ATTR{dev_id}==\"0x0\", ATTR{type}==\"1\", KERNEL==\"eth*\", NAME=\"eth$IDX\"" >> $OUT
+        IDX=$(expr $IDX + 1)
+    done
+
+    for i in $(cat $2 | sort); do
+        echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$i\", NAME=\"eth$IDX\"" >> $OUT
+        IDX=$(expr $IDX + 1)
+    done
+}
+
+function generate_interfaces {
+    OUT="interfaces"
+    rm -rf $OUT
+    echo "# This file describes the network interfaces available on your system" >> $OUT
+    echo "# and how to activate them. For more information, see interfaces(5)." >> $OUT
+    echo "" >> $OUT
+    echo "# The loopback network interface" >> $OUT
+    echo "auto lo" >> $OUT
+    echo "iface lo inet loopback" >> $OUT
+    echo "" >> $OUT
+
+    IDX=0
+    FIRST=1
+    for i in $(cat $1); do
+        if [ $FIRST -eq 1 ]; then
+            echo "auto eth$IDX" >> $OUT
+            echo "iface eth$IDX inet static" >> $OUT
+            echo "    address $BASE.$LEAF.$SERVER" >> $OUT
+            echo "    network $BASE.$LEAF.0" >> $OUT
+            echo "    netmask 255.255.255.0" >> $OUT
+            FIRST=0
+        else
+            echo "iface eth$IDX inet manual" >> $OUT
+        fi
+        echo "" >> $OUT
+        IDX=$(expr $IDX + 1)
+    done
+
+    FIRST=1
+    for i in $(cat $2); do
+        if [ $FIRST -eq 1 ]; then
+            echo "auto eth$IDX" >> $OUT
+            echo "iface eth$IDX inet dhcp" >> $OUT
+            FIRST=0
+        else
+            echo "iface eth$IDX inet manual" >> $OUT
+        fi
+        echo "" >> $OUT
+        IDX=$(expr $IDX + 1)
+    done
+}
+
+LIST_ETH=$(mktemp -u)
+LIST_40G=$(mktemp -u)
+IFACES=$(ifconfig -a | grep "^[a-z]" | awk '{print $1}')
+
+for i in $IFACES; do
+    TYPE=$(guess_type $i)
+    case $TYPE in
+        ETH)
+            echo "$(get_mac $i)" >> $LIST_ETH
+            ;;
+        I40G)
+            echo "$(get_mac $i)" >> $LIST_40G
+            ;;
+        *) ;;
+    esac
+done
+
+generate_persistent_names $LIST_40G $LIST_ETH
+generate_interfaces $LIST_40G $LIST_ETH
+rm -rf $LIST_ETH
+rm -rf $LIST_40G
diff --git a/roles/compute-node/meta/main.yml b/roles/compute-node/meta/main.yml
new file mode 100644
index 0000000..45b0e96
--- /dev/null
+++ b/roles/compute-node/meta/main.yml
@@ -0,0 +1,15 @@
+---
+galaxy_info:
+  author: Ciena Blueplanet
+  description: CORD POD Compute Node Base
+  company: Ciena Blueplanet
+  license: Apache 2.0
+  min_ansible_version: 2.0
+  platforms:
+    - name: Ubuntu
+      versions:
+        - trusty
+  galaxy_tags:
+    - cord
+dependencies:
+  - { role : docker }
diff --git a/roles/compute-node/tasks/i40e_driver.yml b/roles/compute-node/tasks/i40e_driver.yml
new file mode 100644
index 0000000..5f6b199
--- /dev/null
+++ b/roles/compute-node/tasks/i40e_driver.yml
@@ -0,0 +1,40 @@
+---
+- name: Copy i40e Interface Driver
+  unarchive:
+    src=files/i40e-1.4.25.tar.gz
+    dest={{ ansible_env.HOME }}
+    owner=ubuntu
+    group=ubuntu
+
+- name: Build i40e Driver
+  command: make
+  args:
+    chdir: i40e-1.4.25/src
+    creates: "{{ ansible_env.HOME }}/i40e-1.4.25/src/i40e/i40e.ko"
+
+- name: Unload i40e Driver
+  become: yes
+  modprobe: name=i40e state=absent
+
+- name: Install i40e Driver
+  become: yes
+  command: make install
+  args:
+    chdir: i40e-1.4.25/src
+
+- name: Load i40e Driver
+  become: yes
+  modprobe: name=i40e state=present
+
+- name: Persist i40e Driver Loadi
+  become: yes
+  lineinfile:
+    dest=/etc/modules
+    line="i40e"
+    state=present
+    insertafter=EOF
+
+- name: Remove Build Files
+  file:
+    path={{ ansible_env.HOME }}/i40e-1.4.25
+    state=absent
diff --git a/roles/compute-node/tasks/main.yml b/roles/compute-node/tasks/main.yml
new file mode 100644
index 0000000..d64adfe
--- /dev/null
+++ b/roles/compute-node/tasks/main.yml
@@ -0,0 +1,32 @@
+---
+- name: Applications
+  become: yes
+  apt: name={{ item }} state=present
+  with_items:
+    - build-essential 
+
+- name: Set Default Password
+  become: yes
+  user:
+    name=ubuntu
+    password="$6$TjhJuOgh8xp.v$z/4GwFbn5koVmkD6Ex9wY7bgP7L3uP2ujZkZSs1HNdzQdz9YclbnZH9GvqMC/M1iwC0MceL05.13HoFz/bai0/"
+
+- name: Verify i40e Driver
+  command: modinfo --field=version i40e
+  register: i40e_version
+  changed_when: False
+
+- name: Update i40e Driver
+  include: tasks/i40e_driver.yml
+  when: i40e_version.stdout != '1.4.25'
+
+- name: Consistent Interface Naming
+  become: yes
+  script: files/rename_ifaces.sh {{ fabric_ip }}
+  register: ifaces_changed
+  changed_when: ifaces_changed.stdout != "false"
+
+- name: Reboot Required
+  become: yes
+  command: /sbin/reboot
+  when: ifaces_changed.stdout != "false"