Esempio n. 1
0
 def test_multiple_entries(self):
     '''Ensure multiple criteria handled correctly'''
     criteria = ['ARCH=i86pc', 'MEM=1024', 'IPV4=129.224.45.185',
               'PLATFORM=SUNW,Sun-Fire-T1000',
               'MAC=0:14:4F:20:53:94-0:14:4F:20:53:A0']
     cri_dict = publish_manifest.criteria_to_dict(criteria)
     self.assertEquals(len(cri_dict), 5)
     self.assertTrue(cri_dict['arch'], 'i86pc')
     self.assertTrue(cri_dict['mem'], '1024')
     self.assertTrue(cri_dict['ipv4'], '129.224.45.185')
     self.assertTrue(cri_dict['platform'], 'sunw,sun-fire-t1000')
     self.assertTrue(cri_dict['mac'], '0:14:4f:20:53:94-0:14:4f:20:53:a0')
 def test_multiple_entries(self):
     '''Ensure multiple criteria handled correctly'''
     criteria = ['ARCH=i86pc', 'MEM=1024', 'IPV4=129.224.45.185',
               'PLATFORM=SUNW,Sun-Fire-T1000',
               'MAC=0:14:4F:20:53:94-0:14:4F:20:53:A0']
     cri_dict = publish_manifest.criteria_to_dict(criteria)
     self.assertEquals(len(cri_dict), 5)
     self.assertTrue(cri_dict['arch'], 'i86pc')
     self.assertTrue(cri_dict['mem'], '1024')
     self.assertTrue(cri_dict['ipv4'], '129.224.45.185')
     self.assertTrue(cri_dict['platform'], 'sunw,sun-fire-t1000')
     self.assertTrue(cri_dict['mac'], '0:14:4f:20:53:94-0:14:4f:20:53:a0')
Esempio n. 3
0
def do_create_profile(cmd_options=None):
    ''' external entry point for installadm
    Arg: cmd_options - command line options
    Effect: add profiles to database per command line
    Raises SystemExit if condition cannot be handled
    '''
    # check for authorization and euid
    try:
        check_auth_and_euid(PROFILE_AUTH)
    except UnauthorizedUserError as err:
        raise SystemExit(err)

    options = parse_options(DO_CREATE, cmd_options)

    # get AI service image path and database name
    service = AIService(options.service_name)
    image_dir = service.image.path
    dbname = service.database_path

    # open database
    dbn = AIdb.DB(dbname, commit=True)
    dbn.verifyDBStructure()
    queue = dbn.getQueue()
    root = None
    criteria_dict = dict()

    # Handle old DB versions which did not store a profile.
    if not AIdb.tableExists(queue, AIdb.PROFILES_TABLE):
        raise SystemExit(
            _("Error:\tService %s does not support profiles") %
            options.service_name)
    try:
        if options.criteria_file:  # extract criteria from file
            root = df.verifyCriteria(df.DataFiles.criteriaSchema,
                                     options.criteria_file, dbn,
                                     AIdb.PROFILES_TABLE)
        elif options.criteria_c:
            # if we have criteria from cmd line, convert into dictionary
            criteria_dict = pub_man.criteria_to_dict(options.criteria_c)
            root = df.verifyCriteriaDict(df.DataFiles.criteriaSchema,
                                         criteria_dict, dbn,
                                         AIdb.PROFILES_TABLE)
    except ValueError as err:
        raise SystemExit(_("Error:\tcriteria error: %s") % err)
    # Instantiate a Criteria object with the XML DOM of the criteria.
    criteria = df.Criteria(root)
    sc.validate_criteria_from_user(criteria, dbn, AIdb.PROFILES_TABLE)
    # track exit status for all profiles, assuming no errors
    has_errors = False

    # loop through each profile on command line
    for profile_file in options.profile_file:
        # take option name either from command line or from basename of profile
        if options.profile_name:
            profile_name = options.profile_name
        else:
            profile_name = os.path.basename(profile_file)
        # check for any scope violations
        if sc.is_name_in_table(profile_name, queue, AIdb.PROFILES_TABLE):
            print >> sys.stderr, \
                    _("Error:  A profile named %(name)s is already in the "
                      "database for service %(service)s.") % \
                      {'name': profile_name, 'service': options.service_name}
            has_errors = True
            continue
        # open profile file specified by user on command line
        if not os.path.exists(profile_file):
            print >> sys.stderr, _("File %s does not exist") % profile_file
            has_errors = True
            continue

        # validates the profile and report errors if found
        raw_profile = df.validate_file(profile_name,
                                       profile_file,
                                       image_dir,
                                       verbose=False)
        if not raw_profile:
            has_errors = True
            continue

        # create file from profile string and report failures
        full_profile_path = copy_profile_internally(raw_profile)
        if not full_profile_path:
            has_errors = True
            continue

        # add new profile to database
        if not add_profile(criteria, profile_name, full_profile_path, queue,
                           AIdb.PROFILES_TABLE):
            os.unlink(full_profile_path)  # failure, back out internal profile
            has_errors = True
    # exit with status if any errors in any profiles
    if has_errors:
        sys.exit(1)
 def test_no_criteria(self):
     '''Ensure case of no criteria is handled'''
     criteria = []
     cri_dict = publish_manifest.criteria_to_dict(criteria)
     self.assertEquals(len(cri_dict), 0)
     self.assertTrue(isinstance(cri_dict, dict))
 def test_list_values(self):
     '''Ensure lists are saved correctly'''
     criteria = ['zonename="z1 z2 Z3"']
     cri_dict = publish_manifest.criteria_to_dict(criteria)
     self.assertEquals(len(cri_dict), 1)
     self.assertTrue(cri_dict['zonename'], 'z1 z2 Z3')
 def test_range_values(self):
     '''Ensure ranges saved correctly'''
     criteria = ['mem=1048-2096']
     cri_dict = publish_manifest.criteria_to_dict(criteria)
     self.assertEquals(len(cri_dict), 1)
     self.assertTrue(cri_dict['mem'], '1048-2096')
 def test_case_conversion(self):
     '''Ensure keys and converted to lower case, values kept as input'''
     criteria = ['ARCH=Sparc']
     cri_dict = publish_manifest.criteria_to_dict(criteria)
     self.assertEquals(len(cri_dict), 1)
     self.assertEquals(cri_dict['arch'], 'Sparc')
Esempio n. 8
0
 def test_no_criteria(self):
     '''Ensure case of no criteria is handled'''
     criteria = []
     cri_dict = publish_manifest.criteria_to_dict(criteria)
     self.assertEquals(len(cri_dict), 0)
     self.assertTrue(isinstance(cri_dict, dict))
Esempio n. 9
0
 def test_list_values(self):
     '''Ensure lists are saved correctly'''
     criteria = ['zonename="z1 z2 Z3"']
     cri_dict = publish_manifest.criteria_to_dict(criteria)
     self.assertEquals(len(cri_dict), 1)
     self.assertTrue(cri_dict['zonename'], 'z1 z2 Z3')
Esempio n. 10
0
 def test_range_values(self):
     '''Ensure ranges saved correctly'''
     criteria = ['mem=1048-2096']
     cri_dict = publish_manifest.criteria_to_dict(criteria)
     self.assertEquals(len(cri_dict), 1)
     self.assertTrue(cri_dict['mem'], '1048-2096')
Esempio n. 11
0
 def test_case_conversion(self):
     '''Ensure keys and converted to lower case, values kept as input'''
     criteria = ['ARCH=Sparc']
     cri_dict = publish_manifest.criteria_to_dict(criteria)
     self.assertEquals(len(cri_dict), 1)
     self.assertEquals(cri_dict['arch'], 'Sparc')
Esempio n. 12
0
def do_set_criteria(cmd_options=None):
    '''
    Modify the criteria associated with a manifest.

    '''
    # check that we are root
    if os.geteuid() != 0:
        raise SystemExit(
            _("Error: Root privileges are required for "
              "this command."))

    options = parse_options(cmd_options)

    # Get the install service's properties.
    if not config.is_service(options.service_name):
        raise SystemExit(_("Failed to find service %s") % options.service_name)

    service = AIService(options.service_name)
    database = service.database_path

    # Open the database
    dbn = AIdb.DB(database, commit=True)

    # Check to make sure that the manifest whose criteria we're
    # updating exists in the install service.
    if (options.manifest_name and
            not check_published_manifest(service, dbn, options.manifest_name)):
        raise SystemExit(1)

    # Process and validate criteria from -a, -c, or -C, and store
    # store the criteria in a Criteria object.
    try:
        if options.criteria_file:
            root = df.verifyCriteria(df.DataFiles.criteriaSchema,
                                     options.criteria_file, dbn,
                                     AIdb.MANIFESTS_TABLE)
        elif options.criteria_a:
            criteria_dict = pub_man.criteria_to_dict(options.criteria_a)
            root = df.verifyCriteriaDict(df.DataFiles.criteriaSchema,
                                         criteria_dict, dbn,
                                         AIdb.MANIFESTS_TABLE)
        elif options.criteria_c:
            criteria_dict = pub_man.criteria_to_dict(options.criteria_c)
            root = df.verifyCriteriaDict(df.DataFiles.criteriaSchema,
                                         criteria_dict, dbn,
                                         AIdb.MANIFESTS_TABLE)
        else:
            raise SystemExit("Error: Missing required criteria.")

    except (AssertionError, IOError, ValueError) as err:
        raise SystemExit(err)
    except (lxml.etree.LxmlError) as err:
        raise SystemExit(_("Error:\tmanifest error: %s") % err)

    # Instantiate a Criteria object with the XML DOM of the criteria.
    criteria = df.Criteria(root)

    if options.manifest_name:
        # Ensure the criteria we're adding/setting for this manifest doesn't
        # cause a criteria collision in the DB.
        colliding_criteria = pub_man.find_colliding_criteria(
            criteria, dbn, exclude_manifests=[options.manifest_name])
        # If we're appending criteria pass the manifest name
        if options.criteria_a:
            pub_man.find_colliding_manifests(
                criteria,
                dbn,
                colliding_criteria,
                append_manifest=options.manifest_name)
        else:
            pub_man.find_colliding_manifests(criteria,
                                             dbn,
                                             colliding_criteria,
                                             append_manifest=None)
    # validate criteria for profile
    for pname in options.profile_name:
        if not sc.is_name_in_table(pname, dbn.getQueue(), AIdb.PROFILES_TABLE):
            raise SystemExit(
                _("Error:\tservice has no profile named %s." % pname))
        # Validate profile criteria
        sc.validate_criteria_from_user(criteria, dbn, AIdb.PROFILES_TABLE)

    # all validation complete - update database

    # indicate whether criteria are added or replaced
    if options.criteria_a:
        append = True  # add new criteria
    else:
        append = False  # replace any existing criteria with new
    if options.manifest_name:
        # Update the criteria for manifest
        set_criteria(criteria, options.manifest_name, dbn,
                     AIdb.MANIFESTS_TABLE, append)
        print >> sys.stderr, _("Criteria updated for manifest %s.") % \
                options.manifest_name
    for pname in options.profile_name:
        # Update the criteria for profile
        set_criteria(criteria, pname, dbn, AIdb.PROFILES_TABLE, append)
        print >> sys.stderr, _("Criteria updated for profile %s.") % pname
def do_set_criteria(cmd_options=None):
    '''
    Modify the criteria associated with a manifest.

    '''
    # check that we are root
    if os.geteuid() != 0:
        raise SystemExit(_("Error: Root privileges are required for "
                           "this command."))

    options = parse_options(cmd_options)

   # Get the install service's properties.
    if not config.is_service(options.service_name):
        raise SystemExit(_("Failed to find service %s") % options.service_name)
    
    service = AIService(options.service_name)
    database = service.database_path
    
    # Open the database
    dbn = AIdb.DB(database, commit=True)
    
    # Check to make sure that the manifest whose criteria we're
    # updating exists in the install service.
    if (options.manifest_name and not
        check_published_manifest(service, dbn, options.manifest_name)):
        raise SystemExit(1)

    # Process and validate criteria from -a, -c, or -C, and store
    # store the criteria in a Criteria object.
    try:
        if options.criteria_file:
            root = df.verifyCriteria(df.DataFiles.criteriaSchema,
                    options.criteria_file, dbn, AIdb.MANIFESTS_TABLE)
        elif options.criteria_a:
            criteria_dict = pub_man.criteria_to_dict(options.criteria_a)
            root = df.verifyCriteriaDict(df.DataFiles.criteriaSchema,
                    criteria_dict, dbn, AIdb.MANIFESTS_TABLE)
        elif options.criteria_c:
            criteria_dict = pub_man.criteria_to_dict(options.criteria_c)
            root = df.verifyCriteriaDict(df.DataFiles.criteriaSchema,
                    criteria_dict, dbn, AIdb.MANIFESTS_TABLE)
        else:
            raise SystemExit("Error: Missing required criteria.")

    except (AssertionError, IOError, ValueError) as err:
        raise SystemExit(err)
    except (lxml.etree.LxmlError) as err:
        raise SystemExit(_("Error:\tmanifest error: %s") % err)

    # Instantiate a Criteria object with the XML DOM of the criteria.
    criteria = df.Criteria(root)

    if options.manifest_name:
        # Ensure the criteria we're adding/setting for this manifest doesn't
        # cause a criteria collision in the DB.
        colliding_criteria = pub_man.find_colliding_criteria(criteria, dbn,
                             exclude_manifests=[options.manifest_name])
        # If we're appending criteria pass the manifest name
        if options.criteria_a:
            pub_man.find_colliding_manifests(criteria, dbn, colliding_criteria,
                    append_manifest=options.manifest_name)
        else:
            pub_man.find_colliding_manifests(criteria, dbn, colliding_criteria,
                                             append_manifest=None)
    # validate criteria for profile
    for pname in options.profile_name:
        if not sc.is_name_in_table(pname, dbn.getQueue(), AIdb.PROFILES_TABLE):
            raise SystemExit(_("Error:\tservice has no profile named %s." %
                             pname))
        # Validate profile criteria
        sc.validate_criteria_from_user(criteria, dbn, AIdb.PROFILES_TABLE)

    # all validation complete - update database

    # indicate whether criteria are added or replaced
    if options.criteria_a:
        append = True # add new criteria
    else:
        append = False # replace any existing criteria with new
    if options.manifest_name:
        # Update the criteria for manifest
        set_criteria(criteria, options.manifest_name, dbn,
                AIdb.MANIFESTS_TABLE, append)
        print >> sys.stderr, _("Criteria updated for manifest %s.") % \
                options.manifest_name
    for pname in options.profile_name:
        # Update the criteria for profile
        set_criteria(criteria, pname, dbn, AIdb.PROFILES_TABLE, append)
        print >> sys.stderr, _("Criteria updated for profile %s.") % pname