def startfield(args): this_hostname = Platform().hostname() plandoc = LXCPlanFileDoc(args.lxcplanfile) config = ConfigDictionary() workdir = config.get('etce', 'WORK_DIRECTORY') if not os.path.exists(workdir): raise LXCError('ETCE WORK_DIRECTORY "%s" not found. ' \ 'Please create it before starting.' % workdir) # lockfile lockfilename = \ os.path.join(plandoc.lxc_root_directory(this_hostname), 'etce.lxc.lock') if os.path.isfile(lockfilename): err = 'Detected an active lxc field with root at: %s. ' \ 'Run "etce-lxc stop" first.' % \ plandoc.lxc_root_directory(this_hostname) raise LXCError(err) startlxcs(plandoc, args.writehosts, args.forcelxcroot, args.dryrun) if not args.dryrun: shutil.copy(args.lxcplanfile, lockfilename) other_hosts = set(plandoc.hostnames()).difference( ['localhost', this_hostname]) # start containers on other hosts, if any if other_hosts: client = None try: client = ClientBuilder().build(\ other_hosts, user=args.user, port=args.port) # push the file and execute client.put(args.lxcplanfile, '.', other_hosts, doclobber=True) # on the destination node the netplan file gets pushed to the # ETCE WORK_DIRECTORY command = 'lxcmanager startlxcs %s writehosts=%s forcelxcroot=%s' \ % (os.path.basename(args.lxcplanfile), args.writehosts, args.forcelxcroot) ret = client.execute(command, other_hosts) for k in ret: print '[%s] return: %s' % (k, ret[k].retval['result']) finally: if client: client.close()
def stoplxcs(lxcplan): lxcplanfiledoc = lxcplan if not type(lxcplan) == LXCPlanFileDoc: # assume file name lxcplanfiledoc = LXCPlanFileDoc(lxcplan) try: LXCManagerImpl().stop(lxcplanfiledoc) except Exception as e: raise LXCError(e.message)
def stopfield(args): workdir = ConfigDictionary().get('etce', 'WORK_DIRECTORY') lockfilename = os.path.join(workdir, 'lxcroot', 'etce.lxc.lock') if not os.path.exists(lockfilename) or not os.path.isfile(lockfilename): raise LXCError('Lockfile "%s" not found. Quitting.' % lockfilename) plandoc = LXCPlanFileDoc(lockfilename) other_hosts = set(plandoc.hostnames()).difference( ['localhost', Platform().hostname()]) # stop containers on other hosts, if any try: if other_hosts: client = None try: client = ClientBuilder().build(other_hosts, user=args.user, port=args.port) # push the file and execute client.put(lockfilename, '.', other_hosts, doclobber=True) # on the destination node the netplan file gets pushed to the # ETCE WORK_DIRECTORY command = 'lxcmanager stoplxcs %s' % os.path.basename( lockfilename) ret = client.execute(command, other_hosts) for k in ret: print('[%s] return: %s' % (k, ret[k].retval['result'])) finally: if client: client.close() finally: stoplxcs(plandoc)
def startlxcs(lxcplan, writehosts=False, dryrun=False): lxcplanfiledoc = lxcplan if not type(lxcplan) == LXCPlanFileDoc: # assume file name lxcplanfiledoc = LXCPlanFileDoc(lxcplan) try: LXCManagerImpl().start(lxcplanfiledoc, writehosts=writehosts, dryrun=dryrun) except Exception as e: raise LXCError(e.message)