node_info.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "node_info.h"
  2. #include "proto/nodeinstance/info/v1/info.pb.h"
  3. #include "schema_wrapper_utils.h"
  4. static int generate_node_info(nodeinstance::info::v1::NodeInfo *info, struct aclk_node_info *data)
  5. {
  6. struct label *label;
  7. google::protobuf::Map<std::string, std::string> *map;
  8. if (data->name)
  9. info->set_name(data->name);
  10. if (data->os)
  11. info->set_os(data->os);
  12. if (data->os_name)
  13. info->set_os_name(data->os_name);
  14. if (data->os_version)
  15. info->set_os_version(data->os_version);
  16. if (data->kernel_name)
  17. info->set_kernel_name(data->kernel_name);
  18. if (data->kernel_version)
  19. info->set_kernel_version(data->kernel_version);
  20. if (data->architecture)
  21. info->set_architecture(data->architecture);
  22. info->set_cpus(data->cpus);
  23. if (data->cpu_frequency)
  24. info->set_cpu_frequency(data->cpu_frequency);
  25. if (data->memory)
  26. info->set_memory(data->memory);
  27. if (data->disk_space)
  28. info->set_disk_space(data->disk_space);
  29. if (data->version)
  30. info->set_version(data->version);
  31. if (data->release_channel)
  32. info->set_release_channel(data->release_channel);
  33. if (data->timezone)
  34. info->set_timezone(data->timezone);
  35. if (data->virtualization_type)
  36. info->set_virtualization_type(data->virtualization_type);
  37. if (data->container_type)
  38. info->set_container_type(data->container_type);
  39. if (data->custom_info)
  40. info->set_custom_info(data->custom_info);
  41. for (size_t i = 0; i < data->service_count; i++)
  42. info->add_services(data->services[i]);
  43. if (data->machine_guid)
  44. info->set_machine_guid(data->machine_guid);
  45. nodeinstance::info::v1::MachineLearningInfo *ml_info = info->mutable_ml_info();
  46. ml_info->set_ml_capable(data->ml_info.ml_capable);
  47. ml_info->set_ml_enabled(data->ml_info.ml_enabled);
  48. map = info->mutable_host_labels();
  49. label = data->host_labels_head;
  50. while (label) {
  51. map->insert({label->key, label->value});
  52. label = label->next;
  53. }
  54. return 0;
  55. }
  56. char *generate_update_node_info_message(size_t *len, struct update_node_info *info)
  57. {
  58. nodeinstance::info::v1::UpdateNodeInfo msg;
  59. msg.set_node_id(info->node_id);
  60. msg.set_claim_id(info->claim_id);
  61. if (generate_node_info(msg.mutable_data(), &info->data))
  62. return NULL;
  63. set_google_timestamp_from_timeval(info->updated_at, msg.mutable_updated_at());
  64. msg.set_machine_guid(info->machine_guid);
  65. msg.set_child(info->child);
  66. nodeinstance::info::v1::MachineLearningInfo *ml_info = msg.mutable_ml_info();
  67. ml_info->set_ml_capable(info->ml_info.ml_capable);
  68. ml_info->set_ml_enabled(info->ml_info.ml_enabled);
  69. struct capability *capa;
  70. if (info->node_capabilities) {
  71. capa = info->node_capabilities;
  72. while (capa->name) {
  73. aclk_lib::v1::Capability *proto_capa = msg.mutable_node_info()->add_capabilities();
  74. capability_set(proto_capa, capa);
  75. capa++;
  76. }
  77. }
  78. if (info->node_instance_capabilities) {
  79. capa = info->node_instance_capabilities;
  80. while (capa->name) {
  81. aclk_lib::v1::Capability *proto_capa = msg.mutable_node_instance_info()->add_capabilities();
  82. capability_set(proto_capa, capa);
  83. capa++;
  84. }
  85. }
  86. *len = PROTO_COMPAT_MSG_SIZE(msg);
  87. char *bin = (char*)malloc(*len);
  88. if (bin)
  89. msg.SerializeToArray(bin, *len);
  90. return bin;
  91. }