Added LICENSE file to root folder
[osm/openvim.git] / scripts / configure-compute-node-develop-UBUNTU16.04.sh
old mode 100644 (file)
new mode 100755 (executable)
index 4abe3c7..2f6d72e
@@ -21,7 +21,7 @@
 # contact with: nfvlabs@tid.es
 ##
 
-# Authors: Antonio Lopez, Pablo Montes, Alfonso Tierno
+# Authors: Antonio Lopez, Pablo Montes, Alfonso Tierno, Leonardo Mirabal
 # June 2015
 
 # Personalize RHEL7.1 on compute nodes
@@ -44,42 +44,33 @@ interfaced_path='/etc/network/interfaces.d/'
 #interfaced_path='/home/ubuntu/openvim_install/openvim/test-inter/'
 set_mtu_path='/etc/'
 VLAN_INDEX=20
-function _usage(){
-
-    echo -e "Usage: sudo $0 [-y] <user-name>  <iface-name>"
-    echo -e "  Configure compute host for VIM usage. (version 0.4). Params:"
-    echo -e "     -y  do not prompt for confirmation. If a new user is created, the user name is set as password"
-    echo -e "     <user-name> Create if not exist and configure this user for openvim to connect"
-    echo -e "     <iface-name> if suplied creates bridge interfaces on this interface, needed for openvim"
-}
-
-function _interface_cfg_generator(){
-    #$1 interface name | $2 MTU | $3 type
 
-echo "
-auto ${1}
-iface ${1} inet ${3}
-        mtu ${2}
-        ${bridge_ports}
-" >> ${interfaced_path}${1}."cfg"
+function _usage(){
+    echo -e "Usage: sudo $0 [-f] --user=<user-name> --overlay=ovs --iface-name=<iface-name>"
+    echo -e "  Configure compute host for VIM usage. (version 0.4). OPTIONS:"
+    echo -e "     -h --help    this help"
+    echo -e "     -f --force:  do not prompt for confirmation. If a new user is created, the user name is set as password"
+    echo -e "     -u --user:   Create if not exist and configure this user for openvim to connect"
+    echo -e "     -o --overlay: ovs, bridge and bridge-and-ovs. Specify the networking overlay used by openvim, by default ovs is used"
+    echo -e "     -in --iface-name:  creates bridge interfaces on this interface, needed for openvim overlay networks"
+
+    exit 1
 }
 
-
 function _interface_cfg_generator(){
     #$1 interface name | $2 vlan  | $3 virbrMan | $4  MTU
 
 echo "
 auto ${1}.${2}
 iface ${1}.${2} inet manual
-        mtu ${4}
-        post-up vconfig add ${1} ${2}
-        post-down vconfig rem ${1}.${2}
+        vlan-raw-device ${1}
+        post-up ip link set mtu $MTU dev ${1}.${2}
 
 auto ${3}
 iface ${3} inet manual
         bridge_ports ${1}.${2}
-        mtu ${4}
-        vlan-raw-device $1
+           post-up ip link set dev ${3} && ip link set mtu $MTU dev ${3}
+
 " >> ${interfaced_path}${1}.${2}."cfg"
 }
 
@@ -87,30 +78,30 @@ function _install_user() {
     # create user given by the user and add to groups need it.
     # Add required groups
     groupadd -f admin
-    groupadd -f libvirt   #for other operating systems may be libvirtd
+    groupadd -f libvirtd   #for other operating systems may be libvirtd
 
     # Adds user, default password same as name
-    if grep -q "^${user_name}:" /etc/passwd
+    if grep -q "^${option_user}:" /etc/passwd
     then
         #user exist, add to group
-        echo "adding user ${user_name} to groups libvirt,admin"
-        usermod -a -G libvirt,admin -g admin ${user_name}
+        echo "adding user ${option_user} to groups libvirt,admin"
+        usermod -a -G libvirtd,admin -g admin ${option_user}
     else
         #create user if it does not exist
-        [ -z "$FORCE" ] && read -p "user '${user_name}' does not exist, create (Y/n)" kk
+        [ -z "$FORCE" ] && read -p "user '${option_user}' does not exist, create (Y/n)" kk
         if ! [ -z "$kk" -o "$kk"="y" -o "$kk"="Y" ]
         then
             exit
         fi
-        echo "creating and configuring user ${user_name}"
-        useradd -m -G libvirt,admin -g admin ${user_name}
+        echo "creating and configuring user ${option_user}"
+        useradd -m -G libvirtd,admin -g admin ${option_user}
         #Password
         if [ -z "$FORCE" ]
             then
-                echo "Provide a password for ${user_name}"
-                passwd ${user_name}
+                echo "Provide a password for ${option_user}"
+                passwd ${option_user}
             else
-                echo -e "$user_name\n$user_name" | passwd --stdin ${user_name}
+                echo -e "$option_user\n$option_user" | passwd --stdin ${option_user}
         fi
     fi
 
@@ -122,13 +113,13 @@ function _openmano_img_2_libvirt_img(){
     # should have only a / partition with all possible space available
 
     echo " link /opt/VNF/images to /var/lib/libvirt/images"
-    if [ "$user_name" != "" ]
+    if [ "$option_user" != "" ]
     then
         # The orchestator needs to link the images folder
         rm -f /opt/VNF/images
         mkdir -p /opt/VNF/
         ln -s /var/lib/libvirt/images /opt/VNF/images
-        chown -R ${user_name}:admin /opt/VNF
+        chown -R ${option_user}:admin /opt/VNF
         chown -R root:admin /var/lib/libvirt/images
         chmod g+rwx /var/lib/libvirt/images
     else
@@ -137,13 +128,13 @@ function _openmano_img_2_libvirt_img(){
     fi
 }
 
-function _install_pacckags_dependences()
+function _install_packages_dependencies()
 {
     # Required packages by openvim
     apt-get -y update
     apt-get -y install  grub-common screen virt-manager ethtool build-essential \
                         x11-common x11-utils libguestfs-tools hwloc libguestfs-tools \
-                        numactl vlan nfs-common nfs-kernel-server
+                        numactl vlan nfs-common nfs-kernel-server  openvswitch-switch
     echo "Remove unneeded packages....."
     apt-get -y autoremove
 }
@@ -152,8 +143,6 @@ function _network_configuration(){
     # adding vlan support
     grep -q '8021q' '/etc/modules'; [ $? -eq 1 ] && sudo su -c 'echo "8021q" >> /etc/modules'
 
-    #grep -q ${interface} '/etc/network/interfaces.d/50-cloud-init.cfg'; [ $? -eq 0 ] && sed -e '/'${interface}'/ s/^#*/#/' -i  '/etc/network/interfaces.d/50-cloud-init.cfg'
-
     # Network interfaces static configuration
     echo "Interface ==> $interface"
     if [ -n "$interface" ]
@@ -162,24 +151,25 @@ function _network_configuration(){
         rm -f /etc/udev/rules.d/pci_config.rules # it will be created to define VFs
         # Set ONBOOT=on and MTU=9000 on the interface used for the bridges
         echo "configuring iface $interface"
+    if [ "$option_overlay" == "bridge" ] || [ "$option_overlay" == "bridge-and-ovs" ]
+        then
+            # Static network interface configuration and MTU
+            MTU=9000
+            virbrMan_interface_number=20
 
-    # Static network interface configuration and MTU
-    MTU=9000
-    virbrMan_interface_number=20
-
-    #Create bridge interfaces
-    echo "Creating bridge ifaces: "
-    for ((i =1; i <= ${virbrMan_interface_number}; i++))
-        do
-            i2digits=${i}
-            [ ${i} -lt 10 ] && i2digits="0${i}"
-            echo "    ${interface} ${VLAN_INDEX}${i2digits}"
-            echo "    virbrMan${i}  vlan ${VLAN_INDEX}${i2digits}"
-            j=${i}
-            #$1 interface name | $2 vlan | $3 MTU | $3 virbrMan | $4 bridge_ports
-            _interface_cfg_generator  ${interface} ${VLAN_INDEX}${i2digits} 'virbrMan'${i} ${MTU}
-    done
-
+            #Create bridge interfaces
+            echo "Creating bridge ifaces: "
+            for ((i =1; i <= ${virbrMan_interface_number}; i++))
+                do
+                    i2digits=${i}
+                    [ ${i} -lt 10 ] && i2digits="0${i}"
+                    echo "    ${interface} ${VLAN_INDEX}${i2digits}"
+                    echo "    virbrMan${i}  vlan ${VLAN_INDEX}${i2digits}"
+                    j=${i}
+                    #$1 interface name | $2 vlan | $3 MTU | $3 virbrMan | $4 bridge_ports
+                    _interface_cfg_generator  ${interface} ${VLAN_INDEX}${i2digits} 'virbrMan'${i} ${MTU}
+            done
+        fi
     fi
 }
 
@@ -194,7 +184,6 @@ function _check_interface(){
     if [ -n "$1" ] && ! ifconfig $1 &> /dev/null
     then
         echo "Error: interface '$1' is not present in the system"\n
-        _usage
         exit 1
     fi
 }
@@ -204,7 +193,7 @@ function _user_remainder_pront()
     echo
     echo "Do not forget to create a shared (NFS, Samba, ...) where original virtual machine images are allocated"
     echo
-    echo "Do not forget to copy the public ssh key into /home/${user_name}/.ssh/authorized_keys for authomatic login from openvim controller"
+    echo "Do not forget to copy the public ssh key into /home/${option_user}/.ssh/authorized_keys for authomatic login from openvim controller"
     echo
     echo "Reboot the system to make the changes effective"
 }
@@ -233,12 +222,32 @@ function _hostinfo_config()
     echo "#if compute node contain a different name it must be indicated in this file" >> /opt/VNF/images/hostinfo.yaml
     echo "#with the format extandard-name: compute-name" >> /opt/VNF/images/hostinfo.yaml
     chmod o+r /opt/VNF/images/hostinfo.yaml
+    if [ "$interface" != "" -a "$interface" != "em1" ]
+    then
+      echo "iface_names:"  >> /opt/VNF/images/hostinfo.yaml
+      echo "  em1: ${interface}" >> /opt/VNF/images/hostinfo.yaml
+    fi
 }
 
-function _get_opts()
+function _add_user_to_visudo()
 {
-    [[ ${BASH_SOURCE[0]} != $0 ]] && ___exit="return" || ___exit="exit"
+# Allow admin users to access without password
+if ! grep -q "#openmano" /etc/sudoers
+then
+    cat >> /home/${option_user}/script_visudo.sh << EOL
+#!/bin/bash
+echo "#openmano allow to group admin to grant root privileges without password" >> \$1
+echo "${option_user} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
+EOL
+    chmod +x /home/${option_user}/script_visudo.sh
+    echo "allowing admin user to get root privileges withut password"
+    export EDITOR=/home/${option_user}/script_visudo.sh && sudo -E visudo
+    rm -f /home/${option_user}/script_visudo.sh
+fi
 
+}
+function _get_opts()
+{
     options="$1"
     shift
 
@@ -259,7 +268,7 @@ function _get_opts()
         shift
         if [[ -n $get_argument ]]
         then
-            [[ ${argument:0:1} == "-" ]] && echo "option '-$option' requires an argument"  >&2 && $___exit 1
+            [[ ${argument:0:1} == "-" ]] && echo "option '-$option' requires an argument"  >&2 && return 1
             eval ${get_argument}='"$argument"'
             #echo option $get_argument with argument
             get_argument=""
@@ -303,7 +312,7 @@ function _get_opts()
                         fi
                     done
                 done
-                [[ $bad_option == y ]] && echo "invalid argument '-$option'?  Type -h for help" >&2 && $___exit 1
+                [[ $bad_option == y ]] && echo "invalid argument '-$option'?  Type -h for help" >&2 && return 1
             done
         elif [[ ${argument:0:2} == "--" ]] && [[ ${#argument} -ge 3 ]]
         then
@@ -324,7 +333,7 @@ function _get_opts()
                         bad_option=n
                         if [[ ${option_group:${#option_group}-1} != "=" ]]
                         then #not an argument
-                            [[ -n "${option_argument}" ]] && echo "option '--${option%%=*}' do not accept an argument " >&2 && $___exit 1
+                            [[ -n "${option_argument}" ]] && echo "option '--${option%%=*}' do not accept an argument " >&2 && return 1
                             eval option_${_name}='"${option_'${_name}'}-"'
                         elif [[ -n "${option_argument}" ]]
                         then
@@ -337,7 +346,7 @@ function _get_opts()
                     fi
                 done
             done
-            [[ $bad_option == y ]] && echo "invalid argument '-$option'?  Type -h for help" >&2 && $___exit 1
+            [[ $bad_option == y ]] && echo "invalid argument '-$option'?  Type -h for help" >&2 && return 1
         elif [[ ${argument:0:2} == "--" ]]
         then
             option__="$*"
@@ -351,7 +360,7 @@ function _get_opts()
                     break
                 fi
             done
-            [[ $bad_option == y ]] && echo "invalid argument '--'?  Type -h for help" >&2 && $___exit 1
+            [[ $bad_option == y ]] && echo "invalid argument '--'?  Type -h for help" >&2 && return 1
             break
         else
             params="$params ${argument}"
@@ -359,43 +368,36 @@ function _get_opts()
 
     done
 
-    [[ -n "$get_argument" ]] && echo "option '-$option' requires an argument"  >&2 && $___exit 1
-    $___exit 0
-
+    [[ -n "$get_argument" ]] && echo "option '-$option' requires an argument"  >&2 && return  1
+    return 0
 }
 
 function _parse_opts()
 {
-    FORCE=""
-    if [ -n "$option_help" ];
-    then
-        _usage
-        exit -1
-    fi
+    [ -n "$option_help" ] && _usage && exit 0
 
-    if [ -n "$option_force" ];
-        then
-            FORCE="yes"
-            echo "force = yes"
-        fi
+    FORCE=""
+    [ -n "$option_force" ] && FORCE="yes"
 
-    [ -z "$option_user" ] && echo -e "ERROR: User argument is mandatory, --user=<user>\n" && usage
-    USER=${option_user}
-     echo "user_name = "$option_user
+    [ -z "$option_user" ] && echo -e "ERROR: User argument is mandatory, --user=<user>\n" >&2 && _usage
 
-    [ -z "$option_iface_name" ] && echo -e "ERROR: User argument is mandatory, --user=<user>\n" && usage
+    [ -z "$option_iface_name" ] && echo -e "ERROR: iface-name argument is mandatory, --iface-name=<interface>\n" && _usage
     interface=$option_iface_name
 
+    if [ "$option_overlay" != "bridge" ] && [ "$option_overlay" != "ovs" ] && [ "$option_overlay" != "bridge-and-ovs" ];
+    then
+        option_overlay='ovs'
+        echo 'ERROR: overlay argument must be "ovs", "bridge", "bridge-and-ovs"' && _usage
+    fi
 }
-#1 CHECK input parameters
-#1.1 root privileges
-[ "${USER}" != "root" ] && echo "Needed root privileges" && _usage && exit -1
-
 
 #Parse opts
-_get_opts "help:h force:f user:u= iface-name:in= "  $*
+_get_opts "help:h force:f user:u= overlay:o= iface-name:in= "  $*  || exit 1
 _parse_opts
 
+#check root privileges
+[ "${USER}" != "root" ] && echo "Needed root privileges" >&2 && exit 2
+
 echo "checking interface "$interface
 
 _check_interface $interface
@@ -405,12 +407,12 @@ echo '
 #####       INSTALL USER                                    #####
 #################################################################'
 _install_user
-
+_add_user_to_visudo
 echo '
 #################################################################
 #####       INSTALL NEEDED PACKETS                          #####
 #################################################################'
-_install_pacckags_dependences
+_install_packages_dependencies
 
 echo '
 #################################################################
@@ -424,7 +426,10 @@ echo '
 #################################################################
 #####       NETWORK CONFIGURATION                           #####
 #################################################################'
+
 _network_configuration
+
+
 _disable_aaparmor
 _user_remainder_pront