Initial import

This commit is contained in:
Iain Learmonth 2022-03-10 14:26:22 +00:00
commit 09f0b0672d
64 changed files with 3735 additions and 0 deletions

View file

@ -0,0 +1,28 @@
import json
from app import app
from app.mirror_sites import bridgelines, mirror_sites, mirror_mapping
from app.models import MirrorList
from app.terraform import BaseAutomation
class ListAutomation(BaseAutomation):
def generate_terraform(self):
self.write_terraform_config(
self.template,
lists=MirrorList.query.filter(
MirrorList.destroyed == None,
MirrorList.provider == self.provider,
).all(),
global_namespace=app.config['GLOBAL_NAMESPACE'],
**{
k: app.config[k.upper()]
for k in self.template_parameters
}
)
with open(self.working_directory('bc2.json'), 'w') as out:
json.dump(mirror_sites(), out, indent=2, sort_keys=True)
with open(self.working_directory('bca.json'), 'w') as out:
json.dump(mirror_mapping(), out, indent=2, sort_keys=True)
with open(self.working_directory('bridgelines.json'), 'w') as out:
json.dump(bridgelines(), out, indent=2, sort_keys=True)

View file

@ -0,0 +1,55 @@
from app import app
from app.terraform.list import ListAutomation
class ListGithubAutomation(ListAutomation):
short_name = "list_github"
provider = "github"
template_parameters = [
"github_api_key"
]
template = """
terraform {
required_providers {
github = {
source = "integrations/github"
version = "~> 4.20.1"
}
}
}
{% for list in lists %}
provider "github" {
alias = "list_{{ list.id }}"
owner = "{{ list.container.split("/")[0] }}"
token = "{{ github_api_key }}"
}
data "github_repository" "repository_{{ list.id }}" {
provider = github.list_{{ list.id }}
name = "{{ list.container.split("/")[1] }}"
}
resource "github_repository_file" "file_{{ list.id }}" {
provider = github.list_{{ list.id }}
repository = data.github_repository.repository_{{ list.id }}.name
branch = "{{ list.branch }}"
file = "{{ list.filename }}"
content = file("{{ list.format }}.json")
commit_message = "Managed by Terraform"
commit_author = "Terraform User"
commit_email = "terraform@api.otf.is"
overwrite_on_create = true
}
{% endfor %}
"""
if __name__ == "__main__":
with app.app_context():
auto = ListGithubAutomation()
auto.generate_terraform()
auto.terraform_init()
auto.terraform_apply()

View file

@ -0,0 +1,54 @@
from app import app
from app.terraform.list import ListAutomation
class ListGitlabAutomation(ListAutomation):
short_name = "list_gitlab"
provider = "gitlab"
template_parameters = [
"gitlab_token",
"gitlab_author_email",
"gitlab_author_name",
"gitlab_commit_message"
]
template = """
terraform {
required_providers {
gitlab = {
source = "gitlabhq/gitlab"
version = "~> 3.12.0"
}
}
}
provider "gitlab" {
token = "{{ gitlab_token }}"
}
{% for list in lists %}
data "gitlab_project" "project_{{ list.id }}" {
id = "{{ list.container }}"
}
resource "gitlab_repository_file" "file_{{ list.id }}" {
project = data.gitlab_project.project_{{ list.id }}.id
file_path = "{{ list.filename }}"
branch = "{{ list.branch }}"
content = base64encode(file("{{ list.format }}.json"))
author_email = "{{ gitlab_author_email }}"
author_name = "{{ gitlab_author_name }}"
commit_message = "{{ gitlab_commit_message }}"
}
{% endfor %}
"""
if __name__ == "__main__":
with app.app_context():
auto = ListGitlabAutomation()
auto.generate_terraform()
auto.terraform_init()
auto.terraform_apply()

46
app/terraform/list/s3.py Normal file
View file

@ -0,0 +1,46 @@
from app import app
from app.terraform.list import ListAutomation
class ListGithubAutomation(ListAutomation):
short_name = "list_s3"
provider = "s3"
template_parameters = [
"aws_access_key",
"aws_secret_key"
]
template = """
terraform {
required_providers {
aws = {
version = "~> 4.4.0"
}
}
}
provider "aws" {
access_key = "{{ aws_access_key }}"
secret_key = "{{ aws_secret_key }}"
region = "us-east-1"
}
{% for list in lists %}
resource "aws_s3_object" "object_{{ list.id }}" {
bucket = "{{ list.container }}"
key = "{{ list.filename }}"
source = "{{ list.format }}.json"
content_type = "application/json"
etag = filemd5("{{ list.format }}.json")
}
{% endfor %}
"""
if __name__ == "__main__":
with app.app_context():
auto = ListGithubAutomation()
auto.generate_terraform()
auto.terraform_init()
auto.terraform_apply()