<s> """<STR_LIT>""" <EOL> import json <EOL> import os <EOL> import tempfile <EOL> import time <EOL> from nova import context <EOL> from nova import db <EOL> from nova import exception <EOL> from nova import flags <EOL> from nova import log as logging <EOL> from nova import utils <EOL> LOG = logging . getLogger ( '<STR_LIT>' ) <EOL> FLAGS = flags . FLAGS <EOL> flags . DEFINE_integer ( '<STR_LIT>' , <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT:10> , <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_integer ( '<STR_LIT>' , <NUM_LIT> * <NUM_LIT> * <NUM_LIT> , <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , <EOL> utils . abspath ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_integer ( '<STR_LIT>' , <NUM_LIT:10> , <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_integer ( '<STR_LIT>' , <NUM_LIT:16> , <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_multistring ( '<STR_LIT>' , <EOL> [ '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> _MKFS_COMMAND = { } <EOL> _DEFAULT_MKFS_COMMAND = None <EOL> for s in FLAGS . virt_mkfs : <EOL> os_type , mkfs_command = s . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> if os_type : <EOL> _MKFS_COMMAND [ os_type ] = mkfs_command <EOL> if os_type == '<STR_LIT:default>' : <EOL> _DEFAULT_MKFS_COMMAND = mkfs_command <EOL> def mkfs ( os_type , fs_label , target ) : <EOL> mkfs_command = ( _MKFS_COMMAND . get ( os_type , _DEFAULT_MKFS_COMMAND ) or <EOL> '<STR_LIT>' ) % locals ( ) <EOL> if mkfs_command : <EOL> utils . execute ( * mkfs_command . split ( ) ) <EOL> def extend ( image , size ) : <EOL> """<STR_LIT>""" <EOL> file_size = os . path . getsize ( image ) <EOL> if file_size >= size : <EOL> return <EOL> utils . execute ( '<STR_LIT>' , '<STR_LIT>' , image , size ) <EOL> utils . execute ( '<STR_LIT>' , '<STR_LIT>' , image , check_exit_code = False ) <EOL> utils . execute ( '<STR_LIT>' , image , check_exit_code = False ) <EOL> def inject_data ( image , key = None , net = None , metadata = None , <EOL> partition = None , nbd = False , tune2fs = True ) : <EOL> """<STR_LIT>""" <EOL> device = _link_device ( image , nbd ) <EOL> try : <EOL> if not partition is None : <EOL> out , err = utils . execute ( '<STR_LIT>' , '<STR_LIT>' , device , run_as_root = True ) <EOL> if err : <EOL> raise exception . Error ( _ ( '<STR_LIT>' ) % err ) <EOL> mapped_device = '<STR_LIT>' % ( device . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] , <EOL> partition ) <EOL> else : <EOL> mapped_device = device <EOL> try : <EOL> if not os . path . exists ( mapped_device ) : <EOL> raise exception . Error ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> mapped_device ) <EOL> if tune2fs : <EOL> out , err = utils . execute ( '<STR_LIT>' , '<STR_LIT:-c>' , <NUM_LIT:0> , '<STR_LIT>' , <NUM_LIT:0> , <EOL> mapped_device , run_as_root = True ) <EOL> tmpdir = tempfile . mkdtemp ( ) <EOL> try : <EOL> out , err = utils . execute ( '<STR_LIT>' , mapped_device , tmpdir , <EOL> run_as_root = True ) <EOL> if err : <EOL> raise exception . Error ( _ ( '<STR_LIT>' ) <EOL> % err ) <EOL> try : <EOL> inject_data_into_fs ( tmpdir , key , net , metadata , <EOL> utils . execute ) <EOL> finally : <EOL> utils . execute ( '<STR_LIT>' , mapped_device , run_as_root = True ) <EOL> finally : <EOL> utils . execute ( '<STR_LIT>' , tmpdir ) <EOL> finally : <EOL> if not partition is None : <EOL> utils . execute ( '<STR_LIT>' , '<STR_LIT>' , device , run_as_root = True ) <EOL> finally : <EOL> _unlink_device ( device , nbd ) <EOL> def setup_container ( image , container_dir = None , nbd = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> device = _link_device ( image , nbd ) <EOL> utils . execute ( '<STR_LIT>' , device , container_dir , run_as_root = True ) <EOL> except Exception , exn : <EOL> LOG . exception ( _ ( '<STR_LIT>' ) , exn ) <EOL> _unlink_device ( device , nbd ) <EOL> def destroy_container ( target , instance , nbd = False ) : <EOL> """<STR_LIT>""" <EOL> out , err = utils . execute ( '<STR_LIT>' , run_as_root = True ) <EOL> for loop in out . splitlines ( ) : <EOL> if instance [ '<STR_LIT:name>' ] in loop : <EOL> device = loop . split ( ) [ <NUM_LIT:0> ] <EOL> try : <EOL> container_dir = '<STR_LIT>' % target <EOL> utils . execute ( '<STR_LIT>' , container_dir , run_as_root = True ) <EOL> _unlink_device ( device , nbd ) <EOL> except Exception , exn : <EOL> LOG . exception ( _ ( '<STR_LIT>' ) , exn ) <EOL> def _link_device ( image , nbd ) : <EOL> """<STR_LIT>""" <EOL> if nbd : <EOL> device = _allocate_device ( ) <EOL> utils . execute ( '<STR_LIT>' , '<STR_LIT:-c>' , device , image , run_as_root = True ) <EOL> for i in xrange ( FLAGS . timeout_nbd ) : <EOL> if os . path . exists ( "<STR_LIT>" % os . path . basename ( device ) ) : <EOL> return device <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> raise exception . Error ( _ ( '<STR_LIT>' ) % device ) <EOL> else : <EOL> out , err = utils . execute ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , image , <EOL> run_as_root = True ) <EOL> if err : <EOL> raise exception . Error ( _ ( '<STR_LIT>' ) <EOL> % err ) <EOL> return out . strip ( ) <EOL> def _unlink_device ( device , nbd ) : <EOL> """<STR_LIT>""" <EOL> if nbd : <EOL> utils . execute ( '<STR_LIT>' , '<STR_LIT>' , device , run_as_root = True ) <EOL> _free_device ( device ) <EOL> else : <EOL> utils . execute ( '<STR_LIT>' , '<STR_LIT>' , device , run_as_root = True ) <EOL> _DEVICES = [ '<STR_LIT>' % i for i in xrange ( FLAGS . max_nbd_devices ) ] <EOL> def _allocate_device ( ) : <EOL> while True : <EOL> if not _DEVICES : <EOL> raise exception . Error ( _ ( '<STR_LIT>' ) ) <EOL> device = _DEVICES . pop ( ) <EOL> if not os . path . exists ( "<STR_LIT>" % os . path . basename ( device ) ) : <EOL> break <EOL> return device <EOL> def _free_device ( device ) : <EOL> _DEVICES . append ( device ) <EOL> def inject_data_into_fs ( fs , key , net , metadata , execute ) : <EOL> """<STR_LIT>""" <EOL> if key : <EOL> _inject_key_into_fs ( key , fs , execute = execute ) <EOL> if net : <EOL> _inject_net_into_fs ( net , fs , execute = execute ) <EOL> if metadata : <EOL> _inject_metadata_into_fs ( metadata , fs , execute = execute ) <EOL> def _inject_metadata_into_fs ( metadata , fs , execute = None ) : <EOL> metadata_path = os . path . join ( fs , "<STR_LIT>" ) <EOL> metadata = dict ( [ ( m . key , m . value ) for m in metadata ] ) <EOL> utils . execute ( '<STR_LIT>' , metadata_path , <EOL> process_input = json . dumps ( metadata ) , run_as_root = True ) <EOL> def _inject_key_into_fs ( key , fs , execute = None ) : <EOL> """<STR_LIT>""" <EOL> sshdir = os . path . join ( fs , '<STR_LIT:root>' , '<STR_LIT>' ) <EOL> utils . execute ( '<STR_LIT>' , '<STR_LIT>' , sshdir , run_as_root = True ) <EOL> utils . execute ( '<STR_LIT>' , '<STR_LIT:root>' , sshdir , run_as_root = True ) <EOL> utils . execute ( '<STR_LIT>' , '<STR_LIT>' , sshdir , run_as_root = True ) <EOL> keyfile = os . path . join ( sshdir , '<STR_LIT>' ) <EOL> utils . execute ( '<STR_LIT>' , '<STR_LIT>' , keyfile , <EOL> process_input = '<STR_LIT:\n>' + key . strip ( ) + '<STR_LIT:\n>' , run_as_root = True ) <EOL> def _inject_net_into_fs ( net , fs , execute = None ) : <EOL> """<STR_LIT>""" <EOL> netdir = os . path . join ( os . path . join ( fs , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> utils . execute ( '<STR_LIT>' , '<STR_LIT>' , netdir , run_as_root = True ) <EOL> utils . execute ( '<STR_LIT>' , '<STR_LIT>' , netdir , run_as_root = True ) <EOL> utils . execute ( '<STR_LIT>' , <NUM_LIT> , netdir , run_as_root = True ) <EOL> netfile = os . path . join ( netdir , '<STR_LIT>' ) <EOL> utils . execute ( '<STR_LIT>' , netfile , process_input = net , run_as_root = True ) </s>
