1 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts
\r
3 import com.fasterxml.jackson.databind.node.ObjectNode
\r
4 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
\r
5 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
\r
6 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction
\r
7 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentFunctionScriptingService
\r
9 import java.io.ByteArrayInputStream
\r
10 import java.io.IOException
\r
11 import java.io.InputStream
\r
12 import org.apache.sshd.client.SshClient
\r
13 import org.apache.sshd.client.channel.ClientChannel
\r
14 import org.apache.sshd.client.future.AuthFuture
\r
15 import org.apache.sshd.client.future.ConnectFuture
\r
16 import org.apache.sshd.client.session.ClientSession
\r
17 import org.apache.sshd.common.future.SshFutureListener
\r
18 import org.apache.sshd.common.util.io.NoCloseInputStream
\r
19 import org.apache.sshd.common.util.io.NoCloseOutputStream
\r
20 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.storedContentFromResolvedArtifactNB
\r
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
\r
22 import org.onap.ccsdk.cds.controllerblueprints.core.asListOfString
\r
23 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
\r
24 import org.slf4j.LoggerFactory
\r
25 import java.io.ByteArrayOutputStream
\r
28 open class SampleScriptComponent : AbstractScriptComponentFunction() {
\r
30 private val log = LoggerFactory.getLogger(SampleScriptComponent::class.java)!!
\r
32 override suspend fun processNB(executionRequest: ExecutionServiceInput) {
\r
33 log.info("Hello Kotlin!")
\r
34 val resolution_key = getDynamicProperties("resolution-key").asText()
\r
35 log.info("resolution_key: $resolution_key")
\r
36 val payload = storedContentFromResolvedArtifactNB(resolution_key, "userconfig")
\r
37 val payloadObject = JacksonUtils.jsonNode(payload) as ObjectNode
\r
38 val freeradius_ip: String = payloadObject.get("freeradius_ip").asText()
\r
39 log.info("freeradius_ip: $freeradius_ip")
\r
40 val user_config: String = payloadObject.get("user_config").asText()
\r
41 log.info("user_config: $user_config")
\r
42 log.info("Waiting 2 minutes for VM to initialize")
\r
43 Thread.sleep(120000)
\r
44 val client = SshClient.setUpDefaultClient()
\r
46 log.info("SSH Client Service started successfully")
\r
47 val session = client.connect("cloud", freeradius_ip, 22).verify(3000).session
\r
48 session.addPasswordIdentity("password")
\r
49 log.info("SSH Client authenticating...")
\r
50 val authFuture = session.auth().verify(3000)
\r
51 log.info("SSH client session($session) created")
\r
52 log.info("SSH Authenticated: $authFuture.isSuccess()")
\r
53 val command="echo '$user_config' | sudo tee -a /etc/freeradius/users"
\r
54 log.info("Executing host($session) command($command)")
\r
55 val channel = session.createExecChannel(command)
\r
56 val outputStream = ByteArrayOutputStream()
\r
57 channel!!.out = outputStream
\r
58 channel!!.err = outputStream
\r
59 channel!!.open().await()
\r
60 //val waitMask = channel!!.waitFor(Collections.unmodifiableSet(EnumSet.of(ClientChannelEvent.CLOSED)), 3000)
\r
61 //if (waitMask.contains(ClientChannelEvent.TIMEOUT)) {
\r
62 // throw BluePrintProcessorException("Failed to retrieve command result in time: $command")
\r
65 val exitStatus = channel!!.exitStatus
\r
66 //ClientChannel. .validateCommandExitStatusCode(command, exitStatus!!)
\r
67 if (channel != null) {
\r
70 if (client.isOpen) {
\r
73 log.info(outputStream.toString())
\r
74 log.info("SSH Client Service stopped successfully")
\r
80 override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
\r