Fix for container .env file and small updates
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / rapid / k8sdeployment.py
1 #!/usr/bin/env python2.7
2
3 ##
4 ## Copyright (c) 2019-2020 Intel Corporation
5 ##
6 ## Licensed under the Apache License, Version 2.0 (the "License");
7 ## you may not use this file except in compliance with the License.
8 ## You may obtain a copy of the License at
9 ##
10 ##     http://www.apache.org/licenses/LICENSE-2.0
11 ##
12 ## Unless required by applicable law or agreed to in writing, software
13 ## distributed under the License is distributed on an "AS IS" BASIS,
14 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ## See the License for the specific language governing permissions and
16 ## limitations under the License.
17 ##
18
19 import sys
20 from kubernetes import client, config
21 import ConfigParser
22 import logging
23 from logging import handlers
24
25 from pod import Pod
26
27 class K8sDeployment:
28     """Deployment class to create containers for test execution in Kubernetes
29     environment. 
30     """
31     LOG_FILE_NAME = "createrapidk8s.log"
32     SSH_PRIVATE_KEY = "./rapid_rsa_key"
33     SSH_USER = "centos"
34
35     POD_YAML_TEMPLATE_FILE_NAME = "pod-rapid.yaml"
36
37     _log = None
38     _create_config = None
39     _runtime_config = None
40     _total_number_of_pods = 0
41     _pods = []
42
43     def __init__(self):
44         # Configure logger
45         self._log = logging.getLogger("k8srapid")
46         self._log.setLevel(logging.DEBUG)
47
48         console_formatter = logging.Formatter("%(message)s")
49         console_handler = logging.StreamHandler(sys.stdout)
50         console_handler.setLevel(logging.DEBUG)
51         console_handler.setFormatter(console_formatter)
52
53         file_formatter = logging.Formatter("%(asctime)s - "
54                                            "%(levelname)s - "
55                                            "%(message)s")
56         file_handler = logging.handlers.RotatingFileHandler(self.LOG_FILE_NAME,
57                                                             backupCount=10)
58         file_handler.setLevel(logging.DEBUG)
59         file_handler.setFormatter(file_formatter)
60
61         self._log.addHandler(file_handler)
62         self._log.addHandler(console_handler)
63
64         # Initialize k8s plugin
65         config.load_kube_config()
66         Pod.k8s_CoreV1Api = client.CoreV1Api()
67
68     def load_create_config(self, config_file_name):
69         """Read and parse configuration file for the test environment.
70         """
71         self._log.info("Loading configuration file %s", config_file_name)
72         self._create_config = ConfigParser.RawConfigParser()
73         try:
74             self._create_config.read(config_file_name)
75         except Exception as e:
76             self._log.error("Failed to read config file!\n%s\n" % e)
77             return -1
78
79         # Now parse config file content
80         # Parse [DEFAULT] section
81         if self._create_config.has_option("DEFAULT", "total_number_of_pods"):
82             self._total_number_of_pods = self._create_config.getint(
83                 "DEFAULT", "total_number_of_pods")
84         else:
85             self._log.error("No option total_number_of_pods in DEFAULT section")
86             return -1
87
88         self._log.debug("Total number of pods %d" % self._total_number_of_pods)
89
90         # Parse [PODx] sections
91         for i in range(1, int(self._total_number_of_pods) + 1):
92             # Search for POD name
93             if self._create_config.has_option("POD%d" % i,
94                                               "name"):
95                 pod_name = self._create_config.get(
96                     "POD%d" % i, "name")
97             else:
98                 pod_name = "pod-rapid-%d" % i
99
100             # Search for POD hostname
101             if self._create_config.has_option("POD%d" % i,
102                                               "nodeSelector_hostname"):
103                 pod_nodeselector_hostname = self._create_config.get(
104                     "POD%d" % i, "nodeSelector_hostname")
105             else:
106                 pod_nodeselector_hostname = None
107
108             # Search for POD dataplane static IP
109             if self._create_config.has_option("POD%d" % i,
110                                               "dp_ip"):
111                 pod_dp_ip = self._create_config.get(
112                     "POD%d" % i, "dp_ip")
113             else:
114                 pod_dp_ip = None
115
116             pod = Pod(pod_name)
117             pod.set_nodeselector(pod_nodeselector_hostname)
118             pod.set_dp_ip(pod_dp_ip)
119             pod.set_id(i)
120
121             # Add POD to the list of PODs which need to be created
122             self._pods.append(pod)
123
124         return 0
125
126     def create_pods(self):
127         """ Create test PODs and wait for them to start.
128         Collect information for tests to run.
129         """
130         self._log.info("Creating PODs...")
131
132         # Create PODs using template from yaml file
133         for pod in self._pods:
134             self._log.info("Creating POD %s...", pod.get_name())
135             pod.create_from_yaml(K8sDeployment.POD_YAML_TEMPLATE_FILE_NAME)
136
137         # Wait for PODs to start
138         for pod in self._pods:
139             pod.wait_for_start()
140
141         # Collect information from started PODs for test execution
142         for pod in self._pods:
143             pod.set_ssh_credentials(K8sDeployment.SSH_USER, K8sDeployment.SSH_PRIVATE_KEY)
144             pod.get_sriov_dev_mac()
145
146     def save_runtime_config(self, config_file_name):
147         self._log.info("Saving config %s for runrapid script...",
148                        config_file_name)
149         self._runtime_config = ConfigParser.RawConfigParser()
150
151         # Section [DEFAULT]
152 #        self._runtime_config.set("DEFAULT",
153 #                                 "total_number_of_test_machines",
154 #                                 self._total_number_of_pods)
155
156         # Section [ssh]
157         self._runtime_config.add_section("ssh")
158         self._runtime_config.set("ssh",
159                                  "key",
160                                  K8sDeployment.SSH_PRIVATE_KEY)
161         self._runtime_config.set("ssh",
162                                  "user",
163                                  K8sDeployment.SSH_USER)
164
165         # Section [rapid]
166         self._runtime_config.add_section("rapid")
167         self._runtime_config.set("rapid",
168                                  "total_number_of_machines",
169                                  self._total_number_of_pods)
170
171         # Export information about each pod
172         # Sections [Mx]
173         for pod in self._pods:
174             self._runtime_config.add_section("M%d" % pod.get_id())
175             self._runtime_config.set("M%d" % pod.get_id(),
176                                      "admin_ip", pod.get_admin_ip())
177             self._runtime_config.set("M%d" % pod.get_id(),
178                                      "dp_mac1", pod.get_dp_mac())
179             self._runtime_config.set("M%d" % pod.get_id(),
180                                      "dp_pci_dev", pod.get_dp_pci_dev())
181             self._runtime_config.set("M%d" % pod.get_id(),
182                                      "dp_ip1", pod.get_dp_ip())
183
184         # Section [Varia]
185         self._runtime_config.add_section("Varia")
186         self._runtime_config.set("Varia",
187                                  "vim",
188                                  "kubernetes")
189
190         # Write runtime config file
191         with open(config_file_name, "w") as file:
192             self._runtime_config.write(file)
193
194     def delete_pods(self):
195         for pod in self._pods:
196             pod.terminate()