9eb675edd27755ec5f31ae23b27bcba1e621d37f
[osm/openvim.git] / scripts / configure-dhcp-server-UBUNTU16.0.4.sh
1 #!/bin/bash
2 ##
3 # This file is part of openvim
4 # All Rights Reserved.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
16 # under the License.
17 #
18 # For those usages not covered by the Apache License, Version 2.0 please
19 # contact with: nfvlabs@tid.es
20 #
21 # Authors: Leonardo Mirabal
22 # February 2017
23
24
25
26 function _usage(){
27 echo -e "Usage: sudo $0 <user-name> "
28 echo -e " Configure dhcp server for VIM usage. (version 1.0). Params:"
29 echo -e " <user-name> Create if not exist and configure this user for openvim to connect"
30 echo -e " -h --help this help"
31 exit 1
32 }
33
34 function _install_packages_dependencies()
35 {
36 # Required packages by openvim
37 apt-get -y update
38 apt-get -y install ethtool build-essential dnsmasq openvswitch-switch
39 echo "Remove unneeded packages....."
40 apt-get -y autoremove
41 }
42
43 function _add_user_to_visudo()
44 {
45 # Allow admin users to access without password
46 if ! grep -q "#openmano" /etc/sudoers
47 then
48 cat >> /home/${option_user}/script_visudo.sh << EOL
49 #!/bin/bash
50 echo "#openmano allow to group admin to grant root privileges without password" >> \$1
51 echo "${option_user} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
52 EOL
53 chmod +x /home/${option_user}/script_visudo.sh
54 echo "allowing admin user to get root privileges withut password"
55 export EDITOR=/home/${option_user}/script_visudo.sh && sudo -E visudo
56 rm -f /home/${option_user}/script_visudo.sh
57 fi
58
59 }
60
61 function _create_ovs_controller_config_path() {
62 mkdir -p '/var/lib/openvim'
63 }
64
65 function _install_user() {
66 # create user given by the user and add to groups need it.
67 # Add required groups
68 groupadd -f admin
69
70 # Adds user, default password same as name
71 if grep -q "^${option_user}:" /etc/passwd
72 then
73 #user exist, add to group
74 echo "adding user ${option_user} to group admin"
75 usermod -a -G admin -g admin ${option_user}
76 else
77 #create user if it does not exist
78 [ -z "$FORCE" ] && read -p "user '${option_user}' does not exist, create (Y/n)" kk
79 if ! [ -z "$kk" -o "$kk"="y" -o "$kk"="Y" ]
80 then
81 exit
82 fi
83 echo "creating and configuring user ${option_user}"
84 useradd -m -G admin -g admin ${option_user}
85 #Password
86 if [ -z "$FORCE" ]
87 then
88 echo "Provide a password for ${option_user}"
89 passwd ${option_user}
90 else
91 echo -e "$option_user\n$option_user" | passwd --stdin ${option_user}
92 fi
93 fi
94
95 }
96
97
98
99
100 #1.2 input parameters
101 FORCE=""
102 while getopts "h" o; do
103 case "${o}" in
104 h)
105 _usage
106 exit -1
107 ;;
108 esac
109 done
110 shift $((OPTIND-1))
111
112
113
114 if [ $# -lt 1 ]
115 then
116 usage
117 exit
118 fi
119
120 [ -z "$1" ] && echo -e "ERROR: User argument is mandatory, --user=<user>\n" && _usage
121
122 option_user=$1
123
124 #check root privileges
125 [ "${USER}" != "root" ] && echo "Needed root privileges" >&2 && exit 2
126
127
128 echo '
129 #################################################################
130 ##### INSTALL USER #####
131 #################################################################'
132
133 _install_user
134 _add_user_to_visudo
135
136 echo '
137 #################################################################
138 ##### INSTALL NEEDED PACKETS #####
139 #################################################################'
140 _install_packages_dependencies
141
142 _create_ovs_controller_config_path
143
144 echo
145 echo "Do not forget to copy the public ssh key into /home/${option_user}/.ssh/authorized_keys for authomatic login from openvim controller"
146 echo
147
148 echo "Reboot the system to make the changes effective"