use of io.fabric8.openshift.api.model.Project in project fabric8 by fabric8io.
the class DevOpsConnectors method createDevOpsConnector.
/**
* Returns a DevOpsConnector for the given project repository
*/
public static DevOpsConnector createDevOpsConnector(ProjectRepository project) {
DevOpsConnector connector = new DevOpsConnector();
connector.setGitUrl(project.getGitUrl());
String repoName = project.getRepoName();
connector.setRepoName(repoName);
String username = project.getUser();
connector.setUsername(username);
String buildName = ProjectRepositories.createBuildName(username, repoName);
if (project.isGitHubProject()) {
// lets default the issue tracker
String url = project.getUrl();
if (Strings.isNotBlank(url)) {
connector.setIssueTrackerUrl(URLUtils.pathJoin(url, "issues"));
connector.setTeamUrl(URLUtils.pathJoin(url, "graphs/contributors"));
connector.setReleasesUrl(URLUtils.pathJoin(url, "tags"));
connector.setRepositoryBrowseLink(url);
}
ProjectConfig config = ProjectConfigs.loadFromUrl(URLUtils.pathJoin(url, "blob/master/fabric8.yml"));
if (config == null) {
config = new ProjectConfig();
// lets add a dummy build so we can at least build snapshots on demand in OpenShift
config.setPipeline("maven/Deploy.groovy");
}
config.setBuildName(buildName);
connector.setProjectConfig(config);
connector.setRegisterWebHooks(false);
System.out.println("Created config " + config.getBuildName() + " with flow " + config.getPipeline());
}
return connector;
}
use of io.fabric8.openshift.api.model.Project in project fabric8 by fabric8io.
the class SessionListener method preprocessEnvironment.
protected void preprocessEnvironment(KubernetesClient client, Controller controller, Configuration configuration, Session session) {
if (configuration.isUseGoFabric8()) {
// lets invoke gofabric8 to configure the security and secrets
Logger logger = session.getLogger();
Commands.assertCommand(logger, "oc", "project", session.getNamespace());
Commands.assertCommand(logger, "gofabric8", "deploy", "-y", "--console=false", "--templates=false");
Commands.assertCommand(logger, "gofabric8", "secrets", "-y");
}
}
use of io.fabric8.openshift.api.model.Project in project fabric8 by fabric8io.
the class SessionListener method start.
public void start(@Observes final Start event, KubernetesClient client, Controller controller, Configuration configuration) throws Exception {
Objects.requireNonNull(client, "KubernetesClient most not be null!");
Session session = event.getSession();
final Logger log = session.getLogger();
String namespace = session.getNamespace();
System.setProperty(Constants.KUBERNETES_NAMESPACE, namespace);
log.status("Using Kubernetes at: " + client.getMasterUrl());
log.status("Creating kubernetes resources inside namespace: " + namespace);
log.info("if you use OpenShift then type this switch namespaces: oc project " + namespace);
log.info("if you use kubernetes then type this to switch namespaces: kubectl namespace " + namespace);
clearTestResultDirectories(session);
controller.setNamespace(namespace);
controller.setThrowExceptionOnError(true);
controller.setRecreateMode(true);
controller.setIgnoreRunningOAuthClients(true);
if (configuration.isCreateNamespaceForTest()) {
createNamespace(client, controller, session);
} else {
String namespaceToUse = configuration.getNamespace();
checkNamespace(client, controller, session, configuration);
updateConfigMapStatus(client, session, Constants.RUNNING_STATUS);
namespace = namespaceToUse;
controller.setNamespace(namespace);
}
List<KubernetesList> kubeConfigs = new LinkedList<>();
shutdownHook = new ShutdownHook(client, controller, configuration, session, kubeConfigs);
Runtime.getRuntime().addShutdownHook(shutdownHook);
try {
URL configUrl = configuration.getEnvironmentConfigUrl();
List<String> dependencies = !configuration.getEnvironmentDependencies().isEmpty() ? configuration.getEnvironmentDependencies() : resolver.resolve(session);
if (configuration.isEnvironmentInitEnabled()) {
for (String dependency : dependencies) {
log.info("Found dependency: " + dependency);
loadDependency(log, kubeConfigs, dependency, controller, configuration, namespace);
}
OpenShiftClient openShiftClient = controller.getOpenShiftClientOrNull();
if (configUrl == null) {
// lets try find the default configuration generated by the new fabric8-maven-plugin
String resourceName = "kubernetes.yml";
if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.IMAGE) && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.ROUTE)) {
resourceName = "openshift.yml";
}
configUrl = findConfigResource("/META-INF/fabric8/" + resourceName);
}
if (configUrl != null) {
log.status("Applying kubernetes configuration from: " + configUrl);
String configText = readAsString(configUrl);
Object dto = null;
String configPath = configUrl.getPath();
if (configPath.endsWith(".yml") || configPath.endsWith(".yaml")) {
dto = loadYaml(configText, KubernetesResource.class);
} else {
dto = loadJson(configText);
}
dto = expandTemplate(controller, configuration, log, namespace, configUrl.toString(), dto);
KubernetesList kubeList = KubernetesHelper.asKubernetesList(dto);
List<HasMetadata> items = kubeList.getItems();
kubeConfigs.add(kubeList);
}
// Lets also try to load the image stream for the project.
if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.IMAGE)) {
File targetDir = new File(System.getProperty("basedir", ".") + "/target");
if (targetDir.exists() && targetDir.isDirectory()) {
File[] files = targetDir.listFiles();
if (files != null) {
for (File file : files) {
if (file.getName().endsWith("-is.yml")) {
loadDependency(log, kubeConfigs, file.toURI().toURL().toString(), controller, configuration, namespace);
}
}
}
}
//
}
}
if (!configuration.isEnvironmentInitEnabled() || applyConfiguration(client, controller, configuration, session, kubeConfigs)) {
displaySessionStatus(client, session);
} else {
throw new IllegalStateException("Failed to apply kubernetes configuration.");
}
} catch (Exception e) {
try {
cleanupSession(client, controller, configuration, session, kubeConfigs, Constants.ERROR_STATUS);
} catch (MultiException me) {
throw e;
} finally {
if (shutdownHook != null) {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
}
}
throw new RuntimeException(e);
}
}
use of io.fabric8.openshift.api.model.Project in project fabric8 by fabric8io.
the class BuildConfigHelper method createBuildConfig.
public static BuildConfig createBuildConfig(KubernetesClient kubernetesClient, String namespace, String projectName, String cloneUrl, Map<String, String> annotations) {
LOG.info("Creating a BuildConfig for namespace: " + namespace + " project: " + projectName);
String jenkinsUrl = null;
try {
jenkinsUrl = getJenkinsServiceUrl(kubernetesClient, namespace);
} catch (Exception e) {
// ignore missing Jenkins service issue
}
BuildConfig buildConfig = Builds.createDefaultBuildConfig(projectName, cloneUrl, jenkinsUrl);
Map<String, String> currentAnnotations = KubernetesHelper.getOrCreateAnnotations(buildConfig);
currentAnnotations.putAll(annotations);
return buildConfig;
}
use of io.fabric8.openshift.api.model.Project in project fabric8 by fabric8io.
the class BuildConfigHelper method createAndApplyBuildConfig.
/**
* Returns the created BuildConfig for the given project name and git repository
*/
public static BuildConfig createAndApplyBuildConfig(KubernetesClient kubernetesClient, String namespace, String projectName, String cloneUrl, Map<String, String> annotations) {
BuildConfig buildConfig = createBuildConfig(kubernetesClient, namespace, projectName, cloneUrl, annotations);
Controller controller = new Controller(kubernetesClient);
controller.setNamespace(namespace);
controller.applyBuildConfig(buildConfig, "from project " + projectName);
return buildConfig;
}
Aggregations