Skip to content

Commit

Permalink
Readding only the files for checking in the fallback and writing the …
Browse files Browse the repository at this point in the history
…script to update buckets.
  • Loading branch information
gmechali committed Jan 23, 2025
1 parent 783332e commit 22ddf54
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 1 deletion.
60 changes: 60 additions & 0 deletions scripts/update_gcs_feature_flags.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Script to upload feature flag configurations to the website GCS bucket.
# Used for updating the feature flags used in different environments.
# Will upload the specified configuration file to the corresponding
# environment's bucket.
#
# To Use:
# (1) Make sure you're running from root with a clean HEAD
# (2) Make sure you've signed into authenticated to gcloud using
# `gcloud auth application-default login`
# (3) Run `./scripts/update_gcs_feature_flags.sh <environment>`
# Where <environment> is one of: dev, staging, production, autopush

# Define the valid environments
valid_environments=("dev" "staging" "production" "autopush")

# Check if an environment is provided
if [ -z "$1" ]; then
echo "Error: Please provide an environment as an argument."
echo "Usage: $0 <environment>"
exit 1
fi

# Get the environment from the command line argument
environment="$1"

# Check if the provided environment is valid
if [[ ! " ${valid_environments[@]} " =~ " ${environment} " ]]; then
echo "Error: Invalid environment '$environment'."
exit 1
fi

# Construct the filename (e.g., dev.json, staging.json)
file="${environment}.json"

# Construct the bucket name, handling the "prod" case for production
if [[ "$environment" == "production" ]]; then
bucket_name="datcom-website-prod-resources"
else
bucket_name="datcom-website-${environment}-resources"
fi

echo "Uploading ${file} to gs://${bucket_name}/feature_flags.json"
gsutil cp "server/config/feature_flag_configs/${file}" "gs://${bucket_name}/feature_flags.json"

echo "Upload complete!"
12 changes: 12 additions & 0 deletions server/config/feature_flag_configs/autopush.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[{
"name": "autocomplete",
"enabled": true
},
{
"name": "dev_place_experiment",
"enabled": true
},
{
"name": "dev_place_ga",
"enabled": true
}]
12 changes: 12 additions & 0 deletions server/config/feature_flag_configs/dev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[{
"name": "autocomplete",
"enabled": true
},
{
"name": "dev_place_experiment",
"enabled": true
},
{
"name": "dev_place_ga",
"enabled": true
}]
12 changes: 12 additions & 0 deletions server/config/feature_flag_configs/production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[{
"name": "autocomplete",
"enabled": true
},
{
"name": "dev_place_experiment",
"enabled": false
},
{
"name": "dev_place_ga",
"enabled": false
}]
12 changes: 12 additions & 0 deletions server/config/feature_flag_configs/staging.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[{
"name": "autocomplete",
"enabled": true
},
{
"name": "dev_place_experiment",
"enabled": false
},
{
"name": "dev_place_ga",
"enabled": false
}]
25 changes: 24 additions & 1 deletion server/lib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def get_feature_flag_bucket_name() -> str:
return 'datcom-website-' + env_for_bucket + '-resources'


def load_feature_flags():
def load_feature_flags_from_gcs():
"""Loads the feature flags into app config."""
storage_client = storage.Client()
bucket_name = get_feature_flag_bucket_name()
Expand All @@ -424,6 +424,29 @@ def load_feature_flags():
else:
logging.warning("Feature flag file not found in the bucket.")

return data

def load_fallback_feature_flags():
"""Loads the fallback feature flags into app config."""
env_to_use = os.environ.get('FLASK_ENV')
if env_to_use == 'local':
env_to_use = 'autopush'

filepath = os.path.join(get_repo_root(), "config", "feature_flag_configs",
env_to_use + ".json")
with open(filepath, 'r') as f:
data = json.load(f)
return data


def load_feature_flags():
"""Loads the feature flags into app config."""
data = load_feature_flags_from_gcs()

if not data:
print("I came in here and loaded.")
data = load_fallback_feature_flags()

# Create the dictionary using a dictionary comprehension
feature_flag_dict = {
flag["name"]: flag["enabled"]
Expand Down

0 comments on commit 22ddf54

Please sign in to comment.