Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repurpose PSE terraform config to initialize cloud resources #2

Merged
merged 6 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 0 additions & 40 deletions .github/workflows/docker-build-push.yml

This file was deleted.

2 changes: 0 additions & 2 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
prune .github
prune docs
prune notebooks
prune docker

global-exclude .gitignore .gitattributes .git-blame-ignore-revs
global-exclude .bandit.yml .codecov.yml .coveragerc .mypy.ini
global-exclude .pre-commit-config.yaml .readthedocs.yml
global-exclude docker-compose.yml
4 changes: 0 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ mozilla-sec-eia: Developing a linkage between SEC and EIA
:target: https://github.com/catalyst-cooperative/mozilla-sec-eia/actions?query=workflow%3Atox-pytest
:alt: Tox-PyTest Status

.. image:: https://github.com/catalyst-cooperative/mozilla-sec-eia/workflows/docker-build-push/badge.svg
:target: https://github.com/catalyst-cooperative/mozilla-sec-eia/actions?query=workflow%3Adocker-build-push
:alt: Docker build status

.. image:: https://img.shields.io/codecov/c/github/catalyst-cooperative/mozilla-sec-eia?style=flat&logo=codecov
:target: https://codecov.io/gh/catalyst-cooperative/mozilla-sec-eia
:alt: Codecov Test Coverage
Expand Down
26 changes: 0 additions & 26 deletions docker/Dockerfile

This file was deleted.

139 changes: 139 additions & 0 deletions terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
terraform {
backend "gcs" {
bucket = "f3441e415e6e5e7d-bucket-tfstate"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the right shape of things, but you'll run into issues with f34... bucket not existing in the new project! You'll have to:

  1. take away this gcs backend
  2. create a new state bucket with a new random prefix
  3. re-configure this gcs backend with the new state bucket

It's fine to do that all locally before committing the changes, which should look exactly like this but with a different bucket name.

prefix = "terraform/state"
jdangerx marked this conversation as resolved.
Show resolved Hide resolved
}
required_providers {
google = {
source = "hashicorp/google"
version = "4.51.0"
}
}
}

variable "project_id" {
type = string
default = "catalyst-cooperative-pudl"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything weird about using the PUDL project from a different repo? Should we just create a mozilla project?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're billing infrastructure costs to Mozilla, we should probably make a separate mozilla project - that will make the bookkeeping much easier.

If not, I think it's fine to use the same GCP project - though if we do that, I'd rather this terraform configuration live in the pudl repo. I think one tfstate per GCP project is easier to think about.

}

variable "catalyst_people" {
type = list(string)
default = [
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to add @bendnorman too.

]
}

provider "google" {
project = var.project_id
region = "us-east1"
zone = "us-east1-c"
}

resource "google_service_account" "mozilla_dev_sa" {
account_id = "mozilla-dev-sa"
display_name = "Mozilla dev"
}

resource "random_id" "bucket_prefix" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super-nit: might be a little easier to read later if this bucket prefix is defined right next to the only place it's used - the tfstate bucket.

byte_length = 8
}

resource "google_storage_bucket" "sec_10ks" {
name = "${random_id.bucket_prefix.hex}-bucket-sec-10ks"
location = "US"
storage_class = "STANDARD"
versioning {
enabled = true
}
}

resource "google_storage_bucket" "tfstate" {
name = "${random_id.bucket_prefix.hex}-tfstate"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if we create a new Mozilla project, we'll need to actually follow this guide which basically says:

  1. use a block such as this one to create a bucket to hold tfstate
  2. only then do you configure the tfstate to be remote like in lines 1-4 - though point the gcs backend at the new bucket

If you want to stay in the PUDL project because the billing isn't separate, you can avoid making this new bucket and just use what we have already set up (the f344... bucket).

location = "US"
storage_class = "STANDARD"
versioning {
enabled = true
}
}

resource "google_storage_bucket_iam_binding" "catalyst_gcs_access" {
bucket = google_storage_bucket.sec_10ks.name
role = "roles/storage.admin"
members = ["serviceAccount:${google_service_account.mozilla_dev_sa.email}"]
}

resource "google_project_iam_binding" "catalyst_cloudsql_instance_user" {
project = var.project_id
role = "roles/cloudsql.instanceUser"
members = ["serviceAccount:${google_service_account.mozilla_dev_sa.email}"]
}

resource "google_project_iam_binding" "catalyst_cloudsql_client" {
project = var.project_id
role = "roles/cloudsql.client"
members = ["serviceAccount:${google_service_account.mozilla_dev_sa.email}"]
}

resource "google_project_iam_binding" "catalyst_people_editors" {
jdangerx marked this conversation as resolved.
Show resolved Hide resolved
project = var.project_id
role = "roles/editor"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this making everyone an editor on the entire project?

members = [for email in var.catalyst_people : "user:${email}"]
}

resource "google_project_iam_binding" "catalyst_iam_act_as_sa" {
jdangerx marked this conversation as resolved.
Show resolved Hide resolved
project = var.project_id
role = "roles/iam.serviceAccountUser"
members = [for email in var.catalyst_people : "user:${email}"]
}

# cloud sql instance for usage
resource "google_sql_database_instance" "mozilla" {
name = "pg-mozilla"
database_version = "POSTGRES_14"
region = "us-central1"

settings {
tier = "db-f1-micro"
activation_policy = "ALWAYS"
database_flags {
name = "cloudsql.iam_authentication"
value = "on"
}
}
}

resource "google_sql_database" "database" {
name = "mozilla"
instance = google_sql_database_instance.mozilla.name
}

resource "google_sql_user" "default_user" {
name = "${google_service_account.mozilla_dev_sa.account_id}@${var.project_id}.iam"
instance = google_sql_database_instance.mozilla.name
type = "CLOUD_IAM_SERVICE_ACCOUNT"
}

resource "google_sql_user" "catalyst_users" {
for_each = toset(var.catalyst_people)
name = each.value
instance = google_sql_database_instance.mozilla.name
type = "CLOUD_IAM_USER"
}

resource "google_secret_manager_secret" "postgres_pass" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that you'll have to create a new "secret version" manually to actually hold a secret password value.

secret_id = "mozilla-postgres-pass"

replication {
automatic = true
}
}

# DB permissions can't be granted with vanilla TF, so we have to do that manually:

# 1. gcloud sql connect <instance> --user=postgres (using password stored in Secret Manager)
# 2. CREATE ROLE mozillareadwrite;
# 3. GRANT ALL ON DATABASE <db name> TO mozillareadwrite;
# 4. GRANT mozillareadwrite to "[email protected]", ...;
4 changes: 1 addition & 3 deletions tests/integration/jupyter_notebooks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

@pytest.mark.parametrize(
"notebook",
[
"notebooks/notebook.ipynb",
],
[],
)
def test_notebook_exec(notebook: str, test_dir: Path):
"""Test that maintained notebooks can be executed."""
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ passenv =
GCP_*
HOME
SQLALCHEMY_WARN_20
covargs = --cov={envsitepackagesdir}/mozilla-sec-eia --cov-append --cov-report=xml
covargs = --cov={envsitepackagesdir}/mozilla_sec_eia --cov-append --cov-report=xml
covreport = coverage report --sort=cover

#######################################################################################
Expand Down Expand Up @@ -96,7 +96,7 @@ extras =
tests
commands =
pytest {posargs} {[testenv]covargs} \
--doctest-modules {envsitepackagesdir}/mozilla-sec-eia \
--doctest-modules {envsitepackagesdir}/mozilla_sec_eia \
tests/unit

[testenv:integration]
Expand Down
Loading