def _create_hosts_and_validation_profiles(self, validations): profile = ServerProfile( name="test_profile_2", ui_name="Not Test Profile", ui_description="Not Test Profile", managed=False, worker=False, corosync=False, corosync2=False, ntp=False, user_selectable=False, initial_state="monitored", ) profile.save() for profile in ServerProfile.objects.all(): for validation in validations: x = ServerProfileValidation(**validation) profile.serverprofilevalidation_set.add(x, bulk=False) x.save() response = self.api_client.post( self.RESOURCE_PATH, data={ "objects": [{ "address": host_name, "server_profile": "/api/server_profile/test_profile_2/" } for host_name in MockAgentRpc.mock_servers] }, ) self.assertHttpAccepted(response) content = json.loads(response.content) self.assertEqual(len(content["objects"]), len(MockAgentRpc.mock_servers)) for object in content["objects"]: self.assertEqual(object["error"], None) self.assertEqual(object["traceback"], None) self.assertEqual(object["command_and_host"].keys(), ["host", "command"]) hosts = ManagedHost.objects.all() self.assertEqual(hosts[0].server_profile.name, "test_profile_2") return hosts
def _create_hosts_and_validation_profiles(self, validations): profile = ServerProfile(name='test_profile_2', ui_name='Not Test Profile', ui_description='Not Test Profile', managed=False, worker=False, corosync=False, corosync2=False, ntp=False, user_selectable=False, initial_state="monitored") profile.save() profile.bundles.add(Bundle.objects.get(bundle_name='agent')) for profile in ServerProfile.objects.all(): for validation in validations: profile.serverprofilevalidation_set.add( ServerProfileValidation(**validation)) response = self.api_client.post( self.RESOURCE_PATH, data={ 'objects': [{ 'address': host_name, 'server_profile': '/api/server_profile/test_profile_2/' } for host_name in MockAgentRpc.mock_servers] }) self.assertHttpAccepted(response) content = json.loads(response.content) self.assertEqual(len(content['objects']), len(MockAgentRpc.mock_servers)) for object in content['objects']: self.assertEqual(object['error'], None) self.assertEqual(object['traceback'], None) self.assertEqual(object['command_and_host'].keys(), ['host', 'command']) hosts = ManagedHost.objects.all() self.assertEqual(hosts[0].server_profile.name, 'test_profile_2') return hosts
def register_profile(profile_file): default_profile = { "ui_name": "Default Profile", "managed": False, "worker": False, "name": "default", "repolist": ["base"], "ui_description": "This is the hard coded default profile.", "user_selectable": True, "initial_state": "monitored", "packages": [], "validation": [], "ntp": False, "corosync": False, "corosync2": False, "pacemaker": False, } # create new profile record try: data = json.load(profile_file) except ValueError as e: raise RuntimeError("Profile %s is malformed: %s" % (profile_file.name, e.message)) log.debug("Loaded profile '%s' from %s" % (data["name"], profile_file)) # Validate: check for all repo files referenced in profile missing_repos = [] validate_repos = set(data["repolist"]) for repo_name in validate_repos: if not Repo.objects.filter(repo_name=repo_name).exists(): missing_repos.append(repo_name) # Make sure new keys have a default value set. for key in data.keys(): assert key in default_profile, "Key %s is not in the default profile" % key # Take the default and replace the values that are in the data data = dict(default_profile.items() + data.items()) if missing_repos: log.error("Repos not found for profile '%s': %s" % (data["name"], ", ".join(missing_repos))) sys.exit(-1) calculated_profile_fields = set( ["packages", "repolist", "name", "validation"]) regular_profile_fields = set(data.keys()) - calculated_profile_fields try: profile = ServerProfile.objects.get(name=data["name"]) log.debug("Updating profile %s" % data["name"]) for field in regular_profile_fields: setattr(profile, field, data[field]) profile.save() except ServerProfile.DoesNotExist: log.debug("Creating profile %s" % data["name"]) kwargs = dict([(f, data[f]) for f in regular_profile_fields]) kwargs["name"] = data["name"] profile = ServerProfile.objects.create(**kwargs) # Remove absent repos for repo in profile.repolist.all(): if repo.repo_name not in data["repolist"]: profile.repolist.remove(repo) for name in data["repolist"]: profile.repolist.add(Repo.objects.get(repo_name=name)) # Remove absent packages for package_name in profile.packages: if package_name not in data["packages"]: ServerProfilePackage.objects.filter( server_profile=profile, package_name=package_name).delete() for package_name in data["packages"]: ServerProfilePackage.objects.get_or_create(server_profile=profile, package_name=package_name) profile.serverprofilevalidation_set.all().delete() for validation in data["validation"]: profile_validation = ServerProfileValidation(**validation) profile.serverprofilevalidation_set.add(profile_validation, bulk=False) profile_validation.save()
profile = ServerProfile.objects.create(**kwargs) for name in data['bundles']: profile.bundles.add(Bundle.objects.get(bundle_name=name)) for bundle_name, package_list in data['packages'].items(): for package_name in package_list: ServerProfilePackage.objects.get_or_create( server_profile=profile, bundle=Bundle.objects.get(bundle_name=bundle_name), package_name=package_name) profile.serverprofilevalidation_set.all().delete() for validation in data['validation']: profile.serverprofilevalidation_set.add( ServerProfileValidation(**validation)) def delete_profile(name): # remove profile record try: profile = ServerProfile.objects.get(name=name) profile.delete() except ServerProfile.DoesNotExist: # doesn't exist anyway, so just exit silently return def default_profile(name): """ Set the default flag on the named profile and clear the default
kwargs['name'] = data['name'] profile = ServerProfile.objects.create(**kwargs) for name in data['bundles']: profile.bundles.add(Bundle.objects.get(bundle_name=name)) for bundle_name, package_list in data['packages'].items(): for package_name in package_list: ServerProfilePackage.objects.get_or_create( server_profile=profile, bundle=Bundle.objects.get(bundle_name=bundle_name), package_name=package_name) profile.serverprofilevalidation_set.all().delete() for validation in data['validation']: profile.serverprofilevalidation_set.add(ServerProfileValidation(**validation)) def delete_profile(name): # remove profile record try: profile = ServerProfile.objects.get(name=name) profile.delete() except ServerProfile.DoesNotExist: # doesn't exist anyway, so just exit silently return def default_profile(name): """ Set the default flag on the named profile and clear the default