Ejemplo n.º 1
0
 def delete_persistent_volume_claim(self, name, namespace):
     try:
         api_response = self.coreApi.delete_namespaced_persistent_volume_claim(name=name, namespace=namespace)
         return util.Result(success=True, data=api_response)
     except ApiException as e:
         err_msg = "Exception when calling CoreV1Api->delete_namespaced_persistent_volume_claim: %s\n" % e
         return util.Result(success=hiss.hiss(err_msg), msg=err_msg)
Ejemplo n.º 2
0
    def delete_stateful(self, name, namespace, delete_pvc=False):
        action = 'Delete'
        if delete_pvc == True:
            action = 'Terminate'
            read_result = self.read_stateful_set(name, namespace)
            if read_result.success == False:
                return read_result
            volume_claim_templates = read_result.data.spec.volume_claim_templates
            if volume_claim_templates != None:
                for pvt in volume_claim_templates:
                    pvt_name = pvt.metadata.name
                    list_pvc = self.find_pvc(namespace, keyword=pvt_name)
                    for pvc in list_pvc:
                        hiss.echo('Delete pvc %s ' % pvc)
                        self.delete_persistent_volume_claim(
                            name=pvc, namespace=namespace)

        try:
            body = client.V1DeleteOptions(propagation_policy='Background')
            api_response = self.appsApi.delete_namespaced_stateful_set(
                name, namespace, body=body)
            self.check_pod_status_by_keyword(keyword=name,
                                             namespace=namespace,
                                             is_delete=True)
            hiss.echo('%s stateful set %s on namespace %s success' %
                      (action, name, namespace))
            return util.Result(success=True, data=api_response)
        except ApiException as e:
            err_msg = "Exception when calling AppsV1Api->delete_namespaced_stateful_set: %s\n" % e
            return util.Result(success=hiss.hiss(err_msg), msg=err_msg)
Ejemplo n.º 3
0
 def read_stateful_set(self, name, namespace):
     try:
         api_response = self.appsApi.read_namespaced_stateful_set(name, namespace)
         return util.Result(success=True, data=api_response)
     except ApiException as e:
         msg = "Exception when calling AppsV1Api->read_namespaced_stateful_set: %s\n" % e
         return util.Result(success=False, msg=msg)
Ejemplo n.º 4
0
 def delete_job(self, name, namespace):
     try:
         body = client.V1DeleteOptions(propagation_policy='Background')
         api_response = self.batchApi.delete_namespaced_job(name, namespace, body=body)
         hiss.echo('Delete job %s on namespace %s success' % (name, namespace))
         return util.Result(success=True, data=api_response)
     except ApiException as e:
         err_msg = "Exception when calling BatchV1Api->delete_namespaced_job: %s\n" % e
         return util.Result(success=hiss.hiss(err_msg), msg=err_msg)
Ejemplo n.º 5
0
 def exec_pod(self, podName, namespace, command):
     try:
         resp = stream(self.coreApi.connect_get_namespaced_pod_exec,
                       name=podName, namespace=namespace, stderr=True, stdin=True, stdout=True, command=command)
         # return util.resultDict(success=True, msg='Success', data=resp)
         return util.Result(success=True, msg='Success', data=resp)
     except ApiException as e:
         err_msg = "Exception when calling CoreV1Api->connect_get_namespaced_pod_exec: %s\n" % e
         return util.Result(success=hiss.hiss(err_msg), msg=err_msg)
Ejemplo n.º 6
0
    def __init__(self, urdf_num, bb, image_data, n_samples, beta, gps, nn=None):
        """
        Initialize one of these for each BusyBox.
        """
        self.sample_policies = []
        self.nn_samples = []
        self.nn = nn
        self.mech = bb._mechanisms[0]
        self.beta = beta
        self.gps = gps
        self.n_samples = n_samples
        self.saved_im = None

        # Generate random policies.
        for _ in range(n_samples):
            random_policy = generate_policy(self.mech)
            policy_type = random_policy.type
            policy_tuple = random_policy.get_policy_tuple()

            results = [util.Result(policy_tuple, None, 0.0, None, None, None, \
                                    image_data, None)]
            self.sample_policies.append(results)

            if self.nn is not None:
                nn_preds, self.dataset = get_nn_preds(results, nn, ret_dataset=True, use_cuda=False)
                self.nn_samples.append(nn_preds)
            else:
                self.dataset = None
                self.nn_samples.append(None)
        # print('Max:', np.max(self.nn_samples))

        self.log = []
Ejemplo n.º 7
0
def setup_prometheus():

    hiss.echo('Generate prometheus config')
    config_path = generate_prom_config()
    if not cp_config_efs(config_path):
        return util.Result(success=False,
                           msg='Cannot copy prom config to efs pod')

    hiss.echo('Deploy prometheus')
    # Get domain
    prom_namespace = get_namespace()

    # Create temp folder & namespace
    settings.k8s.prereqs(prom_namespace)

    dict_env = {
        'DOMAIN': prom_namespace,
        'EFS_SERVER': settings.EFS_SERVER,
        'EFS_PATH': settings.EFS_PATH,
        'EFS_EXTEND': settings.EFS_EXTEND,
        'PVS_PATH': settings.PVS_PATH
    }

    # Deploy prometheus sts
    prom_template = '%s/prometheus/prometheus-stateful.yaml' % util.get_k8s_template_path(
    )
    settings.k8s.apply_yaml_from_template(namespace=prom_namespace,
                                          k8s_template_file=prom_template,
                                          dict_env=dict_env)
    prom_svc_template = '%s/prometheus/prometheus-service-stateful.yaml' % util.get_k8s_template_path(
    )
    settings.k8s.apply_yaml_from_template(namespace=prom_namespace,
                                          k8s_template_file=prom_svc_template,
                                          dict_env=dict_env)
Ejemplo n.º 8
0
 def read_pod_log(self, name, namespace):
     pods = self.find_pod(namespace, name)
     if not pods:
         msg = 'Pod %s not found!' % name
         return util.Result(success=False, msg=msg)
     
     for pod in pods:
         try:
             api_response = self.coreApi.read_namespaced_pod_log(pod, namespace)
             print(api_response)
         except ApiException as e:
             print("Exception when calling CoreV1Api->read_namespaced_pod_log: %s\n" % e)
def generate_dataset(args, git_hash):
    bb_dataset = get_bb_dataset(args.bb_fname, args.n_bbs, args.mech_types, args.max_mech, args.urdf_num)
    if args.n_samples == 0:
        return bb_dataset

    results = []
    for (i, bb_results) in enumerate(bb_dataset):
        bb = BusyBox.bb_from_result(bb_results[0])
        image_data, gripper = setup_env(bb, args.viz, args.debug)
        bb_results = []
        for j in range(args.n_samples):
            sys.stdout.write("\rProcessing sample %i/%i for busybox %i/%i" % (j+1, args.n_samples, i+1, args.n_bbs))
            for mech in bb._mechanisms:
                # generate either a random or model-based policy and goal configuration
                policy = policies.generate_policy(mech)
                pose_handle_world_init = mech.get_handle_pose()

                # calculate trajectory
                pose_handle_base_world = mech.get_pose_handle_base_world()
                traj = policy.generate_trajectory(pose_handle_base_world, args.debug, color=[0, 0, 1])

                # execute trajectory
                cumu_motion, net_motion, pose_handle_world_final = \
                        gripper.execute_trajectory(traj, mech, policy.type, args.debug)
                # save result data
                policy_params = policy.get_policy_tuple()
                mechanism_params = mech.get_mechanism_tuple()
                result = util.Result(policy_params, mechanism_params, net_motion, \
                            cumu_motion, pose_handle_world_init, pose_handle_world_final, \
                            image_data, git_hash)
                bb_results.append(result)

                gripper.reset(mech)
        results.append(bb_results)
        p.disconnect()
    print()
    return results
def get_bb_dataset(bb_fname, n_bbs, mech_types, max_mech, urdf_num):
    # Create a dataset of busyboxes.
    if bb_fname == '' or bb_fname is None:
        print('Creating Busyboxes.')
        mech_classes = []
        for mech_type in mech_types:
            if mech_type == 'slider': mech_classes.append(Slider)
            if mech_type == 'door': mech_classes.append(Door)

        bb_dataset = []
        for _ in range(n_bbs):
            # TODO: i think there is a bug here...
            bb = BusyBox.generate_random_busybox(max_mech=max_mech,
                                                    mech_types=mech_classes,
                                                    urdf_tag=urdf_num)
            mechanism_params = bb._mechanisms[0].get_mechanism_tuple()
            image_data, gripper = setup_env(bb, False, False)
            bb_dataset.append([util.Result(None, mechanism_params, None, None, None,
                                None, image_data, None)])
        print('BusyBoxes created.')
    else:
        # Load in a file with predetermined BusyBoxes.
        bb_dataset = read_from_file(bb_fname)[:n_bbs]
    return bb_dataset
Ejemplo n.º 11
0
def create_single_bb_gpucb_dataset(bb_result, nn_fname, plot, args, bb_i,
                                   n_interactions=None, plot_dir_prefix='',
                                   ret_regret=False, success_regret=None):
    use_cuda = False
    dataset = []
    viz = False
    debug = False
    # interact with BB
    bb = BusyBox.bb_from_result(bb_result, urdf_num=args.urdf_num)
    mech = bb._mechanisms[0]
    image_data, gripper = setup_env(bb, viz, debug)

    pose_handle_base_world = mech.get_pose_handle_base_world()
    sampler = UCB_Interaction(bb, image_data, plot, args, nn_fname=nn_fname)
    for ix in itertools.count():
        gripper.reset(mech)
        if args.debug:
            sys.stdout.write('\rProcessing sample %i' % ix)
        # if we are done sampling n_interactions OR we need to get regret after
        # each interaction
        if ((not n_interactions is None) and ix==n_interactions) or \
            (not success_regret is None):

            regret, start_x, stop_x, policy_type = test_model(sampler, args, gripper=gripper)
            gripper.reset(mech)

            #print('Current regret', regret)
            opt_points = (policy_type, [(start_x, 'g'), (stop_x, 'r')])
            sample_points = {'Prismatic': [(sample, 'k') for sample in sampler.xs['Prismatic']],
                            'Revolute': [(sample, 'k') for sample in sampler.xs['Revolute']]}

            # if done sampling n_interactions
            if (not n_interactions is None) and ix==n_interactions:
                if plot:
                    viz_circles(util.GP_PLOT,
                                image_data,
                                mech,
                                BETA,
                                sample_points=sample_points,
                                opt_points=opt_points,
                                gps=sampler.gps,
                                nn=sampler.nn,
                                bb_i=bb_i,
                                plot_dir_prefix=plot_dir_prefix)
                    plt.show()
                    input('Enter to close')
                    plt.close()
                if ret_regret:
                    return dataset, sampler.gps, regret
                else:
                    return dataset, sampler.gps
            # if got successful interaction or timeout
            elif (not success_regret is None) and \
                            ((regret < success_regret) or (ix >= 100)):
                if ix >= 100:
                    print('timeout interactions')
                elif regret < success_regret:
                    print('succcessful interaction!')
                if plot:
                    viz_circles(util.GP_PLOT,
                                image_data,
                                mech,
                                BETA,
                                sample_points=sample_points,
                                opt_points=opt_points,
                                gps=sampler.gps,
                                nn=sampler.nn,
                                bb_i=bb_i,
                                plot_dir_prefix=plot_dir_prefix)
                return dataset, sampler.gps, ix

        # sample a policy
        # image_data, gripper = setup_env(bb, False, debug)

        gripper.reset(mech)
        x, policy = sampler.sample()

        # execute
        traj = policy.generate_trajectory(pose_handle_base_world, debug=debug)
        c_motion, motion, handle_pose_final = gripper.execute_trajectory(traj, mech, policy.type, False)
        result = util.Result(policy.get_policy_tuple(), mech.get_mechanism_tuple(),
                             motion, c_motion, handle_pose_final, handle_pose_final,
                             image_data, None)
        dataset.append(result)

        # update GP
        sampler.update(result, x)