Example #1
0
 def update_node(self, user, computer, obj, objold, node, action,
                 job_ids_by_computer, force_update):
     updated = False
     if action not in ['changed', 'created']:
         raise ValueError('The action should be changed or created')
     if obj['type'] in RESOURCES_RECEPTOR_TYPES:  # ou, user, comp, group
         if force_update or self.is_updating_policies(obj, objold):
             rule_type = 'policies'
             for policy_id, action in self.get_policies(
                     rule_type, action, obj, objold):
                 policy = self.db.policies.find_one(
                     {"_id": ObjectId(policy_id)})
                 if action == DELETED_POLICY_ACTION:
                     rules, obj_ui = self.get_rules_and_object(
                         rule_type, objold, node, policy)
                 else:
                     rules, obj_ui = self.get_rules_and_object(
                         rule_type, obj, node, policy)
                 node, updated_policy = self.update_node_from_rules(
                     rules, user, computer, obj_ui, obj, action, node,
                     policy, rule_type, job_ids_by_computer)
                 if not updated and updated_policy:
                     updated = True
         return (node, updated)
     elif obj[
             'type'] in RESOURCES_EMITTERS_TYPES:  # printer, storage, repository
         rule_type = 'save'
         if force_update or self.is_updated_node(obj, objold):
             policy = self.db.policies.find_one(
                 {'slug': emiter_police_slug(obj['type'])})
             rules, obj_receptor = self.get_rules_and_object(
                 rule_type, obj, node, policy)
             node, updated = self.update_node_from_rules(
                 rules, user, computer, obj, obj_receptor, action, node,
                 policy, rule_type, job_ids_by_computer)
         return (node, updated)
Example #2
0
 def update_node(self, user, computer, obj, objold, node, action, job_ids_by_computer, force_update):
     updated = False
     if action not in ['changed', 'created']:
         raise ValueError('The action should be changed or created')
     if obj['type'] in RESOURCES_RECEPTOR_TYPES:  # ou, user, comp, group
         if force_update or self.is_updating_policies(obj, objold):
             rule_type = 'policies'
             for policy_id, action in self.get_policies(rule_type, action, obj, objold):
                 policy = self.db.policies.find_one({"_id": ObjectId(policy_id)})
                 if action == DELETED_POLICY_ACTION:
                     rules, obj_ui = self.get_rules_and_object(rule_type, objold, node, policy)
                 else:
                     rules, obj_ui = self.get_rules_and_object(rule_type, obj, node, policy)
                 node, updated_policy = self.update_node_from_rules(rules, user, computer, obj_ui, obj, action, node, policy, rule_type, job_ids_by_computer)
                 if not updated and updated_policy:
                     updated = True
         return (node, updated)
     elif obj['type'] in RESOURCES_EMITTERS_TYPES:  # printer, storage, repository
         rule_type = 'save'
         if force_update or self.is_updated_node(obj, objold):
             policy = self.db.policies.find_one({'slug': emiter_police_slug(obj['type'])})
             rules, obj_receptor = self.get_rules_and_object(rule_type, obj, node, policy)
             node, updated = self.update_node_from_rules(rules, user, computer, obj, obj_receptor, action, node, policy, rule_type, job_ids_by_computer)
         return (node, updated)
Example #3
0
    def command(self):
        api = _get_chef_api(self.settings.get('chef.url'),
                            toChefUsername(self.options.chef_username),
                            self.options.chef_pem)
        cookbook_name = self.settings['chef.cookbook_name']

        cookbook = get_cookbook(api, cookbook_name)

        languages = self.settings.get('pyramid.locales')
        languages.remove(self.settings.get('pyramid.default_locale_name'))

        policies = {}
        try:
            for key, value in cookbook['metadata']['attributes']['json_schema']['object']['properties']['gecos_ws_mgmt']['properties'].items():
                for k, policy in value['properties'].items():
                    policy['path'] = '%s.%s.%s' % (cookbook_name, key, k)
                    policies[k] = policy
        except KeyError:
            print "Can not found policies in cookbook %s" % cookbook_name
            sys.exit(1)

        policies_to_import = self.options.policies
        if policies_to_import:
            found = set(policies_to_import).intersection(set(policies.keys()))
            not_found = set(policies_to_import).difference(set(policies.keys()))
            if not_found:
                print "%s policies to import. Policies NOT FOUND: %s" % (len(found), list(not_found))
            else:
                print "%s policies to import" % len(found)
        else:
            print "%s policies to import" % len(policies.keys())

        for key, value in policies.items():
            if policies_to_import and key not in policies_to_import:
                continue
            elif key in EXCLUDE_POLICIES:
                continue
            if key == PACKAGE_POLICY:
                self.set_packages_url(value)
            for ex_attr in EXCLUDE_GENERIC_ATTRS:
                if ex_attr in value['properties']:
                    del(value['properties'][ex_attr])
            path = value.pop('path')

            support_os = value['properties']['support_os']['default']

            del value['properties']['support_os']

            if is_user_policy(path):
                targets = ['ou', 'user', 'group']
                title = value['title']
                titles = {}
                for lan in languages:
                    titles[lan] = value['title_' + lan]

                value = value['properties']['users']['patternProperties']['.*']
                if 'updated_by' in value.get('properties', {}):
                    del value['properties']['updated_by']
                value['title'] = title
                for lan in languages:
                    value['title_' + lan] = titles[lan]

            elif 'network_mgmt' in path:
                targets = ['computer']
            else:
                targets = DEFAULT_TARGETS

            policy = {
                'name': value['title'],
                'slug': key,
                'path': path,
                'schema': value,
                'targets': targets,
                'is_emitter_policy': False,
                'support_os': support_os,
            }

            for lan in languages:
                policy['name_' + lan] = value['title_' + lan]

            self.treatment_policy(policy)

        self.create_software_profiles_policy(policies, languages)

        if not self.options.ignore_emitter_policies:
            for emiter in RESOURCES_EMITTERS_TYPES:
                slug = emiter_police_slug(emiter)
                schema = deepcopy(SCHEMA_EMITTER)
                schema['properties']['object_related_list']['title'] = '%s list' % emiter.capitalize()
                for lan in languages:
                    schema['properties']['object_related_list']['title_' + lan] = EMITTER_LIST_LOCALIZED[lan] % EMITTER_LOCALIZED[lan][emiter]
                schema['properties']['object_related_list']['autocomplete_url'] = POLICY_EMITTER_URL[slug]
                policy = {
                    'name': POLICY_EMITTER_NAMES[slug],
                    'slug': slug,
                    'path': POLICY_EMITTER_PATH[slug],
                    'targets': POLICY_EMITTER_TARGETS[slug],
                    'is_emitter_policy': True,
                    'schema': schema,
                    'support_os': policies[POLICY_EMITTER_PATH[slug].split('.')[2]]['properties']['support_os']['default']
                }
                for lan in languages:
                    policy['name_' + lan] = POLICY_EMITTER_NAMES_LOCALIZED[lan][slug]
                self.treatment_policy(policy)
Example #4
0
    def command(self):
        api = _get_chef_api(self.settings.get('chef.url'),
                            toChefUsername(self.options.chef_username),
                            self.options.chef_pem, self.settings.get('chef.ssl.verify'), self.settings.get('chef.version'))
        cookbook_name = self.settings['chef.cookbook_name']

        cookbook = get_cookbook(api, cookbook_name)

        languages = self.settings.get('pyramid.locales')
        languages.remove(self.settings.get('pyramid.default_locale_name'))

        policies = {}
        try:
            for key, value in cookbook['metadata']['attributes']['json_schema']['object']['properties']['gecos_ws_mgmt']['properties'].items():
                for k, policy in value['properties'].items():
                    policy['path'] = '%s.%s.%s' % (cookbook_name, key, k)
                    policies[k] = policy
        except KeyError:
            print "Can not found policies in cookbook %s" % cookbook_name
            sys.exit(1)

        policies_to_import = self.options.policies
        if policies_to_import:
            found = set(policies_to_import).intersection(set(policies.keys()))
            not_found = set(policies_to_import).difference(set(policies.keys()))
            if not_found:
                print "%s policies to import. Policies NOT FOUND: %s" % (len(found), list(not_found))
            else:
                print "%s policies to import" % len(found)
        else:
            print "%s policies to import" % len(policies.keys())

        for key, value in policies.items():
            if policies_to_import and key not in policies_to_import:
                continue
            elif key in EXCLUDE_POLICIES:
                continue
            if key == PACKAGE_POLICY:
                self.set_packages_url(value)
            
            if key == MIMETYPES_POLICY:
                self.set_mimetypes_url(value)

            if key == MOBILE_BROADBAND_POLICY:
                self.set_serviceproviders_url(value)

            support_os = value['properties']['support_os']['default']

            for ex_attr in EXCLUDE_GENERIC_ATTRS:
                if ex_attr in value['properties']:
                    del(value['properties'][ex_attr])

            path = value.pop('path')

            form_layout = value.pop('form', {})
            is_mergeable = value.pop('is_mergeable', False)
            autoreverse = value.pop('autoreverse', False)

            if is_user_policy(path):
                targets = ['ou', 'user', 'group']
                title = value['title']
                titles = {}
                for lan in languages:
                    titles[lan] = value['title_' + lan]

                value = value['properties']['users']['patternProperties']['.*']
                if 'updated_by' in value.get('properties', {}):
                    del value['properties']['updated_by']
                value['title'] = title
                for lan in languages:
                    value['title_' + lan] = titles[lan]

            elif 'single_node' in path:
                targets = ['computer']
            else:
                targets = DEFAULT_TARGETS

            policy = {
                'name': value['title'],
                'slug': key,
                'path': path,
                'schema': value,
                'form': form_layout,
                'targets': targets,
                'is_emitter_policy': False,
                'support_os': support_os,
                'is_mergeable': is_mergeable,
                'autoreverse': autoreverse,
            }

            for lan in languages:
                policy['name_' + lan] = value['title_' + lan]

            self.treatment_policy(policy)

        if not self.options.ignore_emitter_policies:
            for emiter in RESOURCES_EMITTERS_TYPES:
                slug = emiter_police_slug(emiter)
                schema = deepcopy(SCHEMA_EMITTER)
                schema['properties']['object_related_list']['title'] = '%s list' % emiter.capitalize()
                for lan in languages:
                    schema['properties']['object_related_list']['title_' + lan] = EMITTER_LIST_LOCALIZED[lan] % EMITTER_LOCALIZED[lan][emiter]
                schema['properties']['object_related_list']['autocomplete_url'] = POLICY_EMITTER_URL[slug]
                policy = {
                    'name': POLICY_EMITTER_NAMES[slug],
                    'slug': slug,
                    'path': POLICY_EMITTER_PATH[slug],
                    'targets': POLICY_EMITTER_TARGETS[slug],
                    'is_emitter_policy': True,
                    'schema': schema,
                    'support_os': policies[POLICY_EMITTER_PATH[slug].split('.')[2]]['properties']['support_os']['default'],
                    'is_mergeable': True,
                    'autoreverse': policies[POLICY_EMITTER_PATH[slug].split('.')[-2]].get('autoreverse', False)
                }
                for lan in languages:
                    policy['name_' + lan] = POLICY_EMITTER_NAMES_LOCALIZED[lan][slug]
                self.treatment_policy(policy)
                
        # Check non imported policies
        print "Check non imported policies..."
        dbpolicies = self.db.policies.find({})
        found = False
        for policy in dbpolicies:
            if (policy['slug'] not in policies and 
                not policy['slug'].endswith('_can_view')):
                print "Policy '%s' wasn't imported. Probably is deprecated." % (policy['slug'])
                found = True
                if self.options.delete:
                    # Delete deprecated policy
                    self.db.policies.remove({'slug': policy['slug']})
                    print "Policy '%s' deleted!" % (policy['slug'])
                    if policy['slug'] == 'package_profile_res':
                        # Also delete software_profiles collection
                        print "Drop software profiles collection!"
                        self.db.software_profiles.drop()
                        self.db.settings.remove({'key' : 'software_profiles'})
                        
        if not found:
            print "There are no deprecated policies"
Example #5
0
 def get_policy_emiter_id(self, obj):
     return self.db.policies.find_one(
         {'slug': emiter_police_slug(obj['type'])})['_id']
Example #6
0
 def get_policy_emiter_id(self, obj):
     return self.db.policies.find_one({'slug': emiter_police_slug(obj['type'])})['_id']