<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import re <EOL> import os . path <EOL> import logging <EOL> import salt . utils <EOL> from salt . ext . six import string_types <EOL> from salt . exceptions import CommandExecutionError <EOL> LOG = logging . getLogger ( __name__ ) <EOL> UUID_RE = re . compile ( '<STR_LIT>' . format ( '<STR_LIT>' ) ) <EOL> NAME_RE = re . compile ( '<STR_LIT>' . format ( '<STR_LIT>' ) ) <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if vboxcmd ( ) : <EOL> if __opts__ . get ( '<STR_LIT>' , False ) is True : <EOL> if not __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' ) : <EOL> __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' ) <EOL> return True <EOL> return ( False , '<STR_LIT>' ) <EOL> def vboxcmd ( ) : <EOL> '''<STR_LIT>''' <EOL> return salt . utils . which ( '<STR_LIT>' ) <EOL> def list_ostypes ( ) : <EOL> '''<STR_LIT>''' <EOL> return list_items ( '<STR_LIT>' , True , '<STR_LIT>' ) <EOL> def list_nodes_min ( ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> cmd = '<STR_LIT>' . format ( vboxcmd ( ) ) <EOL> for line in salt . modules . cmdmod . run ( cmd ) . splitlines ( ) : <EOL> if not line . strip ( ) : <EOL> continue <EOL> comps = line . split ( ) <EOL> name = comps [ <NUM_LIT:0> ] . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> ret [ name ] = True <EOL> return ret <EOL> def list_nodes_full ( ) : <EOL> '''<STR_LIT>''' <EOL> return list_items ( '<STR_LIT>' , True , '<STR_LIT:Name>' ) <EOL> def list_nodes ( ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> nodes = list_nodes_full ( ) <EOL> for node in nodes : <EOL> ret [ node ] = { <EOL> '<STR_LIT:id>' : nodes [ node ] [ '<STR_LIT>' ] , <EOL> '<STR_LIT:image>' : nodes [ node ] [ '<STR_LIT>' ] , <EOL> '<STR_LIT:name>' : nodes [ node ] [ '<STR_LIT:Name>' ] , <EOL> '<STR_LIT:state>' : None , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> } <EOL> ret [ node ] [ '<STR_LIT:size>' ] = '<STR_LIT>' . format ( <EOL> nodes [ node ] [ '<STR_LIT>' ] , <EOL> nodes [ node ] [ '<STR_LIT>' ] , <EOL> ) <EOL> return ret <EOL> def start ( name ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> cmd = '<STR_LIT>' . format ( vboxcmd ( ) , name ) <EOL> ret = salt . modules . cmdmod . run ( cmd ) . splitlines ( ) <EOL> return ret <EOL> def stop ( name ) : <EOL> '''<STR_LIT>''' <EOL> cmd = '<STR_LIT>' . format ( vboxcmd ( ) , name ) <EOL> ret = salt . modules . cmdmod . run ( cmd ) . splitlines ( ) <EOL> return ret <EOL> def register ( filename ) : <EOL> '''<STR_LIT>''' <EOL> if not os . path . isfile ( filename ) : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' . format ( filename ) <EOL> ) <EOL> cmd = '<STR_LIT>' . format ( vboxcmd ( ) , filename ) <EOL> ret = salt . modules . cmdmod . run_all ( cmd ) <EOL> if ret [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> return True <EOL> return ret [ '<STR_LIT>' ] <EOL> def unregister ( name , delete = False ) : <EOL> '''<STR_LIT>''' <EOL> nodes = list_nodes_min ( ) <EOL> if name not in nodes : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' . format ( name ) <EOL> ) <EOL> cmd = '<STR_LIT>' . format ( vboxcmd ( ) , name ) <EOL> if delete is True : <EOL> cmd += '<STR_LIT>' <EOL> ret = salt . modules . cmdmod . run_all ( cmd ) <EOL> if ret [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> return True <EOL> return ret [ '<STR_LIT>' ] <EOL> def destroy ( name ) : <EOL> '''<STR_LIT>''' <EOL> return unregister ( name , True ) <EOL> def create ( name , <EOL> groups = None , <EOL> ostype = None , <EOL> register = True , <EOL> basefolder = None , <EOL> new_uuid = None , <EOL> ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> nodes = list_nodes_min ( ) <EOL> if name in nodes : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' . format ( name ) <EOL> ) <EOL> params = '<STR_LIT>' <EOL> if name : <EOL> if NAME_RE . search ( name ) : <EOL> raise CommandExecutionError ( '<STR_LIT>' ) <EOL> params += '<STR_LIT>' . format ( name ) <EOL> if groups : <EOL> if isinstance ( groups , string_types ) : <EOL> groups = [ groups ] <EOL> if isinstance ( groups , list ) : <EOL> params += '<STR_LIT>' . format ( '<STR_LIT:U+002C>' . join ( groups ) ) <EOL> else : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> ostypes = list_ostypes ( ) <EOL> if ostype not in ostypes : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' . format ( name ) <EOL> ) <EOL> else : <EOL> params += '<STR_LIT>' + ostype <EOL> if register is True : <EOL> params += '<STR_LIT>' <EOL> if basefolder : <EOL> if not os . path . exists ( basefolder ) : <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( basefolder ) ) <EOL> params += '<STR_LIT>' . format ( basefolder ) <EOL> if new_uuid : <EOL> if NAME_RE . search ( new_uuid ) : <EOL> raise CommandExecutionError ( '<STR_LIT>' ) <EOL> params += '<STR_LIT>' . format ( new_uuid ) <EOL> cmd = '<STR_LIT>' . format ( vboxcmd ( ) , params ) <EOL> ret = salt . modules . cmdmod . run_all ( cmd ) <EOL> if ret [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> return True <EOL> return ret [ '<STR_LIT>' ] <EOL> def clonevm ( name = None , <EOL> uuid = None , <EOL> new_name = None , <EOL> snapshot_uuid = None , <EOL> snapshot_name = None , <EOL> mode = '<STR_LIT>' , <EOL> options = None , <EOL> basefolder = None , <EOL> new_uuid = None , <EOL> register = False , <EOL> groups = None , <EOL> ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> if ( name and uuid ) or ( not name and not uuid ) : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> params = '<STR_LIT>' <EOL> nodes_names = list_nodes_min ( ) <EOL> nodes_uuids = list_items ( '<STR_LIT>' , True , '<STR_LIT>' ) . keys ( ) <EOL> if name : <EOL> if name not in nodes_names : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' . format ( name ) <EOL> ) <EOL> params += '<STR_LIT:U+0020>' + name <EOL> elif uuid : <EOL> if uuid not in nodes_uuids : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' . format ( name ) <EOL> ) <EOL> params += '<STR_LIT:U+0020>' + uuid <EOL> if snapshot_name and snapshot_uuid : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> if snapshot_name : <EOL> if NAME_RE . search ( snapshot_name ) : <EOL> raise CommandExecutionError ( '<STR_LIT>' ) <EOL> params += '<STR_LIT>' . format ( snapshot_name ) <EOL> elif snapshot_uuid : <EOL> if UUID_RE . search ( snapshot_uuid ) : <EOL> raise CommandExecutionError ( '<STR_LIT>' ) <EOL> params += '<STR_LIT>' . format ( snapshot_uuid ) <EOL> valid_modes = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:all>' ) <EOL> if mode and mode not in valid_modes : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( valid_modes ) ) <EOL> ) <EOL> else : <EOL> params += '<STR_LIT>' + mode <EOL> valid_options = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if options and options not in valid_options : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( valid_options ) ) <EOL> ) <EOL> else : <EOL> params += '<STR_LIT>' + options <EOL> if new_name : <EOL> if NAME_RE . search ( new_name ) : <EOL> raise CommandExecutionError ( '<STR_LIT>' ) <EOL> params += '<STR_LIT>' . format ( new_name ) <EOL> if groups : <EOL> if isinstance ( groups , string_types ) : <EOL> groups = [ groups ] <EOL> if isinstance ( groups , list ) : <EOL> params += '<STR_LIT>' . format ( '<STR_LIT:U+002C>' . join ( groups ) ) <EOL> else : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> if basefolder : <EOL> if not os . path . exists ( basefolder ) : <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( basefolder ) ) <EOL> params += '<STR_LIT>' . format ( basefolder ) <EOL> if new_uuid : <EOL> if NAME_RE . search ( new_uuid ) : <EOL> raise CommandExecutionError ( '<STR_LIT>' ) <EOL> params += '<STR_LIT>' . format ( new_uuid ) <EOL> if register is True : <EOL> params += '<STR_LIT>' <EOL> cmd = '<STR_LIT>' . format ( vboxcmd ( ) , name ) <EOL> ret = salt . modules . cmdmod . run_all ( cmd ) <EOL> if ret [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> return True <EOL> return ret [ '<STR_LIT>' ] <EOL> def clonemedium ( medium , <EOL> uuid_in = None , <EOL> file_in = None , <EOL> uuid_out = None , <EOL> file_out = None , <EOL> mformat = None , <EOL> variant = None , <EOL> existing = False , <EOL> ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> params = '<STR_LIT>' <EOL> valid_mediums = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if medium in valid_mediums : <EOL> params += medium <EOL> else : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( valid_mediums ) ) <EOL> ) <EOL> if ( uuid_in and file_in ) or ( not uuid_in and not file_in ) : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> if uuid_in : <EOL> if medium == '<STR_LIT>' : <EOL> item = '<STR_LIT>' <EOL> elif medium == '<STR_LIT>' : <EOL> item = '<STR_LIT>' <EOL> elif medium == '<STR_LIT>' : <EOL> item = '<STR_LIT>' <EOL> items = list_items ( item ) <EOL> if uuid_in not in items : <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( uuid_in ) ) <EOL> params += '<STR_LIT:U+0020>' + uuid_in <EOL> elif file_in : <EOL> if not os . path . exists ( file_in ) : <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( file_in ) ) <EOL> params += '<STR_LIT:U+0020>' + file_in <EOL> if ( uuid_out and file_out ) or ( not uuid_out and not file_out ) : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> if uuid_out : <EOL> params += '<STR_LIT:U+0020>' + uuid_out <EOL> elif file_out : <EOL> try : <EOL> salt . utils . fopen ( file_out , '<STR_LIT:w>' ) . close ( ) <EOL> os . unlink ( file_out ) <EOL> params += '<STR_LIT:U+0020>' + file_out <EOL> except OSError : <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( file_out ) ) <EOL> if mformat : <EOL> valid_mformat = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if mformat not in valid_mformat : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( valid_mformat ) ) <EOL> ) <EOL> else : <EOL> params += '<STR_LIT>' + mformat <EOL> valid_variant = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if variant and variant not in valid_variant : <EOL> if not os . path . exists ( file_in ) : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( valid_variant ) ) <EOL> ) <EOL> else : <EOL> params += '<STR_LIT>' + variant <EOL> if existing : <EOL> params += '<STR_LIT>' <EOL> cmd = '<STR_LIT>' . format ( vboxcmd ( ) , params ) <EOL> ret = salt . modules . cmdmod . run_all ( cmd ) <EOL> if ret [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> return True <EOL> return ret [ '<STR_LIT>' ] <EOL> def list_items ( item , details = False , group_by = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> types = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> if item not in types : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( types ) ) <EOL> ) <EOL> flag = '<STR_LIT>' <EOL> if details is True : <EOL> flag = '<STR_LIT>' <EOL> ret = { } <EOL> tmp_id = None <EOL> tmp_dict = { } <EOL> cmd = '<STR_LIT>' . format ( vboxcmd ( ) , flag , item ) <EOL> for line in salt . modules . cmdmod . run ( cmd ) . splitlines ( ) : <EOL> if not line . strip ( ) : <EOL> continue <EOL> comps = line . split ( '<STR_LIT::>' ) <EOL> if len ( comps ) < <NUM_LIT:1> : <EOL> continue <EOL> if tmp_id is not None : <EOL> ret [ tmp_id ] = tmp_dict <EOL> line_val = '<STR_LIT::>' . join ( comps [ <NUM_LIT:1> : ] ) . strip ( ) <EOL> if comps [ <NUM_LIT:0> ] == group_by : <EOL> tmp_id = line_val <EOL> tmp_dict = { } <EOL> tmp_dict [ comps [ <NUM_LIT:0> ] ] = line_val <EOL> return ret </s>
