def createNewVersion(crc_env="stage", property_env="STAGING"):
    # Get the number of the latest prod version to use as a base
    previous_version = util.getLatestVersionNumber(crc_env, property_env)

    # Save this number for later: create a file that contains the latest version number
    with open("previousversion.txt", "w") as f:
        f.write(str(previous_version))

    body = {"createFromVersion": previous_version}

    print("API - Creating new version based on v{}".format(previous_version))
    response_content = json.loads(
        util.akamaiPost(
            "/papi/v1/properties/{}/versions?contractId=ctr_3-1MMN3Z&groupId=grp_134508"
            .format(util.getPropertyIDForEnv(crc_env)), body))

    new_version = 0
    m = re.search('versions\/(.+?)\?contractId',
                  response_content["versionLink"])
    if m:
        new_version = m.group(1)
    print("Version {} created.".format(new_version))

    # Save this number for later: create a file that contains the new version number
    with open("newversion.txt", "w") as f:
        f.write(str(new_version))
    return new_version
def main():
    # Authenticate with EdgeGrid
    # TODO: Change this authentication to get rid of the httpie dependency. Apprently there's a vulnerability
    util.initEdgeGridAuth()

    if len(sys.argv) > 2:
        version_to_activate = sys.argv[2]
    else:
        sys.exit("Activation failed: no property version number specified")
    if len(sys.argv) > 3:
        environment = sys.argv[3]
    else:
        environment = "STAGING"

    previous_version = util.getLatestVersionNumber(environment)
    with open("previousversion.txt", "w") as f:
        f.write(str(previous_version))

    print(
        ">>>>>>>>>>>>>>>>>>>>>>>> Beginning activation in {}! <<<<<<<<<<<<<<<<<<<<<<<<"
        .format(environment))
    print("Activating v{}".format(version_to_activate))

    # Activate on given env
    util.activateVersion(version_to_activate, environment)
def waitForActiveVersion(version_number, env="STAGING"):
    print("Waiting for version {} to finish activating...".format(
        version_number))
    active_version = ""
    timeout = 180
    while active_version != version_number:
        time.sleep(10)
        try:
            active_version = util.getLatestVersionNumber(env)
        except:
            print("Failed to retrieve current version")
        timeout -= 1
        if (timeout == 0):
            sys.exit("Retried too many times! New version not activated.")
        print("Property active in {} is v{}".format(env, active_version))
    print("Success! Property v{} now active on {}.".format(
        active_version, env))
def main():
    # Authenticate with EdgeGrid
    util.initEdgeGridAuth()

    waitForActivation = False

    if len(sys.argv) > 1:
        version_to_activate = sys.argv[1]
    else:
        sys.exit("Activation failed: no property version number specified")
    
    if len(sys.argv) > 2:
        aka_env = sys.argv[2]
    else:
        aka_env = "STAGING"
    
    if len(sys.argv) > 3:
        crc_env = sys.argv[3]
    else:
        crc_env = "stage"
    
    if len(sys.argv) > 4:
        waitForActivation = (sys.argv[4].lower() == "true")
    
    previous_version = util.getLatestVersionNumber(crc_env, aka_env)
    with open("previousversion.txt", "w") as f:
        f.write(str(previous_version))

    print(">>>>>>>>>>>>>>>>>>>>>>>> Beginning activation for {} in {}! <<<<<<<<<<<<<<<<<<<<<<<<".format(crc_env, aka_env))
    print("Activating v{}".format(version_to_activate))

    # Activate on given env
    util.activateVersion(version_to_activate, aka_env, crc_env)

    # Wait, if necessary
    if waitForActivation:
        util.waitForActiveVersion(int(version_to_activate), aka_env, crc_env)