def handle(event, _context): """Lambda Handler""" log_event(event) with ChefAPI(CHEF_SERVER_URL, get_pem(), USERNAME): instance_id = get_instance_id(event) try: search = Search('node', 'ec2_instance_id:' + instance_id) except ChefServerNotFoundError as err: LOGGER.error(err) return False if len(search) != 0: for instance in search: node = Node(instance.object.name) client = Client(instance.object.name) try: node.delete() LOGGER.info('===Node Delete: SUCCESS===') client.delete() LOGGER.info('===Client Delete: SUCCESS===') return True except ChefServerNotFoundError as err: LOGGER.error(err) return False else: LOGGER.info('=Instance does not appear to be Chef Server managed.=') return True
def computer_deleted(self, user, obj, computers=None): node_chef_id = obj.get('node_chef_id', None) if node_chef_id: api = get_chef_api(self.app.conf, user) node = Node(node_chef_id, api) node.delete() client = Client(node_chef_id, api=api) client.delete() self.log_action('deleted', 'Computer', obj)
def handle(event, _context): """Lambda Handler""" log_event(event) node_name = None node_ip = None # Remove from one of the chef servers for URL in CHEF_SERVER_URLS: with ChefAPI(URL, CHEF_PEM, CHEF_USERNAME): instance_id = get_instance_id(event) try: search = Search('node', 'ec2_instance_id:' + instance_id) except ChefServerNotFoundError as err: LOGGER.error(err) return False if len(search) != 0: for instance in search: node_name = instance.object.name node = Node(node_name) node_ip = node['ipaddress'] client = Client(node_name) try: node.delete() client.delete() LOGGER.info( '=====SUCCESSFULLY REMOVED INSTANCE FROM CHEF SERVER===== {}' .format(URL)) break except ChefServerNotFoundError as err: LOGGER.error(err) return False else: LOGGER.info( '===Instance does not appear to be Chef Server managed.=== {}' .format(URL)) # Remove from Spacewalk spacewalk_cleanup(node_ip) # Remove from DNS dns_cleanup(node_name) # Remove from AD active_directory_cleanup(node_name) # Remove fom Solarwinds solarwinds_cleanup(node_ip, node_name) # Remove from Chef Automate chef_automate_cleanup(node_name)
def destroy_node(self, node): """ Destroys chef node from openstack :param node: node to destroy :type node: ChefNode """ cnode = Node(node.name, node.environment.local_api) if cnode.exists: self.compute_client.servers.get(node['uuid']).delete() cnode.delete() client = Client(node.name, node.environment.local_api) if client.exists: client.delete()
def _clean_client(self, hostid, config, target_system, **kwargs): """clean client""" from chef import Client try: client = Client( self._get_client_name( config['hostname'], config['clusterid'], target_system), api=self.api_) client.delete() logging.debug('client is removed for host %s ' 'config %s target_system %s', hostid, config, target_system) except Exception as error: logging.debug('no client to delete for host %s ' 'config %s target_system %s', hostid, config, target_system)
def _clean_client(self, hostid, config, target_system, **kwargs): """clean client""" from chef import Client try: client = Client(self._get_client_name(config['hostname'], config['clusterid'], target_system), api=self.api_) client.delete() logging.debug( 'client is removed for host %s ' 'config %s target_system %s', hostid, config, target_system) except Exception as error: logging.debug( 'no client to delete for host %s ' 'config %s target_system %s', hostid, config, target_system)
def delete(self): node_id = self.request.GET.get('node_id') if node_id is None: return {'ok': False, 'message': 'Missing node ID'} settings = get_current_registry().settings api = get_chef_api(settings, self.request.user) chef_node = ChefNode(node_id, api) if not chef_node.exists: return {'ok': False, 'message': 'This node does not exists'} chef_node.delete() chef_client = ChefClient(node_id, api) if not chef_client.exists: return {'ok': False, 'message': 'This client does not exists'} chef_client.delete() return {'ok': True, 'message': 'Node and client have been deleted'}