8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#!/usr/bin/env python3
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek# -*- coding: utf-8 -*-
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek# pyconsole: Example program showing use of console functions
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek# in the lxc python binding
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek# (C) Copyright Oracle. 2013
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek# Authors:
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek# Dwight Engen <dwight.engen@oracle.com>
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek# This library is free software; you can redistribute it and/or
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek# modify it under the terms of the GNU Lesser General Public
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek# License as published by the Free Software Foundation; either
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek# version 2.1 of the License, or (at your option) any later version.
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek# This library is distributed in the hope that it will be useful,
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek# but WITHOUT ANY WARRANTY; without even the implied warranty of
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek# Lesser General Public License for more details.
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek# You should have received a copy of the GNU Lesser General Public
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek# License along with this library; if not, write to the Free Software
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek# USA
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek#
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekimport lxc
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekimport sys
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekimport time
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozekif __name__ == '__main__':
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek ttynum = -1
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek escape = 1
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek if len(sys.argv) < 2:
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek sys.exit("Usage: %s container-name [ttynum [escape]]" % sys.argv[0])
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek if len(sys.argv) > 2:
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek ttynum = int(sys.argv[2])
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek if len(sys.argv) > 3:
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek escape = ord(sys.argv[3]) - ord('a') + 1
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek ct = lxc.Container(sys.argv[1])
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek print("Container:%s tty:%d Ctrl-%c q to quit" %
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek (ct.name, ttynum, ord('a') + escape-1))
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek time.sleep(1)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek if not ct.defined:
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek sys.exit("Container %s not defined" % ct.name)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek if not ct.running:
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek sys.exit("Container %s not running" % ct.name)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek ct.console(ttynum, 0, 1, 2, escape)
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek print("Console done")
8df69bbc58c2f4d3f0b34be9756d9ddf24b1db6dJakub Hrozek