def is_ec2_node_defined_in_region(ec2_instance_id_to_be_found, ec2_instance_ids): is_ec2_node_defined = False for ec2_instance_id in ec2_instance_ids: if(ec2_instance_id == ec2_instance_id_to_be_found): is_ec2_node_defined = True return is_ec2_node_defined if (len(chef_nodes) == 0): print("No chef nodes registered... doing nothing") else: print("{0} chef nodes found".format(len(chef_nodes))) for chef_node_row in chef_nodes: chef_node_name = chef_node_row.object.name chef_node = Node(chef_node_name) if chef_node.has_key('ec2'): chef_node_ec2_instance_id = chef_node['ec2']['instance_id'] if(is_ec2_node_defined_in_region(chef_node_ec2_instance_id, ec2_alive_instance_ids)): print "chef node: {0} {1} exists as a live EC2 node: NO DELETION IN CHEF org".format(chef_node_name,chef_node_ec2_instance_id) else: print "chef node: {0} {1} exists in chef org, but not in ec2, hence WILL BE DELETED FROM CHEF org".format(chef_node_name,chef_node_ec2_instance_id) if(args.dry_run): print "chef node: {0} would have been deleted from {0} -- avoid --dry-run option to delete it".format(api.url) else: chef_node.delete() print "chef node: {0} HAS BEEN DELETED IN CHEF org".format(chef_node_name)