blob: 15a99131b7a037c9bc7b85b1aa92772a741b24a1 [file] [log] [blame]
Mirabale9317ff2017-01-18 16:10:58 +00001#!/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
26function _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
34function _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
43function _add_user_to_visudo()
44{
45# Allow admin users to access without password
46if ! grep -q "#openmano" /etc/sudoers
47then
48 cat >> /home/${option_user}/script_visudo.sh << EOL
49#!/bin/bash
50echo "#openmano allow to group admin to grant root privileges without password" >> \$1
51echo "${option_user} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
52EOL
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
57fi
58
59}
60
61function _create_ovs_controller_config_path() {
62 mkdir -p '/var/lib/openvim'
63}
64
65function _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
101FORCE=""
102while getopts "h" o; do
103 case "${o}" in
104 h)
105 _usage
106 exit -1
107 ;;
108 esac
109done
110shift $((OPTIND-1))
111
112
113
114if [ $# -lt 1 ]
115then
tiernoa6933042017-05-24 16:54:33 +0200116 _usage
Mirabale9317ff2017-01-18 16:10:58 +0000117 exit
118fi
119
120[ -z "$1" ] && echo -e "ERROR: User argument is mandatory, --user=<user>\n" && _usage
121
122option_user=$1
123
124#check root privileges
125[ "${USER}" != "root" ] && echo "Needed root privileges" >&2 && exit 2
126
127
128echo '
129#################################################################
130##### INSTALL USER #####
131#################################################################'
132
133_install_user
134_add_user_to_visudo
135
136echo '
137#################################################################
138##### INSTALL NEEDED PACKETS #####
139#################################################################'
140_install_packages_dependencies
141
142_create_ovs_controller_config_path
143
144echo
145echo "Do not forget to copy the public ssh key into /home/${option_user}/.ssh/authorized_keys for authomatic login from openvim controller"
146echo
147
148echo "Reboot the system to make the changes effective"