Ejemplo n.º 1
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")

    tar_path, tarball_path = None, 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"),
            "staticfiles": config_value(local_config, "app", "staticfiles"),
            "site_media_url": config_value(local_config, "app", "site_media_url"),
        }
        include_files = [x.strip() for x in config_value(local_config, "files", "include", "").split("\n") if x]
        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")
            try:
                git = utils.find_command("git")
            except utils.BadCommand, e:
                error(e.args[0])
            check, sha = utils.run_proc([git, "rev-parse", commit])
            if check != 0:
                error("could not map '%s' to a SHA\n" % commit)
            if commit == "HEAD":
                commit = sha
            tar_path = os.path.abspath(os.path.join(repo_root, "%s-%s.tar" % (label, sha)))
            cmd = [git, "archive", "--format=tar", commit, "-o", tar_path]
        elif vcs == "hg":
            try:
                repo_root = utils.find_nearest(os.getcwd(), ".hg")
            except OSError:
                error("unable to find a .hg directory.\n")
            try:
                hg = utils.find_command("hg")
            except utils.BadCommand, e:
                error(e.args[0])
Ejemplo n.º 2
0
def cmd_deploy(args, env, config):
    label = args.label[0]
    commit = args.commit[0]
    
    tar_path, tarball_path = None, None
    
    try:
        if config["gondor.vcs"] == "git":
            try:
                git = utils.find_command("git")
            except utils.BadCommand, e:
                error(e.args[0])
            check, sha = utils.run_proc([git, "rev-parse", commit])
            if check != 0:
                error("could not map '%s' to a SHA\n" % commit)
            if commit == "HEAD":
                commit = sha
            tar_path = os.path.abspath(os.path.join(env["repo_root"], "%s-%s.tar" % (label, sha)))
            cmd = [git, "archive", "--format=tar", commit, "-o", tar_path]
        elif config["gondor.vcs"] == "hg":
            try:
                hg = utils.find_command("hg")
            except utils.BadCommand, e:
                error(e.args[0])
Ejemplo n.º 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")
    
    tar_path, tarball_path = None, 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"),
            "staticfiles": config_value(local_config, "app", "staticfiles"),
        }
        include_files = [
            x.strip()
            for x in config_value(local_config, "files", "include", "").split("\n")
            if x
        ]
        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")
            check, sha = utils.run_proc(["git", "rev-parse", commit])
            if check != 0:
                error("could not map '%s' to a SHA\n" % commit)
            if commit == "HEAD":
                commit = sha
            tar_path = os.path.abspath(os.path.join(repo_root, "%s-%s.tar" % (label, sha)))
            cmd = ["git", "archive", "--format=tar", commit, "-o", tar_path]
        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.run_proc(["hg", "branches"])[1]
            tags_stdout = utils.run_proc(["hg", "tags"])[1]
            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)
            tar_path = os.path.abspath(os.path.join(repo_root, "%s-%s.tar" % (label, sha)))
            cmd = ["hg", "archive", "-p", ".", "-t", "tar", "-r", commit, tar_path]
        else:
            error("'%s' is not a valid version control system for Gondor\n" % vcs)
        
        out("Archiving code from %s... " % commit)
        check, output = utils.run_proc(cmd, cwd=repo_root)
        if check != 0:
            error(output)
        out("[ok]\n")
        
        if include_files:
            out("Adding untracked files... ")
            try:
                tar_fp = tarfile.open(tar_path, "a")
                for f in include_files:
                    tar_fp.add(os.path.abspath(os.path.join(repo_root, f)), arcname=f)
            finally:
                tar_fp.close()
            out("[ok]\n")
        
        tarball_path = os.path.abspath(os.path.join(repo_root, "%s-%s.tar.gz" % (label, sha)))
        
        out("Building tarball... ")
        with open(tar_path, "rb") as tar_fp:
            try:
                tarball = gzip.open(tarball_path, mode="wb")
                tarball.writelines(tar_fp)
            finally:
                tarball.close()
        out("[ok]\n")
        
        pb = ProgressBar(0, 100, 77)
        out("Pushing tarball to Gondor... \n")
        url = "%s/deploy/" % endpoint
        
        with open(tarball_path, "rb") as tarball:
            params = {
                "version": __version__,
                "site_key": site_key,
                "label": label,
                "sha": sha,
                "commit": commit,
                "tarball": tarball,
                "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)
            except urllib2.HTTPError, e:
                out("\nReceived an error [%d: %s]" % (e.code, e.read()))
                sys.exit(1)
            else:
Ejemplo n.º 4
0
         git = utils.find_command("git")
     except utils.BadCommand, e:
         error(e.args[0])
     check, sha = utils.run_proc([git, "rev-parse", commit])
     if check != 0:
         error("could not map '%s' to a SHA\n" % commit)
     if commit == "HEAD":
         commit = sha
     tar_path = os.path.abspath(os.path.join(env["repo_root"], "%s-%s.tar" % (label, sha)))
     cmd = [git, "archive", "--format=tar", commit, "-o", tar_path]
 elif config["gondor.vcs"] == "hg":
     try:
         hg = utils.find_command("hg")
     except utils.BadCommand, e:
         error(e.args[0])
     branches_stdout = utils.run_proc([hg, "branches"])[1]
     tags_stdout = utils.run_proc([hg, "tags"])[1]
     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)
     tar_path = os.path.abspath(os.path.join(env["repo_root"], "%s-%s.tar" % (label, sha)))
     cmd = [hg, "archive", "-p", ".", "-t", "tar", "-r", commit, tar_path]
 else:
     error("'%s' is not a valid version control system for Gondor\n" % config["gondor.vcs"])