• This repository has been archived on 10/Jan/2021
  • Stars
    star
    117
  • Rank 301,828 (Top 6 %)
  • Language
    Shell
  • License
    MIT License
  • Created almost 7 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Script to fully automate Terraform import of Github Org (teams, users, and repos)

WHAT

It's a simple bash script to simply import a Github Organization into Terraform. It uses the Github API and Terraform CLI to import the following resources:

  • all public repos (includes pagination support for Orgs with 100+ repos)
  • all private repos (includes pagination support for Orgs with 100+ repos)
  • all team repos (includes pagination support for Orgs with 100+ repos)
  • all teams
  • all team memberships
  • all organization users

After importing the resources the script also writes a basic terraform.tf config for each resource, making this a fully automated process.

WHY

I like managing things with Terraform. Sometimes those things were created before Terraform supported them. Manually importing hundreds of repos/teams/users/etc is a tedious process.

HOW

How the script works

Public repos

Imports all public repos owned by the organization (includes full pagination support for Orgs with 100+ repos). Also writes a Terraform resource block in a single file (github-public-repos.tf), using the following template and populating it with values pulled via the Github API:

resource "github_repository" "$PUBLIC_REPO_NAME" {
  name        = "$PUBLIC_REPO_NAME"
  private     = false
  description = "$PUBLIC_REPO_DESCRIPTION"
  has_wiki    = "$PUBLIC_REPO_WIKI"
  has_downloads = "$PUBLIC_REPO_DOWNLOADS"
  has_issues  = "$PUBLIC_REPO_ISSUES"
}

Private repos

Imports all private repos owned by the organization (includes full pagination support for Orgs with 100+ repos). Also writes a Terraform resource block in a single file (github-private-repos.tf), using the following template and populating it with values pulled via the Github API:

resource "github_repository" "$PRIVATE_REPO_NAME" {
  name        = "$PRIVATE_REPO_NAME"
  private     = true
  description = "$PRIVATE_REPO_DESCRIPTION"
  has_wiki    = "$PRIVATE_REPO_WIKI"
  has_downloads = "$PRIVATE_REPO_DOWNLOADS"
  has_issues  = "$PRIVATE_REPO_ISSUES"
}

Team repos

Imports all team repos owned by the organization (includes full pagination support for Orgs with 100+ repos). Also writes a Terraform resource block in a unique file per team (github-teams-$TEAM_NAME.tf), using the following template and populating it with values pulled via the Github API:

resource "github_team_repository" "$TEAM_NAME-$TERRAFORM_TEAM_REPO_NAME" {
  team_id    = "$TEAM_ID"
  repository = "$REPO_NAME"
  permission = "admin" or "push" or "pull"
}

Teams

Imports all teams belonging to the organization. Also writes a Terraform resource block in a unique file per team (github-teams-$TEAM_NAME.tf), using the following template and populating it with values pulled via the Github API:

resource "github_team" "$TEAM_NAME" {
  name        = "$TEAM_NAME"
  description = "$TEAM_DESCRIPTION"
  privacy     = "closed" or "secret"
}

Team memberships

Imports the team membership for all teams owned by the organization (what users belong to what teams). Also writes a Terraform resource block in a unique file per team (github-team-memberships-$TEAM_NAME.tf), using the following template and populating it with values pulled via the Github API:

resource "github_team_membership" "$TEAM_NAME-$USER_NAME" {
  username    = "$USER_NAME"
  team_id     = "$TEAM_ID"
  role        = "maintainer" or "member"
}

Organization users

Imports all users belonging to the organization. Also writes a Terraform resource block in a single file (github-users.tf), using the following template and populating it with values pulled via the Github API:

resource "github_membership" "$USER_NAME" {
  username        = "$USER_NAME"
  role            = "member"
}

How to use the script

Requirements

  • An existing Github account with a user that belongs to an organization
  • A github personal access token with the following permissions:
    • repo (all)
    • admin:org (all)
  • Terraform
  • jq

Do it

  • git clone this repo

  • create a basic terraform configuration file, e.g. main.tf with something like:

    provider "github" {
      token        = "TOKENGOESHERE"
      organization = "my_org"
      # optional, if using GitHub Enterprise
      base_url     =  "https://github.mycompany.com/api/v3/"
    }
  • run terraform init to e.g. install the GitHub provider

  • configure the variables at the top of the script

    • GITHUB_TOKEN=...
    • ORG=...
    • if you're using GitHub Enterprise, API_URL_PREFIX=... or remember to pass them in via the environment
  • run the scriptm, perhaps passing the necessary environment variables

    GITHUB_TOKEN=12334...4555 ORG=my_org terraform-import-github-org.sh
    
  • run a terraform plan to see that everything was imported and that no changes are required.

    • some manual modifications could be required since not every field supported by Terraform has been implemented by this script.
    • HEADS UP - the script hardcodes user roles to "member".

Using with GitHub Enterprise

This should also work with GitHub Enterprise deployments if you also set (either editing the script or via an environment variable) the API_URL_PREFIX correctly, e.g. https://github.mycompany.com/api/v3.

FAQ

  • Q) Why bash?
    • A) I like bash.
  • Q) Do you plan on extending this?
    • A) Sure, see the TODO section.

TODO

Issues

More Repositories

1

Open-Publisher

Using Jekyll to create outputs that can be used as Pandoc inputs. In short - input markdown, output mobi, epub, pdf, and print-ready pdf. With a focus on fiction.
Ruby
244
star
2

starving-artist-jekyll-theme

A portfolio theme for Jekyll for artists to display their work - http://chrisanthropic.github.io/starving-artist-jekyll-theme/
CSS
120
star
3

terraform-infra-as-code-coverage-badges

Terraform / infrastructure-as-code coverage badges - how much of your AWS infrastructure is managed by Terraform?
Shell
67
star
4

comical-jekyll-theme

A Configurable Webcomic Theme for Jekyll
CSS
44
star
5

slim-pickins-jekyll-theme

A minimal Jekyll theme
CSS
42
star
6

packer-kali

Packer stuff to build a Kali 1.0.9 vagrant box
Shell
18
star
7

packer-pfsense

Packer stuff to build a pfSense vagrant box.
Shell
14
star
8

ansible-aws-template

A tiny Ansible quick-start for AWS and Dynamic Inventory
Python
13
star
9

packer-debian2kali-ec2

An attempt at a Packer template to build an HVM Kali AMI that is identical to a Kali install using the official ISO.
Shell
12
star
10

docker-kali-msf

Docker image with Kali + metasploit/msfconsole + postgresql
Shell
8
star
11

docker-kali

Base Kali image for Docker built with debootstrap
Shell
8
star
12

docker-kali-msf-micro

Docker image with Kali + metasploit/msfconsole + postgresql -without any metasploit modules
Shell
7
star
13

docker-alpine-rpi-glibc-builder

Docker container to build an Alpine glibc package for the Raspberry Pi 2.
4
star
14

docker-alpine-rpi-glibc

An Alpine Linux Docker container for Rasberry Pi 2 with Glibc installed.
3
star
15

image_optim-jekyll-plugin

A simple Jekyll plugin to optimize images using image_optim
Ruby
3
star
16

docker-alpine-rpi-base

Build your own Alpine Linux container compatible with a Raspberry Pi 2
Shell
3
star
17

chrisanthropic.github.io

CSS
2
star
18

phantom_ledger

This is a collection of scripts working towards automated checking of sales reports for authors on the following sites; Createspace, DriveThru, iTunes, KDP, Kobo, Nook, Smashwords
Ruby
2
star
19

LinuxMusicProduction

My personal notes for making music on Linux
1
star
20

docker-alpine-rpi-jenkins

An Alpine Linux Docker container for Rasberry Pi 2 with Glibc and Java JDK 8 and Jenkins installed.
1
star
21

Open-Publisher-Documentation

Documentation website for the Open-Publisher project
SCSS
1
star
22

docker-alpine-rpi-java8

An Alpine Linux Docker container for Rasberry Pi 2 with Glibc and Java JDK 8 installed.
1
star
23

ansible-tdd-test-kitchen-inspec

Sample Ansbile role tested with test-kitchen and inspec.
Ruby
1
star