cli: implement stopall
This commit is contained in:
parent
f5c6fe9c2b
commit
7e8a76e9cf
|
@ -6,6 +6,7 @@ import dspawn
|
|||
import logging
|
||||
from dspawn.container import Machine
|
||||
from dspawn.container import logger
|
||||
from dspawn.manager import Manager
|
||||
|
||||
machinectl_actions = ['list', 'list-images', 'start', 'stop', 'show',
|
||||
'shell', 'remove', 'login', 'enable', 'disable',
|
||||
|
@ -15,7 +16,8 @@ machinectl_actions = ['list', 'list-images', 'start', 'stop', 'show',
|
|||
def main():
|
||||
parser = argparse.ArgumentParser(description=dspawn.__doc__)
|
||||
parser.add_argument('action',
|
||||
choices=['create', 'config'] + machinectl_actions,
|
||||
choices=['create', 'config', 'stopall']
|
||||
+ machinectl_actions,
|
||||
help='which task shall we perform?')
|
||||
parser.add_argument('name',
|
||||
nargs='*',
|
||||
|
@ -66,5 +68,9 @@ def main():
|
|||
ar = ''
|
||||
os.system('machinectl %s %s' % (args.action, ar))
|
||||
|
||||
if args.action == 'stopall':
|
||||
mgr = Manager(**vars(args))
|
||||
mgr.stopall()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
|
@ -22,7 +22,8 @@ default_configuration = [
|
|||
jessie_fix = [
|
||||
'echo "deb http://ftp.debian.org/debian jessie-backports main" >'
|
||||
'/etc/apt/sources.list.d/backports.list',
|
||||
'apt update; apt install -y -t jessie-backports dbus systemd systemd-sysv']
|
||||
'apt update; apt install -y -t jessie-backports dbus systemd systemd-sysv'
|
||||
]
|
||||
|
||||
pub_keys = glob.glob('/home/*/.ssh/id_rsa.pub') + \
|
||||
glob.glob('/root/.ssh/id_rsa.pub') + \
|
||||
|
@ -38,7 +39,7 @@ def use_aptcacherng():
|
|||
|
||||
|
||||
class Machine(object):
|
||||
def __init__(self, name, release, mode='private', address=None,
|
||||
def __init__(self, name, release='stable', mode='private', address=None,
|
||||
proxy=False, macaddress=None, privateoff=False, **kwargs):
|
||||
|
||||
if isinstance(name, list):
|
||||
|
@ -82,6 +83,12 @@ class Machine(object):
|
|||
def start(self):
|
||||
os.system('machinectl start %s' % self.name)
|
||||
|
||||
def stop(self):
|
||||
os.system('machinectl poweroff %s' % self.name)
|
||||
|
||||
def terminate(self):
|
||||
os.system('machinectl terminate %s' % self.name)
|
||||
|
||||
def create(self):
|
||||
if self.exist():
|
||||
logger.warning('container "%s" already exists' % self.name)
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
#!/usr/bin/env python
|
||||
from dspawn.container import Machine
|
||||
import subprocess
|
||||
import shlex
|
||||
import time
|
||||
|
||||
|
||||
class Manager:
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.machines = []
|
||||
self.get_list()
|
||||
|
||||
def get_list(self):
|
||||
ll = subprocess.check_output(shlex.split('machinectl list'))
|
||||
for l in ll.decode().splitlines():
|
||||
if 'container' in l:
|
||||
n = l.split()[0]
|
||||
m = Machine(n)
|
||||
self.machines.append(m)
|
||||
|
||||
def stopall(self):
|
||||
for m in self.machines:
|
||||
print(m.name)
|
||||
m.stop()
|
||||
time.sleep(1)
|
||||
for m in self.machines:
|
||||
m.terminate()
|
Loading…
Reference in New Issue