示例#1
0
 def delete_all_pods_for_service(dto):
     """
     停止所有的某类容器[根据关键字删]
     :param service: 容器名中的关键字
     :return: 结果
     """
     service = dto["service"]
     result_list = []
     target_host = "10.60.38.181"
     name_list = K8sObserver.get_pod_name_list('sock-shop')
     for pod_name in name_list:
         if service in pod_name and (service + "-db") not in pod_name and (
                 service + "-external") not in pod_name:
             target_inject = Cmd['k8s'] + pod_name
             r = Runner()
             r.run_ad_hoc(hosts=target_host,
                          module='shell',
                          args=target_inject)
             result = r.get_adhoc_result()
             result_list.append(
                 handle_inject_result('k8s', target_host, target_inject,
                                      result,
                                      sys._getframe().f_code.co_name,
                                      dto['open']))
     return result_list
示例#2
0
 def chaos_inject_random(dto):
     global Default_cmd
     Default_cmd = DefaultCmd.get_default_cmd()
     find = 0
     timeout = ''
     pod_inject = ''
     (target_host, is_exist) = get_target_host(dto)
     if not is_exist:
         return 'Host: ' + target_host + ' does not exist.'
     j = random.randint(0, len(Default_cmd) - 1)
     inject_type = Default_cmd.keys()[j]
     if inject_type == "k8s":
         pod_list = K8sObserver.get_pod_name_list("sock-shop")
         k = random.randint(0, len(pod_list) - 1)
         pod_inject = pod_list[k]
         target_inject = Default_cmd[Default_cmd.keys()
                                     [j]] + " " + pod_inject
     else:
         target_inject = Default_cmd[Default_cmd.keys()[j]]
     if dto['timeout'] == 'default':
         timeout = ' --timeout 300'
     elif dto['timeout'] != 'no':
         timeout = ' --timeout ' + dto['timeout']
     for i in range(0, len(has_injected)):
         if has_injected[i]['host'] == target_host and has_injected[i][
                 'inject_type'] == target_inject:
             find = 1
     if find == 0:
         if inject_type != "k8s":
             target_inject = target_inject + timeout
         r = Runner()
         r.run_ad_hoc(hosts=target_host, module='shell', args=target_inject)
         result = r.get_adhoc_result()
         return handle_inject_result(inject_type, target_host,
                                     target_inject, result,
                                     sys._getframe().f_code.co_name,
                                     dto['open'])
     else:
         Logger.log(
             "error", "HOST HAS BEEN INJECTED - Method : " +
             sys._getframe().f_code.co_name + "() - - " + str(dto))
         return "The host has been injected by the inject"