示例#1
0
def cmd_manage(args, env, config):
    
    instance_label = args.label[0]
    operation = args.operation[0]
    opargs = args.opargs
    
    if operation == "database:load" and not args.yes:
        out("This command will destroy all data in the database for %s\n" % instance_label)
        answer = raw_input("Are you sure you want to continue? [y/n]: ")
        if answer != "y":
            sys.exit(1)
    
    url = "%s/instance/manage/" % config["gondor.endpoint"]
    params = {
        "version": __version__,
        "site_key": config["gondor.site_key"],
        "instance_label": instance_label,
        "operation": operation,
    }
    handlers = [
        http.MultipartPostHandler,
    ]
    if operation in ["database:load"]:
        if opargs:
            filename = os.path.abspath(os.path.expanduser(opargs[0]))
            try:
                fp = open(filename, "rb")
            except IOError:
                error("unable to open %s\n" % filename)
            out("Compressing file... ")
            fd, tmp = tempfile.mkstemp()
            fpc = gzip.open(tmp, "wb")
            try: 
                while True:
                    chunk = fp.read(8192)
                    if not chunk:
                        break
                    fpc.write(chunk)
            finally:
                fpc.close()
            out("[ok]\n")
            params["stdin"] = open(tmp, "rb")
            pb = ProgressBar(0, 100, 77)
            out("Pushing file to Gondor... \n")
            handlers.extend([
                http.UploadProgressHandler(pb, ssl=True),
                http.UploadProgressHandler(pb, ssl=False)
            ])
        else:
            error("%s takes one argument.\n" % operation)
    params = params.items()
    for oparg in opargs:
        params.append(("arg", oparg))
    try:
        response = make_api_call(config, url, params, extra_handlers=handlers)
    except urllib2.HTTPError, e:
        api_error(e)
示例#2
0
def cmd_list(args, env, config):
    
    url = "%s/site/instances/" % config["gondor.endpoint"]
    params = {
        "version": __version__,
        "site_key": config["gondor.site_key"],
    }
    try:
        response = make_api_call(config, url, urllib.urlencode(params))
    except urllib2.HTTPError, e:
        api_error(e)
示例#3
0
def cmd_open(args, env, config):
    url = "%s/instance/detail/" % config["gondor.endpoint"]
    params = {
        "version": __version__,
        "site_key": config["gondor.site_key"],
        "label": args.label[0],
    }
    url += "?%s" % urllib.urlencode(params)
    try:
        response = make_api_call(config, url)
    except urllib2.HTTPError, e:
        api_error(e)
示例#4
0
def cmd_dashboard(args, env, config):
    params = {
        "version": __version__,
        "site_key": config["gondor.site_key"],
    }
    if args.label:
        url = "%s/instance/detail/" % config["gondor.endpoint"]
        params["label"] = args.label
    else:
        url = "%s/site/detail/" % config["gondor.endpoint"]
    url += "?%s" % urllib.urlencode(params)
    try:
        response = make_api_call(config, url)
    except urllib2.HTTPError, e:
        api_error(e)
示例#5
0
def cmd_sqldump(args, env, config):
    label = args.label[0]
    
    # request SQL dump and stream the response through uncompression
    
    err("Dumping database... ")
    url = "%s/instance/sqldump/" % config["gondor.endpoint"]
    params = {
        "version": __version__,
        "site_key": config["gondor.site_key"],
        "label": label,
    }
    try:
        response = make_api_call(config, url, urllib.urlencode(params))
    except urllib2.HTTPError, e:
        api_error(e)
示例#6
0
def cmd_env_set(args, env, config):
    url = "%s/site/env/" % config["gondor.endpoint"]
    bits = args.bits
    params = [
        ("version", __version__),
        ("site_key", config["gondor.site_key"]),
    ]
    # api will reject the else case
    if bits:
        if "=" in bits[0]: # set var(s) on site
            params.extend([("variable", v) for v in bits])
        else: # set var(s) on instance
            params.append(("label", bits[0]))
            params.extend([("variable", v) for v in bits[1:]])
    try:
        response = make_api_call(config, url, urllib.urlencode(params))
    except urllib2.HTTPError, e:
        api_error(e)
示例#7
0
def cmd_create(args, env, config):
    
    label = args.label[0]
    
    kind = args.kind
    if kind is None:
        kind = "dev"
    
    text = "Creating instance on Gondor... "
    url = "%s/instance/create/" % config["gondor.endpoint"]
    params = {
        "version": __version__,
        "site_key": config["gondor.site_key"],
        "label": label,
        "kind": kind,
        "project_root": os.path.basename(env["project_root"]),
    }
    try:
        response = make_api_call(config, url, urllib.urlencode(params))
    except urllib2.HTTPError, e:
        api_error(e)
示例#8
0
def cmd_delete(args, env, config):
    
    instance_label = args.label[0]
    
    text = "ARE YOU SURE YOU WANT TO DELETE THIS INSTANCE? [Y/N] "
    out(text)
    user_input = raw_input()
    if user_input != "Y":
        out("Exiting without deleting the instance.\n")
        sys.exit(0)
    text = "Deleting... "
    
    url = "%s/instance/delete/" % config["gondor.endpoint"]
    params = {
        "version": __version__,
        "site_key": config["gondor.site_key"],
        "instance_label": instance_label,
    }
    try:
        response = make_api_call(config, url, urllib.urlencode(params))
    except urllib2.HTTPError, e:
        api_error(e)
示例#9
0
def cmd_env(args, env, config):
    url = "%s/site/env/" % config["gondor.endpoint"]
    bits = args.bits
    params = [
        ("version", __version__),
        ("site_key", config["gondor.site_key"]),
    ]
    if args.scoped:
        params.append(("scoped", "1"))
    # default case is to get all vars on site
    if bits:
        # check if bits[0] is upper-case; if so we know it is not an instance
        # label
        if bits[0].isupper(): # get explicit var(s) on site
            params.extend([("key", k) for k in bits])
        else: # get var(s) on instance
            params.append(("label", bits[0]))
            params.extend([("key", k) for k in bits[1:]])
    url += "?%s" % urllib.urlencode(params)
    try:
        response = make_api_call(config, url)
    except urllib2.HTTPError, e:
        api_error(e)
示例#10
0
def cmd_run(args, env, config):
    
    instance_label = args.instance_label[0]
    command = args.command_
    
    if args.detached:
        err("Spawning... ")
    else:
        err("Attaching... ")
    url = "%s/instance/run/" % config["gondor.endpoint"]
    params = {
        "version": __version__,
        "site_key": config["gondor.site_key"],
        "instance_label": instance_label,
        "project_root": os.path.relpath(env["project_root"], env["repo_root"]),
        "detached": {True: "true", False: "false"}[args.detached],
        "command": " ".join(command),
        "app": json.dumps(config["app"]),
    }
    if sys.platform == "win32":
        params["term"] = "win32"
    if "TERM" in os.environ:
        try:
            params.update({
                "tc": utils.check_output(["tput", "cols"]).strip(),
                "tl": utils.check_output(["tput", "lines"]).strip(),
            })
        except (OSError, subprocess.CalledProcessError):
            # if the above fails then no big deal; we just can't set correct
            # terminal info so it will default some common values
            pass
    try:
        response = make_api_call(config, url, urllib.urlencode(params))
    except urllib2.HTTPError, e:
        err("[failed]\n")
        api_error(e)
示例#11
0
             "run_on_deploy": {True: "true", False: "false"}[not args.no_on_deploy],
             "app": json.dumps(config["app"]),
         }
         handlers = [
             http.MultipartPostHandler,
             http.UploadProgressHandler(pb, ssl=True),
             http.UploadProgressHandler(pb, ssl=False)
         ]
         try:
             response = make_api_call(config, url, params, extra_handlers=handlers)
         except KeyboardInterrupt:
             out("\nCanceling uploading... [ok]\n")
             sys.exit(1)
         except urllib2.HTTPError, e:
             out("\n")
             api_error(e)
         else:
             out("\n")
             data = json.loads(response.read())
 
 finally:
     if tar_path and os.path.exists(tar_path):
         os.unlink(tar_path)
     if tarball_path and os.path.exists(tarball_path):
         os.unlink(tarball_path)
 
 if data["status"] == "error":
     error("%s\n" % data["message"])
 if data["status"] == "success":
     deployment_id = data["deployment"]
     if "url" in data: