summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRadostin Stoyanov <rstoyanov1@gmail.com>2017-08-26 21:42:01 +0100
committerRadostin Stoyanov <rstoyanov1@gmail.com>2017-08-28 16:00:57 +0100
commit9302266b220db454bdf517cbc59727d9fd482915 (patch)
treea09602382ad0986cece6b6589385ace6eb006ceb /src
parent9844b2696efa015c0fd80686459d6d4a65f8f677 (diff)
downloadvirt-bootstrap.git-9302266b220db454bdf517cbc59727d9fd482915.tar.gz
virt-bootstrap.git-9302266b220db454bdf517cbc59727d9fd482915.tar.xz
virt-bootstrap.git-9302266b220db454bdf517cbc59727d9fd482915.zip
Make set_root_password_in_rootfs reusable
Make the code for setting password hash in the content of shadow file reusable and hence can be used with qcow2 output format.
Diffstat (limited to 'src')
-rw-r--r--src/virtBootstrap/utils.py22
1 files changed, 15 insertions, 7 deletions
diff --git a/src/virtBootstrap/utils.py b/src/virtBootstrap/utils.py
index 96ddab4..ea650fb 100644
--- a/src/virtBootstrap/utils.py
+++ b/src/virtBootstrap/utils.py
@@ -366,6 +366,19 @@ def str2float(element):
return None
+def set_password_in_shadow_content(shadow_content, password, user='root'):
+ """
+ Find a user the content of shadow file and set a hash of the password.
+ """
+ for index, line in enumerate(shadow_content):
+ if line.startswith(user):
+ line_split = line.split(':')
+ line_split[1] = passlib.hosts.linux_context.hash(password)
+ shadow_content[index] = ':'.join(line_split)
+ break
+ return shadow_content
+
+
def set_root_password_in_rootfs(rootfs, password):
"""
Set password on the root user within root filesystem
@@ -380,15 +393,10 @@ def set_root_password_in_rootfs(rootfs, password):
with open(shadow_file) as orig_file:
shadow_content = orig_file.read().split('\n')
- for index, line in enumerate(shadow_content):
- if line.startswith('root'):
- line_split = line.split(':')
- line_split[1] = passlib.hosts.linux_context.hash(password)
- shadow_content[index] = ':'.join(line_split)
- break
+ new_content = set_password_in_shadow_content(shadow_content, password)
with open(shadow_file, "w") as new_file:
- new_file.write('\n'.join(shadow_content))
+ new_file.write('\n'.join(new_content))
except Exception:
raise