def enable_service_on_project(dns_project_id, service_url): LOGGER.info("Enabling {} on project {}".format(service_url, dns_project_id)) run_command([ 'gcloud', 'services', 'enable', service_url, '--project', dns_project_id ])
def log_ssl_certificate_machine(compute_zone, dns_project_id): LOGGER.info("Logging ssl certificate machine") cmd = [ 'gcloud', 'compute', 'ssh', '--zone', compute_zone, '--project', dns_project_id, 'ssl-certificate-gen' ] run_command(cmd)
def delete_addresses(dns_project_id, compute_region, name): LOGGER.info("Deleting Adresses") cmd = [ "gcloud", "compute", "addresses", "delete", name, "--region", compute_region, "--project", dns_project_id, "-q" ] run_command(cmd)
def create_external_address(dns_project_id, compute_region, name): with log_exception(): LOGGER.info("Creating external region %s, project %s", compute_region, dns_project_id) cmd = [ 'gcloud', 'compute', 'addresses', 'create', name, '--region', compute_region, '--project', dns_project_id ] run_command(cmd)
def delegate_zone(root_dns_project_id, root_dns_zone_name, delegate_dns_project_id, delegate_dns_zone, custom_domain): LOGGER.info("Delegating zone to %s/%s for domain %s", root_dns_project_id, root_dns_zone_name, custom_domain) records = get_servers_names(delegate_dns_zone, delegate_dns_project_id) ttl = get_dns_ttl(delegate_dns_project_id, delegate_dns_zone, custom_domain, "NS") create_new_dns_record(root_dns_project_id, root_dns_zone_name, "NS", custom_domain, ttl, records)
def create_dns_zone(custom_domain, dns_zone, dns_project_id): LOGGER.info("Creating DNS Zone %s, on domain %s project %s", dns_zone, custom_domain, dns_project_id) run_command([ "gcloud", "dns", "managed-zones", "create", '--dns-name="{}."'.format(custom_domain), '--description="{} A zone" "{}"'.format(custom_domain, dns_zone), "--project", dns_project_id ])
def retrieve_servers_names(dns_zone, dns_project_id): LOGGER.info("Retrieving names servers in %s, project %s", dns_zone, dns_project_id) returned_servers = run_command_readonly([ 'gcloud', 'dns', 'managed-zones', 'describe', dns_zone, '--project', dns_project_id ]) servers = returned_servers.decode("utf-8") LOGGER.warning("Keep these names servers for the next step\n") LOGGER.warning(servers)
def get_servers_names(dns_zone, dns_project_id): LOGGER.info("Retrieving names servers in %s, project %s", dns_zone, dns_project_id) returned_servers = run_command_readonly([ 'gcloud', 'dns', 'managed-zones', 'describe', dns_zone, '--project', dns_project_id, "--format='value(nameServers)'" ]) servers = returned_servers.decode("utf-8")\ .replace("\r", "").replace("\n", "").split(";") return servers
def retrieve_servers_names(dns_zone, dns_project_id): LOGGER.info("Retrieving names servers in {}, project {}".format( dns_zone, dns_project_id)) returned_servers = run_command([ 'gcloud', 'dns', 'managed-zones', 'describe', dns_zone, '--project', dns_project_id ]) if helpers.DRY_RUN: servers = "server1" else: servers = returned_servers.decode("utf-8") LOGGER.warning("Keep these names servers for the next step\n") LOGGER.warning(servers)
def execute_docker_file_remotely(filename, dns_project_id, compute_zone): with log_exception(): LOGGER.info("Sending docker_creation_file to docker") cmd = [ "gcloud", "compute", "scp", "--project", dns_project_id, "--zone", compute_zone, "docker_creation_file.sh", "ssl-certificate-gen:{}".format(filename) ] run_command(cmd) LOGGER.info("Executing file") cmd = [ "gcloud", "compute", "ssh", '"ssl-certificate-gen"', "--project", dns_project_id, "--zone", compute_zone, "--command", "'. docker_creation_file.sh'" ] run_command(cmd)
def create_record(dns_project_id, dns_zone_name, record_type, record_name, ttl, records): LOGGER.info("Adding %s records on transaction", record_type) command = [ 'gcloud', 'dns', 'record-sets', 'transaction', 'add ', '-z', dns_zone_name, '--name', record_name, '--type', record_type, '--ttl', ttl ] if isinstance(records, str): command.append("'" + records + "'") else: for record in records: command.append("'" + record + "'") command.append('--project') command.append(dns_project_id) run_command(command)
def drop_docker_file(filename): try: LOGGER.info("Removing docker file") os.remove(filename) except OSError: pass
records[record_name]['value'] = ssl_external_ip with dns_records(args.dns_project_id, args.dns_zone, records): with gce_machine(args.dns_project_id, compute_zone, ssl_external_ip): sub_domains = (args.prefix_sub_domains.split(",") if args.prefix_sub_domains else []) generate_certs_file(args.main_domain, args.email, compute_zone, args.dns_project_id, sub_domains) certs_path = download_certs_file(args.dns_project_id, compute_zone, args.main_domain) green_print(certs_path) if __name__ == '__main__': LOGGER.info("Starting process!") parser = create_parser() add_simulation_arguments(parser) args = parser.parse_args() helpers.DRY_RUN = args.dry_run helpers.DEBUG = args.debug execute_steps(args) LOGGER.info("Finished!")
def start_transaction(dns_project_id, dns_zone_name): LOGGER.info("Starting transaction to %s %s", dns_project_id, dns_zone_name) run_command([ 'gcloud', 'dns', 'record-sets', 'transaction', 'start', '-z', dns_zone_name, '--project', dns_project_id ])