use of io.fabric8.insight.metrics.model.Server in project fabric8 by fabric8io.
the class DevOpsConnector method createGerritRepo.
protected void createGerritRepo(String repoName, String gerritUser, String gerritPwd, String gerritGitInitialCommit, String gerritGitRepoDescription) throws Exception {
// lets add defaults if not env vars
if (Strings.isNullOrBlank(gerritUser)) {
gerritUser = "admin";
}
if (Strings.isNullOrBlank(gerritPwd)) {
gerritPwd = "secret";
}
log.info("A Gerrit git repo will be created for this name : " + repoName);
String gerritAddress = KubernetesHelper.getServiceURL(kubernetes, ServiceNames.GERRIT, namespace, "http", true);
log.info("Found gerrit address: " + gerritAddress + " for namespace: " + namespace + " on Kubernetes address: " + kubernetes.getMasterUrl());
if (Strings.isNullOrBlank(gerritAddress)) {
throw new Exception("No address for service " + ServiceNames.GERRIT + " in namespace: " + namespace + " on Kubernetes address: " + kubernetes.getMasterUrl());
}
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpClient httpclientPost = HttpClients.createDefault();
String GERRIT_URL = gerritAddress + "/a/projects/" + repoName;
HttpGet httpget = new HttpGet(GERRIT_URL);
System.out.println("Requesting : " + httpget.getURI());
try {
// Initial request without credentials returns "HTTP/1.1 401 Unauthorized"
HttpResponse response = httpclient.execute(httpget);
System.out.println(response.getStatusLine());
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
// Get current current "WWW-Authenticate" header from response
// WWW-Authenticate:Digest realm="My Test Realm", qop="auth",
// nonce="cdcf6cbe6ee17ae0790ed399935997e8", opaque="ae40d7c8ca6a35af15460d352be5e71c"
Header authHeader = response.getFirstHeader(AUTH.WWW_AUTH);
System.out.println("authHeader = " + authHeader);
DigestScheme digestScheme = new DigestScheme();
// Parse realm, nonce sent by server.
digestScheme.processChallenge(authHeader);
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(gerritUser, gerritPwd);
httpget.addHeader(digestScheme.authenticate(creds, httpget, null));
HttpPost httpPost = new HttpPost(GERRIT_URL);
httpPost.addHeader(digestScheme.authenticate(creds, httpPost, null));
httpPost.addHeader("Content-Type", "application/json");
CreateRepositoryDTO createRepoDTO = new CreateRepositoryDTO();
createRepoDTO.setDescription(gerritGitRepoDescription);
createRepoDTO.setName(repoName);
createRepoDTO.setCreate_empty_commit(Boolean.valueOf(gerritGitInitialCommit));
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(createRepoDTO);
HttpEntity entity = new StringEntity(json);
httpPost.setEntity(entity);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclientPost.execute(httpPost, responseHandler);
System.out.println("responseBody : " + responseBody);
}
} catch (MalformedChallengeException e) {
e.printStackTrace();
} catch (AuthenticationException e) {
e.printStackTrace();
} catch (ConnectException e) {
System.out.println("Gerrit Server is not responding");
} catch (HttpResponseException e) {
System.out.println("Response from Gerrit Server : " + e.getMessage());
throw new Exception("Repository " + repoName + " already exists !");
} finally {
httpclient.close();
httpclientPost.close();
}
}
use of io.fabric8.insight.metrics.model.Server in project fabric8 by fabric8io.
the class Java2SwaggerJsonMojo method execute.
public void execute() throws MojoExecutionException {
List<Class<?>> resourceClasses = loadResourceClasses();
List<Object> resourceObjects = new ArrayList<Object>();
for (Class<?> resourceClass : resourceClasses) {
try {
resourceObjects.add(resourceClass.newInstance());
} catch (InstantiationException e) {
throw new MojoExecutionException(e.getMessage(), e);
} catch (IllegalAccessException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
Thread.currentThread().setContextClassLoader(getClassLoader());
List<Feature> features = new ArrayList<Feature>();
features.add(new SwaggerFeature());
JAXRSServerFactoryBean serverFacBean = new JAXRSServerFactoryBean();
serverFacBean.setAddress(address);
serverFacBean.setServiceBeans(resourceObjects);
serverFacBean.setFeatures(features);
Server server = serverFacBean.create();
InputStream in = null;
try {
String serverAddress = server.getEndpoint().getEndpointInfo().getAddress();
String apiDocs = serverAddress + "/swagger.json";
URL url = new URL(apiDocs);
in = url.openStream();
String res = getStringFromInputStream(in);
generateJson(resourceClasses, res);
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
} finally {
server.stop();
}
}
use of io.fabric8.insight.metrics.model.Server in project zalenium by zalando.
the class KubernetesContainerMock method getMockedKubernetesContainerClient.
public static KubernetesContainerClient getMockedKubernetesContainerClient() {
// Mocking the environment variable to return false for video recording enabled
Environment environment = mock(Environment.class);
when(environment.getStringEnvVariable("ZALENIUM_KUBERNETES_CPU_REQUEST", null)).thenReturn("250m");
when(environment.getStringEnvVariable("ZALENIUM_KUBERNETES_CPU_LIMIT", null)).thenReturn("500m");
when(environment.getStringEnvVariable("ZALENIUM_KUBERNETES_MEMORY_REQUEST", null)).thenReturn("1Gi");
when(environment.getStringEnvVariable("ZALENIUM_KUBERNETES_MEMORY_LIMIT", null)).thenReturn("4Gi");
String hostName;
try {
hostName = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
hostName = "";
}
KubernetesServer server = new KubernetesServer();
server.before();
Map<String, String> zaleniumPodLabels = new HashMap<>();
zaleniumPodLabels.put("app", "zalenium");
zaleniumPodLabels.put("role", "grid");
Pod zaleniumPod = new PodBuilder().withNewMetadata().withLabels(zaleniumPodLabels).addToLabels(zaleniumPodLabels).withNamespace("test").withGenerateName(hostName).withName(hostName).and().build();
String videosVolumeName = RandomStringUtils.randomAlphabetic(5).toLowerCase();
String generalVolumeName = RandomStringUtils.randomAlphabetic(5).toLowerCase();
VolumeMount volumeMountVideos = new VolumeMountBuilder().withName(videosVolumeName).withMountPath("/tmp/videos").build();
VolumeMount volumeMountGeneral = new VolumeMountBuilder().withName(generalVolumeName).withMountPath("/tmp/mounted").build();
Container zaleniumContainer = new ContainerBuilder().withVolumeMounts(volumeMountVideos, volumeMountGeneral).build();
Volume videosVolume = new VolumeBuilder().withName(videosVolumeName).build();
Volume generalVolume = new VolumeBuilder().withName(generalVolumeName).build();
HostAlias hostAlias = new HostAliasBuilder().withHostnames("foo.local").withIp("127.0.0.1").build();
Map<String, String> nodeSelector = new HashMap<>();
nodeSelector.put("nodeLabelKey", "nodeLabelValue");
PodSpec zaleniumPodSpec = new PodSpecBuilder().withContainers(zaleniumContainer).withVolumes(videosVolume, generalVolume).withHostAliases(hostAlias).withNodeSelector(nodeSelector).build();
zaleniumPod.setSpec(zaleniumPodSpec);
String podsPath = String.format("/api/v1/namespaces/test/pods/%s", hostName);
server.expect().withPath(podsPath).andReturn(200, zaleniumPod).once();
Map<String, String> dockerSeleniumPodLabels = new HashMap<>();
dockerSeleniumPodLabels.put("createdBy", "zalenium");
Pod dockerSeleniumPod = new PodBuilder().withNewMetadata().withLabels(dockerSeleniumPodLabels).withNamespace("test").withName(hostName).and().build();
Container dockerSeleniumContainer = new ContainerBuilder().withEnv(new EnvVarBuilder().withName("NOVNC_PORT").withValue("40000").build()).build();
PodSpec dockerSeleniumPodSpec = new PodSpecBuilder().withContainers(dockerSeleniumContainer).build();
dockerSeleniumPod.setSpec(dockerSeleniumPodSpec);
PodStatus dockerSeleniumPodStatus = new PodStatusBuilder().withPodIP("localhost").build();
dockerSeleniumPod.setStatus(dockerSeleniumPodStatus);
server.expect().withPath("/api/v1/namespaces/test/pods?labelSelector=createdBy%3Dzalenium").andReturn(200, new PodListBuilder().withItems(dockerSeleniumPod).build()).always();
ServiceSpec serviceSpec = new ServiceSpecBuilder().withPorts(new ServicePortBuilder().withNodePort(40000).build()).build();
Service service = new ServiceBuilder().withSpec(serviceSpec).build();
server.expect().withPath("/api/v1/namespaces/test/services").andReturn(201, service).always();
String bashCommand = String.format("/api/v1/namespaces/test/pods/%s/exec?command=bash&command=-c&command=", hostName);
String tarCommand = String.format("/api/v1/namespaces/test/pods/%s/exec?command=tar&command=-C&command=", hostName);
String commandSuffix = "&stdout=true&stderr=true";
String expectedOutput = "test";
List<String> execPaths = new ArrayList<>();
execPaths.add(String.format("%stransfer-logs.sh%s", bashCommand, commandSuffix));
execPaths.add(String.format("%s/var/log/cont/&command=-c&command=.%s", tarCommand, commandSuffix));
execPaths.add(String.format("%s/videos/&command=-c&command=.%s", tarCommand, commandSuffix));
execPaths.add(String.format("%s/videos/&command=-C&command=.%s", tarCommand, commandSuffix));
execPaths.add(String.format("%sstop-video%s", bashCommand, commandSuffix));
execPaths.add(String.format("%sstart-video%s", bashCommand, commandSuffix));
execPaths.add(String.format("%stransfer-logs.sh%s", bashCommand, commandSuffix));
execPaths.add(String.format("%scleanup-container.sh%s", bashCommand, commandSuffix));
String notifyComplete = bashCommand.concat("notify%20%27Zalenium%27,%20%27TEST%20COMPLETED%27,%20--icon=/home/seluser/images/completed.png").concat(commandSuffix);
String notifyTimeout = bashCommand.concat("notify%20%27Zalenium%27,%20%27TEST%20TIMED%20OUT%27,%20--icon=/home/seluser/images/timeout.png").concat(commandSuffix);
execPaths.add(notifyComplete);
execPaths.add(notifyTimeout);
for (String execPath : execPaths) {
server.expect().withPath(execPath).andUpgradeToWebSocket().open(new OutputStreamMessage(expectedOutput)).done().once();
}
server.expect().withPath("/api/v1/namespaces/test/pods").andReturn(201, new PodBuilder().build()).always();
KubernetesClient client = server.getClient();
return new KubernetesContainerClient(environment, KubernetesContainerClient::createDoneablePodDefaultImpl, client);
}
Aggregations