common.py revision 2339
#
# 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
#
#
#
"""
Linked image module classes.
The following classes for manipulating linked images are defined here:
LinkedImage
LinkedImgaeChild
The following template classes which linked image plugins should inherit from
are also defined here:
LinkedImagePlugin
LinkedImageChildPlugin
"""
#
# Too many lines in module; pylint: disable-msg=C0302
#
# standard python classes
import operator
import os
import simplejson as json
import sys
# pkg classes
import pkg.pkgsubprocess
# linked image relationship types (returned by LinkedImage.list_related())
REL_PARENT = "parent"
REL_SELF = "self"
REL_CHILD = "child"
# linked image properties
PROP_NAME = "li-name"
PROP_ALTROOT = "li-altroot"
PROP_PARENT_PATH = "li-parent"
PROP_PATH = "li-path"
PROP_MODEL = "li-model"
PROP_RECURSE = "li-recurse"
prop_values = frozenset([
])
# properties that never get saved
])
# special linked image name values (PROP_NAME)
PV_NAME_NONE = "-"
# linked image model values (PROP_MODEL)
PV_MODEL_PUSH = "push"
PV_MODEL_PULL = "pull"
])
# files which contain linked image data
__DATA_DIR = "linked"
class LinkedImagePlugin(object):
"""This class is a template that all linked image plugins should
inherit from. Linked image plugins derived from this class are
designed to manage linked aspects of the current image (vs managing
linked aspects of a specific child of the current image).
All the interfaces exported by this class and its descendants are
private to the linked image subsystem and should not be called
directly by any other subsystem."""
# functionality flags
"""Initialize a linked image plugin.
'pname' is the name of the plugin class derived from this
base class.
'linked' is the LinkedImage object initializing this plugin.
"""
return
"""Called when the path to the image that we're operating on
is changing. This normally occurs when we clone an image
after we've planned and prepared to do an operation."""
# return value: None
raise NotImplementedError
def get_altroot(self):
"""If the linked image plugin is able to detect that we're
operating on an image in an alternate root then return the
path of the alternate root."""
# return value: string or None
raise NotImplementedError
"""Return a list of the child images associated with the
current image."""
# return value: list
raise NotImplementedError
"""Get the linked image properties associated with the
specified child image."""
# return value: dict
raise NotImplementedError
"""Attach the specified child image. This operation should
only affect in-memory state of the current image. It should
not update any persistent on-disk linked image state or access
the child image in any way. This routine should assume that
the linked image properties have already been validated."""
# return value: None
raise NotImplementedError
"""Detach the specified child image. This operation should
only affect in-memory state of the current image. It should
not update any persistent on-disk linked image state or access
the child image in any way."""
# return value: None
raise NotImplementedError
def sync_children_todisk(self):
"""Sync out the in-memory linked image state of this image to
disk."""
# return value: tuple:
# (pkgdefs EXIT_* return value, exception object or None)
raise NotImplementedError
class LinkedImageChildPlugin(object):
"""This class is a template that all linked image child plugins should
inherit from. Linked image child plugins derived from this class are
designed to manage linked aspects of children of the current image.
(vs managing linked aspects of the current image itself).
All the interfaces exported by this class and its descendants are
private to the linked image subsystem and should not be called
directly by any other subsystem."""
"""Initialize a linked image child plugin.
'lic' is the LinkedImageChild object initializing this plugin.
"""
return
"""Called before a parent image saves linked image properties
into a child image. Gives the linked image child plugin a
chance to update the properties that will be saved within the
child image."""
# return value: None
raise NotImplementedError
class LinkedImageName(object):
"""A class for naming child linked images. Linked image names are
used for all child images (and only child images), and they encode two
pieces of information. The name of the plugin used to manage the
image and a linked image name. Linked image names have the following
format "<linked_image_plugin>:<linked_image_name>"""
try:
except ValueError:
if not other:
return 1
if other == PV_NAME_NONE:
return 1
if c != 0:
return c
return c
return False
class LinkedImage(object):
"""A LinkedImage object is used to manage the linked image aspects of
an image. This image could be a child image, a parent image, or both
a parent and child. This object allows for access to linked image
properties and also provides routines that allow operations to be
performed on child images."""
# Too many instance attributes; pylint: disable-msg=R0902
# Too many public methods; pylint: disable-msg=R0904
# Properties that a parent image with push children should save locally.
])
# Properties that a pull child image should save locally.
])
# Properties that a parent image with push children should save in
# those children.
])
# make sure there is no invalid overlap
assert not (temporal_props & (
"""Initialize a new LinkedImage object."""
# globals
# variables reset by self.__update_props()
# variables reset by self.reset_recurse()
self.__lic_list = []
# variables reset by self._init_root()
self.__path_ppkgs = None
self.__path_prop = None
self.__path_ppubs = None
# initialize with no properties
# initialize linked image plugin objects
# if the image has a path setup, we can load data from it.
"""Get a pointer to the image object associated with this
linked image object."""
def _init_root(self):
"""Called during object initialization and by
image.py`__set_root() to let us know when we're changing the
root location of the image. (The only time we change the root
path is when changes BEs during operations which clone BEs.
So when this happens most our metadata shouldn't actually
change."""
# save the old root image path
old_root = None
# figure out the new root image path
if new_root == "":
# initialize paths for linked image data files
# if this isn't a reset, then load data from the image
if not old_root:
# we're not linked or we're not changing root paths we're done
return
# get the old altroot directory
# update the altroot property
# Tell linked image plugins about the updated paths
# Unused variable 'plugin'; pylint: disable-msg=W0612
# pylint: enable-msg=W0612
# Tell linked image children about the updated paths
"""Internal helper routine used when we want to update any
linked image properties. This routine sanity check the
new properties, updates them, and resets any cached state
that is affected by property values."""
if props == None:
elif props:
# all temporal properties must exist
# update state
"""Perform internal consistency checks for a set of linked
image properties. Don't update any state."""
# if we're not a child image ourselves, then we're done
return props
# make sure PROP_MODEL was specified
# validate the linked image name
try:
except apx.LinkedImageException:
# make sure PROP_MODEL was specified
if PROP_MODEL not in props:
if model not in model_values:
if model == PV_MODEL_PUSH:
if missing:
if model == PV_MODEL_PULL:
if missing:
def __unset_altroot(props):
"""Given a set of linked image properties, strip out any
altroot properties. This involves removing the altroot
component from the image path property. This is normally done
before we write image properties to disk."""
# get the current altroot
# remove it from the image path
if PROP_PARENT_PATH in props:
# remove it from the parent image path
# delete the current altroot
del props[PROP_ALTROOT]
"""Given a set of linked image properties, the image paths
stored within those properties may not match the actual image
paths if we're executing within an alternate root environment.
We try to detect this condition here, and if this situation
occurs we update the linked image paths to reflect the current
image paths and we fabricate a new linked image altroot
property that points to the new path prefix that was
pre-pended to the image paths."""
# we may have to update the parent image path as well
p_path = None
if PROP_PARENT_PATH in props:
if old_root:
# get the old altroot
# remove the altroot from the image paths
if p_path:
# get the new altroot
else:
# update properties with altroot
if p_path:
props[PROP_PARENT_PATH] = \
def __guess_altroot(self):
"""If we're initializing parent linked image properties for
the first time (or if those properties somehow got deleted)
then we need to know if the parent image that we're currently
operating on is located within an alternate root. One way to
do this is to ask our linked image plugins if they can
determine this (the zones linked image plugin usually can
if the image is a global zone)."""
# ask each plugin if we're operating in an alternate root
p_altroots = []
if p_altroot:
if not p_altroots:
# no altroot suggested by plugins
# check for conflicting altroots
# Unused variable; pylint: disable-msg=W0612
# pylint: enable-msg=W0612
]))
# we have an altroot from our plugins
return altroots[0]
# we have conflicting altroots, time to die
def __fabricate_parent_props(self):
"""Fabricate the minimum set of properties required for a
parent image."""
return props
"""Load linked image properties from disk and return them to
the caller. We sanity check the properties, but we don't
update any internal linked image state.
linked image metadata files, or if we should access temporary
versions (which have ".<runid>" appended to them."""
# read the linked image properties from disk
elif path_exists(path):
else:
return None
# make sure there are no saved temporal properties
# convert PROP_NAME into a linked image name obj
try:
except apx.LinkedImageException:
# sanity check our properties
return props
"""Load linked image parent constraints from disk.
Don't update any internal state.
linked image metadata files, or if we should access temporary
versions (which have ".<runid>" appended to them."""
return frozenset([
])
if path_exists(path):
return frozenset([
])
return None
"""Load linked image parent publishers from disk.
Don't update any internal state.
linked image metadata files, or if we should access temporary
versions (which have ".<runid>" appended to them."""
if path_exists(path):
return None
"""Load linked image properties and constraints from disk.
Update the linked image internal state with the loaded data."""
# load properties
# we're not linked
return
if not props:
# there are no properties on disk but we're a parent
# image so we're missing properties. oops. rather
# than die (which would prevent the user from being
# able to fix the problem) fabricate up some props
# with reasonably guessed values which the user can
else:
# load parent publisher data. if publisher data is missing
# continue along and we'll just skip the publisher checks,
# it's better than failing and preventing any image updates.
def __validate_prop_recurse(v):
"""Verify property value for PROP_RECURSE."""
return True
return True
return False
"""Validate user supplied linked image attach properties.
Don't update any internal state."""
# make sure that only attach time options have been
# specified, and that they have allowed values.
validate_props = {
}
if model == PV_MODEL_PUSH:
else:
assert model == PV_MODEL_PULL
errs = []
# check each property the user specified.
# did the user specify an allowable property?
if k not in validate_props:
attach_bad_prop=k))
continue
# did the user specify a valid property value?
if not validate_props[k](v):
attach_bad_prop_value=(k, v)))
continue
# is this property valid for this type of image?
if k not in allowed_props:
attach_bad_prop=k))
continue
if errs:
"""Initialize an Image object which can be used to access a
parent image."""
try:
except OSError:
try:
except apx.ImageNotFoundException:
return pimg
"""Return the altroot path prefix for the current image."""
def nothingtodo(self):
"""If our in-memory linked image state matches the on-disk
linked image state then there's nothing to do. If the state
differs then there is stuff to do since the new state needs
to be saved to disk."""
# compare in-memory and on-disk properties
if li_ondisk_props == None:
li_ondisk_props = dict()
if li_ondisk_props != li_inmemory_props:
return False
# compare in-memory and on-disk constraints
if li_ondisk_ppkgs == None:
return False
# compare in-memory and on-disk parent publishers
return False
return True
"""Return publisher information for the specified image. If
no image is specified we return publisher information for the
current image.
Publisher information is returned in a sorted list of lists
of the format:
<publisher name>, <sticky>
Where:
<publisher name> is a string
<sticky> is a boolean
The tuples are sorted by publisher rank.
"""
# default to ourselves
if img == None:
# get a sorted list of the images publishers
rv = []
for p in pubs:
return rv
"""If we're a child image's, verify that the parent image
publisher configuration is a subset of the child images
publisher configuration. This means that all publishers
configured within the parent image must also be configured
within the child image with the same:
- publisher rank
- sticky and disabled settings
The child image may have additional publishers configured but
they must all be lower ranked than the parent's publishers.
"""
# if we're not a child image then bail
return
# if we're using the sysrepo then don't bother
return
# we don't need to do a pubcheck for detach
return
if ppubs == None:
# parent publisher data is missing, press on and hope
# for the best.
return
# child image needs at least as many publishers as the parent
raise apx.PlanCreationException(
# check rank, sticky, and disabled settings
if p == pp:
continue
raise apx.PlanCreationException(
"""Update linked image constraint, publisher data, and
state from our parent image."""
# we're not a child image, nothing to do
return
# parent pushes data to us, nothing to do
return
# initalize the parent image
# generate new constraints
# generate new publishers
# check if anything has changed
# we have new constraints
# parent has new publishers
if not need_sync:
# nothing changed
return
# if we're not planning an image attach operation then write
# the linked image metadata to disk.
"""Write in-memory linked image state to disk."""
# create a list of metadata file paths
# cleanup any temporary files
# we're no longer linked; delete metadata
return
# save our properties, but first remove altroot path prefixes
# and any temporal properties
# if we're not a child we don't have constraints
return
# we're a child so save our latest constraints
def child_name(self):
"""If the current image is a child image, this function
returns a linked image name object which represents the name
of the current image."""
raise self.__apx_not_child()
"""Indicates whether the current image is a child image."""
"""Indicates whether the current image is a parent image."""
"""Return a dictionary which represents the linked image
properties associated with a linked image.
'lin' is the name of the child image. If lin is None then
the current image is assumed to be a linked image and it's
properties are returned.
Always returns a copy of the properties in case the caller
tries to update them."""
if lin == None:
# If we're not linked we'll return an empty
# dictionary. That's ok.
# make sure the specified child exists
# make a copy of the props in case they are updated
# add temporal properties
return props
def __apx_not_child(self):
"""Raise an exception because the current image is not a child
image."""
"""Check if a specific child image exists."""
for i in self.__list_children():
if i[0] == lin:
return True
if raise_except:
return False
def parent_fmris(self):
"""A set of the fmris installed in our parent image."""
# We return None since frozenset() would indicate
# that there are no packages installed in the parent
# image.
return None
"""Given a string representing a linked image child name,
returns linked image name object representing the same name.
'allow_unknown' indicates whether the name must represent
actual children or simply be syntactically correct."""
if not allow_unknown:
return lin
"""Returns a list of linked child images associated with the
current image.
'li_ignore' see list_related() for a description.
The returned value is a list of tuples where each tuple
contains (<li name>, <li path>)."""
if li_ignore == []:
# ignore all children
return []
li_children = [
]
# sort by linked image name
if li_ignore == None:
# don't ignore any children
return li_children
errs = [
]
if errs:
return [
]
"""Returns a list of linked images associated with the
current image. This includes both child and parent images.
'li_ignore' is either None or a list. If it's None (the
default), all children will be listed. If it's an empty list
no children will be listed. Otherwise, any children listed
in li_ignore will be ommited from the results.
The returned value is a list of tuples where each tuple
contains (<li name>, <relationship>, <li path>)."""
li_list = [
]
# we're not linked
return []
# we're linked so append ourself to the list
# if we have a path to our parent then append that as well.
# sort by linked image name
return li_list
"""We only update in-memory state; nothing is written to
disk, to sync linked image state to disk call syncmd."""
if props == None:
raise apx.LinkedImageException(
# Path must be an absolute path.
# we don't bother to cleanup the path to the parent image here
# because when we allocate an Image object for the parent
# image, it will do that work for us.
# make sure we're not linking to ourselves
# make sure we're not linking the root image as a child
raise apx.LinkedImageException(
# get the cleaned up parent image path.
# If we're in an alternate root, the parent must also be within
# that alternate root.
raise apx.LinkedImageException(
# make a copy of the properties
if k not in self.__pull_child_props:
# this prop doesn't apply to pull images
continue
if k not in props:
props[k] = v
"""We only update in memory state; nothing is written to
disk, to sync linked image state to disk call syncmd."""
if not force:
raise apx.LinkedImageException(
if not lip.support_detach:
raise apx.LinkedImageException(
# Generate a new set of linked image properties. If we have
# no children then we don't need any more properties.
props = None
# If we have children we'll need to keep some properties.
strip = prop_values - \
# Update our linked image properties.
"""Determine if an image is in sync with its constraints."""
sync_fmris = []
# get parent dependencies from the catalog
parent_deps = [
a
if a.name == "depend" and \
]
# get extra parent dependencies
parent_deps += [
a
if a.name == "depend" and \
]
if parent_deps:
if not sync_fmris:
# No packages to sync
return True
# create a dictionary of packages installed in the parent
ppkgs_dict = dict([
])
for fmri in sync_fmris:
return False
return False
return True
"""If the current image is a child image, this function
audits the current image to see if it's in sync with its
parent."""
try:
if li_parent_sync:
# try to refresh linked image constraints from
# the parent image.
except apx.LinkedImageException, e:
return (e.lix_exitrv, e)
e = apx.LinkedImageException(
return (pkgdefs.EXIT_DIVERGED, e)
"""Internal helper function that takes a dictionary returned
from an operations on multiple children and merges the results
into a single return code."""
assert type(k) == LinkedImageName
assert err is None or \
if not rvdict:
if not rv_map:
if (rv_seen == rv_map_set):
return (rv_map_rv, None)
# keep track of all the return values that are mapped
# the mappings better have included pkgdefs.EXIT_OK
# if we had errors for unmapped return values, bundle them up
errs = [
e
]
if errs:
else:
err = None
# we have one consistent return value
"""Convenience function that takes a dictionary returned from
an operations on multiple children and merges the results into
a single return code."""
rv_map = [
]
"""Convenience function that takes a dictionary returned from
an operations on multiple children and merges the results into
a single return code."""
rv_map = [
]
"""Convenience function that takes a dictionary returned from
an operations on multiple children and merges the results into
a single return code."""
"""Sanity check the parameters associated with a child image
that we are trying to attach."""
# check the name to make sure it doesn't already exist
# Path must be an absolute path.
# If we're in an alternate root, the child must also be within
# that alternate root
raise apx.LinkedImageException(
# path must be an image
try:
except OSError:
if not img_prefix:
# Does the parent image (ourselves) reside in clonable BE?
# Unused variable 'be_uuid'; pylint: disable-msg=W0612
# pylint: enable-msg=W0612
if be_name:
else:
# If the parent image is clonable then the new child image
# must be nested within the parents filesystem namespace.
raise apx.LinkedImageException(
# Find the common parent directory of the both parent and the
# child image.
# Make sure there are no additional images in between the
# parent and the child. (Ie, prevent linking of images if one
# of the images is nested within another unrelated image.)
# This is done by looking at all the parent directories for
# both the parent and the child image until we reach a common
# ancestor.
# First check the parent directories of the child.
try:
except OSError, e:
# W0212 Access to a protected member
# pylint: disable-msg=W0212
raise apx._convert_error(e)
if not tmp:
continue
# Then check the parent directories of the parent.
try:
except OSError, e:
# W0212 Access to a protected member
# pylint: disable-msg=W0212
raise apx._convert_error(e)
if not tmp:
continue
# Child image should not already be linked
try:
except OSError, e:
# W0212 Access to a protected member
# pylint: disable-msg=W0212
raise apx._convert_error(e)
if exists and not allow_relink:
"""Attach an image as a child to the current image (the
current image will become a parent image. This operation
results in attempting to sync the child image with the parent
image."""
# Too many arguments; pylint: disable-msg=R0913
# Too many return statements; pylint: disable-msg=R0911
if props == None:
if li_md_only:
e = apx.LinkedImageException(
return (e.lix_exitrv, e)
# Path must be an absolute path.
return (e.lix_exitrv, e)
# cleanup specified path
try:
except OSError, e:
return (e.lix_exitrv, e)
# make sure we're not linking to ourselves
# make sure we're not linking the root image as a child
raise apx.LinkedImageException(
# if the current image isn't linked yet then we need to
# generate some linked image properties for ourselves
# sanity check the input
try:
except apx.LinkedImageException, e:
return (e.lix_exitrv, e)
# make a copy of the options and start updating them
# fill in any missing defaults options
if k not in child_props:
child_props[k] = v
# attach the child in memory
if noexecute and li_md_only:
# we've validated parameters, nothing else to do
# update the child
try:
except apx.LinkedImageException, e:
return (e.lix_exitrv, e)
return (rv, e)
if noexecute:
# if noexecute then we're done
# save child image properties
if e:
# save parent image properties
"""Audit one or more children of the current image to see if
they are in sync with this image."""
"""Sync one or more children of the current image."""
"""Detach one or more children from the current image. This
operation results in the removal of any constraint package
from the child images."""
# get parameter meant for __detach_child()
if "force" in kwargs:
if "noexecute" in kwargs:
# expand lin_list before calling __detach_child()
if not lin_list:
# if the detach failed leave metadata in parent
# Unused variable 'rv'; pylint: disable-msg=W0612
# pylint: enable-msg=W0612
assert e == None or \
if e and not force:
continue
# detach the child in memory
if not noexecute:
# sync out the fact that we detached the child
assert e2 == None or \
if not e:
# don't overwrite previous errors
# we're not linked anymore, so delete all our linked
# properties.
return rvdict
"""Perform a linked image operation on multiple children."""
assert "lin" not in kwargs
assert "lic" not in kwargs
if not lin_list:
try:
# perform the requested operation
# Unused variable; pylint: disable-msg=W0612
# pylint: enable-msg=W0612
assert e == None or \
except apx.LinkedImageException, e:
return rvdict
"""Recurse into a child image and audit it."""
# Unused argument 'lin'; pylint: disable-msg=W0613
return lic.child_audit()
"""Recurse into a child image and sync it."""
# Too many arguments; pylint: disable-msg=R0913
# Unused argument 'lin'; pylint: disable-msg=W0613
# no new constraints currently implies no pkg updates.
if li_md_only:
# now try to sync the image
return lic.child_sync(
progtrack=None):
"""Recurse into a child image and detach it."""
# we can't detach this type of image.
e = apx.LinkedImageException(
# remove linked data from the child
def reset_recurse(self):
"""Reset all child recursion state."""
self.__lic_list = []
update_index, args):
"""When planning changes on a parent image, prepare to
recurse into all child images and operate on them as well."""
# we don't need to recurse for these operations
self.__lic_list = []
return
# don't bother to recurse into children
self.__lic_list = []
return
self.__lic_list = []
# Unused variable 'path'; pylint: disable-msg=W0612
# pylint: enable-msg=W0612
if not self.__lic_list:
# no child images to recurse into
return
# given the api operation being performed on the
# current image, figure out what api operation should
# be performed on child images.
else:
"""When planning changes within a parent image, recurse into
all child images and operate on them as well."""
def recurse_nothingtodo(self):
"""Return True if there is no planned work to do on child
image."""
if not lic.child_nothingtodo():
return False
return True
"""Check if a package has a parent dependency."""
return True
return False
"""Since we don't publish packages with parent dependencies
yet, but we want to be able to sync packages between zones,
we'll need to fake up some extra package parent dependencies.
Here we'll inspect the catalog to find packages that we think
should have parent dependencies and then we'll return a
dictionary, indexed by fmri, which contains the extra
dependency actions that should be added to each package."""
# create a parent dependency action with a nonglobal zone
# variant tag.
# Used * or ** magic; pylint: disable-msg=W0142
# pylint: enable-msg=W0142
# we're not operating on a nonglobal zone image so we
# don't need to fabricate parent zone dependencies
return dict()
# we're not a child image so parent dependencies are
# irrelevant
return dict()
else:
# assume that the osnet and ips incorporations should always
# have a parent dependencies.
for tgt in [
excludes):
if (a.name != "depend") or \
continue
# create an fmri for the incorporated package
a.attrs["fmri"],
# translate the incorporated package fmris into actual
# packages in the known catalog
# all the fmris we want to add dependencies to.
# eliminate renamed or obsoleted fmris
class LinkedImageChild(object):
"""A LinkedImageChild object is used when a parent image wants to
access a child image. These accesses may include things like:
auditing a child image, or recursing into a child image to keep it in
sync with planned changes in the parent image."""
# Too many instance attributes; pylint: disable-msg=R0902
# globals
# cache properties.
try:
except OSError:
raise apx.LinkedImageException(
if not imgdir:
raise apx.LinkedImageException(
# initialize paths for linked image data files
# initialize a linked image child plugin
# variables reset by self.child_reset_recurse()
self.__r_progtrack = None
def child_name(self):
"""Get the path associated with a child image."""
def child_path(self):
"""Get the path associated with a child image."""
def child_pimage(self):
"""Get a pointer to the parent image object associated with
this child."""
"""Write data to a child image."""
# first save our data to a temporary file
# check if we're updating the data
try:
except OSError, e:
# W0212 Access to a protected member
# pylint: disable-msg=W0212
raise apx._convert_error(e)
if exists:
try:
except OSError, e:
# W0212 Access to a protected member
# pylint: disable-msg=W0212
raise apx._convert_error(e)
# if we're not actually updating any data, or if we were just
# doing a test to see if the data has changed, then delete the
# temporary data file
return updated
if not tmp:
# we are updating the real data.
try:
except OSError, e:
# W0212 Access to a protected member
# pylint: disable-msg=W0212
raise apx._convert_error(e)
return True
"""Sync linked image parent constraint data to a child image.
linked image metadata files, or if we should access temporary
versions (which have ".<runid>" appended to them."""
# there has to be an image plan to export
# if there's an image plan the we need to update the
# installed packages based on that plan.
continue
if src:
if dst:
# paranoia
# save the planned cips
"""Sync linked image properties data to a child image.
linked image metadata files, or if we should access temporary
versions (which have ".<runid>" appended to them."""
# make a copy of the props we want to push
assert PROP_PARENT_PATH not in props
# delete temporal properties
"""Sync linked image parent publisher data to a child image.
linked image metadata files, or if we should access temporary
versions (which have ".<runid>" appended to them."""
"""Sync linked image data to a child image.
linked image metadata files, or if we should access temporary
versions (which have ".<runid>" appended to them."""
if ip:
def __flush_output():
"""We flush stdout and stderr before and after operating on
child images to avoid any out-of-order output problems that
could be caused by caching of output."""
try:
except IOError:
pass
except OSError, e:
# W0212 Access to a protected member
# pylint: disable-msg=W0212
raise apx._convert_error(e)
try:
except IOError:
pass
except OSError, e:
# W0212 Access to a protected member
# pylint: disable-msg=W0212
raise apx._convert_error(e)
"""Perform a pkg(1) operation on a child image."""
if stage == None:
#
# Build up a command line to execute. Note that we take care
# to try to run the exact same pkg command that we were
# executed with. We do this because pkg commonly tries to
# access the image that the command is being run from.
#
pkg_bin = "pkg"
try:
# check if the currently running pkg command
# exists and is accessible.
except OSError:
pass
pkg_cmd = [
]
# propagate certain debug options
for k in [
"allow-overlays",
"broken-conflicting-action-handling",
"disp_linked_cmds",
"plan"]:
if DebugValues[k]:
# add the subcommand argument
# propagate stage option
# add the subcommand argument options
if progtrack:
# flush all output before recursing into child
if not disp_linked_cmds and \
if disp_linked_cmds:
else:
# Start the operation on the child
try:
p.wait()
except OSError, e:
# W0212 Access to a protected member
# pylint: disable-msg=W0212
raise apx._convert_error(e)
# flush all output generated by the child
if progtrack:
return (p.returncode, pkg_cmd)
"""Detach a child image."""
# When issuing a detach from a prent we must always use the
# force flag. (Normally a child will refuse to detach from a
# parent unless it attached to the parent, which is never the
# case here.)
pkg_args = ["-f"]
if noexecute:
# Unused variable 'pkg_cmd'; pylint: disable-msg=W0612
# pylint: enable-msg=W0612
# if the detach command ran, return its status.
return (rv, e)
def child_audit(self):
"""Audit a child image to see if it's in sync with its
constraints."""
# first sync our metadata
# recurse into the child image
pkg_args = ["-q"]
# Unused variable 'pkg_cmd'; pylint: disable-msg=W0612
# pylint: enable-msg=W0612
# if the audit command ran, return its status.
return (rv, None)
# something went unexpectedly wrong.
return (rv, e)
"""Try to bring a child image into sync with its
constraints."""
# Too many arguments; pylint: disable-msg=R0913
if li_md_only:
# we're not going to recurse into the child image,
# we're just going to update its metadata.
try:
except apx.LinkedImageException, e:
return (e.lix_exitrv, e)
if updated:
else:
# first sync the metadata
try:
# if we're doing this sync as part of an attach, then
# temporarily sync the metadata since we don't know
# yet if the attach will succeed. if the attach
# doesn't succeed this means we don't have to delete
# any metadata. if the attach succeeds the child will
# make the temporary metadata permanent as part of the
# commit.
except apx.LinkedImageException, e:
return (e.lix_exitrv, e)
pkg_args = []
if noexecute:
if accept:
if show_licenses:
if not refresh_catalogs:
if not update_index:
if not li_pkg_updates:
# Unused variable 'pkg_cmd'; pylint: disable-msg=W0612
# pylint: enable-msg=W0612
# if the audit command ran, return its status.
return (rv, None)
# something went unexpectedly wrong.
return (rv, e)
"""Our image path is being updated, so figure out our new
child image paths. This interface only gets invoked when:
- We're doing a packaging operation on a parent image and
we've just cloned that parent to create a new BE that we're
going to update. This clone also cloned all the children
and so now we need to update our paths to point to the newly
created children.
- We tried to update a cloned image (as described above) and
our update failed, hence we're changing paths back to the
original images that were the source of the clone."""
# get the image path without the altroot
# update the path with the current altroot
# update properties with altroot
# we don't bother to update update PROP_PARENT_PATH since
# that is only used when reading constraint data from the
# parent image, and this interface is only invoked when we're
# starting or finishing execution of a plan on a cloned image
# (at which point we have no need to access the parent
# anymore).
def child_nothingtodo(self):
"""Check if there are any changes planned for a child
image."""
return self.__r_rv_nop
def child_reset_recurse(self):
"""Reset child recursion state for child."""
self.__r_progtrack = None
update_index, args):
"""When planning changes on a parent image, prepare to
recurse into a child image."""
pkg_args = []
if noexecute:
# W0511 XXX / FIXME Comments; pylint: disable-msg=W0511
# XXX: also need to support --licenses.
# pylint: enable-msg=W0511
if accept:
if not refresh_catalogs:
if not update_index:
# options specific to: attach, set-property-linked, sync
# skip ipkg up to date check for child images
"""When planning changes within a parent image, recurse into
a child image."""
# sync our metadata
if self.__r_rv_nop:
# the child image told us it has no changes planned.
# common case (we hope)
pass
else:
raise apx.LinkedImageException(
# we're done with this operation
return rv
# ---------------------------------------------------------------------------
# Utility Functions
#
"""Save JSON encoded linked image metadata to a file."""
# make sure the directory we're about to save data into exists.
try:
# write the output to a temporary file
# atomically create the desired file
except OSError, e:
# W0212 Access to a protected member
# pylint: disable-msg=W0212
raise apx._convert_error(e)
"""Load JSON encoded linked image metadata from a file."""
try:
return missing_val
except OSError, e:
# W0212 Access to a protected member
# pylint: disable-msg=W0212
raise apx._convert_error(e)
return data
"""Utility class used when json encoding linked image metadata."""
# E0202 An attribute inherited from JSONEncoder hide this method
# pylint: disable-msg=E0202
"""Required routine that overrides the default base
class version. This routine must serialize 'obj' when
attempting to save 'obj' json format."""
def PkgDecoder(dct):
"""Utility class used when json decoding linked image metadata."""
rvdct = {}
# unicode must die
k = k.encode("utf-8")
v = v.encode("utf-8")
# convert boolean strings values back into booleans
if v.lower() == "true":
v = True
elif v.lower() == "false":
v = False
rvdct[k] = v
return rvdct
def rm_dict_ent(d, keys):
"""Remove a set of keys from a dictionary."""
return dict([
(k, v)
for k, v in d.iteritems()
if k not in keys
])
bad_cp=None,
bad_iup=None,
bad_lin_type=None,
bad_prop=None,
missing_props=None,
multiple_altroots=None,
saved_temporal_props=None):
"""Oops. We hit a runtime error. Die with a nice informative
message. Note that runtime errors should never happen and usually
indicate bugs (or possibly corrupted linked image metadata), so they
are not localized (just like asserts are not localized)."""
# Too many arguments; pylint: disable-msg=R0913
if bad_cp:
assert err == None
elif bad_iup:
assert err == None
elif bad_lin_type:
assert err == None
elif bad_prop:
assert err == None
elif missing_props:
assert err == None
err = "Missing required linked properties: %s" % \
elif multiple_altroots:
assert err == None
err = "Multiple plugins reported different altroots:"
elif saved_temporal_props:
assert err == None
err = "Found saved temporal linked properties: %s" % \
else:
assert err != None
if li:
if lic:
err_prefix = "Linked image error: "
if lin:
err_suffix = ""
elif path:
raise RuntimeError(
# ---------------------------------------------------------------------------
# Functions for accessing files in the current root
#
def path_exists(path):
"""Simple wrapper for accessing files in the current root."""
try:
except OSError, e:
# W0212 Access to a protected member
# pylint: disable-msg=W0212
raise apx._convert_error(e)
def path_isdir(path):
"""Simple wrapper for accessing files in the current root."""
try:
except OSError, e:
# W0212 Access to a protected member
# pylint: disable-msg=W0212
raise apx._convert_error(e)
"""Simple wrapper for accessing files in the current root."""
try:
except OSError, e:
# W0212 Access to a protected member
# pylint: disable-msg=W0212
raise apx._convert_error(e)
"""Simple wrapper for accessing files in the current root."""
try:
except OSError, e:
# W0212 Access to a protected member
# pylint: disable-msg=W0212
raise apx._convert_error(e)
# ---------------------------------------------------------------------------
# Functions for managing images which may be in alternate roots
#
"""Check if 'path' is nested within 'altroot'"""
# make sure both paths have one trailing os.sep.
# check for nested or equal paths
return True
return False
"""Return a path where 'path' is nested within 'altroot'"""
# sanity check
return altroot_path
"""Return the relative porting of 'path', which must be nested within
'altroot'"""
if rv == "":
rv = "/"
return rv
"""Given 'path', and a relative path 'path_suffix' that must match
the suffix of 'path', return the unmatched prefix of 'path'."""
# make sure both paths have one trailing os.sep.
if i <= 0:
# path and path_suffix are either unrelated or equal
else:
# sanity check
return altroot