From 83077870596e2747cde448f9bfef0d88269ef21f Mon Sep 17 00:00:00 2001
From: Nikos Skalkotos <skalkoto@grnet.gr>
Date: Wed, 22 Oct 2014 17:42:29 +0300
Subject: [PATCH] Create REMOTE_CONNECTION property for Linux
For now only ssh is detected
---
image_creator/os_type/__init__.py | 6 +++
image_creator/os_type/linux.py | 72 +++++++++++++++++++++++++++++-
image_creator/os_type/slackware.py | 12 +++++
3 files changed, 89 insertions(+), 1 deletion(-)
diff --git a/image_creator/os_type/__init__.py b/image_creator/os_type/__init__.py
index 2af3d04..818ad65 100644
--- a/image_creator/os_type/__init__.py
+++ b/image_creator/os_type/__init__.py
@@ -517,6 +517,8 @@ class OSBase(object):
http://libguestfs.org/guestfs.3.html#guestfs_readdir
* exclude: Exclude all files that follow this pattern.
+
+ * include: Only include files that follow this pattern.
"""
if not self.image.g.is_dir(directory):
self.out.warn("Directory: `%s' does not exist!" % directory)
@@ -531,6 +533,7 @@ class OSBase(object):
kargs['maxdepth'] = maxdepth
exclude = None if 'exclude' not in kargs else kargs['exclude']
+ include = None if 'include' not in kargs else kargs['include']
ftype = None if 'ftype' not in kargs else kargs['ftype']
has_ftype = lambda x, y: y is None and True or x['ftyp'] == y
@@ -543,6 +546,9 @@ class OSBase(object):
if exclude and re.match(exclude, full_path):
continue
+ if include and not re.match(include, full_path):
+ continue
+
if has_ftype(f, 'd'):
self._foreach_file(full_path, action, **kargs)
diff --git a/image_creator/os_type/linux.py b/image_creator/os_type/linux.py
index c5b82a0..26cb735 100644
--- a/image_creator/os_type/linux.py
+++ b/image_creator/os_type/linux.py
@@ -300,13 +300,83 @@ class Linux(Unix):
def _do_collect_metadata(self):
"""Collect metadata about the OS"""
super(Linux, self)._do_collect_metadata()
- self.meta["USERS"] = " ".join(self._get_passworded_users())
+ users = self._get_passworded_users()
+ self.meta["USERS"] = " ".join(users)
# Delete the USERS metadata if empty
if not len(self.meta['USERS']):
self.out.warn("No passworded users found!")
del self.meta['USERS']
+ if self.is_enabled('sshd'):
+ ssh = []
+ opts = self.ssh_connection_options(users)
+ for user in opts['users']:
+ ssh.append("ssh:port=%d,user=%s" % (opts['port'], user))
+
+ if 'REMOTE_CONNECTION' not in self.meta:
+ self.meta['REMOTE_CONNECTION'] = ""
+ else:
+ self.meta['REMOTE_CONNECTION'] += " "
+
+ if len(ssh):
+ self.meta['REMOTE_CONNECTION'] += " ".join(ssh)
+ else:
+ self.meta['REMOTE_CONNECTION'] += "ssh:port=%d" % opts['port']
+ else:
+ self.out.warn("OpenSSH Daemon is not configured to run on boot")
+
+ def is_enabled(self, service):
+ """Check if a service is enabled to run on boot"""
+
+ systemd_services = '/etc/systemd/system/multi-user.target.wants'
+ exec_start = re.compile(r'^\s*ExecStart=.+bin/%s\s?' % service)
+ if self.image.g.is_dir(systemd_services):
+ for entry in self.image.g.readdir(systemd_services):
+ if entry['ftyp'] not in ('l', 'f'):
+ continue
+ service_file = "%s/%s" % (systemd_services, entry['name'])
+ for line in self.image.g.cat(service_file).splitlines():
+ if exec_start.search(line):
+ return True
+
+ found = set()
+
+ def check_file(path):
+ regexp = re.compile(r"[/=\s'\"]%s('\")?\s" % service)
+ for line in self.image.g.cat(path).splitlines():
+ line = line.split('#', 1)[0].strip()
+ if len(line) == 0:
+ continue
+ if regexp.search(line):
+ found.add(path)
+ return
+
+ # Check upstart config files under /etc/init
+ # Only examine *.conf files
+ if self.image.g.is_dir('/etc/init'):
+ self._foreach_file('/etc/init', check_file, maxdepth=1,
+ include='.+\.conf$')
+ if len(found):
+ return True
+
+ # Check scripts under /etc/rc[1-5].d/ and /etc/rc.d/rc[1-5].d/
+ for conf in ["/etc/%src%d.d" % (d, i) for i in xrange(1, 6)
+ for d in ('', 'rc.d/')]:
+ try:
+ for entry in self.image.g.readdir(conf):
+ if entry['ftyp'] not in ('l', 'f'):
+ continue
+ check_file("%s/%s" % (conf, entry['name']))
+
+ if len(found):
+ return True
+
+ except RuntimeError:
+ continue
+
+ return False
+
def _get_passworded_users(self):
"""Returns a list of non-locked user accounts"""
users = []
diff --git a/image_creator/os_type/slackware.py b/image_creator/os_type/slackware.py
index 6914e02..475f69f 100644
--- a/image_creator/os_type/slackware.py
+++ b/image_creator/os_type/slackware.py
@@ -32,4 +32,16 @@ class Slackware(Linux):
self._foreach_file('/var/log', self.image.g.truncate, ftype='r',
exclude='/var/log/packages')
+ def is_enabled(self, service):
+ """Check if a service is enabled to start on boot"""
+
+ name = '/etc/rc.d/%s' % service
+ # In slackware a service will be executed during boot if the
+ # execute bit is set for the root
+ if self.image.g.is_file(name):
+ return self.image.g.stat(name)['mode'] & 0400
+
+ self.out.warn('Service %s not found on the media' % service)
+ return False
+
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
--
GitLab