This is an automated email from the ASF dual-hosted git repository. sijie pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/pulsar-manager.git
The following commit(s) were added to refs/heads/master by this push: new 6459601 Add default configuration for environment (#242) 6459601 is described below commit 645960115a80ffb042d5265868b55336b1b8203f Author: Guangning <guangn...@apache.org> AuthorDate: Mon Jan 13 17:15:31 2020 +0800 Add default configuration for environment (#242) Fixes https://github.com/apache/pulsar-manager/issues/233 https://github.com/apache/pulsar-manager/issues/225 ### Motivation When deploying k8s, a default environment is required. ### Modifications * Add a default configuration for environment --- .../pulsar/manager/PulsarApplicationListener.java | 95 ++++++++++++++++++++++ src/main/resources/application.properties | 4 + 2 files changed, 99 insertions(+) diff --git a/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java b/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java new file mode 100644 index 0000000..ec1048f --- /dev/null +++ b/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java @@ -0,0 +1,95 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.pulsar.manager; + +import com.github.pagehelper.Page; +import com.google.common.collect.Maps; +import org.apache.commons.lang3.StringUtils; +import org.apache.pulsar.manager.entity.EnvironmentEntity; +import org.apache.pulsar.manager.entity.EnvironmentsRepository; +import org.apache.pulsar.manager.utils.HttpUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.stereotype.Component; + +import java.util.Map; +import java.util.Optional; + +/** + * PulsarApplicationListener do something after the spring framework initialization is complete. + */ +@Component +public class PulsarApplicationListener implements ApplicationListener<ContextRefreshedEvent> { + + private static final Logger log = LoggerFactory.getLogger(PulsarApplicationListener.class); + + private final EnvironmentsRepository environmentsRepository; + + @Value("${default.environment.name}") + private String defaultEnvironmentName; + + @Value("${default.environment.service_url}") + private String defaultEnvironmentServiceUrl; + + @Value("${backend.jwt.token}") + private String pulsarJwtToken; + + public PulsarApplicationListener(EnvironmentsRepository environmentsRepository) { + this.environmentsRepository = environmentsRepository; + } + + @Override + public void onApplicationEvent(ContextRefreshedEvent event) { + log.info("Start onApplicationEvent"); + Page<EnvironmentEntity> environmentEntities = environmentsRepository + .getEnvironmentsList(1, 1); + if (environmentEntities.getResult().size() <= 0) { + Optional<EnvironmentEntity> environmentEntityOptional = environmentsRepository + .findByName(defaultEnvironmentName); + if (defaultEnvironmentName != null + && defaultEnvironmentServiceUrl != null + && defaultEnvironmentName.length() > 0 + && defaultEnvironmentServiceUrl.length() > 0 + && !environmentEntityOptional.isPresent()) { + Map<String, String> header = Maps.newHashMap(); + header.put("Content-Type", "application/json"); + if (StringUtils.isNotBlank(pulsarJwtToken)) { + header.put("Authorization", String.format("Bearer %s", pulsarJwtToken)); + } + String httpTestResult = HttpUtil.doGet(defaultEnvironmentServiceUrl + "/metrics", header); + if (httpTestResult != null) { + EnvironmentEntity environmentEntity = new EnvironmentEntity(); + environmentEntity.setBroker(defaultEnvironmentServiceUrl); + environmentEntity.setName(defaultEnvironmentName); + environmentsRepository.save(environmentEntity); + log.info("Successfully added a default environment: name = {}, service_url = {}.", + defaultEnvironmentName, defaultEnvironmentServiceUrl); + } else { + log.error("Unable to connect default environment {} via {}, " + + "please check if `environment.default.name` " + + "and `environment.default.broker` are set correctly, " + + "environmentDefaultName, environmentDefaultBroker", + defaultEnvironmentName, defaultEnvironmentServiceUrl); + System.exit(-1); + } + } else { + log.warn("The default environment already exists."); + } + } + log.debug("Environments already exist."); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 22f5045..fd9f41a 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -130,3 +130,7 @@ spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.servlet.content-type=text/html spring.thymeleaf.mode=HTML5 + +# default environment configuration +default.environment.name= +default.environment.service_url=