From e47c9839d857ac47fd3aa7572aeb5c879e87d996 Mon Sep 17 00:00:00 2001
From: Igor Malinovskiy <imalinovskiy@mirantis.com>
Date: Wed, 3 Jun 2015 12:04:18 +0300
Subject: [PATCH] Remove devuser element from dependencies

---
 build.sh                                      | 11 ++++---
 .../manila-ssh/environment.d/50-manila-user   |  3 ++
 .../manila-ssh/extra-data.d/50-manila-user    | 13 +++++++++
 elements/manila-ssh/install.d/50-manila-user  | 29 +++++++++++++++++++
 ...blic-ssh-keys => 60-fetch-public-ssh-keys} |  0
 5 files changed, 50 insertions(+), 6 deletions(-)
 create mode 100755 elements/manila-ssh/environment.d/50-manila-user
 create mode 100755 elements/manila-ssh/extra-data.d/50-manila-user
 create mode 100755 elements/manila-ssh/install.d/50-manila-user
 rename elements/manila-ssh/install.d/{50-fetch-public-ssh-keys => 60-fetch-public-ssh-keys} (100%)

diff --git a/build.sh b/build.sh
index 95fa8f2..db71d49 100755
--- a/build.sh
+++ b/build.sh
@@ -8,7 +8,7 @@ DISABLE_IMG_COMPRESSION=${DISABLE_IMG_COMPRESSION:-"no"}
 # Manila user settings
 MANILA_USER=${MANILA_USER:-"manila"}
 MANILA_PASSWORD=${MANILA_PASSWORD:-"manila"}
-MANILA_USER_AUTHORIZED_KEYS=
+MANILA_USER_AUTHORIZED_KEYS="None"
 
 # Manila image settings
 MANILA_IMG_ARCH=${MANILA_IMG_ARCH:-"i386"}
@@ -23,7 +23,7 @@ MANILA_ENABLE_CIFS_SUPPORT=${MANILA_ENABLE_CIFS_SUPPORT:-"yes"}
 
 # Verify configuration
 # --------------------
-REQUIRED_ELEMENTS="manila-ssh vm $MANILA_IMG_OS dhcp-all-interfaces devuser cleanup-kernel-initrd"
+REQUIRED_ELEMENTS="manila-ssh vm $MANILA_IMG_OS dhcp-all-interfaces cleanup-kernel-initrd"
 OPTIONAL_ELEMENTS=
 OPTIONAL_DIB_ARGS=
 
@@ -64,10 +64,9 @@ export DIB_DEFAULT_INSTALLTYPE=package
 export DIB_RELEASE=$MANILA_IMG_OS_VER
 
 # User settings
-export DIB_DEV_USER_USERNAME=$MANILA_USER
-export DIB_DEV_USER_PWDLESS_SUDO=yes
-export DIB_DEV_USER_PASSWORD=$MANILA_PASSWORD
-export DIB_DEV_USER_AUTHORIZED_KEYS=$MANILA_USER_AUTHORIZED_KEYS
+export DIB_MANILA_USER_USERNAME=$MANILA_USER
+export DIB_MANILA_USER_PASSWORD=$MANILA_PASSWORD
+export DIB_MANILA_USER_AUTHORIZED_KEYS=$MANILA_USER_AUTHORIZED_KEYS
 
 # Build image
 # -----------
diff --git a/elements/manila-ssh/environment.d/50-manila-user b/elements/manila-ssh/environment.d/50-manila-user
new file mode 100755
index 0000000..52ac622
--- /dev/null
+++ b/elements/manila-ssh/environment.d/50-manila-user
@@ -0,0 +1,3 @@
+export DIB_MANILA_USER_USERNAME=${DIB_MANILA_USER_USERNAME:-manila}
+export DIB_MANILA_USER_AUTHORIZED_KEYS=${DIB_MANILA_USER_AUTHORIZED_KEYS:-}
+export DIB_MANILA_USER_PASSWORD=${DIB_MANILA_USER_PASSWORD:-}
\ No newline at end of file
diff --git a/elements/manila-ssh/extra-data.d/50-manila-user b/elements/manila-ssh/extra-data.d/50-manila-user
new file mode 100755
index 0000000..e1e5fcf
--- /dev/null
+++ b/elements/manila-ssh/extra-data.d/50-manila-user
@@ -0,0 +1,13 @@
+#!/bin/bash
+
+if [ ${DIB_DEBUG_TRACE:-0} -gt 0 ]; then
+    set -x
+fi
+set -eu
+set -o pipefail
+
+if [ -n "$DIB_MANILA_USER_AUTHORIZED_KEYS" ]; then
+    if [ -f "$DIB_MANILA_USER_AUTHORIZED_KEYS" ]; then
+        cat $DIB_MANILA_USER_AUTHORIZED_KEYS >> $TMP_HOOKS_PATH/manilauser-ssh-authorized-keys
+    fi
+fi
\ No newline at end of file
diff --git a/elements/manila-ssh/install.d/50-manila-user b/elements/manila-ssh/install.d/50-manila-user
new file mode 100755
index 0000000..fc1ded6
--- /dev/null
+++ b/elements/manila-ssh/install.d/50-manila-user
@@ -0,0 +1,29 @@
+#!/bin/bash
+
+if [ ${DIB_DEBUG_TRACE:-0} -gt 0 ]; then
+    set -x
+fi
+set -eu
+set -o pipefail
+
+useradd -m ${DIB_MANILA_USER_USERNAME}
+set +x
+if [ -n "${DIB_MANILA_USER_PASSWORD}" ]; then
+    echo "Setting password."
+    echo "${DIB_MANILA_USER_USERNAME}:${DIB_MANILA_USER_PASSWORD}" | chpasswd
+fi
+set -x
+
+cat > /etc/sudoers.d/${DIB_MANILA_USER_USERNAME} << EOF
+${DIB_MANILA_USER_USERNAME} ALL=(ALL) NOPASSWD:ALL
+EOF
+chmod 0440 /etc/sudoers.d/${DIB_MANILA_USER_USERNAME}
+visudo -c || rm /etc/sudoers.d/${DIB_MANILA_USER_USERNAME}
+
+
+if [ -f /tmp/in_target.d/devuser-ssh-authorized-keys ]; then
+    mkdir -p /home/${DIB_MANILA_USER_USERNAME}/.ssh
+    cp /tmp/in_target.d/manilauser-ssh-authorized-keys /home/${DIB_MANILA_USER_USERNAME}/.ssh/authorized_keys
+fi
+
+chown -R ${DIB_MANILA_USER_USERNAME}:${DIB_MANILA_USER_USERNAME} /home/${DIB_MANILA_USER_USERNAME}
\ No newline at end of file
diff --git a/elements/manila-ssh/install.d/50-fetch-public-ssh-keys b/elements/manila-ssh/install.d/60-fetch-public-ssh-keys
similarity index 100%
rename from elements/manila-ssh/install.d/50-fetch-public-ssh-keys
rename to elements/manila-ssh/install.d/60-fetch-public-ssh-keys