CSIT-649 Add library for creating lxc container
[csit.git] / resources / libraries / python / ssh.py
index 0009bde..db39a07 100644 (file)
@@ -90,7 +90,8 @@ class SSH(object):
 
             logger.debug('Connect peer: {0}'.
                          format(self._ssh.get_transport().getpeername()))
-            logger.debug('Connections: {0}'.format(str(SSH.__existing_connections)))
+            logger.debug('Connections: {0}'.
+                         format(str(SSH.__existing_connections)))
         except:
             if attempts > 0:
                 self._reconnect(attempts-1)
@@ -206,6 +207,29 @@ class SSH(object):
             command = 'sudo -S {c} <<< "{i}"'.format(c=cmd, i=cmd_input)
         return self.exec_command(command, timeout)
 
+    def exec_command_lxc(self, lxc_cmd, lxc_name, lxc_params='', sudo=True,
+                         timeout=30):
+        """Execute command in LXC on a new SSH channel on the connected Node.
+
+        :param lxc_cmd: Command to be executed.
+        :param lxc_name: LXC name.
+        :param lxc_params: Additional parameters for LXC attach.
+        :param sudo: Run in privileged LXC mode. Default: privileged
+        :param timeout: Timeout.
+        :type lxc_cmd: str
+        :type lxc_name: str
+        :type lxc_params: str
+        :type sudo: bool
+        :type timeout: int
+        :return: return_code, stdout, stderr
+        """
+        command = "lxc-attach {p} --name {n} -- /bin/sh -c '{c}'"\
+            .format(p=lxc_params, n=lxc_name, c=lxc_cmd)
+
+        if sudo:
+            command = 'sudo -S {c}'.format(c=command)
+        return self.exec_command(command, timeout)
+
     def interactive_terminal_open(self, time_out=30):
         """Open interactive terminal on a new channel on the connected Node.
 
@@ -271,8 +295,8 @@ class SSH(object):
             except socket.timeout:
                 raise Exception('Socket timeout: {0}'.format(buf))
         tmp = buf.replace(cmd.replace('\n', ''), '')
-        for p in prompt:
-            tmp.replace(p, '')
+        for item in prompt:
+            tmp.replace(item, '')
         return tmp
 
     @staticmethod