Пример #1
0
    def handle_post(self, request, user, *args, **kwargs):
        """Treat requests POST to create the configuration file.

        URL: rack/gerar-arq-config/id_rack
        """
        try:
            self.log.info('CONFIG')

            # User permission
            if not has_perm(user, AdminPermission.SCRIPT_MANAGEMENT, AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            rack_id = kwargs.get('id_rack')
            rack = Rack()
            rack = rack.get_by_pk(rack_id)
            var = False

            # Chama o script para gerar os arquivos de configuracao
            var = gera_config(rack)

            rack.__dict__.update(id=rack_id, config=var)
            rack.save()

            success_map = dict()
            success_map['rack_conf'] = var
            map = dict()
            map['sucesso'] = success_map

            return self.response(dumps_networkapi(map))

        except RackConfigError, e:
            return self.response_error(382, e.param, e.value)
    def handle_post(self, request, user, *args, **kwargs):
        """Treat requests POST to create the configuration file.

        URL: rack/gerar-arq-config/id_rack
        """
        try:
            self.log.info("CONFIG")

            # User permission
            if not has_perm(user, AdminPermission.SCRIPT_MANAGEMENT, AdminPermission.WRITE_OPERATION):
                self.log.error(u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            rack_id = kwargs.get('id_rack')
            rack = Rack()
            rack = rack.get_by_pk(rack_id)
            var = False

            #Chama o script para gerar os arquivos de configuracao
            var = gera_config(rack)

            rack.__dict__.update(id=rack_id, config=var)
            rack.save()

            success_map = dict()
            success_map['rack_conf'] = var
            map = dict()
            map['sucesso'] = success_map
                        
            return self.response(dumps_networkapi(map))


        except RackConfigError, e:
            return self.response_error(382, e.param, e.value)
    def handle_delete(self, request, user, *args, **kwargs):
        """Treat requests POST to delete Rack.

        URL: rack/id_rack/
        """
        try:
            self.log.info("Delete Rack")

            ########################################################                                     User permission
            if not has_perm(user, AdminPermission.SCRIPT_MANAGEMENT, AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            rack_id = kwargs.get('id_rack')
            rack = Rack()
            rack = rack.get_by_pk(rack_id)

            ######################################################## Mover os arquivos de configuracao que foram gerados
            try:
                LEAF = get_variable("leaf")
                OOB = get_variable("oob")
                SPN = get_variable("spn")
                FORMATO = get_variable("formato")
                PATH_TO_CONFIG = get_variable("path_to_config")
                PATH_TO_MV = get_variable("path_to_mv")
            except ObjectDoesNotExist:
                raise var_exceptions.VariableDoesNotExistException("Erro buscando as variáveis <LEAF,OOB,SPN> ou FORMATO ou PATH_TO_<MV,CONFIG>.")

            try:
                for i in range(1,3):
                    nome_lf = LEAF+"-"+rack.nome+"-0"+str(i)+FORMATO
                    nome_lf_b = PATH_TO_CONFIG+nome_lf
                    nome_lf_a = PATH_TO_MV+nome_lf
                    shutil.move(nome_lf_b, nome_lf_a)
                    nome_oob = OOB+"-0"+str(i)+"-ADD-"+rack.nome+FORMATO
                    nome_oob_b = PATH_TO_CONFIG+nome_oob
                    nome_oob_a = PATH_TO_MV+nome_oob
                    shutil.move(nome_oob_b, nome_oob_a)
                for i in range(1,5):
                    nome_spn = SPN+"-0"+str(i)+"-ADD-"+rack.nome+FORMATO
                    nome_spn_b = PATH_TO_CONFIG+nome_spn
                    nome_spn_a = PATH_TO_MV+nome_spn
                    shutil.move(nome_spn_b, nome_spn_a)

                nome_oob = OOB+"-"+rack.nome+"-01"+FORMATO
                nome_oob_b = PATH_TO_CONFIG+nome_oob
                nome_oob_a = PATH_TO_MV+nome_oob
                shutil.move(nome_oob_b, nome_oob_a)
            except:
                pass

            ########################################################                 Remover as Vlans, redes e ambientes
            try:
                desativar_vlan_rede(user, rack)
                lista_amb = remover_ambiente_rack(user, rack)
                remover_ambiente(user, lista_amb, rack)
                remover_vlan_so(user, rack)
            except:
                raise RackError(None, u'Failed to remove the Vlans and Environments.')

            ########################################################         Remove rack config from spines and core oob
            #aplicar(rack)

            ########################################################                                         Remove Rack
            with distributedlock(LOCK_RACK % rack_id):

                try:
                    rack.delete()
                except RackNumberNotFoundError, e:
                    raise e
                except Exception, e:
                    self.log.error(u'Failed to remove the Rack.')
                    raise RackError(e, u'Failed to remove the Rack.')                   
    def handle_delete(self, request, user, *args, **kwargs):
        """Treat requests POST to delete Rack.

        URL: rack/id_rack/
        """
        try:
            self.log.info("Delete Rack")

            ########################################################                                     User permission
            if not has_perm(user, AdminPermission.SCRIPT_MANAGEMENT, AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            rack_id = kwargs.get('id_rack')
            rack = Rack()
            rack = rack.get_by_pk(rack_id)

            ######################################################## Mover os arquivos de configuracao que foram gerados
            try:
                for i in range(1,3):
                    nome_lf = settings.LEAF+"-"+rack.nome+"-0"+str(i)+settings.FORMATO
                    nome_lf_b = settings.PATH_TO_CONFIG+nome_lf
                    nome_lf_a = settings.PATH_TO_MV+nome_lf
                    shutil.move(nome_lf_b, nome_lf_a)
                    nome_oob = settings.OOB+"-0"+str(i)+"-ADD-"+rack.nome+settings.FORMATO
                    nome_oob_b = settings.PATH_TO_CONFIG+nome_oob
                    nome_oob_a = settings.PATH_TO_MV+nome_oob
                    shutil.move(nome_oob_b, nome_oob_a)
                for i in range(1,5):
                    nome_spn = settings.SPN+"-0"+str(i)+"-ADD-"+rack.nome+settings.FORMATO
                    nome_spn_b = settings.PATH_TO_CONFIG+nome_spn
                    nome_spn_a = settings.PATH_TO_MV+nome_spn
                    shutil.move(nome_spn_b, nome_spn_a)

                nome_oob = settings.OOB+"-"+rack.nome+"-01"+settings.FORMATO
                nome_oob_b = settings.PATH_TO_CONFIG+nome_oob
                nome_oob_a = settings.PATH_TO_MV+nome_oob
                shutil.move(nome_oob_b, nome_oob_a)
            except:
                pass

            ########################################################                 Remover as Vlans, redes e ambientes
            try:
                desativar_vlan_rede(user, rack)
                lista_amb = remover_ambiente_rack(user, rack)
                remover_ambiente(user, lista_amb, rack)
                remover_vlan_so(user, rack)
            except:
                raise RackError(None, u'Failed to remove the Vlans and Environments.')

            ########################################################         Remove rack config from spines and core oob
            aplicar(rack)

            ########################################################                                         Remove Rack
            with distributedlock(LOCK_RACK % rack_id):

                try:
                    rack.delete(user)
                except RackNumberNotFoundError, e:
                    raise e
                except Exception, e:
                    self.log.error(u'Failed to remove the Rack.')
                    raise RackError(e, u'Failed to remove the Rack.')                   
    def handle_post(self, request, user, *args, **kwargs):
        """Treat requests POST to create the configuration file.

        URL: rack/alocar-config/id_rack
        """
        try:

            # User permission
            if not has_perm(user, AdminPermission.SCRIPT_MANAGEMENT, AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            rack_id = kwargs.get('id_rack')
            rack = Rack()
            rack = rack.get_by_pk(rack_id)

            #Validar configuracao
            if not rack.config:
                raise RackAplError(None, rack.nome, "Os arquivos de configuracao devem ser gerados antes.")

            if rack.create_vlan_amb:
                raise RackAplError(None, rack.nome, "As vlans, redes e ambientes ja foram criados.")

            #variaveis
            name_core1, name_core2 =  get_core_name(rack)

            environment_list = []

            variablestochangecore1 = {}
            variablestochangecore2 = {}
            variablestochangecore1 = dic_vlan_core(variablestochangecore1, rack.numero, name_core1, rack.nome)
            variablestochangecore2 = dic_vlan_core(variablestochangecore2, rack.numero, name_core2, rack.nome)

            #######################################################################           VLAN Gerencia SO
            ambientes=dict()
            try:
                DIVISAODC_MGMT = get_variable("divisaodc_mngt")
                AMBLOG_MGMT = get_variable("amblog_mngt")
                GRPL3_MGMT = get_variable("grpl3_mngt")
            except ObjectDoesNotExist:
                raise var_exceptions.VariableDoesNotExistException("Erro buscando as variáveis <DIVISAODC,AMBLOG,GRPL3>_MGMT.")

            ambientes['DC']=DIVISAODC_MGMT
            ambientes['LOG']=AMBLOG_MGMT
            ambientes['L3']=GRPL3_MGMT
    
            try:
                #criar vlan
                vlan = criar_vlan(user, variablestochangecore1, ambientes)
            except:
                raise RackAplError(None, rack.nome, "Erro ao criar a VLAN_SO.")
            try:
                #criar rede
                network = criar_rede(user, "Rede invalida equipamentos", variablestochangecore1, vlan)
            except:
                raise RackAplError(None, rack.nome, "Erro ao criar a rede da VLAN_SO")
            try:
                #inserir os Core
                inserir_equip(user, variablestochangecore1, network.id)           
                inserir_equip(user, variablestochangecore2, network.id)            
            except:
                raise RackAplError(None, rack.nome, "Erro ao inserir o core 1 e 2")
                
            #######################################################################                   Ambientes

            #BE - SPINE - LEAF
            try:
                environment_list = ambiente_spn_lf(user, rack, environment_list)
            except:
                raise RackAplError(None, rack.nome, "Erro ao criar os ambientes e alocar as vlans do Spine-leaf.")

            #BE - PRODUCAO
            try:
                environment_list = ambiente_prod(user, rack, environment_list)
            except ObjectDoesNotExist, e:
                raise var_exceptions.VariableDoesNotExistException(e)
            except:
                raise RackAplError(None, rack.nome, "Erro ao criar os ambientes de produção.")

            #BE - Hosts - CLOUD
            try:
                environment_list = ambiente_cloud(user, rack, environment_list)
            except:
                raise RackAplError(None, rack.nome, "Erro ao criar os ambientes e alocar as vlans da Cloud.")

            #FE
            try:
                environment_list = ambiente_prod_fe(user, rack, environment_list)
            except ObjectDoesNotExist, e:
                raise var_exceptions.VariableDoesNotExistException(e)
    def handle_delete(self, request, user, *args, **kwargs):
        """Treat requests POST to delete Rack.

        URL: rack/id_rack/
        """
        try:
            self.log.info('Delete Rack')

            # User permission
            if not has_perm(user, AdminPermission.SCRIPT_MANAGEMENT,
                            AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            rack_id = kwargs.get('id_rack')
            rack = Rack()
            rack = rack.get_by_pk(rack_id)

            # Mover os arquivos de configuracao que foram gerados
            try:
                LEAF = get_variable('leaf')
                OOB = get_variable('oob')
                SPN = get_variable('spn')
                FORMATO = get_variable('formato')
                PATH_TO_CONFIG = get_variable('path_to_config')
                PATH_TO_MV = get_variable('path_to_mv')
            except ObjectDoesNotExist:
                raise var_exceptions.VariableDoesNotExistException(
                    'Erro buscando as variáveis <LEAF,OOB,SPN> ou FORMATO ou PATH_TO_<MV,CONFIG>.'
                )

            try:
                for i in range(1, 3):
                    nome_lf = LEAF + '-' + rack.nome + '-0' + str(i) + FORMATO
                    nome_lf_b = PATH_TO_CONFIG + nome_lf
                    nome_lf_a = PATH_TO_MV + nome_lf
                    shutil.move(nome_lf_b, nome_lf_a)
                    nome_oob = OOB + '-0' + \
                        str(i) + '-ADD-' + rack.nome + FORMATO
                    nome_oob_b = PATH_TO_CONFIG + nome_oob
                    nome_oob_a = PATH_TO_MV + nome_oob
                    shutil.move(nome_oob_b, nome_oob_a)
                for i in range(1, 5):
                    nome_spn = SPN + '-0' + \
                        str(i) + '-ADD-' + rack.nome + FORMATO
                    nome_spn_b = PATH_TO_CONFIG + nome_spn
                    nome_spn_a = PATH_TO_MV + nome_spn
                    shutil.move(nome_spn_b, nome_spn_a)

                nome_oob = OOB + '-' + rack.nome + '-01' + FORMATO
                nome_oob_b = PATH_TO_CONFIG + nome_oob
                nome_oob_a = PATH_TO_MV + nome_oob
                shutil.move(nome_oob_b, nome_oob_a)
            except:
                pass

            # Remover as Vlans, redes e ambientes
            try:
                desativar_vlan_rede(user, rack)
                lista_amb = remover_ambiente_rack(user, rack)
                remover_ambiente(user, lista_amb, rack)
                remover_vlan_so(user, rack)
            except:
                raise RackError(
                    None, u'Failed to remove the Vlans and Environments.')

            # Remove rack config from spines and core oob
            # aplicar(rack)

            # Remove Rack

            with distributedlock(LOCK_RACK % rack_id):
                try:
                    rack.delete()
                except RackNumberNotFoundError, e:
                    raise e
                except Exception, e:
                    self.log.error(u'Failed to remove the Rack.')
                    raise RackError(e, u'Failed to remove the Rack.')
Пример #7
0
    def handle_post(self, request, user, *args, **kwargs):
        """Treat requests POST to create the configuration file.

        URL: rack/alocar-config/id_rack
        """
        try:

            # User permission
            if not has_perm(user, AdminPermission.SCRIPT_MANAGEMENT,
                            AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            rack_id = kwargs.get('id_rack')
            rack = Rack()
            rack = rack.get_by_pk(rack_id)

            # Validar configuracao
            if not rack.config:
                raise RackAplError(
                    None, rack.nome,
                    'Os arquivos de configuracao devem ser gerados antes.')

            if rack.create_vlan_amb:
                raise RackAplError(
                    None, rack.nome,
                    'As vlans, redes e ambientes ja foram criados.')

            # variaveis
            name_core1, name_core2 = get_core_name(rack)

            environment_list = []

            variablestochangecore1 = {}
            variablestochangecore2 = {}
            variablestochangecore1 = dic_vlan_core(variablestochangecore1,
                                                   rack.numero, name_core1,
                                                   rack.nome)
            variablestochangecore2 = dic_vlan_core(variablestochangecore2,
                                                   rack.numero, name_core2,
                                                   rack.nome)

            # VLAN Gerencia SO
            ambientes = dict()
            try:
                DIVISAODC_MGMT = get_variable('divisaodc_mngt')
                AMBLOG_MGMT = get_variable('amblog_mngt')
                GRPL3_MGMT = get_variable('grpl3_mngt')
            except ObjectDoesNotExist:
                raise var_exceptions.VariableDoesNotExistException(
                    'Erro buscando as variáveis <DIVISAODC,AMBLOG,GRPL3>_MGMT.'
                )

            ambientes['DC'] = DIVISAODC_MGMT
            ambientes['LOG'] = AMBLOG_MGMT
            ambientes['L3'] = GRPL3_MGMT

            try:
                # criar vlan
                vlan = criar_vlan(user, variablestochangecore1, ambientes)
            except:
                raise RackAplError(None, rack.nome, 'Erro ao criar a VLAN_SO.')
            try:
                # criar rede
                network = criar_rede(user, 'Rede invalida equipamentos',
                                     variablestochangecore1, vlan)
            except:
                raise RackAplError(None, rack.nome,
                                   'Erro ao criar a rede da VLAN_SO')
            try:
                # inserir os Core
                inserir_equip(user, variablestochangecore1, network.id)
                inserir_equip(user, variablestochangecore2, network.id)
            except:
                raise RackAplError(None, rack.nome,
                                   'Erro ao inserir o core 1 e 2')

            # Ambientes

            # BE - SPINE - LEAF
            try:
                environment_list = ambiente_spn_lf(user, rack,
                                                   environment_list)
            except:
                raise RackAplError(
                    None, rack.nome,
                    'Erro ao criar os ambientes e alocar as vlans do Spine-leaf.'
                )

            # BE - PRODUCAO
            try:
                environment_list = ambiente_prod(user, rack, environment_list)
            except ObjectDoesNotExist, e:
                raise var_exceptions.VariableDoesNotExistException(e)
            except:
                raise RackAplError(None, rack.nome,
                                   'Erro ao criar os ambientes de produção.')

            # BE - Hosts - CLOUD
            try:
                environment_list = ambiente_cloud(user, rack, environment_list)
            except:
                raise RackAplError(
                    None, rack.nome,
                    'Erro ao criar os ambientes e alocar as vlans da Cloud.')

            # FE
            try:
                environment_list = ambiente_prod_fe(user, rack,
                                                    environment_list)
            except ObjectDoesNotExist, e:
                raise var_exceptions.VariableDoesNotExistException(e)
    def handle_post(self, request, user, *args, **kwargs):
        """Treat requests POST to create the configuration file.

        URL: rack/aplicar-config/id_rack
        """
        try:

            # User permission
            if not has_perm(user, AdminPermission.SCRIPT_MANAGEMENT, AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            rack_id = kwargs.get('id_rack')
            rack = Rack()
            rack = rack.get_by_pk(rack_id)

            #Validar configuracao
            if not rack.config:
                raise RackAplError(None, rack.nome, "Os arquivos de configuracao devem ser gerados antes.")

            if rack.create_vlan_amb:
                raise RackAplError(None, rack.nome, "As vlans, redes e ambientes ja foram criados.")

            #variaveis
            name_core1, name_core2 =  get_core_name(rack)

            environment_list = []

            variablestochangecore1 = {}
            variablestochangecore2 = {}
            variablestochangecore1 = dic_vlan_core(variablestochangecore1, rack.numero, name_core1, rack.nome)
            variablestochangecore2 = dic_vlan_core(variablestochangecore2, rack.numero, name_core2, rack.nome)

            #######################################################################           VLAN Gerencia SO
            ambientes=dict()
            ambientes['DC']=settings.DIVISAODC_MGMT
            ambientes['LOG']=settings.AMBLOG_MGMT
            ambientes['L3']=settings.GRPL3_MGMT
    
            try:
                #criar e ativar a vlan
                vlan = criar_vlan(user, variablestochangecore1, ambientes)
            except:
                raise RackAplError(None, rack.nome, "Erro ao criar a VLAN_SO.")
            try:
                #criar e ativar a rede
                network = criar_rede(user, "Ponto a ponto", variablestochangecore1, vlan)
            except:
                raise RackAplError(None, rack.nome, "Erro ao criar a rede da VLAN_SO")
            try:
                #inserir os Core
                inserir_equip(user, variablestochangecore1, network.id)           
                inserir_equip(user, variablestochangecore2, network.id)            
            except:
                raise RackAplError(None, rack.nome, "Erro ao inserir o core 1 e 2")
                
            #######################################################################                   Ambientes

            #BE - SPINE - LEAF
            environment_list = ambiente_spn_lf(user, rack, environment_list)

            #BE - PRODUCAO
            environment_list = ambiente_prod(user, rack, environment_list)

            #BE - Hosts - CLOUD
            environment_list = ambiente_cloud(user, rack, environment_list)
            
            #FE 
            environment_list = ambiente_prod_fe(user, rack, environment_list)

            #Borda
            environment_list = ambiente_borda(user, rack, environment_list)

            #######################################################################                   Backuper

            aplicar(rack)
            environment_rack(user, environment_list, rack)

            rack.__dict__.update(id=rack.id, create_vlan_amb=True)
            rack.save(user)

            success_map = dict()
            success_map['rack_conf'] = True
            map = dict()
            map['sucesso'] = success_map

            return self.response(dumps_networkapi(map))

        except RackConfigError, e:
            return self.response_error(382, e.param, e.value)