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 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 startfield(args): this_hostname = Platform().hostname() config = ConfigDictionary() workdir = config.get('etce', 'WORK_DIRECTORY') workdir = os.getenv('WORKDIR', workdir) if not os.path.exists(workdir): raise DOCKERError('ETCE WORK_DIRECTORY "%s" not found. ' \ 'Please create it before starting.' % workdir) if args.dockerplanfile: dockerplanfile = args.dockerplanfile else: dockerplanfile = os.path.join(workdir, 'dockerplan.xml') plandoc = DOCKERPlanFileDoc(dockerplanfile) # lockfile lockfilename = \ os.path.join(workdir, 'etce.docker.lock') if os.path.isfile(lockfilename): err = 'Detected an active docker field with root at: %s. ' \ 'Run "etce-docker stop" first.' % \ plandoc.docker_root_directory(this_hostname) raise DOCKERError(err) cidr = os.getenv('CIDR', '10.99.0.0/16') containers = [] for hostname, _ in plandoc.hostnames(): for container in plandoc.containers(hostname): for bridgename, interfaceparams in container.interfaces.items(): if IPAddress(interfaceparams['ipv4']) in IPNetwork(cidr): containers.append( (container.docker_name, interfaceparams['ipv4'])) break ipexist = [] for _, ip in containers: ipexist.append(ip) my_ip = '' for ip in IPNetwork(cidr)[1:]: if not str(ip) in ipexist: my_ip = str(ip) break my_ip = my_ip + '/' + cidr.split('/')[1] # write to /etc/hosts in container/machine controller all external ip writehosts(plandoc, containers) hostfile = \ os.path.join(workdir, 'hosts') if not args.dryrun: shutil.copy(dockerplanfile, lockfilename) shutil.copy('/etc/hosts', hostfile) startdockers(plandoc, args.writehosts, args.forcedockerroot, args.dryrun) other_hosts = [] for hostname, ip in plandoc.hostnames(): if hostname != (this_hostname and 'localhost'): other_hosts.append(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, password=args.password) # push the file and execute client.put(dockerplanfile, '.', other_hosts, doclobber=True) # push the file client.put('/etc/hosts', '.', other_hosts, doclobber=True) # on the destination node the netplan file gets pushed to the # ETCE WORK_DIRECTORY command = 'dockermanager startdockers %s writehosts=%s forcedockerroot=%s' \ % (os.path.basename(dockerplanfile), args.writehosts, args.forcedockerroot) ret = client.execute(command, other_hosts) for k in ret: print '[%s] return: %s' % (k, ret[k].retval['result']) finally: if client: client.close() # A valid ETCE Test Directory. TESTDIRECTORY = os.path.join(workdir, 'pub-tdmact') # The output directory to place the built Test Directory. TESTROOT = os.path.join( workdir, TESTDIRECTORY + '_' + etce.utils.timestamp()) os.system('etce-test publish %s %s --verbose' % (TESTDIRECTORY, TESTROOT)) # A user tag to prepend to the name of each test result directory. TESTPREFIX = 'tdmact' # Run scenario order steps #if not args.collect: os.system( 'etce-test run --user root --policy autoadd -v --kill before --nocollect %s %s %s' % (TESTPREFIX, HOSTFILE, TESTROOT))
def stopfield(args): workdir = ConfigDictionary().get('etce', 'WORK_DIRECTORY') workdir = os.getenv('WORKDIR', workdir) lockfilename = os.path.join(workdir, 'etce.docker.lock') if not os.path.exists(lockfilename) or not os.path.isfile(lockfilename): raise DOCKERError('Lockfile "%s" not found. Quitting.' % lockfilename) if args.dockerplanfile: dockerplanfile = args.dockerplanfile else: dockerplanfile = os.path.join(workdir, 'dockerplan.xml') plandoc = DOCKERPlanFileDoc(dockerplanfile) this_hostname = Platform().hostname() other_hosts = [] for hostname, ip in plandoc.hostnames(): if hostname != (this_hostname and 'localhost'): other_hosts.append(hostname) # stop containers on other hosts, if any try: if other_hosts: if args.collect: client_nodes = None try: print 'Collecting results.' time = 'collect_on_%s' % etce.timeutils.getstrtimenow( ).split('.')[0] localtestresultsdir = os.path.join(workdir, 'data', time) field = Field(os.path.join(workdir, 'HOSTFILE')) # root nodes host the filesystem for all of the virtual nodes attached filesystemnodes = list(field.roots()) testdir = 'data' client_nodes = ClientBuilder().build( filesystemnodes, user=args.user, port=args.port, password=args.password, policy=args.policy) try: client_nodes.collect(testdir, localtestresultsdir, filesystemnodes) except: pass finally: if client_nodes: client_nodes.close() client = None try: client = ClientBuilder().build(other_hosts, user=args.user, port=args.port, password=args.password) # 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 = 'dockermanager stopdockers %s' % os.path.basename( dockerplanfile) 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: # os.system('ip link del vxlan1') stopdockers(plandoc) os.system('rm -f %s' % lockfilename)