Пример #1
0
    def form_valid(self, form):
        print('Here we go deploying')
        minion = self.get_value_from_workflow('minion', '')
        salt_util = salt_utils.SaltUtil()

        template = snippet_utils.render_snippet_template(
            self.service, self.app_dir, self.get_workflow())

        res = salt_util.deploy_template(template)
        context = dict()
        context['base_html'] = self.base_html

        try:
            if res is None:
                context[
                    'results'] = 'Could not get deployed VM list, no valid return from CPE'
                return render(self.request,
                              'pan_cnc/results.html',
                              context=context)

            response_obj = json.loads(res)
            # {"return": [{"compute-01.c.vistoq-demo.internal": {"shoaf1": "shutdown", "stuart1": "shutdown"}}]}
            fm = response_obj['return'][0]
            vms = list()
            if minion in fm:
                minion_dict = response_obj['return'][0][minion]
                for m in minion_dict:
                    vm_detail = dict()
                    vm_detail['hostname'] = m
                    vm_detail['status'] = minion_dict[m]
                    vms.append(vm_detail)

                context['vms'] = vms
                context['minion'] = minion
                return render(self.request,
                              'vistoq/deployed_vms.html',
                              context=context)
            else:
                context['results'] = response_obj['return'][0]
                return render(self.request,
                              'pan_cnc/results.html',
                              context=context)
        except ValueError as ve:
            print('Could not parse json')
            print(ve)
            context['results'] = {'Error': 'Could not get deployed VMs list!'}
            return render(self.request,
                          'pan_cnc/results.html',
                          context=context)
Пример #2
0
    def form_valid(self, form):
        context = self.get_snippet_context()

        # special case to hide FW_NAME field from iron-skillet
        if 'hostname' in context and 'FW_NAME' in context:
            if context['FW_NAME'] != context['hostname']:
                context['FW_NAME'] = context['hostname']

        bs = snippet_utils.render_snippet_template(self.service, self.app_dir, context)
        if bs is not None:
            bsb = bytes(bs, 'utf-8')
            encoded_bootstrap_string = urlsafe_b64encode(bsb)
            self.save_value_to_workflow('bootstrap_string', encoded_bootstrap_string.decode('utf-8'))

        return HttpResponseRedirect('include_content')
Пример #3
0
    def get_context_data(self, **kwargs):
        hostname = self.kwargs['hostname']
        minion = self.kwargs['minion']

        service = snippet_utils.load_snippet_with_name('delete_single_vm',
                                                       self.app_dir)
        jinja_context = dict()
        for v in service['variables']:
            if kwargs.get(v['name']):
                jinja_context[v['name']] = kwargs.get(v['name'])

        template = snippet_utils.render_snippet_template(
            service, self.app_dir, jinja_context)
        salt_util = salt_utils.SaltUtil()
        res = salt_util.deploy_template(template)
        print(res)
        print('deleting hostname %s' % hostname)
        context = dict()
        context['base_html'] = self.base_html
        context['results'] = res

        return context
Пример #4
0
    def form_valid(self, form):
        print('Here we go deploying %s' % self.app_dir)
        jinja_context = dict()
        service = snippet_utils.load_snippet_with_name('provision_firewall',
                                                       self.app_dir)
        for v in service['variables']:
            if self.request.POST.get(v['name']):
                jinja_context[v['name']] = self.request.POST.get(v['name'])

        template = snippet_utils.render_snippet_template(
            service, self.app_dir, jinja_context)
        print(template)

        salt_util = salt_utils.SaltUtil()
        res = salt_util.deploy_template(template)
        print(res)
        context = dict()
        context['base_html'] = self.base_html
        context['title'] = 'Deploy Next Generation Firewall'
        context['header'] = 'Deployment Results'

        try:
            results_json = json.loads(res)
        except ValueError as ve:
            print('Could not load results from provisioner!')
            print(ve)
            context['results'] = 'Error deploying VM!'
            return render(self.request,
                          'pan_cnc/results.html',
                          context=context)
        except TypeError as te:
            print('Could not load results from provisioner!')
            print(te)
            context['results'] = 'Error deploying VM!'
            return render(self.request,
                          'pan_cnc/results.html',
                          context=context)

        if 'minion' not in jinja_context:
            context[
                'results'] = 'Error deploying VM! No compute node found in response'
            return render(self.request,
                          'pan_cnc/results.html',
                          context=context)

        minion = jinja_context['minion']
        if 'return' in results_json and minion in results_json['return'][0]:
            r = results_json['return'][0][minion]
            steps = r.keys()
            for step in steps:
                step_detail = r[step]
                if step_detail['result'] is not True:
                    context[
                        'results'] = 'Error deploying VM! Not all steps completed successfully!\n\n'
                    context['results'] += step_detail['comment']
                    return render(self.request,
                                  'pan_cnc/results.html',
                                  context=context)

        context['results'] = 'VM Deployed Successfully on CPE: %s' % minion
        return render(self.request, 'pan_cnc/results.html', context=context)
Пример #5
0
    def form_valid(self, form):
        context = self.get_snippet_context()

        if 'panorama_ip' not in context and 'TARGET_IP' in context:
            print('Setting panorama ip on context')
            context['panorama_ip'] = context['TARGET_IP']

        print('Compiling init-cfg.txt')
        ic = snippet_utils.render_snippet_template(self.service, self.app_dir, context, 'init_cfg.txt')
        print(ic)
        if ic is not None:
            icb = bytes(ic, 'utf-8')
            encoded_init_cfg_string = urlsafe_b64encode(icb)
            self.save_value_to_workflow('init_cfg_string', encoded_init_cfg_string.decode('utf-8'))

        payload = self.render_snippet_template()

        # get the bootstrapper host and port from the .panrc file, environrment, or default name lookup
        # docker-compose will host bootstrapper under the 'bootstrapper' domain name
        bootstrapper_host = cnc_utils.get_config_value('BOOTSTRAPPER_HOST', 'bootstrapper')
        bootstrapper_port = cnc_utils.get_config_value('BOOTSTRAPPER_PORT', '5000')
        print(f'Using bootstrapper_host: {bootstrapper_host}')
        print(f'Using bootstrapper_port: {bootstrapper_port}')
        resp = requests.post(f'http://{bootstrapper_host}:{bootstrapper_port}/generate_bootstrap_package',
                             json=json.loads(payload)
                             )
        content_type = ''
        if 'Content-Type' in resp.headers:
            content_type = resp.headers['Content-Type']
        if 'Content-Disposition' in resp.headers:
            filename = resp.headers['Content-Disposition'].split('=')[1]
        else:
            filename = context.get('hostname')

        print(resp.headers)
        if resp.status_code == 200:
            if 'json' in content_type:
                return_json = resp.json()
                if 'response' in return_json:
                    result_text = return_json["response"]
                else:
                    result_text = resp.text

                results = dict()
                results['results'] = str(resp.status_code)
                results['results'] += '\n'
                results['results'] += result_text
                return render(self.request, 'pan_cnc/results.html', context=results)

            else:
                response = HttpResponse(content_type=content_type)
                response['Content-Disposition'] = 'attachment; filename=%s' % filename
                response.write(resp.content)
                return response
        else:
            results = super().get_context_data()
            results['results'] = str(resp.status_code)
            results['results'] += '\n'
            results['results'] += resp.text

            return render(self.request, 'pan_cnc/results.html', context=results)