<s> """<STR_LIT>""" <EOL> import parted <EOL> DOCUMENTATION = """<STR_LIT>""" <EOL> class PartitionSpec ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , size , weight ) : <EOL> """<STR_LIT>""" <EOL> self . size = size <EOL> self . weight = weight <EOL> self . count = - <NUM_LIT:1> <EOL> def set_count ( self , count ) : <EOL> """<STR_LIT>""" <EOL> self . count = count <EOL> def assign_space ( total_size , specs ) : <EOL> """<STR_LIT>""" <EOL> total_weight = <NUM_LIT:0.0> <EOL> for spec in specs : <EOL> total_weight += float ( spec . weight ) <EOL> for spec in specs : <EOL> num_blocks = int ( ( float ( spec . weight ) / total_weight ) * ( total_size / float ( spec . size ) ) ) <EOL> spec . set_count ( num_blocks ) <EOL> total_size -= num_blocks * spec . size <EOL> total_weight -= spec . weight <EOL> def partition ( diskname , specs , force = False , check_mode = False ) : <EOL> """<STR_LIT>""" <EOL> count = <NUM_LIT:0> <EOL> dev = parted . getDevice ( diskname ) <EOL> try : <EOL> disk = parted . newDisk ( dev ) <EOL> except parted . DiskException : <EOL> disk = None <EOL> if disk and len ( disk . partitions ) > <NUM_LIT:0> and not force : <EOL> print "<STR_LIT>" , diskname <EOL> return <NUM_LIT:0> <EOL> disk = parted . freshDisk ( dev , '<STR_LIT>' ) <EOL> assign_space ( dev . getSize ( ) , specs ) <EOL> last_megabyte = <NUM_LIT:1> <EOL> for spec in specs : <EOL> for _ in range ( spec . count ) : <EOL> start = parted . sizeToSectors ( last_megabyte , "<STR_LIT>" , dev . sectorSize ) <EOL> length = parted . sizeToSectors ( spec . size , "<STR_LIT>" , dev . sectorSize ) <EOL> geo = parted . Geometry ( device = dev , start = start , length = length ) <EOL> filesystem = parted . FileSystem ( type = '<STR_LIT>' , geometry = geo ) <EOL> part = parted . Partition ( <EOL> disk = disk , <EOL> type = parted . PARTITION_NORMAL , <EOL> fs = filesystem , <EOL> geometry = geo ) <EOL> disk . addPartition ( partition = part , constraint = dev . optimalAlignedConstraint ) <EOL> last_megabyte += spec . size <EOL> count += <NUM_LIT:1> <EOL> try : <EOL> if not check_mode : <EOL> disk . commit ( ) <EOL> except parted . IOException : <EOL> pass <EOL> return count <EOL> def parse_spec ( text ) : <EOL> """<STR_LIT>""" <EOL> tokens = text . split ( "<STR_LIT:U+002C>" ) <EOL> specs = [ ] <EOL> for token in tokens : <EOL> if not "<STR_LIT::>" in token : <EOL> token += "<STR_LIT>" <EOL> ( sizespec , weight ) = token . split ( '<STR_LIT::>' ) <EOL> weight = float ( weight ) <EOL> units = { "<STR_LIT:m>" : <NUM_LIT:1> , "<STR_LIT:g>" : <NUM_LIT:1> << <NUM_LIT:10> , "<STR_LIT:t>" : <NUM_LIT:1> << <NUM_LIT:20> , "<STR_LIT:p>" : <NUM_LIT:1> << <NUM_LIT:30> } <EOL> unit = units . get ( sizespec [ - <NUM_LIT:1> ] . lower ( ) , None ) <EOL> if not unit : <EOL> size = float ( sizespec ) <EOL> unit = <NUM_LIT:1> <EOL> else : <EOL> size = float ( sizespec [ : - <NUM_LIT:1> ] ) <EOL> spec = PartitionSpec ( int ( size * unit ) , weight ) <EOL> specs . append ( spec ) <EOL> return specs <EOL> def get_partitions ( diskpath ) : <EOL> """<STR_LIT>""" <EOL> dev = parted . getDevice ( diskpath ) <EOL> disk = parted . newDisk ( dev ) <EOL> partitions = [ ] <EOL> for part in disk . partitions : <EOL> ( _ , _ , pname ) = part . path . rsplit ( "<STR_LIT:/>" ) <EOL> partitions . append ( { "<STR_LIT:name>" : pname , "<STR_LIT:size>" : part . getLength ( ) * dev . sectorSize } ) <EOL> return partitions <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> module = AnsibleModule ( <EOL> argument_spec = dict ( <EOL> disks = dict ( required = True , type = '<STR_LIT:str>' ) , <EOL> force = dict ( required = False , default = "<STR_LIT>" , type = '<STR_LIT:bool>' ) , <EOL> sizes = dict ( required = True , type = '<STR_LIT:str>' ) <EOL> ) , <EOL> supports_check_mode = True , <EOL> ) <EOL> disks = module . params [ '<STR_LIT>' ] <EOL> force = module . params [ '<STR_LIT>' ] <EOL> if force is None : <EOL> force = False <EOL> sizes = module . params [ '<STR_LIT>' ] <EOL> try : <EOL> specs = parse_spec ( sizes ) <EOL> except ValueError , ex : <EOL> err = "<STR_LIT>" + sizes + "<STR_LIT>" + str ( ex ) <EOL> module . fail_json ( msg = err ) <EOL> partitions = [ ] <EOL> changed_count = <NUM_LIT:0> <EOL> for disk in disks . split ( "<STR_LIT:U+002C>" ) : <EOL> try : <EOL> changed_count += partition ( disk , specs , force , module . check_mode ) <EOL> except Exception , ex : <EOL> err = "<STR_LIT>" + disk + "<STR_LIT>" + str ( ex ) <EOL> raise <EOL> partitions += get_partitions ( disk ) <EOL> module . exit_json ( changed = ( changed_count > <NUM_LIT:0> ) , ansible_facts = { "<STR_LIT>" : partitions } ) <EOL> from ansible . module_utils . basic import * <EOL> main ( ) </s>
