Example #1
0
    def test_create_project(self):

        with mock.patch.object(
            Client,
            "do_post",
            mock.Mock(return_value=(1, 2))
        ) as do_post:

            c = Client()

            token = 'token'
            code, result = c.create_project(
                token,
                "user",
                "project",
                "source_control",
                "checkout_key_id"
            )
            # print do_post
            call = do_post.call_args
            self.assertEqual(len(call.call_list()), 1)
            path = c.wercker_url + '/api/' + PATH_CREATE_PROJECT.format(
                token=token
            )
            self.assertEqual(call[0].count(path), 1)
Example #2
0
def get_targets(valid_token, project_id):
    c = Client()

    puts("\nRetrieving list of deploy targets...")
    code, result = c.get_deploy_targets_by_project(valid_token, project_id)

    return result
Example #3
0
def check_services(services):

    term = get_term()
    c = Client()

    response, result = c.get_boxes()

    for service in services:

        if len(service.splitlines()) > 1:
            puts("""{t.yellow}Warning:{t.normal} Incorrect service \
specification detected.
Reason: A new line detected in declaration:
{service}""".format(t=term, service=service))

        else:
            check_service(service, result)
        # if
    if services is None or len(services) == 0:
        puts(
            """{t.yellow}Warning:{t.normal} No services specified in {file}"""
            .format(
                t=term,
                file=DEFAULT_WERCKER_YML
            )
        )
Example #4
0
def get_targets(valid_token, project_id):
    c = Client()

    puts("\nRetrieving list of deploy targets...")
    code, result = c.get_deploy_targets_by_project(
        valid_token,
        project_id
    )

    return result
Example #5
0
def _add_heroku_by_git(token, project_id, git_url):
    term = get_term()
    puts("Heroku remote %s selected." % git_url)

    puts("Looking for Heroku API key...", level=DEBUG)
    heroku_token = heroku.get_token()

    if not heroku_token:
        puts(term.red("Error: "))
        # with indent(2):
        puts("  Please make sure the heroku-toolbelt is installed")
        puts("  and you are loged in.")
        return

    puts("API key found...", level=DEBUG)
    puts("Retrieving applications from Heroku...", level=DEBUG)

    # fp = open("werckercli/tests/data/apps.response.json")
    # import json
    # apps = json.load(fp)
    # fp.close()
    apps = heroku.get_apps()

    preferred_app = None
    for app in apps:
        # print app
        if app['git_url'] == git_url:
            # print app['name']
            # print app
            preferred_app = app

    if not preferred_app:
        raise ValueError(
            "No matching heroku remote repository found in the \
apps for current heroku user"
        )

    c = Client()

    code, result = c.create_deploy_target(
        token,
        project_id,
        preferred_app['name'],
        heroku_token
    )

    if 'success' in result and result['success'] is True:
        puts("Heroku deploy target %s \
successfully added to the wercker application\n" % preferred_app['name'])

    elif result['errorMessage']:
        puts(term.red("Error: ") + result['errorMessage'])
        puts("Please check if the wercker addon was added to the heroku\
application or run heroku addons:add wercker")
Example #6
0
    def test_core_and_environ_settings(self):

        c = Client()

        self.assertEqual(c.wercker_url, self.wercker_url)
        self.assertTrue(c.api_version, '1.0')

        del os.environ['wercker_url']

        c = Client()

        self.assertTrue(c.wercker_url == self.wercker_url)
Example #7
0
def project_list_queue(valid_token=None):

    term = get_term()

    if not valid_token:
        raise ValueError("A valid token is required!")

    project_id = get_value(VALUE_PROJECT_ID, print_warnings=False)

    if not project_id:
        puts(
            term.red("Error: ") +
            "No application found. Please create or link an application first"
        )
        return

    term = get_term()

    puts("Retrieving list of unfinished builds.")

    result = get_builds(valid_token, project_id)

    unknowns = filter(lambda r: r['result'] == "unknown", result)

    print_builds(unknowns)
    # print unknowns

    result = get_targets(valid_token, project_id)

    for target in result['data']:
        # print target['id']
        c = Client()
        code, deploys = c.get_deploys(valid_token, target['id'])

        # print result
        # puts("Target: " + term.yellow(target['name']))

        if 'data' in deploys:

            unknowns = filter(
                lambda d: d['result'] == 'unknown',
                deploys['data']
            )

            puts(("\nFound {amount} scheduled deploys for {target}").format(
                amount=len(unknowns),
                target=term.white(target['name'])
            ))

            print_deploys(unknowns)

    else:
        puts("\nNo scheduled deploys found.")
Example #8
0
def project_list_queue(valid_token=None):

    term = get_term()

    if not valid_token:
        raise ValueError("A valid token is required!")

    project_id = get_value(VALUE_PROJECT_ID, print_warnings=False)

    if not project_id:
        puts(
            term.red("Error: ") +
            "No application found. Please create or link an application first"
        )
        return

    term = get_term()

    puts("Retrieving list of unfinished builds.")

    result = get_builds(valid_token, project_id)

    unknowns = filter(lambda r: r['result'] == "unknown", result)

    print_builds(unknowns)
    # print unknowns

    result = get_targets(valid_token, project_id)

    for target in result['data']:
        # print target['id']
        c = Client()
        code, deploys = c.get_deploys(valid_token, target['id'])

        # print result
        # puts("Target: " + term.yellow(target['name']))

        if 'data' in deploys:

            unknowns = filter(
                lambda d: d['result'] == 'unknown',
                deploys['data']
            )

            puts(("\nFound {amount} scheduled deploys for {target}").format(
                amount=len(unknowns),
                target=term.white(target['name'])
            ))

            print_deploys(unknowns)

    else:
        puts("\nNo scheduled deploys found.")
Example #9
0
def get_builds(valid_token, projectId):
    term = get_term()
    c = Client()

    puts("Retrieving builds from wercker...")
    status, result = c.get_builds(valid_token, projectId)

    if status != 200:
        puts(
            term.yellow("Warning: ") +
            "A problem occurred while retrieving builds")

    return result
Example #10
0
def project_link(valid_token=None, puts_result=True, auto_link=True):

    if not valid_token:
        raise ValueError("A valid token is required!")

    term = get_term()

    if puts_result:
        puts("Searching for git remote information... ")

    path = find_git_root(os.curdir)

    if not path:
        if puts_result:
            puts(term.red("error:") + " No git repository found")
        return False

    options = get_remote_options(path)

    if options is None:
        if puts_result:
            puts(term.red("error:") + " No git repository found")
        return False

    if puts_result:
        puts("Retrieving list of applications...")

    c = Client()

    response, result = c.get_applications(valid_token)

    for option in options:
        for app in result:

            if convert_to_url(app['url']) == convert_to_url(option.url):

                if auto_link:
                    set_value(VALUE_PROJECT_ID, app['id'])

                if puts_result:
                    puts(
                        term.green("success:") +
                        " application is now linked to this repository"
                    )
                return True

    if puts_result:
        puts(
            "An application could " + term.white("not") +
            " be linked to this repository")
    return False
Example #11
0
def project_link(valid_token=None, puts_result=True, auto_link=True):

    if not valid_token:
        raise ValueError("A valid token is required!")

    term = get_term()

    if puts_result:
        puts("Searching for git remote information... ")

    path = find_git_root(os.curdir)

    if not path:
        if puts_result:
            puts(term.red("error:") + " No git repository found")
        return False

    options = get_remote_options(path)

    if options is None:
        if puts_result:
            puts(term.red("error:") + " No git repository found")
        return False

    if puts_result:
        puts("Retrieving list of applications...")

    c = Client()

    response, result = c.get_applications(valid_token)

    for option in options:
        for app in result:

            if convert_to_url(app['url']) == convert_to_url(option.url):

                if auto_link:
                    set_value(VALUE_PROJECT_ID, app['id'])

                if puts_result:
                    puts(
                        term.green("success:") +
                        " application is now linked to this repository"
                    )
                return True

    if puts_result:
        puts(
            "An application could " + term.white("not") +
            " be linked to this repository")
    return False
Example #12
0
def get_builds(valid_token, projectId):
    term = get_term()
    c = Client()

    puts("Retrieving builds from wercker...")
    status, result = c.get_builds(valid_token, projectId)

    if status != 200:
        puts(
            term.yellow("Warning: ") +
            "A problem occurred while retrieving builds"
        )

    return result
Example #13
0
def _add_heroku_by_git(token, project_id, git_url):
    term = get_term()
    puts("Heroku remote %s selected." % git_url)

    puts("Looking for Heroku API key...", level=DEBUG)
    heroku_token = heroku.get_token()

    if not heroku_token:
        puts(term.red("Error: "))
        # with indent(2):
        puts("  Please make sure the heroku-toolbelt is installed")
        puts("  and you are loged in.")
        return

    puts("API key found...", level=DEBUG)
    puts("Retrieving applications from Heroku...", level=DEBUG)

    # fp = open("werckercli/tests/data/apps.response.json")
    # import json
    # apps = json.load(fp)
    # fp.close()
    apps = heroku.get_apps()

    preferred_app = None
    for app in apps:
        # print app
        if app['git_url'] == git_url:
            # print app['name']
            # print app
            preferred_app = app

    if not preferred_app:
        raise ValueError("No matching heroku remote repository found in the \
apps for current heroku user")

    c = Client()

    code, result = c.create_deploy_target(token, project_id,
                                          preferred_app['name'], heroku_token)

    if 'success' in result and result['success'] is True:
        puts("Heroku deploy target %s \
successfully added to the wercker application\n" % preferred_app['name'])

    elif result['errorMessage']:
        puts(term.red("Error: ") + result['errorMessage'])
        puts("Please check if the wercker addon was added to the heroku\
application or run heroku addons:add wercker")
Example #14
0
    def test_request_oauth_token(self):

        expected_response = {'all_good': 'true'}

        self.register_api_call(PATH_BASIC_ACCESS_TOKEN, [
            {
                'body': '{"all_good":"true"}',
                'status': 200
            },
        ])

        c = Client()

        code, result = c.request_oauth_token('jacco', 'flenter')

        self.assertEqual(expected_response, result)
Example #15
0
    def test_create_project(self):

        with mock.patch.object(Client, "do_post",
                               mock.Mock(return_value=(1, 2))) as do_post:

            c = Client()

            token = 'token'
            code, result = c.create_project(token, "user", "project",
                                            "source_control",
                                            "checkout_key_id")
            # print do_post
            call = do_post.call_args
            self.assertEqual(len(call.call_list()), 1)
            path = c.wercker_url + '/api/' + PATH_CREATE_PROJECT.format(
                token=token)
            self.assertEqual(call[0].count(path), 1)
Example #16
0
    def test_request_oauth_token(self):

        expected_response = {'all_good': 'true'}

        self.register_api_call(
            PATH_BASIC_ACCESS_TOKEN,
            [
                {
                    'body': '{"all_good":"true"}',
                    'status': 200
                },
            ]
        )

        c = Client()

        code, result = c.request_oauth_token('jacco', 'flenter')

        self.assertEqual(expected_response, result)
Example #17
0
    def test_do_post(self):

        asserted_return = {"status_code": "200"}
        asserted_return_string = json.dumps(asserted_return)

        self.register_api_call('test', [
            {
                'body': asserted_return_string,
                'status': 200
            },
        ])

        c = Client()

        code, result = c.do_post(c.wercker_url + '/api/1.0/test', {})

        self.assertEqual(code, 200)
        self.assertEqual(result, asserted_return)
        self.assertEqual(json.dumps(result), asserted_return_string)
Example #18
0
    def test_create_project(self):

        with mock.patch.object(
            Client,
            "do_post",
            mock.Mock(return_value=(1, 2))
        ) as do_post:

            c = Client()

            code, result = c.create_project(
                "git_url",
                # "user",
                # "project",
                "source_control",
                "token"
            )
            # print do_post
            call = do_post.call_args
            self.assertEqual(len(call.call_list()), 1)
            self.assertEqual(call[0].count(PATH_CREATE_PROJECT), 1)
Example #19
0
def project_build(valid_token=None):
    if not valid_token:
        raise ValueError("A valid token is required!")

    term = get_term()

    puts("Triggering build")

    c = Client()
    code, response = c.trigger_build(valid_token, get_value(VALUE_PROJECT_ID))

    if response['success'] is False:
        if "errorMessage" in response:
            puts(term.red("Error: ") + response['errorMessage'])
        else:
            puts("Unable to trigger a build on the default/master branch")

        return False
    else:
        puts("A new build has been created")
        return True
Example #20
0
def project_build(valid_token=None):
    if not valid_token:
        raise ValueError("A valid token is required!")

    term = get_term()

    puts("Triggering a new build.")

    c = Client()
    code, response = c.trigger_build(valid_token, get_value(VALUE_PROJECT_ID))

    if response['success'] is False:
        if "errorMessage" in response:
            puts(term.red("Error: ") + response['errorMessage'])
        else:
            puts("Unable to trigger a build on the default/master branch")

        return False
    else:
        puts("done.")
        return True
Example #21
0
def validate_box_name(name, validate_web=False, version=0):

    unversioned_pattern = "^[a-zA-Z0-9]+/[A-z0-9][A-z0-9-.]+[A-z0-9]$"

    match = re.match(unversioned_pattern, name)

    if validate_web is False:
        return match is not None
    else:
        if match:
            c = Client()
            response, result = c.get_box(name, version)

            if response == 404:
                return False
            else:
                if result:
                    return True
                else:
                    raise TransportError("Unexpected response from server")
        else:
            return False
Example #22
0
    def test_do_post(self):

        asserted_return = {"status_code": "200"}
        asserted_return_string = json.dumps(asserted_return)

        self.register_api_call(
            'test',
            [
                {
                    'body': asserted_return_string,
                    'status': 200
                },
            ]
        )

        c = Client()

        code, result = c.do_post(c.wercker_url + '/api/1.0/test', {})

        self.assertEqual(code, 200)
        self.assertEqual(result, asserted_return)
        self.assertEqual(json.dumps(result), asserted_return_string)
Example #23
0
def project_list(valid_token=None):

    if not valid_token:
        raise ValueError("A valid token is required!")

    c = Client()

    response, result = c.get_applications(valid_token)

    header = ['name', 'author', 'status', 'followers', 'url']
    props = [
        'name',
        'author',
        'status',
        'totalFollowers',
        'url'
    ]

    max_lengths = []

    for i in range(len(header)):
        max_lengths.append(0)

    store_highest_length(max_lengths, header)

    puts("Found %d result(s)...\n" % len(result))
    for row in result:
        store_highest_length(max_lengths, row, props)

    result = sorted(result, key=lambda k: k['name'])
    print_hr(max_lengths, first=True)
    print_line(max_lengths, header)
    print_hr(max_lengths)

    for row in result:
        print_line(max_lengths, row, props)
    print_hr(max_lengths)
Example #24
0
def project_list(valid_token=None):

    if not valid_token:
        raise ValueError("A valid token is required!")

    c = Client()

    response, result = c.get_applications(valid_token)

    header = ['name', 'author', 'status', 'followers', 'url']
    props = [
        'name',
        'author',
        'status',
        'totalFollowers',
        'url'
    ]

    max_lengths = []

    for i in range(len(header)):
        max_lengths.append(0)

    store_highest_length(max_lengths, header)

    puts("Found %d result(s)...\n" % len(result))
    for row in result:
        store_highest_length(max_lengths, row, props)

    result = sorted(result, key=lambda k: k['name'])
    print_hr(max_lengths, first=True)
    print_line(max_lengths, header)
    print_hr(max_lengths)

    for row in result:
        print_line(max_lengths, row, props)
    print_hr(max_lengths)
Example #25
0
def info_service(name, version=0):

    term = get_term()
    valid = False

    valid = validate_box_name(name)

    if valid is False:
        puts(
            "{t.red}Error: {t.normal} {name} is not a valid service name"
            .format(
                t=term,
                name=name
            )
        )
        return

    client = Client()

    puts("Retrieving service: {t.bold_white}{name}\n".format(
        # owner=owner,
        name=name,
        t=term)
    )
    response, results = client.get_box(name, version=version)

    if response == 404:
        puts(term.yellow("Warning: ") + "service not found.")
    elif results.get("type") != "service":
        puts(term.yellow("Warning: ") + "found box was not a service.")
    elif results:
        response, release_info = client.get_box_releases(name)
        if results:
            putInfo("Fullname", results.get("fullname"))
            putInfo("Owner", results.get("owner"))
            putInfo("Name", results.get("name"))
            puts("")
            putInfo(
                "All versions",
                '\n'.join(release_info.get("versionNumbers")),
                multiple_lines=True
            )
            putInfo("showing version", results.get("version"))
            puts("")

            license = results.get("license")
            if license is None:
                license = "none specified"
            putInfo("License", license)

            keywords = ", ".join(results.get("keywords"))
            if keywords is None:
                keywords = ""

            putInfo("Keywords", keywords)
            putInfo(
                "\nDescription",
                results.get("description"),
                multiple_lines=True
            )

            packages = ""

            for package in results.get("packages"):
                if len(packages):
                    packages += ", "

                packages += "{name}: {version}".format(
                    name=package.get("name"), version=package.get("version")
                )
            else:
                packages = "None specified"

            putInfo("\nPackages", packages, multiple_lines=True)
            putInfo("\nRead me", results.get("readMe"), multiple_lines=True)
    else:
        puts("{t.red}Error: {t.normal}An error occurred while retrieving \
service information")
Example #26
0
def check_service(service, result=None):
    term = get_term()

    if not result:
        c = Client()
        _response, result = c.get_boxes()

    unversioned_pattern = "(?P<owner>.*)/(?P<name>.*)"
    versioned_pattern = "(?P<owner>.*)/(?P<name>.*)@(?P<version>.*)"

    results = re.search(versioned_pattern, service)

    if not results:
        results = re.search(unversioned_pattern, service)

    info_dict = results.groupdict()

    if not result:
        puts("""{t.red}Error:{t.normal}""".format(t=term))
    else:
        fullname = "{owner}/{name}".format(
            owner=info_dict.get("owner"),
            name=info_dict.get("name")
        )

        boxes = filter(
            lambda box: box.get("fullname") == fullname,
            result
        )

        if len(boxes) == 0:
            puts("""{t.yellow}Warning:{t.normal} Service \
{fullname} not found.""".format(
                t=term)
            )

        else:
            box = boxes[0]

            versions = get_sorted_versions(box)
            latest_version = False
            specified_version = info_dict.get("version", None)

            if not specified_version:
                version_found = len(versions) > 0
                # latest_version = versions[len(versions)-1]
                requested_version = versions[len(versions)-1]
            else:

                version_found = False
                try:
                    requested_version = semantic_version.Version.coerce(
                        specified_version,
                    )
                except ValueError:
                    requested_version = None

                if requested_version:
                    spec = semantic_version.Spec(
                        '==' + str(specified_version)
                    )
                else:
                    try:
                        spec = semantic_version.Spec(
                            str(specified_version)
                        )
                    except ValueError:
                        puts(
                            """{t.red}Error: {t.normal}Invalid version \
specification detected: {version}.
Expected a SemVer version or specification (i.e. 0.1.2 or >0.1.1)
For more information on SemVer see: http://semver.org/"""
                            .format(
                                t=term,
                                version=requested_version
                            )
                        )
                        return

                # print locals()
                requested_version = spec.select(versions)

                if requested_version:
                    version_found = True

                    newer_spec = semantic_version.Spec(
                        ">" + str(requested_version)
                    )
                    latest_version = newer_spec.select(versions)

                    if not latest_version:
                        latest_version = False
                else:
                    version_found = False

            if version_found is False:
                info = "{t.red}not found{t.normal}".format(
                    t=term
                )
            elif latest_version is not False:
                info = "{t.yellow}upgrade to {sem_ver}{t.normal}".\
                    format(
                        t=term,
                        sem_ver=latest_version
                    )
            else:
                info = "{t.green}latest{t.normal}".format(t=term)
            puts(
                "{fullname} - {version} ({info}) - {description}".
                format(
                    fullname=fullname,
                    info=info,
                    version=requested_version,
                    description=box.get("latestDescription")
                )
            )
Example #27
0
def project_check_repo(
    valid_token=None,
    failure_confirmation=False,
    site_url=None
):

    if not valid_token:
        raise ValueError("A valid token is required!")

    term = get_term()

    puts("Checking werckerbot permissions on the repository...")

    while(True):

        c = Client()
        code, response = c.check_permissions(
            valid_token,
            get_value(VALUE_PROJECT_ID)
        )

        if response['success'] is True:
            if response['data']['hasAccess'] is True:
                puts("Werckerbot has access")
                break
            else:
                puts("")  # empty line...
                if "details" in response['data']:
                    # puts
                    puts(
                        term.yellow("Error: ") +
                        response['data']['details']
                    )
                else:
                    puts(
                        term.red("Error: ") +
                        "wercker's werckerbot has no access to this\
 repository."

                    )

                if failure_confirmation is True:

                    puts("werckerbot needs pull/read access to the repository \
to get the code.")
                    puts("Without access to the repository, builds and tests\
 will fail.\n")

                    if(site_url):
                        puts("Go to {url} and add wercker as a collaborator\
".format(url=site_url))
                    exit = not prompt.yn(
                        "Do you want wercker to check the permissions again?",
                        default="y"
                    )
                else:
                    exit = True

                if exit:
                    break
        else:
            puts(term.red("Error: ") + "Could not validate access...")
            if failure_confirmation is True:

                puts("werckerbot needs pull/read access to the repository \
to get the code.")
                puts("Without access to the repository, builds and tests\
will fail.\n")

                if(site_url):
                    puts("Go to {url} and add wercker as a collaborator\
".format(url=site_url))
                exit = not prompt.yn(
                    "Do you want wercker to check the permissions again?",
                    default="y"
                )
            else:
                break
Example #28
0
def build_deploy(valid_token=None):

    if not valid_token:
        raise ValueError("A valid token is required!")

    term = get_term()

    projectId = get_value(VALUE_PROJECT_ID, print_warnings=False)

    if not projectId:
        puts(
            term.red("Error: ") +
            "No application found. Please create or link an application first"
        )
        return

    builds = get_builds(valid_token, projectId)

    if type(builds) is not list or len(builds) == 0:
        puts(term.yellow("warning: ") + "No builds found.")
        return

    passed_builds = [build for build in builds if build['result'] == "passed"]
    if len(passed_builds) == 0:
        puts("No passed deploys found.")
    print_builds(passed_builds, print_index=True)

    deploy_index = -1
    target_index = -1

    while(True):
        result = get_value_with_default("Select which build to deploy", '1')

        valid_values = [str(i + 1) for i in range(len(passed_builds))]
        # valid_values = range(1, len(passed_builds) + 1)

        # print valid_values, result
        if result in valid_values:
            deploy_index = valid_values.index(result)
            break
        else:
            puts(term.red("warning: ") + " invalid build selected.")

    target_index = pick_target(valid_token, projectId)

    c = Client()

    code, result = c.do_deploy(
        valid_token,
        passed_builds[deploy_index]['id'],
        target_index
    )

    if "success" in result and result['success'] is True:
        puts(term.green("Success: ") + """
            Build scheduled for deploy.

You can monitor the scheduled deploy in your browser using:
{command_targets_deploy}
Or query the queue for this application using:
{command_queue}""".format(
            command_targets_deploy=term.white("wercker targets deploy"),
            command_queue=term.white("wercker queue")))
    else:
        puts(term.red("Error: ") + "Unable to schedule deploy")
Example #29
0
def search_services(name):

    client = Client()

    term = get_term()

    response, results = client.get_boxes()

    if results and len(results):
        services = filter(
            lambda box: box.get("latestType", "") == "service",
            results
        )
        services = sorted(
            services,
            key=lambda service: service.get("fullname")
        )

        if name:
            services = filter(
                lambda box: (
                    box.get("fullname", "").find(name) != -1 or
                    box.get("latestDescription").find(name) != -1
                ),
                services
            )

        if len(services) is not 0:
            if term.width:
                pad_length = term.width * 0.2
                pad_length = int(math.floor(pad_length))
                pad_length = max(pad_length, 30)
                pad_length = min(pad_length, 50)
            else:
                pad_length = 0

            for service in services:
                # versions = service.get("versionNumbers")
                versions = get_sorted_versions(service)

                if len(versions):
                    detailName = service["fullname"]
                    paddedName = detailName.ljust(pad_length)

                    if name:
                        paddedName = paddedName.replace(
                            name,
                            term.bold_white + name + term.normal
                        )

                    description = service["latestDescription"]
                    if description is None:
                        description = ""

                    version = versions[len(versions)-1]
                    version = str(version)
                    version = version.rjust(8)
                    if name:
                        description = description.replace(
                            name,
                            term.bold_white + name + term.normal
                        )

                    puts(
                        paddedName + " - " + version + " - " +
                        description
                    )

        else:
            if name:
                puts(
                    "No services found with: {t.bold_white}{0}{t.normal}".
                    format(
                        name,
                        t=term
                    )
                )
            else:
                puts("No services found.")
Example #30
0
def create(path='.', valid_token=None):
    if not valid_token:
        raise ValueError("A valid token is required!")

    term = get_term()

    if get_value(VALUE_PROJECT_ID, print_warnings=False):
        puts("A .wercker file was found.")
        run_create = prompt.yn(
            "Are you sure you want to run `wercker create`?",
            default="n")

        if run_create is False:
            puts("Aborting.")
            return
        else:
            puts("")

    if project_link(
        valid_token=valid_token,
        puts_result=False,
        auto_link=False
    ):
        puts("A matching application was found on wercker.")
        use_link = prompt.yn("Do you want to run 'wercker link' instead of\
 `wercker create`?")

        puts("")

        if use_link is True:
            project_link(valid_token=valid_token)
            return

    path = find_git_root(path)

    if path:
        options = get_remote_options(path)

        heroku_options = filter_heroku_sources(options)
    else:
        options = []
        heroku_options = []

    if not path:
        return False

    puts('''About to create an application on wercker.

This consists of the following steps:
1. Configure application
2. Setup keys
3. Add a deploy target ({heroku_options} heroku targets detected)
4. Trigger initial build'''.format(
        wercker_url=get_value(VALUE_WERCKER_URL),
        heroku_options=len(heroku_options))
    )

    if not path:
        puts(
            term.red("Error:") +
            " Could not find a repository." +
            " wercker create requires a git repository. Create/clone a\
 repository first."
        )
        return

    options = [o for o in options if o not in heroku_options]

    options = [o for o in options if o.priority > 1]

    count = len(options)
    puts('''
Step ''' + term.white('1') + '''. Configure application
-------------
''')
    puts(
        "%s repository location(s) found...\n"
        % term.bold(str(count))
    )

    url = pick_url(options)
    url = convert_to_url(url)

    source = get_preferred_source_type(url)
    puts("\n%s repository detected..." % source)
    puts("Selected repository url is %s\n" % url)

    client = Client()

    code, profile = client.get_profile(valid_token)

    source_type = get_source_type(url)

    if source_type == SOURCE_BITBUCKET:
        if profile.get('hasBitbucketToken', False) is False:
            puts("No Bitbucket account linked with your profile. Wercker uses\
 this connection to linkup some events for your repository on Bitbucket to our\
  service.")
            provider_url = get_value(
                VALUE_WERCKER_URL
            ) + '/provider/add/cli/bitbucket'

            puts("Launching {url} to start linking.".format(
                url=provider_url
            ))
            from time import sleep

            sleep(5)
            import webbrowser

            webbrowser.open(provider_url)

            raw_input("Press enter to continue...")
    elif source_type == SOURCE_GITHUB:
        if profile.get('hasGithubToken', False) is False:
            puts("No GitHub account linked with your profile. Wercker uses\
 this connection to linkup some events for your repository on GitHub to our\
 service.")
            provider_url = get_value(
                VALUE_WERCKER_URL
            ) + '/provider/add/cli/github'

            puts("Launching {url} to start linking.".format(
                url=provider_url
            ))

            from time import sleep

            sleep(5)

            import webbrowser

            webbrowser.open(provider_url)

            raw_input("Press enter to continue...")
    username = get_username(url)
    project = get_project(url)

    puts('''
Step {t.white}2{t.normal}.
-------------
In order to clone the repository on wercker, an ssh key is needed. A new/unique
key can be generated for each repository. There 3 ways of using ssh keys on
wercker:

{t.green}1. Automatically add a deploy key [recommended]{t.normal}
2. Use the checkout key, wercker uses for public projects.
3. Let wercker generate a key, but allow add it manually to github/bitbucket.
(needed when using git submodules)

For more information on this see: http://etc...
'''.format(t=term))
    key_method = None
    while(True):
        result = prompt.get_value_with_default(
            "Options:",
            '1'
        )

        valid_values = [str(i + 1) for i in range(3)]

        if result in valid_values:
            key_method = valid_values.index(result)
            break
        else:
            puts(term.red("warning: ") + " invalid build selected.")

    checkout_key_id = None
    checkout_key_publicKey = None

    if(key_method != 1):
        puts('''Retrieving a new ssh-key.''')
        status, response = client.create_checkout_key()
        puts("done.")

        if status == 200:
            checkout_key_id = response['id']
            checkout_key_publicKey = response['publicKey']

            if key_method == 0:
                puts('Adding deploy key to repository:')
                status, response = client.link_checkout_key(valid_token,
                                                            checkout_key_id,
                                                            username,
                                                            project,
                                                            source_type)
                if status != 200:
                    puts(term.red("Error:") +
                         " uanble to add key to repository.")
                    sys.exit(1)
            elif key_method == 2:
                profile_username = profile.get('username')
                status, response = client.get_profile_detailed(
                    valid_token,
                    profile_username)

                username = response[source_type + 'Username']
                url = None
                if source_type == SOURCE_GITHUB:
                    url = "https://github.com/settings/ssh"
                elif source_type == SOURCE_BITBUCKET:
                    url = "http://bitbucket.org/account/user/{username}/\
ssh-keys/"

                if status == 200:
                    formatted_key = "\n".join(
                        textwrap.wrap(checkout_key_publicKey))

                    puts('''Please add the following public key:
    {publicKey}

    You can add the key here: {url}\n'''.format(publicKey=formatted_key,
                                                url=url.format(
                                                    username=username)))
                    raw_input("Press enter to continue...")
                else:
                    puts(term.red("Error:") +
                         " unable to load wercker profile information.")
                    sys.exit(1)
        else:
            puts(term.red("Error:") + 'unable to retrieve an ssh key.')
            sys.exit(1)

    puts("Creating a new application")
    status, response = client.create_project(
        valid_token,
        username,
        project,
        source,
        checkout_key_id,
    )

    if response['success']:

        puts("done.\n")
        set_value(VALUE_PROJECT_ID, response['data']['id'])

        puts("In the root of this repository a .wercker file has been created\
 which enables the link between the source code and wercker.\n")

        site_url = None

        if source_type == SOURCE_GITHUB:

            site_url = "https://github.com/" + \
                username + \
                "/" + \
                project

        elif source_type == SOURCE_BITBUCKET:

            site_url = "https://bitbucket.org/" + \
                username + \
                "/" + \
                project

        puts('''
Step ''' + term.white('3') + '''.
-------------
''')

        target_options = heroku_options

        nr_targets = len(target_options)
        puts("%s automatic supported target(s) found." % str(nr_targets))

        if nr_targets:
            target_add(valid_token=valid_token)

        puts('''
Step ''' + term.white('4') + '''.
-------------
''')

        project_build(valid_token=valid_token)

        puts('''
Done.
-------------

You are all set up to for using wercker. You can trigger new builds by
committing and pushing your latest changes.

Happy coding!''')
    else:
        puts(
            term.red("Error: ") +
            "Unable to create project. \n\nResponse: %s\n" %
            (response.get('errorMessage'))
        )
        puts('''
Note: only repository where the wercker's user has permissions on can be added.
This is because some event hooks for wercker need to be registered on the
repository. If you want to test a public repository and don't have permissions
 on it: fork it. You can add the forked repository to wercker''')
Example #31
0
def create(path=".", valid_token=None):
    if not valid_token:
        raise ValueError("A valid token is required!")

    term = get_term()

    if get_value(VALUE_PROJECT_ID, print_warnings=False):
        puts("A .wercker file was found.")
        run_create = prompt.yn("Are you sure you want to run `wercker create`?", default="n")

        if run_create is False:
            puts("Aborting.")
            return
        else:
            puts("")

    if project_link(valid_token=valid_token, puts_result=False, auto_link=False):
        puts("A matching application was found on wercker.")
        use_link = prompt.yn(
            "Do you want to run 'wercker link' instead of\
 `wercker create`?"
        )

        puts("")

        if use_link is True:
            project_link(valid_token=valid_token)
            return

    path = find_git_root(path)

    if path:
        options = get_remote_options(path)

        heroku_options = filter_heroku_sources(options)
    else:
        options = []
        heroku_options = []

    if not path:
        return False

    puts(
        """About to create an application on wercker.

This consists of the following steps:
1. Configure application
2. Setup keys
3. Add a deploy target ({heroku_options} heroku targets detected)
4. Trigger initial build""".format(
            wercker_url=get_value(VALUE_WERCKER_URL), heroku_options=len(heroku_options)
        )
    )

    if not path:
        puts(
            term.red("Error:")
            + " Could not find a repository."
            + " wercker create requires a git repository. Create/clone a\
 repository first."
        )
        return

    options = [o for o in options if o not in heroku_options]

    options = [o for o in options if o.priority > 1]

    count = len(options)
    puts(
        """
Step """
        + term.white("1")
        + """. Configure application
-------------
"""
    )
    puts("%s repository location(s) found...\n" % term.bold(str(count)))

    url = pick_url(options)
    url = convert_to_url(url)

    source = get_preferred_source_type(url)
    puts("\n%s repository detected..." % source)
    puts("Selected repository url is %s\n" % url)

    client = Client()

    code, profile = client.get_profile(valid_token)

    source_type = get_source_type(url)

    if source_type == SOURCE_BITBUCKET:
        if profile.get("hasBitbucketToken", False) is False:
            puts(
                "No Bitbucket account linked with your profile. Wercker uses\
 this connection to linkup some events for your repository on Bitbucket to our\
  service."
            )
            provider_url = get_value(VALUE_WERCKER_URL) + "/provider/add/cli/bitbucket"

            puts("Launching {url} to start linking.".format(url=provider_url))
            from time import sleep

            sleep(5)
            import webbrowser

            webbrowser.open(provider_url)

            raw_input("Press enter to continue...")
    elif source_type == SOURCE_GITHUB:
        if profile.get("hasGithubToken", False) is False:
            puts(
                "No GitHub account linked with your profile. Wercker uses\
 this connection to linkup some events for your repository on GitHub to our\
 service."
            )
            provider_url = get_value(VALUE_WERCKER_URL) + "/provider/add/cli/github"

            puts("Launching {url} to start linking.".format(url=provider_url))

            from time import sleep

            sleep(5)

            import webbrowser

            webbrowser.open(provider_url)

            raw_input("Press enter to continue...")
    username = get_username(url)
    project = get_project(url)

    puts(
        """
Step {t.white}2{t.normal}.
-------------
In order to clone the repository on wercker, an ssh key is needed. A new/unique
key can be generated for each repository. There 3 ways of using ssh keys on
wercker:

{t.green}1. Automatically add a deploy key [recommended]{t.normal}
2. Use the checkout key, wercker uses for public projects.
3. Let wercker generate a key, but allow add it manually to github/bitbucket.
(needed when using git submodules)

For more information on this see: http://etc...
""".format(
            t=term
        )
    )
    key_method = None
    while True:
        result = prompt.get_value_with_default("Options:", "1")

        valid_values = [str(i + 1) for i in range(3)]

        if result in valid_values:
            key_method = valid_values.index(result)
            break
        else:
            puts(term.red("warning: ") + " invalid build selected.")

    checkout_key_id = None
    checkout_key_publicKey = None

    if key_method != 1:
        puts("""Retrieving a new ssh-key.""")
        status, response = client.create_checkout_key()
        puts("done.")

        if status == 200:
            checkout_key_id = response["id"]
            checkout_key_publicKey = response["publicKey"]

            if key_method == 0:
                puts("Adding deploy key to repository:")
                status, response = client.link_checkout_key(
                    valid_token, checkout_key_id, username, project, source_type
                )
                if status != 200:
                    puts(term.red("Error:") + " uanble to add key to repository.")
                    sys.exit(1)
            elif key_method == 2:
                profile_username = profile.get("username")
                status, response = client.get_profile_detailed(valid_token, profile_username)

                username = response[source_type + "Username"]
                url = None
                if source_type == SOURCE_GITHUB:
                    url = "https://github.com/settings/ssh"
                elif source_type == SOURCE_BITBUCKET:
                    url = "http://bitbucket.org/account/user/{username}/\
ssh-keys/"

                if status == 200:
                    formatted_key = "\n".join(textwrap.wrap(checkout_key_publicKey))

                    puts(
                        """Please add the following public key:
    {publicKey}

    You can add the key here: {url}\n""".format(
                            publicKey=formatted_key, url=url.format(username=username)
                        )
                    )
                    raw_input("Press enter to continue...")
                else:
                    puts(term.red("Error:") + " unable to load wercker profile information.")
                    sys.exit(1)
        else:
            puts(term.red("Error:") + "unable to retrieve an ssh key.")
            sys.exit(1)

    puts("Creating a new application")
    status, response = client.create_project(valid_token, username, project, source, checkout_key_id)

    if response["success"]:

        puts("done.\n")
        set_value(VALUE_PROJECT_ID, response["data"]["id"])

        puts(
            "In the root of this repository a .wercker file has been created\
 which enables the link between the source code and wercker.\n"
        )

        site_url = None

        if source_type == SOURCE_GITHUB:

            site_url = "https://github.com/" + username + "/" + project

        elif source_type == SOURCE_BITBUCKET:

            site_url = "https://bitbucket.org/" + username + "/" + project

        puts(
            """
Step """
            + term.white("3")
            + """.
-------------
"""
        )

        target_options = heroku_options

        nr_targets = len(target_options)
        puts("%s automatic supported target(s) found." % str(nr_targets))

        if nr_targets:
            target_add(valid_token=valid_token)

        puts(
            """
Step """
            + term.white("4")
            + """.
-------------
"""
        )

        project_build(valid_token=valid_token)

        puts(
            """
Done.
-------------

You are all set up to for using wercker. You can trigger new builds by
committing and pushing your latest changes.

Happy coding!"""
        )
    else:
        puts(term.red("Error: ") + "Unable to create project. \n\nResponse: %s\n" % (response.get("errorMessage")))
        puts(
            """
Note: only repository where the wercker's user has permissions on can be added.
This is because some event hooks for wercker need to be registered on the
repository. If you want to test a public repository and don't have permissions
 on it: fork it. You can add the forked repository to wercker"""
        )
Example #32
0
def build_deploy(valid_token=None):

    if not valid_token:
        raise ValueError("A valid token is required!")

    term = get_term()

    projectId = get_value(VALUE_PROJECT_ID, print_warnings=False)

    if not projectId:
        puts(
            term.red("Error: ") +
            "No application found. Please create or link an application first")
        return

    builds = get_builds(valid_token, projectId)

    if type(builds) is not list or len(builds) == 0:
        puts(term.yellow("warning: ") + "No builds found.")
        return

    passed_builds = [build for build in builds if build['result'] == "passed"]
    if len(passed_builds) == 0:
        puts("No passed deploys found.")
    print_builds(passed_builds, print_index=True)

    deploy_index = -1
    target_index = -1

    while (True):
        result = get_value_with_default("Select which build to deploy", '1')

        valid_values = [str(i + 1) for i in range(len(passed_builds))]
        # valid_values = range(1, len(passed_builds) + 1)

        # print valid_values, result
        if result in valid_values:
            deploy_index = valid_values.index(result)
            break
        else:
            puts(term.red("warning: ") + " invalid build selected.")

    target_index = pick_target(valid_token, projectId)

    c = Client()

    code, result = c.do_deploy(valid_token, passed_builds[deploy_index]['id'],
                               target_index)

    if "success" in result and result['success'] is True:
        puts(
            term.green("Success: ") + """
            Build scheduled for deploy.

You can monitor the scheduled deploy in your browser using:
{command_targets_deploy}
Or query the queue for this application using:
{command_queue}""".format(command_targets_deploy=term.white(
                "wercker targets deploy"),
                          command_queue=term.white("wercker queue")))
    else:
        puts(term.red("Error: ") + "Unable to schedule deploy")
Example #33
0
def create(path='.', valid_token=None):
    if not valid_token:
        raise ValueError("A valid token is required!")

    term = get_term()

    if get_value(VALUE_PROJECT_ID, print_warnings=False):
        puts("A .wercker file was found.")
        run_create = prompt.yn(
            "Are you sure you want to run `wercker create`?",
            default="n")

        if run_create is False:
            puts("Aborting.")
            return
        else:
            puts("")

    if project_link(
        valid_token=valid_token,
        puts_result=False,
        auto_link=False
    ):
        puts("A matching application was found on wercker.")
        use_link = prompt.yn("Do you want to run 'wercker link' instead of\
 `wercker create`?")

        puts("")

        if use_link is True:
            project_link(valid_token=valid_token)
            return

    path = find_git_root(path)

    if path:
        options = get_remote_options(path)

        heroku_options = filter_heroku_sources(options)
    else:
        options = []
        heroku_options = []

    if not path:
        return False

    puts('''About to create an application on wercker.

This consists of the following steps:
1. Validate permissions and create an application
2. Add a deploy target ({heroku_options} heroku targets detected)
3. Trigger initial build'''.format(
        wercker_url=get_value(VALUE_WERCKER_URL),
        heroku_options=len(heroku_options))
    )

    if not path:
        puts(
            term.red("Error:") +
            " Could not find a repository." +
            " wercker create requires a git repository. Create/clone a\
 repository first."
        )
        return

    options = [o for o in options if o not in heroku_options]

    options = [o for o in options if o.priority > 1]

    count = len(options)
    puts('''
Step ''' + term.white('1') + '''.
-------------
''')
    puts(
        "Found %s repository location(s)...\n"
        % term.white(str(count))
    )

    url = pick_url(options)
    url = convert_to_url(url)

    source = get_preferred_source_type(url)
    puts("\n%s repository detected..." % source)
    puts("Selected repository url is %s\n" % url)

    client = Client()

    code, profile = client.get_profile(valid_token)

    source_type = get_source_type(url)

    if source_type == SOURCE_BITBUCKET:
        if profile.get('hasBitbucketToken', False) is False:
            puts("No Bitbucket account linked with your profile. Wercker uses\
 this connection to linkup some events for your repository on Bitbucket to our\
  service.")
            provider_url = get_value(
                VALUE_WERCKER_URL
            ) + '/provider/add/cli/bitbucket'

            puts("Launching {url} to start linking.".format(
                url=provider_url
            ))
            from time import sleep

            sleep(5)
            import webbrowser

            webbrowser.open(provider_url)

            raw_input("Press enter to continue...")
    elif source_type == SOURCE_GITHUB:
        if profile.get('hasGithubToken', False) is False:
            puts("No GitHub account linked with your profile. Wercker uses\
 this connection to linkup some events for your repository on GitHub to our\
 service.")
            provider_url = get_value(
                VALUE_WERCKER_URL
            ) + '/provider/add/cli/github'

            puts("Launching {url} to start linking.".format(
                url=provider_url
            ))

            from time import sleep

            sleep(5)

            import webbrowser

            webbrowser.open(provider_url)

            raw_input("Press enter to continue...")

    puts("Creating a new application")
    status, response = client.create_project(
        url,
        source,
        valid_token
    )

    if response['success']:

        puts("a new application has been created.")

        set_value(VALUE_PROJECT_ID, response['projectId'])

        puts("In the root of this repository a .wercker file has been created\
 which enables the link between the source code and wercker.\n")

        site_url = None

        if source_type == SOURCE_GITHUB:

            site_url = "https://github.com/" + \
                get_username(url) + \
                "/" + \
                get_project(url)

        elif source_type == SOURCE_BITBUCKET:

            site_url = "https://bitbucket.org/" + \
                get_username(url) + \
                "/" + \
                get_project(url)

        project_check_repo(
            valid_token=valid_token,
            failure_confirmation=True,
            site_url=site_url
        )

#         puts("\nSearching for deploy target information (for \
# platforms such as Heroku).")

        puts('''
Step ''' + term.white('2') + '''.
-------------
''')

        target_options = heroku_options

        nr_targets = len(target_options)
        puts("%s automatic supported target(s) found." % str(nr_targets))

        if nr_targets:
            target_add(valid_token=valid_token)

        puts('''
Step ''' + term.white('3') + '''.
-------------
''')

        project_build(valid_token=valid_token)
        # if project_build(valid_token=valid_token):
            # puts("To trigger a build")
            # puts("")

        puts('''
Done.
-------------

You are all set up to for using wercker. You can trigger new builds by
committing and pushing your latest changes.

Happy coding!''')
    else:
        puts(
            term.red("Error: ") +
            "Unable to create project. \n\nResponse: %s\n" %
            (response.get('errorMessage'))
        )
        puts('''
Note: only repository where the wercker's user has permissions on can be added.
This is because some event hooks for wercker need to be registered on the
repository. If you want to test a public repository and don't have permissions
 on it: fork it. You can add the forked repository to wercker''')