def test_metricconfig_add(self): """ Tests creating and adding a new MetricConfig to a MonitoringProfile. """ ctx = sm.ServiceContext(INFILENAME) svc = filter(lambda x: x.name == "CentralQuery", ctx.services)[0] monpro = svc.monitoringProfile self.assertEqual(len(monpro.metricConfigs), 7) mc = sm.monitoringprofile.metricconfig.MetricConfig( ID="foo", name="Foo", description="Test MC") monpro.metricConfigs.append(mc) ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) svc = filter(lambda x: x.name == "CentralQuery", ctx.services)[0] monpro = svc.monitoringProfile self.assertEqual(len(monpro.metricConfigs), 8) mc_ids = [mc.ID for mc in monpro.metricConfigs] self.assertEqual(len(mc_ids), 8) self.assertTrue('foo' in mc_ids) mc_foo = monpro.metricConfigs[mc_ids.index('foo')] self.assertEqual(mc_foo.ID, "foo") self.assertEqual(mc_foo.name, "Foo") self.assertEqual(mc_foo.description, "Test MC") self.assertEqual(mc_foo.metrics, [])
def test_metricconfig_modify(self): """ Tests finding MetricConfig in a MonitoringProfile by ID, and altering it in place. """ ctx = sm.ServiceContext(INFILENAME) svc = filter(lambda x: x.name == "CentralQuery", ctx.services)[0] monpro = svc.monitoringProfile self.assertEqual(len(monpro.metricConfigs), 7) mc_ids = [mc.ID for mc in monpro.metricConfigs] self.assertEqual(len(mc_ids), 7) self.assertTrue('jvm.thread' in mc_ids) mc_jvmthread = monpro.metricConfigs[mc_ids.index('jvm.thread')] mc_jvmthread.description = "JVM Thread with a changed description" ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) svc = filter(lambda x: x.name == "CentralQuery", ctx.services)[0] monpro = svc.monitoringProfile self.assertEqual(len(monpro.metricConfigs), 7) mc_ids = [mc.ID for mc in monpro.metricConfigs] self.assertEqual(len(mc_ids), 7) self.assertTrue('jvm.thread' in mc_ids) mc_jvmthread = monpro.metricConfigs[mc_ids.index('jvm.thread')] self.assertEqual(mc_jvmthread.description, "JVM Thread with a changed description")
def test_commands_replace(self): """ Tests completely replacing the commands list. """ ctx = sm.ServiceContext(INFILENAME) svc = filter(lambda x: x.description == "Zope server", ctx.services)[0] svc.commands = [ sm.Command("foo", "bar", commitOnSuccess=False), sm.Command("bar", "baz", commitOnSuccess=True), ] ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) svc = filter(lambda x: x.description == "Zope server", ctx.services)[0] if not "foo" in [command.name for command in svc.commands]: raise ValueError("Failed to alter commands.") if not "bar" in [command.name for command in svc.commands]: raise ValueError("Failed to alter commands.") for command in svc.commands: if command.name == "foo": self.assertEqual(command.command, "bar") self.assertFalse(command.commitOnSuccess) if command.name == "bar": self.assertEqual(command.command, "baz") self.assertTrue(command.commitOnSuccess) self.assertEqual(len(svc.commands), 2)
def test_commands_add(self): """ Tests adding commands to an existing list. """ ctx = sm.ServiceContext(INFILENAME) svc = filter(lambda x: x.description == "Zope server", ctx.services)[0] self.assertEqual(len(svc.commands), 8) svc.commands.append( sm.Command("foo", "bar", commitOnSuccess=False, description="Description of foo bar")) svc.commands.append( sm.Command("bar", "baz", commitOnSuccess=True, description="Description of bar baz")) ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) svc = filter(lambda x: x.description == "Zope server", ctx.services)[0] if not "foo" in [command.name for command in svc.commands]: raise ValueError("Failed to alter commands.") if not "bar" in [command.name for command in svc.commands]: raise ValueError("Failed to alter commands.") for command in svc.commands: if command.name == "foo": self.assertEqual(command.command, "bar") self.assertEqual(command.description, "Description of foo bar") self.assertFalse(command.commitOnSuccess) if command.name == "bar": self.assertEqual(command.command, "baz") self.assertEqual(command.description, "Description of bar baz") self.assertTrue(command.commitOnSuccess) self.assertEqual(len(svc.commands), 10)
def test_reparentService(self): ctx = sm.ServiceContext(INFILENAME) svc0 = filter(lambda x: x.name == "collectorredis", ctx.services)[0] svc1 = filter(lambda x: x.name == "redis", ctx.services)[0] ctx.reparentService(svc0, svc1) ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) svc0 = filter(lambda x: x.name == "collectorredis", ctx.services)[0] svc1 = filter(lambda x: x.name == "redis", ctx.services)[0] self.assertEqual(ctx.getServiceParent(svc0), svc1) self.assertTrue(svc0 in ctx.getServiceChildren(svc1))
def test_startup_remove(self): """ Tests completely removing a startup. """ ctx = sm.ServiceContext(INFILENAME) svc = filter(lambda x: x.description == "Zope server", ctx.services)[0] svc.startup = None ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) svc = filter(lambda x: x.startup == None, ctx.services) self.assertEqual(len(svc), 1)
def test_desiredState(self): """ Tests changing the desired state. """ ctx = sm.ServiceContext(INFILENAME) svc = filter(lambda x: x.name == "redis", ctx.services)[0] svc.desiredState = sm.RESTART ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) svc = filter(lambda x: x.name == "redis", ctx.services)[0] self.assertEqual(svc.desiredState, sm.RESTART)
def test_ctx_versioning(self): """ Tests ServiceContext versioning Which has been removed """ ctx = sm.ServiceContext(INFILENAME) self.assertEqual(ctx.version, "") ctx.version = "foo.bar.baz" ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) self.assertEqual(ctx.version, "")
def test_deployServiceByParentID(self): ctx = sm.ServiceContext(INFILENAME) core = filter(lambda s: s.name == "Zenoss.core", ctx.services)[0] deploy = """{"Name": "deployed-service"}""" ctx._ServiceContext__deployService(deploy, core._Service__data["ID"]) ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) deployed = filter(lambda d: d["Service"]["Name"] == "deployed-service", ctx._ServiceContext__deploy) self.assertEqual(len(deployed), 1) self.assertEqual(deployed[0]["ParentID"], core._Service__data["ID"])
def test_environment_add(self): """ Tests adding environment to a service. """ ctx = sm.ServiceContext(INFILENAME) svc = filter(lambda s: s.name == "opentsdb", ctx.services)[0] svc.environment = ["unlikely_env_1", "unlikely_env_2"] ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) svc = filter(lambda s: s.name == "opentsdb", ctx.services)[0] self.assertEqual(len(svc.environment), 2)
def test_get_commit_services(self): """ Tests ServiceContext creation and commit. """ ctx = sm.ServiceContext(INFILENAME) self.assertEqual(len(ctx.services), 34) ctx.services.pop() self.assertEqual(len(ctx.services), 33) ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) self.assertEqual(len(ctx.services), 33)
def test_startup_change(self): """ Tests altering a startup. """ ctx = sm.ServiceContext(INFILENAME) svc = filter(lambda x: x.description == "Zope server", ctx.services)[0] svc.startup = "an_unlikely-startup" ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) svc = filter(lambda x: x.startup == "an_unlikely-startup", ctx.services) self.assertEqual(len(svc), 1)
def test_environment_filter(self): """ Tests filtering on environment """ ctx = sm.ServiceContext(INFILENAME) svc = filter(lambda s: s.name == "opentsdb", ctx.services)[0] svc.environment = ["unlikely_env_1", "unlikely_env_2"] ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) svcs = filter( lambda s: "unlikely_env_1" in s.environment and "unlikely_env_2" in s.environment, ctx.services) self.assertEqual(len(svcs), 1)
def test_imageId_change(self): """ Tests changing an imageID """ imageID = "localhost:5000/fake:latest" ctx = sm.ServiceContext(INFILENAME) svc = filter(lambda x: x.description == "Zope server", ctx.services)[0] svc.imageID = imageID ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) svc = filter(lambda x: x.description == "Zope server", ctx.services)[0] self.assertEqual(svc.imageID, imageID)
def test_add_vhost(self): ctx = sm.ServiceContext(INFILENAME) svc = filter(lambda x: x.name == 'Zenoss.core', ctx.services)[0] zproxy_ep = filter(lambda ep: ep.name == 'zproxy', svc.endpoints)[0] vhostlist_len = len(zproxy_ep.vhostlist) zproxy_ep.vhostlist.append(sm.VHost(name="zendebug", enabled=True)) ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) svc = filter(lambda x: x.name == 'Zenoss.core', ctx.services)[0] zproxy_ep = filter(lambda ep: ep.name == 'zproxy', svc.endpoints)[0] self.assertEqual(vhostlist_len + 1, len(zproxy_ep.vhostlist)) vhost = filter(lambda v: v.name == 'zendebug', zproxy_ep.vhostlist)[0] self.assertEqual(vhost.name, "zendebug") self.assertEqual(vhost.enabled, True)
def test_get_commit_services_env_var(self): """ Tests ServiceContext creation and commit when using the environment variables. """ os.environ["MIGRATE_INPUTFILE"] = INFILENAME os.environ["MIGRATE_OUTPUTFILE"] = OUTFILENAME ctx = sm.ServiceContext() self.assertEqual(len(ctx.services), 34) ctx.services.pop() self.assertEqual(len(ctx.services), 33) ctx.commit() os.environ["MIGRATE_INPUTFILE"] = OUTFILENAME ctx = sm.ServiceContext() self.assertEqual(len(ctx.services), 33)
def test_tags_replace(self): """ Tests completely replacing the tag list of a service. """ ctx = sm.ServiceContext(INFILENAME) svc = filter(lambda s: "collector" in s.tags and "daemon" in s.tags, ctx.services)[0] svc.tags = ["unlikely_tag_1", "unlikely_tag_2"] ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) svcs = filter( lambda s: "unlikely_tag_1" in s.tags and "unlikely_tag_2" in s. tags, ctx.services) self.assertEqual(len(svcs), 1)
def test_context_variable_add(self): """ Tests adding context_variable to a service. """ ctx = sm.ServiceContext(INFILENAME) top = ctx.getTopService() self.assertEqual(33, len(top.context)) top.context['test_var'] = "test_val" ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) top = ctx.getTopService() self.assertEqual(34, len(top.context)) self.assertTrue('test_var' in top.context) self.assertEqual('test_val', top.context['test_var'])
def test_instancelimits_change(self): """ Tests changing the instance limits. """ ctx = sm.ServiceContext(INFILENAME) svc = filter(lambda x: x.description == "Zope server", ctx.services)[0] svc.instanceLimits.minimum = 123 svc.instanceLimits.maximum = 1234 svc.instanceLimits.default = 234 ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) svc = filter(lambda x: x.description == "Zope server", ctx.services)[0] self.assertEqual(svc.instanceLimits.minimum, 123) self.assertEqual(svc.instanceLimits.maximum, 1234) self.assertEqual(svc.instanceLimits.default, 234)
def test_tags_alter(self): """ Tests altering the tag list of a service. """ ctx = sm.ServiceContext(INFILENAME) svc = filter(lambda s: "collector" in s.tags and "daemon" in s.tags, ctx.services)[0] svc.tags.remove("collector") svc.tags.append("unlikely_tag") ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) svcs = filter( lambda s: "unlikely_tag" in s.tags and "daemon" in s.tags, ctx.services) self.assertEqual(len(svcs), 1)
def test_clone_service(self): """ Tests cloning a service. """ ctx = sm.ServiceContext(INFILENAME) self.assertEqual(len(ctx.services), 34) redis = filter(lambda s: s.name == "redis", ctx.services)[0] clone = redis.clone() clone.name = "clone name" ctx.services.append(clone) ctx.commit(OUTFILENAME) ctx = sm.ServiceContext(OUTFILENAME) clone = filter(lambda s: s.name == "clone name", ctx.services)[0] self.assertEqual(clone.description, redis.description) self.assertEqual(len(ctx.services), 35)
def cutover(self, dmd): try: ctx = sm.ServiceContext() except sm.ServiceMigrationError: log.info("Couldn't generate service context, skipping.") return service = ctx.getTopService() log.info("Found top level service: '{0}'".format(service.name)) if service.name.find("Zenoss") != 0 and service.name.find( "UCS-PM") != 0: log.info( "Top level service name isn't Zenoss or UCS-PM; skipping.") return filterNames = [ "pythondaemon", "supervisord", "z2_access_logs", "zappdaemon", "zeneventserver", "zope" ] for filterName in filterNames: filename = 'Products/ZenModel/migrate/data/%s-6.0.0.conf' % filterName with open(zenPath(filename)) as filterFile: try: filterDef = filterFile.read() except Exception, e: log.error("Error reading {0} logfilter file: {1}".format( filename, e)) return log.info("Updating log filter named {0}".format(filterName)) ctx.addLogFilter(filterName, filterDef)
def cutover(self, dmd): try: ctx = sm.ServiceContext() except sm.ServiceMigrationError: log.info("Couldn't generate service context, skipping.") return endpoint_map = { 'zep': Endpoint( name="zep", purpose="import", application="zep", portnumber=8084, protocol="tcp" ) } # Get all zeneventd services (normally only 1) log.info("Looking for zeneventd services to migrate") services = filter(lambda s: s.name == "zeneventd", ctx.services) log.info("Found %s services named 'zeneventd'.", len(services)) # Add the zep endpoint import if it does not exist if not services: log.info("Found no 'zeneventd' services to migrate") # short circuit return for service, endpoint_key in itertools.product(services, endpoint_map.keys()): if not filter(lambda endpoint: endpoint.purpose == "import" and endpoint.application == endpoint_key, service.endpoints): log.info("Adding '%s' endpoint import to service '%s'", endpoint_key, service.name) service.endpoints.append( endpoint_map[endpoint_key] ) ctx.commit()
def cutover(self, dmd): try: ctx = sm.ServiceContext() except sm.ServiceMigrationError: log.info("Couldn't generate service context, skipping.") return zenmodelers = filter(lambda s: s.name == "zenmodeler", ctx.services) log.info("Found %i services named 'zenmodeler'." % len(zenmodelers)) remove_metrics = ["dataPoints", "eventCount", "missedRuns", "taskCount"] remove_graphs = ["dataPoints", "events", "missedRuns", "tasks"] for zenmodeler in zenmodelers: for mc in zenmodeler.monitoringProfile.metricConfigs: before = len(mc.metrics) mc.metrics = [m for m in mc.metrics if m.ID not in remove_metrics] after = len(mc.metrics) log.info("Removed %i metrics from zenmodeler." % (before - after)) before = len(zenmodeler.monitoringProfile.graphConfigs) zenmodeler.monitoringProfile.graphConfigs = [ gc for gc in zenmodeler.monitoringProfile.graphConfigs if gc.graphID not in remove_graphs] after = len(zenmodeler.monitoringProfile.graphConfigs) log.info("Removed %i graphs from zenmodeler." % (before - after)) # Commit our changes. ctx.commit()
def tests_logconfigs_alter(self): """ Tests changing the LogConfigs of a service. """ ctx = sm.ServiceContext(INFILENAME) svc = filter(lambda x: x.description == "Zope server", ctx.services)[0] self.assertEqual(len(svc.logConfigs), 3) for lc in svc.logConfigs: if lc.logType == 'zenossaudit': # Add or update logtag "foo" to "bar". for tag in lc.logTags: if tag.name == 'foo': tag.value = 'bar' break else: lc.logTags.append(sm.logtag.LogTag(name="foo", value="bar")) lc.filters.append('baz') svc = filter(lambda x: x.description == "Zope server", ctx.services)[0] audit = filter(lambda x: x.logType == 'zenossaudit', svc.logConfigs)[0] footags = filter(lambda t: t.name == 'foo', audit.logTags) self.assertEqual(len(footags), 1) self.assertEqual(footags[0].value, 'bar', "Failed ot alter logconfigs.") self.assertTrue('baz' in audit.filters, "Failed to alter logconfigs.") self.assertEqual(len(svc.logConfigs), 3)
def cutover(self, dmd): try: ctx = sm.ServiceContext() except sm.ServiceMigrationError: log.info("Couldn't generate service context, skipping.") return centralqueries = filter(lambda s: s.name == "CentralQuery", ctx.services) if not centralqueries: log.info("Couldn't find CentralQuery service, skipping.") return log.info("Found CentralQuery service.") # Locate the health check typos. commit = False for service in centralqueries: typoHealthChecks = filter( lambda healthCheck: healthCheck.name == "anwering", service.healthChecks) typos = len(typoHealthChecks) if typos > 0: log.info("Found %i healthcheck typo in service: %s" % (typos, service.name)) for healthCheck in typoHealthChecks: healthCheck.name = "answering" log.info("Updated healthcheck name.") commit = True if commit: log.info("Committing changes.") ctx.commit()
def cutover(self, dmd): try: ctx = sm.ServiceContext() except sm.ServiceMigrationError: log.info("Couldn't generate service context, skipping.") return changed = False for service in ctx.services: emergencyShutdownLevel, startLevel = serviceRunLevels.get( service.name, (0, 0)) if not service.emergencyShutdownLevel == emergencyShutdownLevel: before = service.emergencyShutdownLevel service.emergencyShutdownLevel = emergencyShutdownLevel changed = True log.info( 'Change emergency shutdown level of %s from %d to %d.', service.name, before, emergencyShutdownLevel) if not service.startLevel == startLevel: before = service.startLevel service.startLevel = startLevel changed = True log.info('Change start level of %s from %d to %d.', service.name, before, startLevel) if changed: ctx.commit() else: log.info('Nothing to change in this migration step.')
def cutover(self, dmd): try: ctx = sm.ServiceContext() except sm.ServiceMigrationError: log.info("Couldn't generate service context, skipping.") return zenoss = ctx.getTopService() memcached = filter(lambda x: x.name == "memcached", ctx.services) if memcached: memcached = memcached[0] configs = [ cfg for cfg in memcached.originalConfigs if cfg.name == '/etc/sysconfig/memcached' ] if hasattr(memcached, "configFiles"): configs.extend([ cfg for cfg in memcached.configFiles if cfg.name == '/etc/sysconfig/memcached' ]) commit = False for config in configs: if self._update_config(config): log.info("/etc/sysconfig/memcached updated.") commit = True if commit: ctx.commit()
def cutover(self, dmd): try: ctx = sm.ServiceContext() except sm.ServiceMigrationError: log.info("Couldn't generate service context, skipping.") return query_svcs = filter(lambda s: s.name == 'CentralQuery', ctx.services) for query_svc in query_svcs: configfiles = query_svc.originalConfigs + query_svc.configFiles for configfile in filter( lambda f: f.name == '/opt/zenoss/etc/central-query/configuration.yaml', configfiles): if "ignoreRateOption" not in configfile.content: insertAfter = 'sendRateOptions:' if insertAfter not in configfile.content: insertAfter = 'metrics:' lines = configfile.content.split('\n') newLines = [] for line in lines: newLines.append(line) if insertAfter in line: newLines.append(' ignoreRateOption: true') newLines.append( ' rateOptionCutoffTs: {{(getContext . "centralquery.ratecutoff")}}' ) configfile.content = '\n'.join(newLines) parent = ctx.getTopService() if "centralquery.ratecutoff" not in parent.context: now = int(time.time() * 1000) parent.context["centralquery.ratecutoff"] = now ctx.commit()
def cutover(self, dmd): try: ctx = sm.ServiceContext() except sm.ServiceMigrationError: log.info("Couldn't generate service context, skipping.") return zentraps = filter(lambda s: s.name == "zentrap", ctx.services) log.info("Found %i services named 'zentrap'." % len(zentraps)) cfFilter = sm.ConfigFile ( name = "/opt/zenoss/etc/zentrap.filter.conf", filename = "/opt/zenoss/etc/zentrap.filter.conf", owner = "zenoss:zenoss", permissions = "0664", content = open(os.path.join(os.path.dirname(__file__), "config-files", "zentrap.filter.conf"), 'r').read() ) for zentrap in zentraps: # First update zentrap.conf. cf = filter(lambda f: f.name == "/opt/zenoss/etc/zentrap.conf", zentrap.originalConfigs)[0] cf.content = open(os.path.join(os.path.dirname(__file__), "config-files", "zentrap.conf"), 'r').read() log.info("Updated '/opt/zenoss/etc/zentrap.conf' contents.") # Now add zentrap.filter.conf. zentrap.originalConfigs.append(cfFilter) log.info("Added '%s'." % cfFilter.name) # Commit our changes. ctx.commit()