Line data Source code
1 : #include <fstream> 2 : #include <boost/filesystem.hpp> 3 : #include "oper/netns_instance_adapter.h" 4 : #include "oper/service_instance.h" 5 : #include "oper/instance_task.h" 6 : #include "agent.h" 7 : #include "init/agent_param.h" 8 : 9 0 : InstanceTask* NetNSInstanceAdapter::CreateStartTask(const ServiceInstance::Properties &props, bool update) { 10 0 : std::stringstream cmd_str; 11 : 12 0 : if (netns_cmd_.length() == 0) { 13 0 : return NULL; 14 : } 15 0 : cmd_str << netns_cmd_ << " create"; 16 : 17 0 : cmd_str << " " << props.ServiceTypeString(); 18 0 : cmd_str << " " << UuidToString(props.instance_id); 19 0 : cmd_str << " " << UuidToString(props.vmi_inside); 20 0 : cmd_str << " " << UuidToString(props.vmi_outside); 21 : 22 0 : if (props.ip_prefix_len_inside != -1) { 23 0 : cmd_str << " --vmi-left-ip " << props.ip_addr_inside << "/"; 24 0 : cmd_str << props.ip_prefix_len_inside; 25 : } else { 26 0 : cmd_str << " --vmi-left-ip 0.0.0.0/0"; 27 : } 28 0 : cmd_str << " --vmi-right-ip " << props.ip_addr_outside << "/"; 29 0 : cmd_str << props.ip_prefix_len_outside; 30 : 31 0 : if (!props.mac_addr_inside.empty()) { 32 0 : cmd_str << " --vmi-left-mac " << props.mac_addr_inside; 33 : } else { 34 0 : cmd_str << " --vmi-left-mac 00:00:00:00:00:00"; 35 : } 36 0 : cmd_str << " --vmi-right-mac " << props.mac_addr_outside; 37 0 : cmd_str << " --gw-ip " << props.gw_ip; 38 : 39 0 : if (props.service_type == ServiceInstance::LoadBalancer) { 40 0 : if (props.loadbalancer_id.empty()) { 41 0 : LOG(ERROR, "loadbalancer id is missing for service instance: " 42 : << UuidToString(props.instance_id)); 43 0 : return NULL; 44 : } 45 0 : cmd_str << " --loadbalancer-id " << props.loadbalancer_id; 46 0 : std::stringstream pathgen; 47 0 : pathgen << loadbalancer_config_path_ 48 0 : << props.loadbalancer_id << ".conf"; 49 0 : const std::string &filename = pathgen.str(); 50 0 : std::ofstream fs(filename.c_str()); 51 0 : if (fs.fail()) { 52 0 : LOG(ERROR, "File create " << filename << ": " << strerror(errno)); 53 0 : return NULL; 54 : } 55 0 : const std::vector<autogen::KeyValuePair> &kvps = props.instance_kvps; 56 0 : bool kvp_found = false; 57 0 : std::vector<autogen::KeyValuePair>::const_iterator iter; 58 0 : autogen::KeyValuePair kvp; 59 0 : for (iter = kvps.begin(); iter != kvps.end(); ++iter) { 60 0 : kvp = *iter; 61 : /* :::: - Key Value Seperator 62 : ::::: - Key Value Pair Seperator */ 63 0 : if (kvp_found == true) 64 0 : fs << ":::::"; 65 0 : fs << kvp.key << "::::" << kvp.value; 66 0 : kvp_found = true; 67 : } 68 0 : if (!agent_->params()->si_lb_ssl_cert_path().empty()) { 69 0 : fs << ":::::" << "lb_ssl_cert_path"; 70 0 : fs << "::::" << agent_->params()->si_lb_ssl_cert_path(); 71 : } 72 0 : if (!agent_->params()->si_lbaas_auth_conf().empty()) { 73 0 : fs << ":::::" << "lbaas_auth_conf"; 74 0 : fs << "::::" << agent_->params()->si_lbaas_auth_conf(); 75 : } 76 0 : fs.close(); 77 0 : if (kvp_found == false) { 78 0 : LOG(ERROR, "KeyValuePair is missing for loadbalancer: " 79 : << props.loadbalancer_id); 80 0 : return NULL; 81 : } 82 0 : cmd_str << " --cfg-file " << pathgen.str(); 83 0 : } 84 : 85 0 : if (update) { 86 0 : cmd_str << " --update"; 87 : } 88 0 : LOG(INFO, "Command to be executed: " + cmd_str.str()); 89 0 : return new InstanceTaskExecvp("NetNS", cmd_str.str(), START, 90 0 : agent_->event_manager()); 91 0 : } 92 : 93 0 : InstanceTask* NetNSInstanceAdapter::CreateStopTask(const ServiceInstance::Properties &props) { 94 0 : std::stringstream cmd_str; 95 : 96 0 : if (netns_cmd_.length() == 0) { 97 0 : return NULL; 98 : } 99 0 : cmd_str << netns_cmd_ << " destroy"; 100 : 101 0 : if (props.instance_id.is_nil() || 102 0 : props.vmi_outside.is_nil()) { 103 0 : return NULL; 104 : } 105 : 106 0 : if (props.interface_count == 2 && props.vmi_inside.is_nil()) { 107 0 : return NULL; 108 : } 109 : 110 0 : cmd_str << " " << props.ServiceTypeString(); 111 0 : cmd_str << " " << UuidToString(props.instance_id); 112 0 : cmd_str << " " << UuidToString(props.vmi_inside); 113 0 : cmd_str << " " << UuidToString(props.vmi_outside); 114 0 : if (props.service_type == ServiceInstance::LoadBalancer) { 115 0 : if (props.loadbalancer_id.empty()) { 116 0 : LOG(ERROR, "loadbalancer id is missing for service instance: " 117 : << UuidToString(props.instance_id)); 118 0 : return NULL; 119 : } 120 0 : cmd_str << " --loadbalancer-id " << props.loadbalancer_id; 121 : } 122 0 : LOG(INFO, "Preparing to destroy with command: " + cmd_str.str()); 123 0 : return new InstanceTaskExecvp("NetNS", cmd_str.str(), STOP, 124 0 : agent_->event_manager()); 125 0 : } 126 : 127 0 : bool NetNSInstanceAdapter::isApplicable(const ServiceInstance::Properties &props) { 128 0 : return (props.virtualization_type == ServiceInstance::NetworkNamespace); 129 : }