def test_split_sections(self): script = """#! switch [*] section1 #! switch [os=linux] section 2 #! switch [os=windows] #! /usr/bin/python echo "#! switch [some_blabla] \ hostname > test.sh" """ res = parser.split_sections(script) self.assertCount(res, 7) self.assertEqual(res[0], "", res) self.assertEqual(res[1], "#! switch [*]", res) self.assertEqual(res[2], "\n\nsection1\n\n") self.assertEqual(res[3], "#! switch [os=linux]") self.assertEqual(res[4], "\n\nsection 2\n\n") self.assertEqual(res[5], "#! switch [os=windows]") self.assertEqual(res[6], """ #! /usr/bin/python echo "#! switch [some_blabla] \ hostname > test.sh" """) self.assertEqual(parser.parse_selectors(res[1]), ("*", "")) self.assertEqual(parser.parse_selectors(res[2]), (None, None))
def test_parse_selectors(self): test_sel1 = "host22" test_args1 = "arg1 arg2 arg3" res = parser.parse_selectors( self.selector_template.format(test_sel1, test_args1) ) self.assertEqual(res[0], test_sel1) self.assertEqual(res[1], test_args1) res = parser.parse_selectors("some broken selector") self.assertIsNone(res[0]) self.assertIsNone(res[1])
def __init__(self, ctx, worker_sock_uri, repl_uri, script, run_event, timeout=None, host_resolver=None, **kwargs): self.session_id = uuid.uuid4().hex self.worker_sock = ctx.socket(zmq.DEALER) self.worker_sock.setsockopt(zmq.IDENTITY, self.session_id) self.worker_sock.connect(worker_sock_uri) self.context = ctx self.script = script self.kwargs = kwargs self.run_event = run_event self.ctx = ctx self.reply_sock = ctx.socket(zmq.DEALER) self.reply_sock.connect(repl_uri) self.host_resolver = host_resolver super(Session, self).__init__() self.sections = parser.split_sections(script) if not self.sections: raise Exception("Invalid request, no executable sections found") self.steps = [] self.timeout = timeout or 10 local_run = self.sections.pop(0) if local_run.strip(): self.local_script = local_run else: self.local_script = '' for i in range(0, len(self.sections), 2): targets, section = self.sections[i], self.sections[i + 1] target_str = parser.parse_selectors(targets)[0] targets = target_str.split() self.steps.append((target_str, targets, section, kwargs.get("includes")))
if self.args.name: req.append(caller=self.args.name) if self.args.test: req.append(test=True) req.append(timeout=self.timeout) script_content = parser.CRN_SHEBANG.sub("", script_content) sections = parser.split_sections(script_content) if not sections: return first_section = sections[0] if not parser.parse_selectors(first_section.strip())[0]: if first_section.strip(): # has content? # Local run console.green("=" * 80) console.green("Running local script", bold=1) console.green("=" * 80) from cloudrunner.core.process import Processor processor = Processor("@") lang = parser.parse_lang(first_section) proc_iter = iter(processor.run(first_section, lang, self.env)) proc = next(proc_iter) if not isinstance(proc, list): while True: try: to_read = proc.select(.2) if proc.poll() is not None: