Exemplo n.º 1
0
def check_template(tmplstr, tmpldict=None):
    """Checks the given template string against the given template variable
    dictionary. Returns a list of variables not provided in the given
    dictionary."""
    def check_tmplitems(items, tmpldict, topkey=""):
        missing = []
        for key, val in items:
            if type(val) == model.Dictionary:
                missing += check_tmplitems(
                    val.items(), tmpldict.get(key, {}),
                    key if not topkey else "%s%s%s" % (topkey, KEYSEP, key))
            else:
                name = key if not topkey else "%s%s%s" % (topkey, KEYSEP, key)
                try:
                    if key not in tmpldict.keys():
                        missing.append(name)
                except:
                    qprompt.warn("Issue checking var `%s`!" % (name))
        return missing

    tmpldict = tmpldict or {}
    try:
        missing = check_tmplitems(infer(tmplstr).items(), tmpldict)
    except:
        missing = []
    return missing
Exemplo n.º 2
0
 def gettemplate(self, template):
     try:
         templat = self.jinja_template_dir + template + '.j2'
         res = self.opentemplate(templat)
         try:
             schema = infer(res)
             js_schema = to_json_schema(schema)
         except Exception:
             js_schema = "error reading schema"
         resultdata = {
                 'status': 'success',
                 'data': {
                     "task_result": {
                         "template_schema": js_schema,
                         "template_data": res
                     }
                 }
         }
         return resultdata
     except Exception as e:
         resultdata = {
                 'status': 'error',
                 'data': str(e)
         }
         return resultdata
Exemplo n.º 3
0
def is_template(documento):
    doc = DocxTemplate(MEDIA_ROOT + documento)
    xml = doc.get_xml()
    xml = doc.patch_xml(xml)  # patch xml for jinja2schema
    variables = jinja2schema.infer(xml)
    list_xml = list(variables.keys())  # create a nice list
    return list_xml
Exemplo n.º 4
0
    def get(self, case_name):
        case_path = os.path.join(consts.TESTCASE_DIR,
                                 '{}.yaml'.format(case_name))

        try:
            with open(case_path) as f:
                data = f.read()
        except IOError as e:
            if e.errno == errno.ENOENT:
                return result_handler(consts.API_ERROR, 'case does not exist')

        options = {
            k: {
                'description': '',
                'type': v.__class__.__name__
            }
            for k, v in jinja2schema.infer(data).items()
        }
        # [('segmentation_id', < scalar >), ('image', < string >), ('provider', < scalar >),
        # ('physical_network', < string >), ('packetsize', < number >)]
        for k, v in options.items():
            if k == 'segmentation_id':
                options[k]['type'] = 'Number'
            if k == 'provider':
                options[k]['type'] = 'String'
        return result_handler(consts.API_SUCCESS, {
            'testcase': data,
            'args': options
        })
Exemplo n.º 5
0
def _process(jinjafile, sample_or_schema=True):
    #click.open_file(jinjafile)
    #if sys.stdin.isatty():
    if jinjafile is None:
        lines = sys.stdin.readlines()
        sample = infer('\n'.join(lines))
        if sample_or_schema is True:
            print(sample)
        else:
            print(to_json_schema(sample))
    else:
        with click.open_file(jinjafile) as f:
            lines = f.readlines()
            sample = infer('\n'.join(lines))
            if sample_or_schema is True:
                print(sample)
            else:
                print(to_json_schema(sample))
Exemplo n.º 6
0
def read_vars(file_path):
    f = open(file_path, 'rb')
    document = Document(f)
    f.close()

    fullText = []
    for para in document.paragraphs:
        fullText.append(para.text)
    temp = '\n'.join(fullText)

    variables = jinja2schema.infer(temp)
    return variables.keys()
Exemplo n.º 7
0
 def get_vars(self):
     # output a list of all variable/fields in the template
     xml = self.get_xml()
     xml = self.patch_xml(xml)
     #Now add the header/footer sections
     # Headers
     headerxml = ""
     for relKey, hxml in self.get_headers_footers_xml(self.HEADER_URI):
         headerxml = headerxml + self.patch_xml(hxml)
     footerxml = ""
     # Footers
     for relKey, fxml in self.get_headers_footers_xml(self.FOOTER_URI):
         footerxml = footerxml + self.patch_xml(fxml)
     return (infer(headerxml + xml + footerxml))
Exemplo n.º 8
0
    def tsumiki(self, line=None, cell=None):
        """
        Write with mixed markup langueges.

        Parameters
        ----------
        -r : render to jinja2 template.

        Examples
        --------

        %%tsumiki

        :Markdown:
        # Title
        * list1
        * list2

        :Markdown::
        ## Sub Title1
        * list3
        * list4

        :HTML::
        <h2>Sub Title2</h2>
        <ul>
          <li>list5</li>
          <li>list6</li>
        </ul>
        """

        opts, args = self.parse_options(line, "r", mode="list")
        if "r" in opts:
            import jinja2
            from jinja2schema import infer

            user_ns = self.shell.user_ns
            template_params = {}
            for x in infer(cell).keys():
                if x in user_ns:
                    template_params[x] = user_ns[x]

            template = jinja2.Template(cell)
            text = template.render(**template_params)
        else:
            text = cell

        tsumiki = Tsumiki(text)
        return display.HTML(tsumiki.html)
Exemplo n.º 9
0
    def schema(self) -> Dict[str, Any]:
        custom_tokens = [
            r"{% include .* %}",
            *[rf"(\| ?)?{i}(\(.*\))?" for i in self.custom_filters],
        ]

        with open(self.source_path, "r") as stream:
            return to_json_schema(
                infer(
                    reduce(
                        lambda acc, x: re.sub(x, "", acc),
                        custom_tokens,
                        stream.read(),
                    )
                )
            )
Exemplo n.º 10
0
def beforeUpdate(record):
    if record.template != '' and record.template != 'null' and (
            not record.data or record.data == '{}'):
        #        print to_json_schema(infer(open('templates/' + record.template, 'r').read()))
        print('NEW DATA')
        schema = to_json_schema(
            infer(
                open(
                    '/home/coins/flask/exchange/app/templates/' +
                    record.template, 'r').read()))
        record.schema = schema
        vars = schema['properties']
        data = {}
        for k, v in vars.items():
            data[k] = ''
        record.data = data
    return
Exemplo n.º 11
0
def schema():
    template = flask.request.get_data()
    try:
        config = jinja2schema.Config(BOOLEAN_CONDITIONS=True)
        struct = jinja2schema.infer(template, config)
    except jinja2schema.InferException as e:
        return flask.jsonify({
            'message': u'jinj2schema error: {}'.format(e),
        }), 400
    except jinja2.TemplateSyntaxError as e:
        return flask.jsonify({
            'message': u'Jinja2 error: {}, line {}'.format(e.message, e.lineno),
        }), 400
    else:
        json_schema = jinja2schema.to_json_schema(struct,
                                                  jsonschema_encoder=jinja2schema.StringJSONSchemaDraft4Encoder)
        return flask.jsonify({
            'schema': json_schema,
        })
Exemplo n.º 12
0
def schema():
    template = flask.request.get_data()
    try:
        config = jinja2schema.Config(BOOLEAN_CONDITIONS=True)
        struct = jinja2schema.infer(template, config)
    except jinja2schema.InferException as e:
        return flask.jsonify({
            'message': u'jinj2schema error: {}'.format(e),
        }), 400
    except jinja2.TemplateSyntaxError as e:
        return flask.jsonify({
            'message':
            u'Jinja2 error: {}, line {}'.format(e.message, e.lineno),
        }), 400
    else:
        json_schema = jinja2schema.to_json_schema(
            struct,
            jsonschema_encoder=jinja2schema.StringJSONSchemaDraft4Encoder)
        return flask.jsonify({
            'schema': json_schema,
        })
Exemplo n.º 13
0
def render_ansible_template(templ):
    template = _macro_stubs + templ.content
    result = infer(template)

    def wrap_stub_value(variable):
        """ Usually needs to wrap iterable objects, as an example:
        {% for country,host in sisu_hosts.iteritems()|sort %}{{ host }},{% endfor %}
        """
        if isinstance(variable, model.Tuple):
            return (wrap_stub_value(sub_value) for sub_value in variable.items)

        if isinstance(variable, model.List):
            return [wrap_stub_value(variable.item)]

        return StubValue(variable)

    # try to render ansible template
    jinja_template = AnsibleJ2Template(template)
    return jinja_template.render(
        {key: wrap_stub_value(value)
         for key, value in result.items()})
Exemplo n.º 14
0
    def makeDict(self, name, source=None):
        if name != "":
            try:
                data = open(os.path.join("szablony", name + ".txt"),
                            "r",
                            encoding="utf-8")
            except FileNotFoundError:
                data = open(os.path.join(source), "r", encoding="utf-8")
            plaincode = data.read()
            schema = infer(plaincode)
            #schema = (json.dumps(schema,indent=3))
            for item in schema.keys():
                schema.__setitem__(item, "TX")

            try:
                f = open(os.path.join("szablony", name + ".txt"),
                         'r+',
                         encoding='utf-8')
            except FileNotFoundError:
                f = open(os.path.join(source), 'r+', encoding='utf-8')
            f.seek(0, 0)
            firstline = f.readline()
            if firstline.startswith('###') or firstline.startswith('{'):
                hardcodeddict = re.search(r'(?<=\{)(.*?)(?=\})', firstline)
                if hardcodeddict != None:
                    decodeddict = dict(
                        ast.literal_eval('{' + hardcodeddict.group() + '}'))
                    diff = set(schema.keys()) - set(decodeddict.keys())
                    #print(diff,list(diff))
                    if not list(diff):
                        return decodeddict
                    else:
                        print('dodano zmnienno')
                        for item in diff:
                            decodeddict[item] = "TX"
                        return decodeddict

            return dict(schema)
        pass
Exemplo n.º 15
0
    def get(self, case_name):
        case_path = os.path.join(consts.TESTCASE_DIR,
                                 '{}.yaml'.format(case_name))

        try:
            with open(case_path) as f:
                data = f.read()
        except IOError as e:
            if e.errno == errno.ENOENT:
                return result_handler(consts.API_ERROR, 'case does not exist')

        options = {
            k: {
                'description': '',
                'type': v.__class__.__name__
            }
            for k, v in jinja2schema.infer(data).items()
        }

        return result_handler(consts.API_SUCCESS, {
            'testcase': data,
            'args': options
        })
Exemplo n.º 16
0
 def getUndeclaredTypes(self):
     template_source = self.templateEnv.loader.get_source(
         self.templateEnv, 'example.js')[0]
     parsed_content = self.templateEnv.parse(template_source)
     return infer(open('example.js').read())
Exemplo n.º 17
0
def get_label_template_vars():
    with open("templates/labels/" + session['labelsvg']) as template_file:
        fields = list(sorted(jinja2schema.infer(template_file.read()).keys()))

    return fields
Exemplo n.º 18
0
class TestEngine:
    def __init__(self):
        '''
        data = {}
        headers_data = TestOauthRequest()
        authorization = headers_data[u'token_type'] + " " + headers_data[u'access_token']
        headers = {
            'content-type': "application/json",
            'tenant': "tenant_test",
            'authorization': authorization,
        }
        self.headers = headers
        #self.request_list["put"] = TestPutRequest
        #self.request_list["get"] = TestGetRequest
        #self.request_list["delete"] = TestDeleteRequest
        '''
        self.request_list = {}
        self.request_list["post"] = TestPostRequest
        self.request_list["put"] = TestPutRequest
        self.request_list["get"] = TestGetRequest
        self.request_list["delete"] = TestDeleteRequest
        self.request_list["patch"] = TestPatchRequest
        self.response_data = {}

    def execute(self, storys):
        for cases in storys:
            self.docase(cases)

    def docase(self, cases):
        for case in cases.steps:
            self.step_execute(cases, case, case.step_name, case.request_type,
                              case.request_url, case.request_data_type,
                              case.request_data, case.expect_code,
                              case.expect_value, case.var_getvalue)

    def step_execute(self, cases, case, step_name, request_type, request_url,
                     request_data_type, request_data, expect_result,
                     expect_value, var_getvalue):
        if len(infer(request_data[0]).items()) <> 0:
            template = Template(request_data[0])
            key = infer(request_data[0]).items()[0][0]
            t = dict()
            t.__setitem__(key, cases.env[key])
            request_data[0] = template.render(t)
            # print request_data[0]
        if len(infer(request_url).items()) <> 0:
            template = Template(request_url)
            key = infer(request_url).items()[0][0]
            t = dict()
            if key in cases.env:
                t.__setitem__(key, cases.env[key])
                request_url = template.render(t)
                # print request_url

        if request_type == None:
            return
        # status, content = self.request_list[request_type.encode('ascii')](request_url, "path", "test")
        status, content = self.request_list[request_type](request_url,
                                                          cases.headers,
                                                          request_data_type,
                                                          request_data)
        # print "status=", status
        # print "content:", content
        content_data = json.loads(content)
        if status != 200:
            error = content_data[u'message'].encode('utf-8')
            print "\033[31;1m[FAILED] %s" % step_name + "请求失败,错误信息为:" + str(
                status) + "," + error
            return
        # print(content_data[u'result'])
        # print(content_data[u'result'] == expect_result)
        if 200 == status and content_data[u'result'] == expect_result:
            print "\033[32;1m[SUCCESS] %s" % step_name
            # 获取出参
            for val in case.var_getvalue:
                if val == "{}" or val == "" or val == None:
                    continue
                t = eval(val)
                result = self.get_content(content, t.items()[0][1])
                cases.env[t.items()[0][0]] = result
                # print(cases.env)
            # 检查入参

            for val in case.expect_value:
                if val == "{}" or val == "" or val == None:
                    continue
                t = eval(val)
                if self.check_content(content,
                                      t.items()[0][1],
                                      t.items()[0][0]):
                    print "\033[32;1m[SUCCESS] %s" % t.items()[0][0]
                else:
                    print "\033[31;1m[FAILED] %s" % t.items()[0][0]
            print cases.env
        else:
            print "\033[31;1m[FAILED] %s" % step_name + content_data[u'message']
        print request_url
Exemplo n.º 19
0
def processChange(change, changeGroup, operation, parentChange={}, parentChangeGroup={}, depth=0, rollerScript=None, data={}):
  if "name" in change:
    name=change["name"]

  if "name" in changeGroup:
    groupName=changeGroup["name"]

  if "target" in change:
    target=change["target"]
  elif "target" in changeGroup:
    target=changeGroup["target"]
  elif "target" in parentChange:
    target=parentChange["target"]
  elif "target" in parentChangeGroup:
    target=parentChangeGroup["target"]
  else:
    target=None

  if "deploy" in change:
    deploy=change["deploy"]
  elif "deploy" in changeGroup:
    deploy=changeGroup["deploy"]
  elif "deploy" in parentChange:
    deploy=parentChange["deploy"]
  elif "deploy" in parentChangeGroup:
    deploy=parentChangeGroup["deploy"]
  else:
    deploy=None

  if "rollback" in change:
    rollback=change["rollback"]
  elif "rollback" in changeGroup:
    rollback=changeGroup["rollback"]
  elif "rollback" in parentChange:
    rollback=parentChange["rollback"]
  elif "rollback" in parentChangeGroup:
    rollback=parentChangeGroup["rollback"]
  else:
    rollback=None

  if "capture" in change:
    capture=change["capture"]
  elif "capture" in changeGroup:
    capture=changeGroup["capture"]
  elif "capture" in parentChange:
    capture=parentChange["capture"]
  elif "capture" in parentChangeGroup:
    capture=parentChangeGroup["capture"]
  else:
    capture=None

  if "deploySuccessIf" in change:
    deploySuccessIf=change["deploySuccessIf"]
  elif "deploySuccessIf" in changeGroup:
    deploySuccessIf=changeGroup["deploySuccessIf"]
  elif "deploySuccessIf" in parentChange:
    deploySuccessIf=parentChange["deploySuccessIf"]
  elif "deploySuccessIf" in parentChangeGroup:
    deploySuccessIf=parentChangeGroup["deploySuccessIf"]
  else:
    deploySuccessIf=None

  if "rollbackSuccessIf" in change:
    rollbackSuccessIf=change["rollbackSuccessIf"]
  elif "rollbackSuccessIf" in changeGroup:
    rollbackSuccessIf=changeGroup["rollbackSuccessIf"]
  elif "rollbackSuccessIf" in parentChange:
    rollbackSuccessIf=parentChange["rollbackSuccessIf"]
  elif "rollbackSuccessIf" in parentChangeGroup:
    rollbackSuccessIf=parentChangeGroup["rollbackSuccessIf"]
  else:
    rollbackSuccessIf=None

  if "deploySkipIf" in change:
    deploySkipIf=change["deploySkipIf"]
  elif "deploySkipIf" in changeGroup:
    deploySkipIf=changeGroup["deploySkipIf"]
  elif "deploySkipIf" in parentChange:
    deploySkipIf=parentChange["deploySkipIf"]
  elif "deploySkipIf" in parentChangeGroup:
    deploySkipIf=parentChangeGroup["deploySkipIf"]
  else:
    deploySkipIf=None

  if "rollbackSkipIf" in change:
    rollbackSkipIf=change["rollbackSkipIf"]
  elif "rollbackSkipIf" in changeGroup:
    rollbackSkipIf=changeGroup["rollbackSkipIf"]
  elif "rollbackSkipIf" in parentChange:
    rollbackSkipIf=parentChange["rollbackSkipIf"]
  elif "rollbackSkipIf" in parentChangeGroup:
    rollbackSkipIf=parentChangeGroup["rollbackSkipIf"]
  else:
    rollbackSkipIf=None

  if "data" in parentChangeGroup:
    data.update(parentChangeGroup["data"])
  if "data" in parentChange:
    data.update(parentChange["data"])
  if "data" in changeGroup:
    data.update(changeGroup["data"])
  if "data" in change:
    data.update(change["data"])

  if "include" in change:
    includeScript=change["include"]
  elif "include" in changeGroup:
    includeScript=changeGroup["include"]
  else:
    includeScript=None

# Nested changeScripts : including changeScripts in changeScripts
# BEGIN
#      Recursive processing of nested changeScripts 
  if includeScript != None:
    processChangeScript(includeScript, operation, change, changeGroup, depth+1, data)
    return
# END

# Do nothing if a change contains no include, deploy or rollback
# BEGIN
  if deploy == None:
    if rollback == None:
      return
# END

# Execute capture before change
# BEGIN
  captureData={}
  if capture != None:
    for captureKey, captureValue in capture.iteritems():
      captureValue=jinja2.Template(captureValue).render(data)
      captureFile=open("./.tmp/"+hashlib.sha512(captureValue).hexdigest(),"w")
      captureFile.write("#!/bin/bash\n")
      captureFile.write(target + "<<" + hashlib.sha512(captureValue).hexdigest() + "\n")
      captureFile.write(captureValue + "\n")
      captureFile.write(hashlib.sha512(captureValue).hexdigest())
      captureFile.close()
      os.system("chmod a+x ./.tmp/"+hashlib.sha512(captureValue).hexdigest())
      process = Popen("./.tmp/"+hashlib.sha512(captureValue).hexdigest(), stdout=PIPE, stderr=PIPE)
      captureOutput, captureError = process.communicate()
      captureReturnCode = process.returncode
      captureData[captureKey]={ 'pre': { 'out': captureOutput, 'err':captureError, 'ret':captureReturnCode } }
      data.update(captureData)
# END
  skip=0
  result="Success"
  if operation == "rollback" and rollback != None and rollbackSkipIf != None:
    rollbackSkipIf=jinja2.Template("{{ " + rollbackSkipIf + " }}").render(data)
    if rollbackSkipIf=="True":
      result="Skipped"
      skip=1
  elif operation == "deploy" and deploy !=None and deploySkipIf != None :
    deploySkipIf=jinja2.Template("{{ " + deploySkipIf + " }}").render(data)
    if deploySkipIf=="True":
      result="Skipped"
      skip=1

# For situations where the value of a variable would contain a jinja2 reference to another variable
# BEGIN
#      Rendering jinja2 repeatedly until no change observed on further rendering
  if operation == "deploy" and deploy !=None and not skip:
    deploy=jinja2.Template(deploy).render(data)
    prev=""
    while prev!=deploy:
      prev=deploy
      deploy=jinja2.Template(deploy).render(data)
  undefinedVariables=jinja2schema.infer(deploy)
  if not undefinedVariables:
    print "Variables not defined:"
    print undefinedVariables
  if operation == "rollback" and rollback != None and not skip:
    rollback=jinja2.Template(rollback).render(data)
    prev=""
    while prev!=rollback:
      prev=rollback
      rollback=jinja2.Template(rollback).render(data)
  undefinedVariables=jinja2schema.infer(rollback)
  if not undefinedVariables:
    print "Variables not defined:"
    print undefinedVariables
# END

# For executing the change
# BEGIN
#      Generating the execution script for the change, granting execute on it and executing it
  deployData={}
  rollbackData={}
  if operation == "rollback" and rollback != None and not skip:
    changeFile=open("./.tmp/"+hashlib.sha512(rollback).hexdigest(),"w")
    changeFile.write("#!/bin/bash\n")
    changeFile.write(target + "<<" + hashlib.sha512(rollback).hexdigest() + "\n")
    changeFile.write(rollback + "\n")
    changeFile.write(hashlib.sha512(rollback).hexdigest())
    changeFile.close()
    os.system("chmod a+x ./.tmp/"+hashlib.sha512(rollback).hexdigest())
    process = Popen("./.tmp/"+hashlib.sha512(rollback).hexdigest(), stdout=PIPE, stderr=PIPE)
    rollbackOutput, rollbackError = process.communicate()
    rollbackReturnCode = process.returncode
    rollbackData['rollback']={ 'out': rollbackOutput, 'err': rollbackError, 'ret': rollbackReturnCode }
    data.update(rollbackData)
  elif operation == "deploy" and deploy !=None and not skip:
    changeFile=open("./.tmp/"+hashlib.sha512(deploy).hexdigest(),"w")
    changeFile.write("#!/bin/bash\n")
    changeFile.write(target + "<<" + hashlib.sha512(deploy).hexdigest() + "\n")
    changeFile.write(deploy + "\n")
    changeFile.write(hashlib.sha512(deploy).hexdigest())
    changeFile.close()
    os.system("chmod a+x ./.tmp/"+hashlib.sha512(deploy).hexdigest())
    process = Popen("./.tmp/"+hashlib.sha512(deploy).hexdigest(), stdout=PIPE, stderr=PIPE)
    deployOutput, deployError = process.communicate()
    deployReturnCode = process.returncode
    deployData['deploy']={ 'out': deployOutput, 'err': deployError, 'ret': deployReturnCode }
    data.update(deployData)
# END

# Execute capture after change
# BEGIN
  if capture != None:
    for captureKey, captureValue in capture.iteritems():
      captureValue=jinja2.Template(captureValue).render(data)
      captureFile=open("./.tmp/"+hashlib.sha512(captureValue).hexdigest(),"w")
      captureFile.write("#!/bin/bash\n")
      captureFile.write(target + "<<" + hashlib.sha512(captureValue).hexdigest() + "\n")
      captureFile.write(captureValue + "\n")
      captureFile.write(hashlib.sha512(captureValue).hexdigest())
      captureFile.close()
      os.system("chmod a+x ./.tmp/"+hashlib.sha512(captureValue).hexdigest())
      process = Popen("./.tmp/"+hashlib.sha512(captureValue).hexdigest(), stdout=PIPE, stderr=PIPE)
      captureOutput, captureError = process.communicate()
      captureReturnCode = process.returncode
      captureData[captureKey].update({ 'post': { 'out': captureOutput, 'err':captureError, 'ret':captureReturnCode } })
      data.update(captureData)
# END
  if operation == "rollback" and rollback != None and not skip and rollbackSuccessIf != None:
    rollbackSuccessIf=jinja2.Template("{{ " + rollbackSuccessIf + " }}").render(data)
    if rollbackSuccessIf=="True":
      result="Success"
    else:
      result="Failure"
  elif operation == "deploy" and deploy !=None and not skip and deploySuccessIf != None:
    deploySuccessIf=jinja2.Template("{{ " + deploySuccessIf + " }}").render(data) 
    if deploySuccessIf=="True":
      result="Success"
    else:
      result="Failure"


# For providing execution log
# BEGIN
#      Displaying key information about each change being executed in json format
  sys.stdout.write("{ ")
  sys.stdout.write("\"name\": \"" + name + "\", ")
  sys.stdout.write("\"group\": \"" + groupName + "\", ")
  sys.stdout.write("\"script\": \"" + rollerScript + "\", ")
  sys.stdout.write("\"depth\": " + str(depth) + ", " )
  sys.stdout.write("\"operation\": \"" + operation + "\", ")
  if result == "Success":
    sys.stdout.write("\"result\": \"" + colored(result, 'green', attrs=['bold']) + "\"")
  elif result == "Failure":
    sys.stdout.write("\"result\": \"" + colored(result, 'red', attrs=['bold']) + "\"")
  elif result == "Skipped":
    sys.stdout.write("\"result\": \"" + colored(result, 'blue', attrs=['bold']) + "\"")
  else:
    sys.stdout.write("\"result\": \"" + result + "\"")
#  sys.stdout.write("\"data\":" + str(data))
  sys.stdout.write(" }\n")
  if result == "Failure":
    sys.exit(3)
Exemplo n.º 20
0
 def __init__(self, template_file: pathlib.Path) -> None:
     loader = FileSystemLoader(str(template_file.parent))
     self._env = Environment(loader=loader)
     self._template = self._env.get_template(template_file.name)
     source, *_ = self._env.loader.get_source(self._env, template_file.name)
     self._vars = infer(source)
Exemplo n.º 21
0
def validateChange(change,
                   changeGroup,
                   operation,
                   parentChange={},
                   parentChangeGroup={},
                   depth=0,
                   rollerScript=None,
                   data={}):
    if "name" in change:
        name = change["name"]

    if "name" in changeGroup:
        groupName = changeGroup["name"]

    if "name" in parentChange:
        parentName = parentChange["name"]
    else:
        parentName = ""

    if "name" in parentChangeGroup:
        parentGroupName = parentChangeGroup["name"]
    else:
        parentGroupName = ""

    change_id = name + "_" + groupName + "_" + parentName + "_" + parentGroupName + "_" + rollerScript
    group_id = groupName + "_" + parentName + "_" + parentGroupName + "_" + rollerScript
    if change_id in changeList:
        if depth == changeList[change_id]:
            print "Duplicate change name " + name + " in change group " + groupName + " of change script " + rollerScript
            sys.exit(5)
        else:
            print "Circular reference of change script " + rollerScript
            sys.exit(6)
    else:
        changeList[change_id] = depth

    if group_id in changeGroupList:
        print "Duplicate group name " + groupName + " in " + rollerScript
        sys.exit(7)

    if "target" in change:
        target = change["target"]
    elif "target" in changeGroup:
        target = changeGroup["target"]
    elif "target" in parentChange:
        target = parentChange["target"]
    elif "target" in parentChangeGroup:
        target = parentChangeGroup["target"]
    else:
        target = None

    if "deploy" in change:
        deploy = change["deploy"]
    elif "deploy" in changeGroup:
        deploy = changeGroup["deploy"]
    elif "deploy" in parentChange:
        deploy = parentChange["deploy"]
    elif "deploy" in parentChangeGroup:
        deploy = parentChangeGroup["deploy"]
    else:
        deploy = None

    if "rollback" in change:
        rollback = change["rollback"]
    elif "rollback" in changeGroup:
        rollback = changeGroup["rollback"]
    elif "rollback" in parentChange:
        rollback = parentChange["rollback"]
    elif "rollback" in parentChangeGroup:
        rollback = parentChangeGroup["rollback"]
    else:
        rollback = None

    if "capture" in change:
        capture = change["capture"]
    elif "capture" in changeGroup:
        capture = changeGroup["capture"]
    elif "capture" in parentChange:
        capture = parentChange["capture"]
    elif "capture" in parentChangeGroup:
        capture = parentChangeGroup["capture"]
    else:
        capture = None

    if "deploySuccessIf" in change:
        deploySuccessIf = change["deploySuccessIf"]
    elif "deploySuccessIf" in changeGroup:
        deploySuccessIf = changeGroup["deploySuccessIf"]
    elif "deploySuccessIf" in parentChange:
        deploySuccessIf = parentChange["deploySuccessIf"]
    elif "deploySuccessIf" in parentChangeGroup:
        deploySuccessIf = parentChangeGroup["deploySuccessIf"]
    else:
        deploySuccessIf = None

    if "rollbackSuccessIf" in change:
        rollbackSuccessIf = change["rollbackSuccessIf"]
    elif "rollbackSuccessIf" in changeGroup:
        rollbackSuccessIf = changeGroup["rollbackSuccessIf"]
    elif "rollbackSuccessIf" in parentChange:
        rollbackSuccessIf = parentChange["rollbackSuccessIf"]
    elif "rollbackSuccessIf" in parentChangeGroup:
        rollbackSuccessIf = parentChangeGroup["rollbackSuccessIf"]
    else:
        rollbackSuccessIf = None

    if "deploySkipIf" in change:
        deploySkipIf = change["deploySkipIf"]
    elif "deploySkipIf" in changeGroup:
        deploySkipIf = changeGroup["deploySkipIf"]
    elif "deploySkipIf" in parentChange:
        deploySkipIf = parentChange["deploySkipIf"]
    elif "deploySkipIf" in parentChangeGroup:
        deploySkipIf = parentChangeGroup["deploySkipIf"]
    else:
        deploySkipIf = None

    if "rollbackSkipIf" in change:
        rollbackSkipIf = change["rollbackSkipIf"]
    elif "rollbackSkipIf" in changeGroup:
        rollbackSkipIf = changeGroup["rollbackSkipIf"]
    elif "rollbackSkipIf" in parentChange:
        rollbackSkipIf = parentChange["rollbackSkipIf"]
    elif "rollbackSkipIf" in parentChangeGroup:
        rollbackSkipIf = parentChangeGroup["rollbackSkipIf"]
    else:
        rollbackSkipIf = None

    if "data" in parentChangeGroup:
        data.update(parentChangeGroup["data"])
    if "data" in parentChange:
        data.update(parentChange["data"])
    if "data" in changeGroup:
        data.update(changeGroup["data"])
    if "data" in change:
        data.update(change["data"])

    if "include" in change:
        includeScript = change["include"]
    elif "include" in changeGroup:
        includeScript = changeGroup["include"]
    else:
        includeScript = None

# Nested changeScripts : including changeScripts in changeScripts
# BEGIN
#      Recursive processing of nested changeScripts
    if includeScript != None:
        validateChangeScript(includeScript, operation, change, changeGroup,
                             depth + 1, data)
        return
# END

# Do nothing if a change contains no include, deploy or rollback
# BEGIN
    if deploy == None:
        if rollback == None:
            return
# END

# Execute capture before change
# BEGIN
    captureData = {}
    if capture != None:
        for captureKey, captureValue in capture.iteritems():
            undefinedVariables = jinja2schema.infer(captureValue)
            for key, value in undefinedVariables.iteritems():
                if dataNotDefined(key, value, data):
                    print "Variable " + colored(
                        dataNotDefined(key, value, data),
                        'red',
                        attrs=['bold']) + " not defined for " + colored(
                            captureValue, 'cyan', attrs=[
                                'bold'
                            ]) + " in " + colored(name + " : " + groupName +
                                                  " : " + rollerScript,
                                                  'green',
                                                  attrs=['bold'])
                    sys.exit(4)
            captureData[captureKey] = {
                'pre': {
                    'out': "<stdout>",
                    'err': "<stderr>",
                    'ret': "<return code>"
                }
            }
            data.update(captureData)
# END
    if operation == "rollback" and rollback != None and rollbackSkipIf != None:
        rollbackSkipIf = jinja2.Template("{{ " + rollbackSkipIf +
                                         " }}").render(data)
        undefinedVariables = jinja2schema.infer(rollbackSkipIf)
        for key, value in undefinedVariables.iteritems():
            if dataNotDefined(key, value, data):
                print "Variable " + colored(key, 'red', attrs=[
                    'bold'
                ]) + " not defined for " + colored(
                    captureValue, 'cyan', attrs=['bold']) + " in " + colored(
                        name + " : " + groupName + " : " + rollerScript,
                        'green',
                        attrs=['bold'])
                sys.exit(4)
    elif operation == "deploy" and deploy != None and deploySkipIf != None:
        deploySkipIf = jinja2.Template("{{ " + deploySkipIf +
                                       " }}").render(data)
        undefinedVariables = jinja2schema.infer(deploySkipIf)
        for key, value in undefinedVariables.iteritems():
            if dataNotDefined(key, value, data):
                print "Variable " + colored(key, 'red', attrs=[
                    'bold'
                ]) + " not defined for " + colored(
                    captureValue, 'cyan', attrs=['bold']) + " in " + colored(
                        name + " : " + groupName + " : " + rollerScript,
                        'green',
                        attrs=['bold'])
                sys.exit(4)

# For situations where the value of a variable would contain a jinja2 reference to another variable
# BEGIN
#      Rendering jinja2 repeatedly until no change observed on further rendering
    if operation == "deploy" and deploy != None:
        deploy = jinja2.Template(deploy).render(data)
        prev = ""
        while prev != deploy:
            prev = deploy
            deploy = jinja2.Template(deploy).render(data)
        undefinedVariables = jinja2schema.infer(deploy)
        for key, value in undefinedVariables.iteritems():
            if dataNotDefined(key, value, data):
                print "Variable " + colored(key, 'red', attrs=[
                    'bold'
                ]) + " not defined for " + colored(
                    captureValue, 'cyan', attrs=['bold']) + " in " + colored(
                        name + " : " + groupName + " : " + rollerScript,
                        'green',
                        attrs=['bold'])
                sys.exit(4)
    if operation == "rollback" and rollback != None:
        rollback = jinja2.Template(rollback).render(data)
        prev = ""
        while prev != rollback:
            prev = rollback
            rollback = jinja2.Template(rollback).render(data)
        undefinedVariables = jinja2schema.infer(rollback)
        for key, value in undefinedVariables.iteritems():
            if dataNotDefined(key, value, data):
                print "Variable " + colored(key, 'red', attrs=[
                    'bold'
                ]) + " not defined for " + colored(
                    captureValue, 'cyan', attrs=['bold']) + " in " + colored(
                        name + " : " + groupName + " : " + rollerScript,
                        'green',
                        attrs=['bold'])
                sys.exit(4)
            else:
                print value
# END

# For executing the change
# BEGIN
#      Generating the execution script for the change, granting execute on it and executing it
    deployData = {}
    rollbackData = {}
    if operation == "rollback" and rollback != None:
        rollbackData['rollback'] = {
            'out': "<stdout>",
            'err': "<stderr>",
            'ret': "<return code>"
        }
        data.update(rollbackData)
    elif operation == "deploy" and deploy != None:
        deployData['deploy'] = {
            'out': "<stdout>",
            'err': "<stderr>",
            'ret': "<return code>"
        }
        data.update(deployData)
# END

# Execute capture after change
# BEGIN
    if capture != None:
        for captureKey, captureValue in capture.iteritems():
            captureValue = jinja2.Template(captureValue).render(data)
            undefinedVariables = jinja2schema.infer(captureValue)
            for key, value in undefinedVariables.iteritems():
                if dataNotDefined(key, value, data):
                    print "Variable " + colored(key, 'red', attrs=[
                        'bold'
                    ]) + " not defined for " + colored(
                        captureValue, 'cyan',
                        attrs=['bold']) + " in " + colored(
                            name + " : " + groupName + " : " + rollerScript,
                            'green',
                            attrs=['bold'])
                    sys.exit(4)
            captureData[captureKey].update({
                'post': {
                    'out': "<stdout>",
                    'err': "<stderr>",
                    'ret': "<return code>"
                }
            })
            data.update(captureData)


# END
    if operation == "rollback" and rollback != None and rollbackSuccessIf != None:
        rollbackSuccessIf = jinja2.Template("{{ " + rollbackSuccessIf +
                                            " }}").render(data)
        undefinedVariables = jinja2schema.infer(rollbackSuccessIf)
        for key, value in undefinedVariables.iteritems():
            if dataNotDefined(key, value, data):
                print "Variable " + colored(key, 'red', attrs=[
                    'bold'
                ]) + " not defined for " + colored(
                    captureValue, 'cyan', attrs=['bold']) + " in " + colored(
                        name + " : " + groupName + " : " + rollerScript,
                        'green',
                        attrs=['bold'])
                sys.exit(4)
    elif operation == "deploy" and deploy != None and deploySuccessIf != None:
        deploySuccessIf = jinja2.Template("{{ " + deploySuccessIf +
                                          " }}").render(data)
        undefinedVariables = jinja2schema.infer(deploySuccessIf)
        for key, value in undefinedVariables.iteritems():
            if dataNotDefined(key, value, data):
                print "Variable " + colored(key, 'red', attrs=[
                    'bold'
                ]) + " not defined for " + colored(
                    captureValue, 'cyan', attrs=['bold']) + " in " + colored(
                        name + " : " + groupName + " : " + rollerScript,
                        'green',
                        attrs=['bold'])
                sys.exit(4)
Exemplo n.º 22
0
 def get_placeholder_values(self, template_file):
     with open(template_file) as file_:
         content = file_.read()
         placeholders = jinja2schema.infer(content).keys()
     return list(placeholders)