def find_alameda_namespace(app_name):
    namespace = ""
    output = OC().get_pods_all_namespace()
    for line in output.split("\n"):
        if line.find(app_name) != -1:
            namespace = line.split()[0]
            break
    if namespace:
        print "find %s's namespace: %s" % (app_name, namespace)
    else:
        raise Exception("ns: %s is not existed" % namespace)
    return namespace
def find_pod_name(app_name, app_namespace):
    pod_name_list = []
    status = ""
    output = OC().get_pods(app_namespace)
    for line in output.split("\n"):
        if line.find(app_name) != -1:
            pod_name = line.split()[0]
            if pod_name.find("build") != -1:
                continue
            status = line.split()[2]
            if status not in ["Running"]:
                raise Exception("%s is %s" % (pod_name, status))
            pod_name_list.append(pod_name)
    if not pod_name_list:
        raise Exception("%s is not existed in %s" % (app_name, app_namespace))
    return pod_name_list
def wait_pod_running(namespace):
    print "wait pods in %s running" % namespace
    start_time = time.time()
    while True:
        output = OC().get_pods(namespace)
        print "wait 30 sec"
        time.sleep(30)
        end_time = time.time()
        if end_time - start_time >= 600:
            raise Exception("timeout for waiting pods running")
            break
        count = 0
        for line in output.split("\n"):
            if line and line.find("NAME") == -1:
                status = line.split()[2]
                ready = line.split()[1]
                if ready != "1/1" and status != "Running":
                    print line
                elif (ready == "1/1" or ready == "2/2" or ready == "3/3") and status == "Running":
                    count += 1
        print "%s pods in %s are running" % (count, namespace) 
        if count >= 8 + initial_consumer:
            print "all pods in %s are running and ready" % namespace
            break
def find_app_location(app_name, namespace=""):
    app_namespace = ""
    app_type = ""
    resource = ""
    app_list = []
    output = OC().get_deployments_all_namespace()
    if output.find(app_name) != -1:
        for line in output.split("\n"):
            if line.find(app_name) != -1:
                app_namespace = line.split()[0]
                app_type = "deployment"
                resource = line.split()[1]
                app = {}
                app["namespace"] = app_namespace
                app["resource_type"] = app_type
                app["resource"] = resource
                app_list.append(app)
    if not app_list:
        output = OC().get_deploymentconfigs_all_namespace()
        if output.find(app_name) != -1:
            for line in output.split("\n"):
                if line.find(app_name) != -1:
                    app_namespace = line.split()[0]
                    app_type = "deploymentconfig"
                    resource = line.split()[1]
                    app = {}
                    app["namespace"] = app_namespace
                    app["resource_type"] = app_type
                    app["resource"] = resource
                    app_list.append(app)
    if not app_list:
        raise Exception("app: %s is not existed" % app_name)

    # do not choose
    if namespace:
        for app in app_list:
            if app["namespace"] == namespace and app["resource"] == app_name:
                break
        return app_namespace, app_type, resource

    app_namespace = app["namespace"]
    app_type = app["resource_type"]
    resource = app["resource"]
    if query_mode:
        # show app
        i = 0
        print "\n"
        print "*******************************************************************"
        print "   Applications:"
        for app in app_list:
            print "    %d) namespace: %s   %s: %s" % (i, app["namespace"], app["resource_type"], app["resource"])
            i = i + 1
        print "*******************************************************************\n"
        sys.stdin = open('/dev/tty')
        try:
            x = raw_input("input prefered application (default:0): ")
            if not x:
                x = 0
        except Exception:
            x = 0
        x = int(x)
        app_namespace = app_list[x]["namespace"]
        app_type = app_list[x]["resource_type"]
        resource = app_list[x]["resource"]

    print "preferred application is %s/%s" % (app_namespace, resource)
    os.environ["NAMESPACE"] = app_namespace
    os.environ["RESOURCE"] = resource
    os.environ["RESOURCE_TYPE"] = app_type
    return app_namespace, app_type, resource