Commit f6ec91ac by Tomasz Ciesielczyk

install functions

parent 8b526d22
......@@ -58,6 +58,12 @@ class RTMApi private constructor() {
}
fun installResources() {
log.info("Start service INIT!")
ServerConfInitializer.installResources()
}
/**
* function used for tracking thread leak
......
......@@ -8,6 +8,8 @@ import psnc.m2dc.rtm.model.rpm.Resource
import psnc.m2dc.rtm.properties.RTMProperties
import psnc.m2dc.rtm.properties.keys.Keys
import psnc.m2dc.rtm.utils.*
import java.io.File
import java.io.FileNotFoundException
/**
* Server's initialization methods
......@@ -17,23 +19,6 @@ object ServerConfInitializer {
fun init() {
try {
// var s = """
// {"0": {"powerAction": "set_property", "value": "[\"-p\", \"cpu_freq\", \"-p\", \"40\"]"}, "16": {"powerAction": "set_property", "value": "[\"-p\", \"cpu_freq\", \"-p\", \"100\"]"}, "37": {"powerAction": "set_property", "value": "[\"-p\", \"cpu_freq\", \"-p\", \"50\"]"}, "39": {"powerAction": "set_property", "value": "[\"-p\", \"cpu_freq\", \"-p\", \"20\"]"}, "100": {"powerAction": "set_property", "value": "[\"-p\", \"cpu_freq\", \"-p\", \"30\"]"}}
//
// """.trimIndent()
// s ="""
// {"0": [{
// "powerAction": "set_property", "properties": {"value": "[\"-p\", \"rapl\", \"-p\", \"50\"]"}
// } ] }
//
// """
// s = """
// {"0": [{"resourceId": "", "powerAction": "set_property", "properties": {"value": "[\"-p\", \"rapl\", \"-p\", \"10\"]"}}], "13": [{"resourceId": "", "powerAction": "set_property", "properties": {"value": "[\"-p\", \"rapl\", \"-p\", \"80\"]"}}], "26": [{"resourceId": "", "powerAction": "set_property", "properties": {"value": "[\"-p\", \"rapl\", \"-p\", \"95\"]"}}], "77": [{"resourceId": "", "powerAction": "set_property", "properties": {"value": "[\"-p\", \"rapl\", \"-p\", \"90\"]"}}], "100": [{"resourceId": "", "powerAction": "set_property", "properties": {"value": "[\"-p\", \"rapl\", \"-p\", \"60\"]"}}]}
// """
// val parsed = s.fromJson<HashMap<Double, List<ResourceAction>>>(
// object : TypeToken<HashMap<Double, List<ResourceAction>>>() {
// }.type)
logInfo("version: $APP_VERSION")
loadConfig()
......@@ -54,6 +39,72 @@ object ServerConfInitializer {
System.exit(-1)
}
}
private fun checkScripts(){
var exists = true
exists = exists && File("bin/get_recs_nodes.sh").exists()
exists = exists && File("bin/install_node.sh").exists()
if(!exists){
throw FileNotFoundException("Check install_node.sh asd get_recs_nodes.sh exist in the bin direcotry ")
}
}
fun installResources() {
try {
logInfo("version: $APP_VERSION")
loadConfig()
RTMProperties.parsePath("${RTMProperties.basePath}/_resource.conf")
val params = mutableListOf<String>()
Keys.RTM.recs_user.getNullable()?.let { params.add("-u");params.add(it) }
Keys.RTM.recs_password.getNullable()?.let { params.add("-p");params.add(it) }
Keys.RTM.recs_host_src_list.get().forEach {
params.add("-h");params.add(it)
}
val result = runScript("bin/get_recs_nodes.sh", params)
val lines = result.split("\n")
val nodeData = File("resource_map").loadDataTable()
lines.forEach {
it.split(";").let {
nodeData[it[0]]?.set("recs_id", it[1])
}
}
val resources = nodeData.filter { nodeRow ->
nodeRow.value["is_active"] ?: "0" == "1"
}.map { nodeRow ->
val cells = nodeRow.value
val nodeId = cells["node_id"]!!
val typeId = cells["type_id"]!!
install(Resource(nodeId, typeId, null, properties = cells as MutableMap<String, Any>))
}.toList()
DAO.ResourcesManagerApi.saveResourcesToFile(resources)
LightStorage.instance.save(null)
logInfo("End init resources")
} catch (e: Exception) {
e.printStackTrace()
logError("Server initialization failed: ${e.message}")
System.exit(-1)
}
}
private val installProperties = listOf("RAPL")
private fun install(resource: Resource): Resource {
val params = mutableListOf<String>()
resource.get("ssh_host")?.let { params.add("-u");params.add(it) }
installProperties.forEach { prop ->
resource.get(prop)?.let {
if (it == "1") {
params.add("-p");params.add(it)
}
}
}
val result = runScript("bin/get_recs_nodes.sh", params)
d
return resource
}
private fun loadConfig() {
......@@ -118,6 +169,7 @@ object ServerConfInitializer {
r.put(Resource.Property.ssh_host, "ssh_host")
r.put(Resource.Property.ipmi_host, "ipmi_host")
r.put(Resource.Property.recs_host, "recs_host")
result.add(r)
// result.add dummy
return result
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package psnc.m2dc.rtm.commands.java;
import com.beust.jcommander.Parameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import psnc.m2dc.rtm.ICommandLine;
import psnc.m2dc.rtm.RTMApi;
/**
* @author Tomek
*/
@Parameters(commandNames = "install", commandDescription = " Init server conf")
public class InstallResourceFileCommand extends ICommandLine {
private static final Logger log = LoggerFactory.getLogger(InstallResourceFileCommand.class);
@Override
public void execute() {
RTMApi.Companion.instance().installResources();
}
}
package psnc.m2dc.rtm.commands.java
val APP_VERSION = "1.0.554_53306"
\ No newline at end of file
val APP_VERSION = "1.0.556_58610"
\ No newline at end of file
......@@ -29,7 +29,13 @@ class EnergySaverHandler : Handlers() {
get(HandlersMap.esm_policy) { req, res ->
EnergySaverHandler.instance.getESMPolicy(req, res)
}
//TODO:
// post(HandlersMap.esm_algorithm) { req, res ->
// EnergySaverHandler.instance.setESMAlgorithm(req, res)
// }
// get(HandlersMap.esm_algorithm) { req, res ->
// EnergySaverHandler.instance.getESMAlgorithm(req, res)
// }
}
}
......
......@@ -58,6 +58,13 @@ class PCMHandler : Handlers() {
get(HandlersMap.pcm_check_state) { req, res ->
instance.checkState(req, res)
}
//TODO:
// post(HandlersMap.pcm_algorithm) { req, res ->
// instance.setPCMAlgorithm(req, res)
// }
// get(HandlersMap.pcm_algorithm) { req, res ->
// instance.getPCMAlgorithm(req, res)
// }
}
}
......
package psnc.m2dc.rtm.client
const val APP_VERSION = "1.1.554_53306"
\ No newline at end of file
const val APP_VERSION = "1.1.556_58611"
\ No newline at end of file
......@@ -10,12 +10,11 @@ import psnc.m2dc.rtm.utils.logError
*/
data class Resource(val id: String, var typeId: String, val parentId: String?,
var childResources: MutableList<Resource>? = null,
val properties: HashMap<String, Any> = HashMap<String, Any>()
/*, var resourceType: ResourceType? = null*/) {
val properties: MutableMap<String, Any> = HashMap<String, Any>()
/*, var resourceType: ResourceType? = null*/) {
constructor() : this("", "", null, null, HashMap<String, Any>())
enum class Property {
is_node, tag, ipmi_host, recs_host, ssh_host, ssh_username, action_power_details, node_id
}
......@@ -168,7 +167,7 @@ data class Resource(val id: String, var typeId: String, val parentId: String?,
data class ResourceType(val id: String, /*val type: Resource.Type*/
val apiMap: HashMap<String, List<ApiDetails>> = HashMap(),
var properties: HashMap<String, Any>? = null) {
var properties: HashMap<String, Any>? = null) {
fun contains(typeName: String) = this.apiMap[typeName]?.size ?: 0 > 0
......
......@@ -19,6 +19,8 @@ class RTM {
val recs_host_src_list = RTMProperty(RTM + "recs_host_src_list", arrayOf("CONFIG"), description = "Available sources: LDAP, FILE (TODO), CONF/CONFIG( @see recs_hosts)")
val recs_hosts_file = RTMProperty(RTM + "recs_hosts_file", null, String::class.java)
val recs_hosts = RTMProperty(RTM + "recs_hosts", arrayOf<String>(), description = "comma separated list of hosts")
val recs_user = RTMProperty(RTM + "recs_user", null, String::class.java,description="RECS username")
val recs_password = RTMProperty(RTM + "recs_password", null, String::class.java,description="RECS password")
val resource_file = RTMProperty(RTM + "resource_file", "\$[basePath]/conf/resources.conf", String::class.java)
val resource_json_file = RTMProperty(RTM + "resource_json_file", "\$[basePath]/conf/resources.json", String::class.java, description = " File containing resource properties in json format")
// val file_storage_location = RTMProperty(RTM + "file_storage_location", null, String::class.java)
......
......@@ -39,7 +39,7 @@ fun logError(log: Logger, e: Exception, msg: String) {
}
fun logError(e: Exception, msg: String) {
RTMProperties.log.error(e::class.java.simpleName + "(" + e.stackTrace[0].lineNumber + ")" + ": " + msg +" "+ e.message)
RTMProperties.log.error(e::class.java.simpleName + "(" + e.stackTrace[0].lineNumber + ")" + ": " + msg + " " + e.message)
}
fun logDebug(e: Exception, msg: String) {
......@@ -287,3 +287,65 @@ fun parseTimeDiff(timeDiffMs: Long): String {
}
//#endregion
fun File.loadDataTable(separator: String = "\t", idIndex: Int = 0, headers: List<String>? = null): Map<String, MutableMap<String, String>> {
var mHeaders = headers
val result = HashMap<String, MutableMap<String, String>>()
this.forEachLine { line ->
if (mHeaders == null) {
mHeaders = line.split(separator)
} else {
val cells = line.split(separator)
val rowKey = cells[idIndex]
val map = cells.mapIndexed { idx, cell ->
val cellKey = headers!![idx]
cellKey to cell
}.associate { it }.toMutableMap()
result[rowKey] = map
}
}
return result
}
fun generateBindToml(data: Map<String, Map<String, String>>, user: String = "tomaszc", port: Int = 22): String {
val parsed = data.values.mapIndexed { idx, nodeData ->
val m = mapOf(
"host" to (nodeData["ssh_host"] ?: ""),
"model" to (nodeData["model"] ?: "default"),
"recs_host" to (nodeData["recs_host"] ?: ""),
"recs_id" to (nodeData["recs_id"] ?: ""),
"user" to (nodeData["ssh_username"] ?: user),
"port" to port
)
val propStr = m.entries.joinToString(separator = ", ", prefix = "{ ", postfix = " }") {
val key = it.key
val value = it.value
if (value is String) {
"$key = \"$value\" "
} else {
"$key = $value "
}
}
val hId = "h$idx"
val bindStr = """
[bind.$hId]
monitor = "default_monitor"
launch = "default_launch"
launch-interval = 30
""".trimIndent()
hId to Pair("h$idx = $propStr", bindStr)
}.toMap()
val hostSection = "[hosts]\n" + parsed.values.joinToString(separator = "\n") { it.first }
val other = "\n[launches]\n#[[app,envlist], [settings_list]]\n" +
"\nlaunch_nasa = [\n]\n\n[monitors]\ndefault_monitor = [\n]\n"
val bindSection = "\n[binds]\n" + parsed.values.joinToString(separator = "\n") { it.first }
return hostSection + other + bindSection
}
package psnc.m2dc.rtm.utils
import psnc.m2dc.rtm.properties.keys.Keys
import psnc.m2dc.rtm.rpm.nativeapi.SSHApiImpl
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.util.*
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
/**
* Created by Tomek on 2019-06-13.
*/
val MAX_SCRIPT_EXECUTION_TIME = 60000L
private fun getOutput(process: Process, stdOut: StringBuilder, stdErr: StringBuilder): Pair<Any, Any> {
val newFixedThreadPool = Executors.newFixedThreadPool(2)
val output = newFixedThreadPool.submit {
var reader = BufferedReader(InputStreamReader(process.inputStream))
var line = reader.readLine()
while (line != null) {
stdOut.append(line).append("\n")
line = reader.readLine()
}
stdOut.toString()
}
val error = newFixedThreadPool.submit {
var reader = BufferedReader(InputStreamReader(process.errorStream))
var line = reader.readLine()
while (line != null) {
stdErr.append(line).append("\n")
line = reader.readLine()
}
stdErr.toString()
}
newFixedThreadPool.shutdown()
// process.waitFor();
if (!process.waitFor(MAX_SCRIPT_EXECUTION_TIME, TimeUnit.MILLISECONDS)) {
logError("script execution timeout")
process.destroy()
}
return Pair(output.get(), error.get())
}
fun runScript(scriptName: String, params: List<String> = ArrayList<String>()): String {
val stdOut = StringBuilder()
val stdErr = StringBuilder()
val p: Process
try {
val array = arrayOf<String>(scriptName, *params.toTypedArray())
p = Runtime.getRuntime().exec(array)
// printConsole("pwait for : ${cmd.host}")
getOutput(p, stdOut, stdErr)//.toJson(true)
if (Keys.Default.debug.get()) {
stdOut.toString()
.let { if (it.length > 250) it.substring(0, 200) else it }
.let { SSHApiImpl.log.debug(it) }
} else {
val str = stdErr.toString()
if (!str.isEmpty())
logError(str)
}
} catch (e: Exception) {
when (e) {
is IOException, is InterruptedException -> {
logError("Script execution error" + e.message)
throw Exception(e)
}
}
throw Exception(e)
}
SSHApiImpl.log.debug(stdOut.toString())
// printConsole("output for ${cmd.host} :$stdOut")
return if (stdOut.length > 1)
stdOut.toString().trim().let { logDebug("${it.take(256).replace("\n", " ")}", true); it }
else {
stdErr.toString().trim().let { logError("script execution error: $scriptName :$it"); it }
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment