def delete_everything(my_settings: Settings): if my_settings.resources_created and om_manager.is_opsman_configured( my_settings): cmd = om_manager.get_om_with_auth(my_settings) + [ "delete-installation" ] # delete blocks on apply-changes, but if other apply changes in progress, doesn't queue up its own util.exponential_backoff_cmd(cmd) out, err, return_code = util.exponential_backoff_cmd(cmd) if return_code != 0: print( "OM cmd failed to delete installation {}".format(return_code)) return out, err, return_code delete_keypair(my_settings) buckets = [ my_settings.pcf_opsmanagers3bucket, my_settings.pcf_elasticruntimes3buildpacksbucket, my_settings.pcf_elasticruntimes3dropletsbucket, my_settings.pcf_elasticruntimes3packagesbucket, my_settings.pcf_elasticruntimes3resourcesbucket ] for bucket_name in buckets: try: expire_bucket(my_settings, bucket_name) except Exception as e: print(e) return "", "", 0
def download_assets(my_settings: settings.Settings, path: str): cmd = ["pivnet", "login", "--api-token", my_settings.pcf_input_pivnettoken] util.exponential_backoff_cmd(cmd) out, err, exit_code = do_github_download( my_settings.aws_broker_release_url, path) if exit_code != 0: return out, err, exit_code out, err, exit_code = do_pivnet_download( 'stemcells-ubuntu-xenial', my_settings.stemcell_release_version, '*aws*xenial*.tgz', my_settings.stemcell_release_sha256, path) if exit_code != 0: return out, err, exit_code # get stemcell for aws service broker # TODO: get from version config, when aws-service-broker tile is on pivnet out, err, exit_code = do_pivnet_download( 'stemcells', "3586.57", '*aws*trusty*.tgz', "4ebb2cb3f936806c547d5a432b3f8fc884cf34a1425aa9fde336d13422b0c94f", path) if exit_code != 0: return out, err, exit_code return do_pivnet_download('cf', my_settings.ert_release_version, 'cf*.pivotal', my_settings.ert_release_sha256, path)
def download_assets(my_settings: settings.Settings, path: str): cmd = ["pivnet", "login", "--api-token", my_settings.pcf_input_pivnettoken] util.exponential_backoff_cmd(cmd) out, err, exit_code = do_pivnet_download( 'stemcells', my_settings.stemcell_release_version, '*aws*.tgz', my_settings.stemcell_release_sha256, path) if exit_code != 0: return out, err, exit_code return do_pivnet_download('cf', my_settings.ert_release_version, 'cf*.pivotal', my_settings.ert_release_sha256, path)
def config_opsman_auth(my_settings: settings.Settings): cmd = [ "om", "-k", "--target", my_settings.opsman_url, "configure-authentication", "--username", my_settings.opsman_user, "--password", my_settings.pcf_opsmanageradminpassword, "--decryption-passphrase", my_settings.pcf_opsmanageradminpassword ] return util.exponential_backoff_cmd(cmd)
def create_required_databases(my_settings: Settings): cmd = "mysql -h {hostname} --user={username} --port={port} --password={password} < templates/required_dbs.sql".format( hostname=my_settings.pcf_rdsaddress, username=my_settings.pcf_rdsusername, port=my_settings.pcf_rdsport, password=my_settings.pcf_rdspassword) return util.exponential_backoff_cmd(cmd)
def do_github_download(url: str, path: str): cmd = ["wget", url, "-P", path] out, err, exit_code = util.exponential_backoff_cmd(cmd) if exit_code != 0: return out, err, exit_code paths = glob.glob("{}/{}".format(path, "aws-service-broker-*.pivotal")) if len(paths) != 1: return "Issue finding tiles on disk after download", "", 1 return "", "", 0
def upload_stemcell(my_settings: settings.Settings, path: str): for stemcell in os.listdir(path): if stemcell.endswith(".tgz"): print("uploading stemcell {0}".format(stemcell)) cmd = "{om_with_auth} upload-stemcell -s '{path}'".format( om_with_auth=om_manager.get_om_with_auth(my_settings), path=os.path.join(path, stemcell)) out, err, exit_code = util.exponential_backoff_cmd(cmd) if exit_code != 0: return out, err, exit_code return "", "", 0
def do_pivnet_download(slug: str, version: str, tile_glob: str, sha256: str, path: str): cmd = "pivnet download-product-files -p {slug} -r {version} -g '{glob}' -d '{dir}'".format( slug=slug, version=version, glob=tile_glob, dir=path) out, err, exit_code = util.exponential_backoff_cmd(cmd) if exit_code != 0: return out, err, exit_code paths = glob.glob("{}/{}".format(path, tile_glob)) if len(paths) != 1: return "Issue finding tiles on disk after download", "", 1 return "", "", verify_sha256(paths[0], sha256)
def configure_ert_multiaz_resources(my_settings: Settings): if my_settings.pcf_pcfdeploymentsize == "Multi-AZ": with open("templates/ert_multiaz_resources_config.j2.json", 'r') as f: template = f.read() else: with open("templates/ert_singleaz_resources_config.j2.json", 'r') as f: template = f.read() ert_resource_config = om_manager.format_om_json_str(template) cmd = "{om_with_auth} configure-product -n cf -pr '{ert_resources}'".format( om_with_auth=om_manager.get_om_with_auth(my_settings), ert_resources=ert_resource_config) return util.exponential_backoff_cmd(cmd)
def upload_assets(my_settings: settings.Settings, path: str): for tile in os.listdir(path): if tile.endswith(".pivotal"): print("uploading product {0}".format(tile)) cmd = om_manager.get_om_with_auth(my_settings) + [ "-r", "3600", "upload-product", "-p", os.path.join(path, tile) ] out, err, exit_code = util.exponential_backoff_cmd(cmd) if exit_code != 0: return out, err, exit_code return "", "", 0
def configure_tile_az(my_settings: Settings, tile_name: str): az_template_ctx = { "singleton_availability_zone": my_settings.zones[0], "zones": my_settings.zones } with open("templates/tile_az_config.j2.json", 'r') as f: az_template = Template(f.read()) az_config = om_manager.format_om_json_str( az_template.render(az_template_ctx)) cmd = om_manager.get_om_with_auth(my_settings) + [ "configure-product", "-n", tile_name, "-pn", az_config ] return util.exponential_backoff_cmd(cmd)
def create_required_databases(my_settings: Settings): cmd = [ "mysql", "-h", my_settings.pcf_rdsaddress, "--user={}".format(my_settings.pcf_rdsusername), "--port={}".format(my_settings.pcf_rdsport), "--password={}".format(my_settings.pcf_rdspassword) ] out = "" err = "" retcode = 1 with open("templates/required_dbs.sql", "r") as f: out, err, retcode = util.exponential_backoff_cmd(cmd, f) return out, err, retcode
def configure_ert_resources(my_settings: Settings): ert_resource_ctx = { "pcf_elb_tcp_name": my_settings.pcf_pcfelbtcpname, "pcf_elb_ssh_name": my_settings.pcf_pcfelbsshname, "pcf_elb_web_name": my_settings.pcf_pcfelbwebname, } with open("templates/ert_resources_config.j2.json", 'r') as f: ert_resource_template = Template(f.read()) ert_resource_config = om_manager.format_om_json_str( ert_resource_template.render(ert_resource_ctx)) cmd = "{om_with_auth} configure-product -n cf -pr '{ert_resources}'".format( om_with_auth=om_manager.get_om_with_auth(my_settings), ert_resources=ert_resource_config) return util.exponential_backoff_cmd(cmd)
def do_pivnet_download(slug: str, version: str, tile_glob: str, sha256: str, path: str): cmd = [ "pivnet", "download-product-files", "-p", slug, "-r", version, "-g", tile_glob, "-d", path ] out, err, exit_code = util.exponential_backoff_cmd(cmd) if exit_code != 0: return out, err, exit_code paths = glob.glob("{}/{}".format(path, tile_glob)) if len(paths) != 1: return "Issue finding tiles on disk after download", "", 1 return "", "", verify_sha256(paths[0], sha256)
def configure_ert_multiaz_resources(my_settings: Settings): if my_settings.pcf_pcfdeploymentsize == "SmallFootPrint": with open("templates/ert_smallfootprint_resources_config.j2.json", 'r') as f: template = f.read() elif my_settings.pcf_pcfdeploymentsize == "Multi-AZ": with open("templates/ert_multiaz_resources_config.j2.json", 'r') as f: template = f.read() else: with open("templates/ert_singleaz_resources_config.j2.json", 'r') as f: template = f.read() ert_resource_config = om_manager.format_om_json_str(template) cmd = om_manager.get_om_with_auth(my_settings) + [ "configure-product", "-n", "cf", "-pr", ert_resource_config ] return util.exponential_backoff_cmd(cmd)
def configure_aws_service_broker_config(my_settings: Settings): cert, key = generate_ssl_cert(my_settings) aws_config_template_ctx = { "aws_region": my_settings.region, "aws_service_bucket": my_settings.pcf_elasticruntimes3buildpacksbucket, "aws_s3_region": my_settings.region, "aws_iam_access_key_id": my_settings.broker_iamuseraccesskey, "aws_iam_secret_access_key": my_settings.broker_iamusersecretaccesskey, "pcf_skipsslvalidation": my_settings.pcf_input_skipsslvalidation, "cert": cert.replace("\n", "\\n"), "key": key.replace("\n", "\\n") } with open("templates/aws_service_broker_config.j2.json", 'r') as f: aws_broker_template = Template(f.read()) aws_config = om_manager.format_om_json_str( aws_broker_template.render(aws_config_template_ctx)) cmd = om_manager.get_om_with_auth(my_settings) + [ "configure-product", "-n", "aws-service-broker", "-p", aws_config ] return util.exponential_backoff_cmd(cmd)
def stage_product(product_name: str, my_settings: settings.Settings): cmd = "{om_with_auth} curl --path /api/v0/available_products".format( om_with_auth=get_om_with_auth(my_settings)) p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True) out, err = p.communicate() if p.returncode != 0: print("Failed to query api") return out, err, p.returncode products = json.loads(out.decode()) cf_version = [ x['product_version'] for x in products if x['name'] == product_name ][0] # ok to call multiple times, no-op when already staged cmd = "{om_with_auth} stage-product -p {product_name} -v {version}".format( om_with_auth=get_om_with_auth(my_settings), product_name=product_name, version=cf_version) return util.exponential_backoff_cmd(cmd)
def configure_ert_resources(my_settings: Settings): ert_resource_ctx = { "pcf_elb_tcp_name": my_settings.pcf_pcfelbtcpname, "pcf_elb_ssh_name": my_settings.pcf_pcfelbsshname, "pcf_elb_web_name": my_settings.pcf_pcfelbwebname, } if my_settings.pcf_pcfdeploymentsize == "SmallFootPrint": with open("templates/ert_resources_smallfootprint_config.j2.json", 'r') as f: ert_resource_template = Template(f.read()) else: with open("templates/ert_resources_config.j2.json", 'r') as f: ert_resource_template = Template(f.read()) ert_resource_config = om_manager.format_om_json_str( ert_resource_template.render(ert_resource_ctx)) cmd = om_manager.get_om_with_auth(my_settings) + [ "configure-product", "-n", "cf", "-pr", ert_resource_config ] return util.exponential_backoff_cmd(cmd)
def stage_product(product_name: str, my_settings: settings.Settings): # cmd = "{om_with_auth} curl --path /api/v0/available_products".format( # om_with_auth=get_om_with_auth(my_settings) # ) cmd = get_om_with_auth(my_settings) + [ "curl", "--path", "/api/v0/available_products" ] p = Popen(cmd, stdout=PIPE, stderr=PIPE) out, err = p.communicate() if p.returncode != 0: print("Failed to query api") return out, err, p.returncode products = json.loads(out.decode()) cf_version = [ x['product_version'] for x in products if x['name'] == product_name ][0] # ok to call multiple times, no-op when already staged cmd = get_om_with_auth(my_settings) + [ "stage-product", "-p", product_name, "-v", cf_version ] return util.exponential_backoff_cmd(cmd)
def configure_ert_config(my_settings: Settings): cert, key = generate_ssl_cert(my_settings) credhub_encryption_key = ''.join( random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(32)) ert_config_template_ctx = { "pcf_rds_address": my_settings.pcf_rdsaddress, "pcf_rds_username": my_settings.pcf_rdsusername, "dns_suffix": my_settings.pcf_input_domain, "pcf_rds_password": my_settings.pcf_rdspassword, "admin_email": my_settings.pcf_input_adminemail, "pcf_elastic_runtime_s3_buildpacks_bucket": my_settings.pcf_elasticruntimes3buildpacksbucket, "pcf_elastic_runtime_s3_droplets_bucket": my_settings.pcf_elasticruntimes3dropletsbucket, "pcf_elastic_runtime_s3_packages_bucket": my_settings.pcf_elasticruntimes3packagesbucket, "pcf_elastic_runtime_s3_resources_bucket": my_settings.pcf_elasticruntimes3resourcesbucket, "pcf_iam_access_key_id": my_settings.pcf_iamuseraccesskey, "pcf_iam_secret_access_key": my_settings.pcf_iamusersecretaccesskey, "pcf_companyname": my_settings.pcf_pcfcompanyname, "s3_endpoint": my_settings.get_s3_endpoint(), "s3_region": my_settings.region, "pcf_skipsslvalidation": my_settings.pcf_input_skipsslvalidation, "credhub_encryption_key": credhub_encryption_key, "cert": cert.replace("\n", "\\n"), "key": key.replace("\n", "\\n") } with open("templates/ert_config.j2.json", 'r') as f: ert_template = Template(f.read()) ert_config = om_manager.format_om_json_str( ert_template.render(ert_config_template_ctx)) cmd = om_manager.get_om_with_auth(my_settings) + [ "configure-product", "-n", "cf", "-p", ert_config ] return util.exponential_backoff_cmd(cmd)
def apply_changes(my_settings: settings.Settings): cmd = "{get_om_with_auth} apply-changes".format( get_om_with_auth=get_om_with_auth(my_settings)) return util.exponential_backoff_cmd(cmd)
def apply_changes(my_settings: settings.Settings): cmd = get_om_with_auth(my_settings) + ["apply-changes"] return util.exponential_backoff_cmd(cmd)
def config_opsman_auth(my_settings: settings.Settings): cmd = "om -k --target {url} configure-authentication --username '{user}' --password '{password}' --decryption-passphrase '{password}'".format( url=my_settings.opsman_url, user=my_settings.opsman_user, password=my_settings.pcf_opsmanageradminpassword) return util.exponential_backoff_cmd(cmd)