Пример #1
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)
Пример #2
0
def cmd_deploy(args, config):
    label = args.label[0]
    commit = args.commit[0]
    
    gondor_dirname = ".gondor"
    try:
        project_root = utils.find_nearest(os.getcwd(), gondor_dirname)
    except OSError:
        error("unable to find a .gondor directory.\n")
    
    tarball = None
    
    try:
        out("Reading configuration... ")
        local_config = ConfigParser.RawConfigParser()
        local_config.read(os.path.join(project_root, gondor_dirname, "config"))
        endpoint = config_value(local_config, "gondor", "endpoint", DEFAULT_ENDPOINT)
        site_key = local_config.get("gondor", "site_key")
        vcs = local_config.get("gondor", "vcs")
        app_config = {
            "requirements_file": config_value(local_config, "app", "requirements_file"),
            "wsgi_entry_point": config_value(local_config, "app", "wsgi_entry_point"),
            "migrations": config_value(local_config, "app", "migrations"),
        }
        out("[ok]\n")
        
        if vcs == "git":
            try:
                repo_root = utils.find_nearest(os.getcwd(), ".git")
            except OSError:
                error("unable to find a .git directory.\n")
            sha = utils.check_output("git rev-parse %s" % commit).strip()
            if commit == "HEAD":
                commit = sha
            tarball = os.path.abspath(os.path.join(repo_root, "%s-%s.tar" % (label, sha)))
            git_archive_all.archive_git(tarball,'tar',repo_root)
            cmd = "(cd %s && gzip %s)" % (repo_root, tarball)
            tarball += '.gz'
            
        elif vcs == "hg":
            try:
                repo_root = utils.find_nearest(os.getcwd(), ".hg")
            except OSError:
                error("unable to find a .hg directory.\n")
            branches_stdout = utils.check_output("hg branches")
            tags_stdout = utils.check_output("hg tags")
            refs = {}
            for line in branches_stdout.splitlines() + tags_stdout.splitlines():
                m = re.search(r"([\w\d\.-]+)\s*([\d]+):([\w]+)$", line)
                if m:
                    refs[m.group(1)] = m.group(3)
            try:
                sha = refs[commit]
            except KeyError:
                error("could not map '%s' to a SHA\n" % commit)
            tarball = os.path.abspath(os.path.join(repo_root, "%s-%s.tar.gz" % (label, sha)))
            cmd = "(cd %s && hg archive -p . -t tgz -r %s %s)" % (repo_root, commit, tarball)
        else:
            error("'%s' is not a valid version control system for Gondor\n" % vcs)
        
        out("Building tarball from %s... " % commit)
        subprocess.call([cmd], shell=True)
        out("[ok]\n")
        
        pb = ProgressBar(0, 100, 77)
        out("Pushing tarball to Gondor... \n")
        url = "%s/deploy/" % endpoint
        params = {
            "version": __version__,
            "site_key": site_key,
            "label": label,
            "sha": sha,
            "commit": commit,
            "tarball": open(tarball, "rb"),
            "project_root": os.path.relpath(project_root, repo_root),
            "app": json.dumps(app_config),
        }
        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)
        else:
            out("\n")
            data = json.loads(response.read())
    finally:
        if tarball:
            os.unlink(tarball)
    
    if data["status"] == "error":
        error("%s\n" % data["message"])
    if data["status"] == "success":
        deployment_id = data["deployment"]
        if "url" in data:
            instance_url = data["url"]
        else:
            instance_url = None
        
        # poll status of the deployment
        out("Deploying... ")
        while True:
            params = {
                "version": __version__,
                "site_key": site_key,
                "instance_label": label,
                "task_id": deployment_id,
            }
            url = "%s/task_status/" % endpoint
            try:
                response = make_api_call(config, url, urllib.urlencode(params))
            except urllib2.URLError:
                # @@@ add max retries
                continue
            data = json.loads(response.read())
            if data["status"] == "error":
                out("[error]\n")
                error("%s\n" % data["message"])
            if data["status"] == "success":
                if data["state"] == "deployed":
                    out("[ok]\n")
                    if instance_url:
                        out("\nVisit: %s\n" % instance_url)
                    break
                elif data["state"] == "failed":
                    out("[failed]\n")
                    out("\n%s\n" % data["reason"])
                    sys.exit(1)
                elif data["state"] == "locked":
                    out("[locked]\n")
                    out("\nYour deployment failed due to being locked. This means there is another deployment already in progress.\n")
                    sys.exit(1)
                else:
                    time.sleep(2)
Пример #3
0
def cmd_deploy(args, config):
    label = args.label[0]
    commit = args.commit[0]

    gondor_dirname = ".gondor"
    try:
        project_root = utils.find_nearest(os.getcwd(), gondor_dirname)
    except OSError:
        error("unable to find a .gondor directory.\n")

    tarball = None

    try:
        out("Reading configuration... ")
        local_config = ConfigParser.RawConfigParser()
        local_config.read(os.path.join(project_root, gondor_dirname, "config"))
        endpoint = config_value(local_config, "gondor", "endpoint",
                                DEFAULT_ENDPOINT)
        site_key = local_config.get("gondor", "site_key")
        vcs = local_config.get("gondor", "vcs")
        app_config = {
            "requirements_file":
            config_value(local_config, "app", "requirements_file"),
            "wsgi_entry_point":
            config_value(local_config, "app", "wsgi_entry_point"),
            "migrations":
            config_value(local_config, "app", "migrations"),
        }
        out("[ok]\n")

        if vcs == "git":
            try:
                repo_root = utils.find_nearest(os.getcwd(), ".git")
            except OSError:
                error("unable to find a .git directory.\n")
            sha = utils.check_output("git rev-parse %s" % commit).strip()
            if commit == "HEAD":
                commit = sha
            tarball = os.path.abspath(
                os.path.join(repo_root, "%s-%s.tar" % (label, sha)))
            git_archive_all.archive_git(tarball, 'tar', repo_root)
            cmd = "(cd %s && gzip %s)" % (repo_root, tarball)
            tarball += '.gz'

        elif vcs == "hg":
            try:
                repo_root = utils.find_nearest(os.getcwd(), ".hg")
            except OSError:
                error("unable to find a .hg directory.\n")
            branches_stdout = utils.check_output("hg branches")
            tags_stdout = utils.check_output("hg tags")
            refs = {}
            for line in branches_stdout.splitlines() + tags_stdout.splitlines(
            ):
                m = re.search(r"([\w\d\.-]+)\s*([\d]+):([\w]+)$", line)
                if m:
                    refs[m.group(1)] = m.group(3)
            try:
                sha = refs[commit]
            except KeyError:
                error("could not map '%s' to a SHA\n" % commit)
            tarball = os.path.abspath(
                os.path.join(repo_root, "%s-%s.tar.gz" % (label, sha)))
            cmd = "(cd %s && hg archive -p . -t tgz -r %s %s)" % (
                repo_root, commit, tarball)
        else:
            error("'%s' is not a valid version control system for Gondor\n" %
                  vcs)

        out("Building tarball from %s... " % commit)
        subprocess.call([cmd], shell=True)
        out("[ok]\n")

        pb = ProgressBar(0, 100, 77)
        out("Pushing tarball to Gondor... \n")
        url = "%s/deploy/" % endpoint
        params = {
            "version": __version__,
            "site_key": site_key,
            "label": label,
            "sha": sha,
            "commit": commit,
            "tarball": open(tarball, "rb"),
            "project_root": os.path.relpath(project_root, repo_root),
            "app": json.dumps(app_config),
        }
        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)
        else:
            out("\n")
            data = json.loads(response.read())
    finally:
        if tarball:
            os.unlink(tarball)

    if data["status"] == "error":
        error("%s\n" % data["message"])
    if data["status"] == "success":
        deployment_id = data["deployment"]
        if "url" in data:
            instance_url = data["url"]
        else:
            instance_url = None

        # poll status of the deployment
        out("Deploying... ")
        while True:
            params = {
                "version": __version__,
                "site_key": site_key,
                "instance_label": label,
                "task_id": deployment_id,
            }
            url = "%s/task_status/" % endpoint
            try:
                response = make_api_call(config, url, urllib.urlencode(params))
            except urllib2.URLError:
                # @@@ add max retries
                continue
            data = json.loads(response.read())
            if data["status"] == "error":
                out("[error]\n")
                error("%s\n" % data["message"])
            if data["status"] == "success":
                if data["state"] == "deployed":
                    out("[ok]\n")
                    if instance_url:
                        out("\nVisit: %s\n" % instance_url)
                    break
                elif data["state"] == "failed":
                    out("[failed]\n")
                    out("\n%s\n" % data["reason"])
                    sys.exit(1)
                elif data["state"] == "locked":
                    out("[locked]\n")
                    out("\nYour deployment failed due to being locked. This means there is another deployment already in progress.\n"
                        )
                    sys.exit(1)
                else:
                    time.sleep(2)