# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Copyright 2010 Anso Labs, LLC
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""
Nova Storage manages creating, attaching, detaching, and
destroying persistent storage volumes, ala EBS.
Currently uses Ata-over-Ethernet.
"""

import glob
import logging
import random
import socket
import subprocess
import time

from nova import vendor
from tornado import ioloop
from twisted.internet import defer

from nova import datastore
from nova import exception
from nova import flags
from nova import rpc
from nova import utils
from nova import validate


FLAGS = flags.FLAGS
flags.DEFINE_string('storage_dev', '/dev/sdb',
                    'Physical device to use for volumes')
flags.DEFINE_string('volume_group', 'nova-volumes',
                    'Name for the VG that will contain exported volumes')
flags.DEFINE_string('aoe_eth_dev', 'eth0',
                    'Which device to export the volumes on')
flags.DEFINE_string('storage_name',
                    socket.gethostname(),
                    'name of this node')
flags.DEFINE_integer('shelf_id',
                    utils.last_octet(utils.get_my_ip()),
                    'AoE shelf_id for this node')
flags.DEFINE_string('storage_availability_zone',
                    'nova',
                    'availability zone of this node')
flags.DEFINE_boolean('fake_storage', False,
                     'Should we make real storage volumes to attach?')

# TODO(joshua) Index of volumes by project

def get_volume(volume_id):
    """ Returns a redis-backed volume object """
    volume_class = Volume
    if FLAGS.fake_storage: