BootEnvironment.py revision f169c0eae91b2ee787cf8d6dcf8edd9159d4c9e2
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
#
"""Boot Environment classes used by beadm."""
import datetime
class BootEnvironment:
"""Boot Environment object that is used by beadm to manage command line
options, arguments and the log."""
self.trgt_rpool = None
self.trgt_be_name_or_snapshot = None
self.src_be_name_or_snapshot = None
self.properties = {}
self.description = None
class listBootEnvironment:
"""Base class for beadm list
Determine the BE's to display. Prints command output according to option:
-d - dataset
-s - snapshot
-a - all (both dataset and snapshot)
<none> - only BE information
The -H option produces condensed, parseable output
The ';' delimits each field in the output. BEs with multiple
datasets will have multiple lines in the output.
"""
""" print all output for beadm list command
be_list - list of all BEs
ddh - if True, Do not Display Headers - just parseable data
be_name - user-specified BE, if any
returns 0 for success
side effect: beadm list output printed to stdout
"""
#If we're listing Headers, initialize the array holding the
#column widths with the header widths themselves. Later on,
#the data in this array will get adjusted as we process actual
#row data and find that a piece of data is wider than its
#column header.
if not ddh:
#iterate all header rows since their fields may not
#be of equal length.
icol = 0
icol += 1
#collect BEs
beout = {} #matrix of output text [row][attribute]
beoutname = {} #list of BE names [row]
be_space = {} #space used totals for BE [BE name]['space_used','ibei']
if 'orig_be_name' in be:
cur_be_obj = be
#if BE name specified, collect matching BEs
continue
attrs = ()
#identify BE|dataset|snapshot attributes
att = ''
if att == 'orig_be_name':
break
#for option -s, withhold subordinate datasets
break
#convert output to readable format and save
#maintain maximum column widths
#sum all snapshots for BE
ibe += 1
if 'space_used' in be:
#sum all snapshots and datasets for BE in 'beadm list'
#list space used separately for other options
#output format total lengths for each BE with any snapshots
#expand column if widest column entry
if (spacecol != -1) and \
#print headers in columns
if not ddh:
outstr = ''
if outstr != '':
print outstr
#print collected output in columns
outstr = ''
prev_be = None
cur_be = None
#find attributes for BE type
curtype = None
break
if curtype == None: #default to BE
curtype = 'orig_be_name'
else: attrs = ()
if not ddh:
#for -d,-s,-a, print BE alone on line
print cur_be
#if this is a 'dataset' or 'snap_name', start line with BE
#name token
if ddh: #add separators for parsing
if outstr != '':
else: #append text justified in column
if outstr != '':
print outstr
outstr = ''
return 0
"""find match on user-specified BE."""
if 'orig_be_name' in be:
if 'dataset' in be:
return True
if 'snap_name' in be:
return True
return True
return False
"""
Extract information by attribute and format for printing
returns '?' if normally present attribute not found - error.
"""
if at == 'blank':
return ' '
if at == 'dash':
return '-'
if at == 'orig_be_name':
return '-'
return ret
if at == 'snap_name':
return '-'
else:
if ddh:
return ret
if at == 'dataset':
return '-'
else:
if ddh:
return ret
if at == 'active':
return '-'
ret = ''
ret += 'N'
ret += 'R'
if ret == '':
return '-'
return ret
if at == 'mountpoint':
return '-'
return '-'
if at == 'space_used':
return '0'
if at == 'mounted':
return '-'
if at == 'date':
return '?'
if ddh:
if at == 'policy':
return '?'
if at == 'root_ds':
return '?'
if at == 'uuid_str':
return '-'
#default case - no match on attribute
"""Readable formatting for disk space size."""
if ddh:
kilo = 1024.0
mega = 1048576.0
giga = 1073741824.0
tera = 1099511627776.0
if num == None:
return '0'
"""Prepend root dataset name with BE name stripped."""
"""Top level "beadm list" derived classes defined here.
Only table definition is done here - all methods are in the base class.
Tables driving list:
hdr - list of text to output for each column
lattrs - dictionary of attributes
Each entry specifies either BE, dataset, snapshot with
an attribute key:
orig_be_name - for BEs
dataset - for datasets
snap_name - for snapshots
Each list item in entry indicates specific datum for
column
Number of hdr columns must equal number of lattrs entries
unless ddh (dontDisplayHeaders) is true.
"""
class BEList(listBootEnvironment):
"""specify header and attribute information for BE-only output"""
"""Init function for the class."""
('BE','Active','Mountpoint','Space','Policy','Created'), \
('--','------','----------','-----','------','-------')
if ddh:
'active', 'mountpoint', 'space_used', 'policy',
'date')}
else:
'mountpoint', 'space_used', 'policy', 'date')}
class DatasetList(listBootEnvironment):
"""
specify header and attribute information for dataset output,
-d option
"""
"""Init function for the class."""
('----------','------','----------','-----','------','-------')
if ddh:
'orig_be_name':('orig_be_name', 'root_ds', 'active',
'mountpoint', 'space_used', 'policy', 'date'), \
'dataset':('dataset', 'dash', 'mountpoint', 'space_used',
'policy', 'date')}
else:
'orig_be_name':('root_ds', 'active', 'mountpoint',
'space_used', 'policy', 'date'), \
'dataset':('dataset', 'dash', 'mountpoint', 'space_used',
'policy', 'date')}
class SnapshotList(listBootEnvironment):
"""
specify header and attribute information for snapshot output,
-s option
"""
"""Init function for the class."""
('-----------','-----','------','-------')
'date')}
class CompleteList(listBootEnvironment):
"""
snapshot output,
-a or -ds options
"""
"""Init function for the class."""
('-------------------','------','----------','-----','------','-------')
if ddh:
'orig_be_name':('orig_be_name', 'root_ds', 'active',
'mountpoint', 'space_used', 'policy', 'date'),
'dataset':('dataset', 'dash', 'mountpoint', 'space_used',
'policy', 'date'),
'snap_name':('snap_name', 'dash', 'dash', 'space_used',
'policy', 'date')}
else:
'orig_be_name':('root_ds', 'active', 'mountpoint',
'space_used', 'policy', 'date'), \
'dataset':('dataset', 'dash', 'mountpoint', 'space_used',
'policy', 'date'),
'snap_name':('snap_name', 'dash', 'dash', 'space_used',
'policy', 'date')}