final String log4jConfigTypeContains = "log4j";
final String defaultVersionTag = "version1";
final String defaultUser = "admin";
LOG.debug("Adding missing configs into Ambari DB.");
ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class);
ClusterServiceDAO clusterServiceDAO = injector.getInstance(ClusterServiceDAO.class);
AmbariMetaInfo ambariMetaInfo = injector.getInstance(AmbariMetaInfo.class);
Gson gson = injector.getInstance(Gson.class);
List <ClusterEntity> clusterEntities = clusterDAO.findAll();
for (final ClusterEntity clusterEntity : clusterEntities) {
Long clusterId = clusterEntity.getClusterId();
String desiredStackVersion = clusterEntity.getDesiredStackVersion();
Map<String, String> clusterInfo =
gson.<Map<String, String>>fromJson(desiredStackVersion, Map.class);
String stackName = clusterInfo.get("stackName");
String stackVersion = clusterInfo.get("stackVersion");
List<ClusterServiceEntity> clusterServiceEntities = clusterServiceDAO.findAll();
for (final ClusterServiceEntity clusterServiceEntity : clusterServiceEntities) {
String serviceName = clusterServiceEntity.getServiceName();
ServiceInfo serviceInfo = null;
try {
serviceInfo = ambariMetaInfo.getService(stackName, stackVersion, serviceName);
} catch (AmbariException e) {
LOG.error("Service " + serviceName + " not found for " + stackName + stackVersion);
continue;
}
List<String> configTypes = serviceInfo.getConfigDependencies();
if (configTypes != null) {
for (String configType : configTypes) {
if (configType.contains(log4jConfigTypeContains)) {
ClusterConfigEntityPK configEntityPK = new ClusterConfigEntityPK();
configEntityPK.setClusterId(clusterId);
configEntityPK.setType(configType);
configEntityPK.setTag(defaultVersionTag);
ClusterConfigEntity configEntity = clusterDAO.findConfig(configEntityPK);
if (configEntity == null) {
String filename = configType + ".xml";
Map<String, String> properties = new HashMap<String, String>();
for (PropertyInfo propertyInfo : serviceInfo.getProperties()) {
if (filename.equals(propertyInfo.getFilename())) {
properties.put(propertyInfo.getName(), propertyInfo.getValue());
}
}
if (!properties.isEmpty()) {
String configData = gson.toJson(properties);
configEntity = new ClusterConfigEntity();
configEntity.setClusterId(clusterId);
configEntity.setType(configType);
configEntity.setTag(defaultVersionTag);
configEntity.setData(configData);
configEntity.setTimestamp(System.currentTimeMillis());
configEntity.setClusterEntity(clusterEntity);
LOG.debug("Creating new " + configType + " config...");
clusterDAO.createConfig(configEntity);
Collection<ClusterConfigMappingEntity> entities =
clusterEntity.getConfigMappingEntities();
ClusterConfigMappingEntity clusterConfigMappingEntity =
new ClusterConfigMappingEntity();
clusterConfigMappingEntity.setClusterEntity(clusterEntity);
clusterConfigMappingEntity.setClusterId(clusterId);
clusterConfigMappingEntity.setType(configType);
clusterConfigMappingEntity.setCreateTimestamp(
Long.valueOf(System.currentTimeMillis()));
clusterConfigMappingEntity.setSelected(1);
clusterConfigMappingEntity.setUser(defaultUser);
clusterConfigMappingEntity.setVersion(configEntity.getTag());
entities.add(clusterConfigMappingEntity);
clusterDAO.merge(clusterEntity);
}
}
}
}