Skip to content

Commit

Permalink
Merge pull request #128 from GoogleCloudPlatform/root-template
Browse files Browse the repository at this point in the history
Simplify root template questions, bump version to 1.17.13
  • Loading branch information
jonchenn authored Sep 21, 2023
2 parents 0452cd9 + 902ece7 commit 831ef82
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "solutions-builder"
version = "1.17.12"
version = "1.17.13"
description = "A solution framework to generate a project with built-in structure and modules"
authors = ["Jon Chen <[email protected]>"]
license = "Apache"
Expand Down
71 changes: 49 additions & 22 deletions solutions_builder/copier_extensions/sb_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,30 @@ def read_yaml(filepath):
data = yaml.safe_load(f)
return data

def print_indent(text, offest=6):
text = " " * offest + text
print(text)


# Execute shell commands
def exec_output(command, working_dir=".", stop_when_error=True):
output = subprocess.check_output(command,
cwd=working_dir,
shell=True,
text=True)
return output
def exec_output(command, working_dir=".", stop_when_error=False):
try:
output = subprocess.check_output(command,
stderr=subprocess.STDOUT,
cwd=working_dir,
shell=True,
text=True)
except subprocess.CalledProcessError as e:
# print("Status : FAIL", e.returncode, e.output)
raise e

else:
return output


def exec_gcloud_output(command, working_dir="."):
output = ""
try:
output = exec_output(command)
except Exception as e:
print(f"Error: {e}")
output = ""

output = exec_output(command)
output = output.strip()
return output

Expand All @@ -51,34 +57,55 @@ def get_project_number(project_id):
"""
Get GCP project number based on project_id using gcloud command.
"""
print(f" (Retrieving project number for {project_id}...)")
print_indent(f"(Retrieving project number for {project_id}...)")
command = f"gcloud projects describe {project_id} --format='value(projectNumber)'"
project_number = exec_gcloud_output(command)
project_number = project_number.strip()
if not project_number.isnumeric():
try:
project_number = exec_gcloud_output(command)
project_number = project_number.strip()

if not project_number.isnumeric():
print_indent(f"project_number is not numeric: {project_number}")
return ""

except subprocess.CalledProcessError as e:
print_indent(f"{e.output}")
print_indent(f"Unable to retrieve project_number for '{project_id}'. GCP project '{project_id}' may not exist on GCP yet.\n")
return ""

return project_number
else:
return project_number


def get_existing_firestore(project_id):
"""
Get boolean whether to initialize Firestore.
"""
print(f" (Retrieving Firestore databases list...)")
print_indent(f"(Retrieving Firestore databases list...)")
command = f"gcloud alpha firestore databases list --format='value(databases[0].name)' --project='{project_id}' --quiet"
database_name = exec_gcloud_output(command)
return database_name

try:
database_name = exec_gcloud_output(command)
return database_name

except subprocess.CalledProcessError as e:
print_indent(f"Unable to retrieve default Firestore database name for '{project_id}'. GCP project '{project_id}' may not exist on GCP yet.\n")
return ""

def get_current_user(project_id):
"""
Get current authenticated gcloud user.
"""
print(f" (Retrieving current authenticated gcloud user...)")
command = f"gcloud config list account --format 'value(core.account)' | head -n 1"
email = exec_gcloud_output(command)
return email

try:
email = exec_gcloud_output(command)
return email

except subprocess.CalledProcessError as e:
print_indent(f"{e.output}")
print_indent(f"Unable to retrieve current authenticated gcloud user.")
return ""


def get_cloud_run_services(project_id):
Expand Down
8 changes: 1 addition & 7 deletions solutions_builder/template_root/copier.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ project_id:

project_number:
type: str
help: What is your Google Cloud project number?
help: What is your Google Cloud project number? (Leave it as empty if the project hasn't created yet.)
default: "{{project_id | get_project_number}}"

gcp_region:
Expand Down Expand Up @@ -91,12 +91,6 @@ secondary_service_ip_cidr_range:
default: 10.2.0.0/16
when: "{{advanced_settings and create_vpc_network}}"

existing_firestore_name:
type: str
help: Existing Firestore path? (e.g. projects/{{project_id}}/databases/(default))
default: "{{project_id | get_existing_firestore}}"
when: "{{advanced_settings and create_vpc_network}}"

_templates_suffix: ""

_exclude:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ vpc_network = "{{vpc_network}}"
vpc_subnetwork = "{{vpc_subnetwork}}"
ip_cidr_range = "{{ip_cidr_range}}"
master_ipv4_cidr_block = "{{master_ipv4_cidr_block}}"
existing_firestore_name = "{{existing_firestore_name}}"
existing_firestore_name = "{{project_id | get_existing_firestore}}"
secondary_ranges_pods = {
range_name = "secondary-pod-range-01"
ip_cidr_range = "{{secondary_pod_ip_cidr_range}}"
Expand Down

0 comments on commit 831ef82

Please sign in to comment.