UpgradeCatalog200 extends AbstractUpgradeCatalog { protected void persistHDPRepo() throws AmbariException{ AmbariManagementController amc = injector.getInstance( AmbariManagementController.class); AmbariMetaInfo ambariMetaInfo = amc.getAmbariMetaInfo(); Map<String, Cluster> clusterMap = amc.getClusters().getClusters(); for (Cluster cluster : clusterMap.values()) { StackId stackId = cluster.getCurrentStackVersion(); String stackName = stackId.getStackName(); String stackVersion = stackId.getStackVersion(); String stackRepoId = stackName + "-" + stackVersion; for (OperatingSystemInfo osi : ambariMetaInfo.getOperatingSystems(stackName, stackVersion)) { MetainfoDAO metaInfoDAO = injector.getInstance(MetainfoDAO.class); String repoMetaKey = AmbariMetaInfo.generateRepoMetaKey(stackName,stackVersion,osi.getOsType(), stackRepoId,AmbariMetaInfo.REPOSITORY_XML_PROPERTY_BASEURL); if (metaInfoDAO.findByKey(repoMetaKey) == null) { RepositoryInfo repositoryInfo = ambariMetaInfo.getRepository(stackName, stackVersion, osi.getOsType(), stackRepoId); String baseUrl = repositoryInfo.getDefaultBaseUrl(); ambariMetaInfo.updateRepo(stackName, stackVersion, osi.getOsType(), stackRepoId, baseUrl, null); } } System.out.printf("Ambari has recorded the following repository base urls for cluster %s. Please verify the " + "values and ensure that these are correct. If necessary, " + "after starting Ambari Server, you can edit them using Ambari UI, " + "Admin -> Stacks and Versions -> Versions Tab and editing the base urls for the current Repo. " + "It is critical that these repo base urls are valid for your environment as they " + "will be used for Add Host/Service operations.", cluster.getClusterName()); System.out.println(repositoryTable(ambariMetaInfo.getStack(stackName, stackVersion).getRepositories())); } } @Inject  UpgradeCatalog200(Injector injector); @Override String getTargetVersion(); @Override void executePreDMLUpdates();  }
@Test public void testPersistHDPRepo() throws Exception { EasyMockSupport easyMockSupport = new EasyMockSupport(); final AmbariManagementController mockAmbariManagementController = easyMockSupport.createStrictMock(AmbariManagementController.class); final AmbariMetaInfo mockAmbariMetaInfo = easyMockSupport.createNiceMock(AmbariMetaInfo.class); final StackInfo mockStackInfo = easyMockSupport.createNiceMock(StackInfo.class); final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class); final Cluster mockCluster = easyMockSupport.createStrictMock(Cluster.class); final Map<String, Cluster> clusterMap = new HashMap<String, Cluster>(); clusterMap.put("c1",mockCluster); OperatingSystemInfo osi = new OperatingSystemInfo("redhat6"); HashSet<OperatingSystemInfo> osiSet = new HashSet<OperatingSystemInfo>(); osiSet.add(osi); StackId stackId = new StackId("HDP","2.2"); final RepositoryInfo mockRepositoryInfo = easyMockSupport.createNiceMock(RepositoryInfo.class); final Injector mockInjector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(AmbariManagementController.class).toInstance(mockAmbariManagementController); bind(Clusters.class).toInstance(mockClusters); bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class)); bind(EntityManager.class).toInstance(createNiceMock(EntityManager.class)); bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class)); } }); expect(mockAmbariManagementController.getAmbariMetaInfo()).andReturn(mockAmbariMetaInfo); expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once(); expect(mockClusters.getClusters()).andReturn(clusterMap).once(); expect(mockCluster.getCurrentStackVersion()).andReturn(stackId).once(); expect(mockCluster.getClusterName()).andReturn("cc").anyTimes(); expect(mockAmbariMetaInfo.getOperatingSystems("HDP", "2.2")).andReturn(osiSet).once(); expect(mockAmbariMetaInfo.getRepository("HDP", "2.2", "redhat6", "HDP-2.2")).andReturn(mockRepositoryInfo).once(); expect(mockAmbariMetaInfo.getStack("HDP", "2.2")).andReturn(mockStackInfo); expect(mockStackInfo.getRepositories()).andReturn(new ArrayList<RepositoryInfo>() {{ add(mockRepositoryInfo); }}); expect(mockRepositoryInfo.getDefaultBaseUrl()).andReturn("http: mockAmbariMetaInfo.updateRepo("HDP", "2.2", "redhat6", "HDP-2.2", "http: expectLastCall().once(); easyMockSupport.replayAll(); mockInjector.getInstance(UpgradeCatalog200.class).persistHDPRepo(); easyMockSupport.verifyAll(); }