Skip to content

Commit

Permalink
[skip ci] initial push of new python provision
Browse files Browse the repository at this point in the history
script
  • Loading branch information
punkdata committed Jan 31, 2023
1 parent de1b158 commit 5c72ece
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 91 deletions.
44 changes: 22 additions & 22 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,28 @@ jobs:
fail-on-issues: false
monitor-on-build: false

provision_terraform_cloud:
docker:
- image: cimg/base:stable
steps:
- checkout
- run:
name: Create .terraformrc file locally
command: |
echo -en "credentials \"app.terraform.io\" {token = \"$TF_CLOUD_TOKEN\"}" > $HOME/.terraformrc
- terraform/install:
terraform_version: "1.2.0"
arch: "amd64"
os: "linux"
- terraform/init:
path: ./terraform/digital_ocean/tfcloud
- run:
name: Provision org and workspaces in Terrform Cloud
command: |
terraform -chdir=./terraform/digital_ocean/tfcloud apply -auto-approve \
-var org_name=$TF_CLOUD_ORGANIZATION \
-var org_email=$TF_CLOUD_ORG_EMAIL \
-var workspace_name=$TF_CLOUD_WORKSPACE
# provision_terraform_cloud:
# docker:
# - image: cimg/base:stable
# steps:
# - checkout
# - run:
# name: Create .terraformrc file locally
# command: |
# echo -en "credentials \"app.terraform.io\" {token = \"$TF_CLOUD_TOKEN\"}" > $HOME/.terraformrc
# - terraform/install:
# terraform_version: "1.2.0"
# arch: "amd64"
# os: "linux"
# - terraform/init:
# path: ./terraform/digital_ocean/tfcloud
# - run:
# name: Provision org and workspaces in Terrform Cloud
# command: |
# terraform -chdir=./terraform/digital_ocean/tfcloud apply -auto-approve \
# -var org_name=$TF_CLOUD_ORGANIZATION \
# -var org_email=$TF_CLOUD_ORG_EMAIL \
# -var workspace_name=$TF_CLOUD_WORKSPACE

create_do_k8s_cluster:
docker:
Expand Down
13 changes: 13 additions & 0 deletions scripts/util/provisioning/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM cimg/python:3.10.0

# Create app directory
WORKDIR /home/circleci/project

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY requirements.txt provision_workshop.py /home/circleci/project/

RUN pip install -r requirements.txt

CMD [ "python", "scripts/util/provisioning/provision_workshop.py" ]
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def strip_spaces(obj):
SNYK_TOKEN = strip_spaces(creds.get('snyk_token'))
DOCKER_LOGIN = strip_spaces(creds.get('docker_login'))
DOCKER_TOKEN = strip_spaces(creds.get('docker_token'))
TF_CLOUD_API_HOST = 'https://app.terraform.io/api/v2'
TF_CLOUD_TOKEN = strip_spaces(creds.get('tf_cloud_token'))
TF_CLOUD_ORG_EMAIL = strip_spaces(creds.get('tf_cloud_org_email'))
TF_CLOUD_ORG_NAME = strip_spaces(creds.get('tf_cloud_org_name'))
Expand All @@ -33,9 +34,13 @@ def strip_spaces(obj):
DIGITAL_OCEAN_TOKEN = strip_spaces(creds.get('digital_ocean_token'))

REQUEST_HEADER = {
'content-type': "application/json",
'Circle-Token': CIRCLE_TOKEN
}
'content-type': "application/json",
'Circle-Token': CIRCLE_TOKEN
}
TF_CLOUD_HEADERS = {
'Authorization' : f'Bearer {TF_CLOUD_TOKEN}',
'Content-Type': 'application/vnd.api+json'
}

def get_circleci_api_request(endpoint, payload_dict):
try:
Expand Down Expand Up @@ -130,14 +135,63 @@ def find_or_create_context_by_name(context_name): # context name - CICD_WORKSH
return circleci_context_id

# Add Env vars to context
print(add_circle_token_to_context_with_name('SNYK', 'SNYK_TOKEN', SNYK_TOKEN))
print(add_circle_token_to_context_with_name('DOCKER', 'DOCKER_LOGIN', DOCKER_LOGIN))
print(add_circle_token_to_context_with_name('DIGITAL_OCEAN', 'DIGITAL_OCEAN_TOKEN', DIGITAL_OCEAN_TOKEN))
print(add_circle_token_to_context_with_name('DOCKER', 'DOCKER_PASSWORD', DOCKER_TOKEN))
print(add_circle_token_to_context_with_name('TERRAFORM_CLOUD', 'TF_CLOUD_TOKEN', TF_CLOUD_TOKEN))
print(add_circle_token_to_context_with_name('TERRAFORM_CLOUD', 'TF_CLOUD_ORG_EMAIL', TF_CLOUD_ORG_EMAIL))
print(add_circle_token_to_context_with_name('TERRAFORM_CLOUD', 'TF_CLOUD_ORGANIZATION', TF_CLOUD_ORGANIZATION))
print(add_circle_token_to_context_with_name('TERRAFORM_CLOUD', 'TF_CLOUD_WORKSPACE', TF_CLOUD_WORKSPACE))
# print(add_circle_token_to_context_with_name('SNYK', 'SNYK_TOKEN', SNYK_TOKEN))
# print(add_circle_token_to_context_with_name('DOCKER', 'DOCKER_LOGIN', DOCKER_LOGIN))
# print(add_circle_token_to_context_with_name('DIGITAL_OCEAN', 'DIGITAL_OCEAN_TOKEN', DIGITAL_OCEAN_TOKEN))
# print(add_circle_token_to_context_with_name('DOCKER', 'DOCKER_PASSWORD', DOCKER_TOKEN))
# print(add_circle_token_to_context_with_name('TERRAFORM_CLOUD', 'TF_CLOUD_TOKEN', TF_CLOUD_TOKEN))
# print(add_circle_token_to_context_with_name('TERRAFORM_CLOUD', 'TF_CLOUD_ORG_EMAIL', TF_CLOUD_ORG_EMAIL))
# print(add_circle_token_to_context_with_name('TERRAFORM_CLOUD', 'TF_CLOUD_ORGANIZATION', TF_CLOUD_ORGANIZATION))
# print(add_circle_token_to_context_with_name('TERRAFORM_CLOUD', 'TF_CLOUD_WORKSPACE', TF_CLOUD_WORKSPACE))

# This section is for provisioning default workspace to hold state
def get_tf_cloud_org(end_point, tfc_headers, org_name):
try:
req = f'{end_point}/organizations/{org_name}'
resp = requests.get(req, headers=tfc_headers)
print(resp.status_code)
print(resp.json())
return resp.json()
except requests.exceptions.HTTPError as errh:
print ("Http Error:",errh)
except requests.exceptions.ConnectionError as errc:
print ("Error Connecting:",errc)
except requests.exceptions.Timeout as errt:
print ("Timeout Error:",errt)
except requests.exceptions.RequestException as err:
print ("OOps: Something Else",err)

def post_tf_cloud_org(end_point, tfc_headers, org_name, email):
try:
req = f'{end_point}/organizations'
pay_load = {
'data': {
'type': 'organizations',
'attributes': {
'name': f'{org_name}',
'email': f'{email}'}
}
}
resp = requests.post(req, headers=tfc_headers, json=pay_load)
print(resp.json())
return resp.json()
except requests.exceptions.HTTPError as errh:
print ("Http Error:",errh)
except requests.exceptions.ConnectionError as errc:
print ("Error Connecting:",errc)
except requests.exceptions.Timeout as errt:
print ("Timeout Error:",errt)
except requests.exceptions.RequestException as err:
print ("OOps: Something Else",err)

tf_response = get_tf_cloud_org(TF_CLOUD_API_HOST, TF_CLOUD_HEADERS, f'Provisioning-Org-{TF_CLOUD_ORGANIZATION}')

if tf_response.get('errors')[0].get('title') == 'not found':
# Create the org
resp = post_tf_cloud_org(TF_CLOUD_API_HOST, TF_CLOUD_HEADERS,f'Provisioning-Org-{TF_CLOUD_ORGANIZATION}', TF_CLOUD_ORG_EMAIL)
# else:
# print(get_tf_cloud_org(TF_CLOUD_API_HOST, TF_CLOUD_HEADERS, f'Provisioning-Org-{TF_CLOUD_ORGANIZATION}').contains('errors'))



# # Warning uncommenting the code block below will delete all the contexts created above
Expand Down
41 changes: 41 additions & 0 deletions scripts/util/provisioning/rando-cmds.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
terraform -chdir=terraform/digital_ocean/tfcloud/ apply --auto-approve -var org_name=test_tf_provision -var [email protected] -var execution_mode=local

# Detroy TF_Cloud_Org and Workspaces
terraform -chdir=terraform/digital_ocean/tfcloud/ destroy --auto-approve -var org_name=test_tf_provision -var [email protected] -var execution_mode=local


# Terraform Backend Config for do_create_k8s
echo -en "organization = \"${TF_CLOUD_ORGANIZATION}\"\nworkspaces{name =\"${TF_CLOUD_WORKSPACE}\"}" > ./terraform/digital_ocean/do_create_k8s/remote_backend_config

# Initialize terraform with unique org name
terraform -chdir=terraform/digital_ocean/do_create_k8s init \
-backend-config=remote_backend_config
# Execute apply comand
terraform -chdir=terraform/digital_ocean/do_create_k8s apply -auto-approve \
-var do_token=$DIGITAL_OCEAN_TOKEN \
-var cluster_name=$CLUSTER_NAME \
-var do_k8s_slug_ver=$DO_K8S_SLUG_VER


# Terraform Backend Config for do_k8s_deploy_app
echo -en "organization = \"${TF_CLOUD_ORGANIZATION}\"\nworkspaces{name =\"${TF_CLOUD_WORKSPACE}-deploy\"}" > ./terraform/digital_ocean/do_k8s_deploy_app/remote_backend_config
# Initialize terraform with unique org name
terraform -chdir=terraform/digital_ocean/do_k8s_deploy_app init \
-backend-config=remote_backend_config
# Execute apply comand
terraform -chdir=./terraform/digital_ocean/do_k8s_deploy_app apply -auto-approve \
-var do_token=$DIGITAL_OCEAN_TOKEN \
-var cluster_name=$CLUSTER_NAME \
-var docker_image=$DOCKER_IMAGE


# Destroy prov

terraform -chdir=./terraform/digital_ocean/tfcloud destroy -auto-approve \
-var org_name=$TF_CLOUD_ORGANIZATION \
-var org_email=$TF_CLOUD_ORG_EMAIL \
-var workspace_name=$TF_CLOUD_WORKSPACE

# Provision container image
cp ../../../requirements.txt . && docker build -t ariv3ra/cicd-ws-provision:latest -t ariv3ra/cicd-ws-provision:v0.0.1 .

31 changes: 0 additions & 31 deletions terraform/digital_ocean/tfcloud/main.tf

This file was deleted.

27 changes: 0 additions & 27 deletions terraform/digital_ocean/tfcloud/variables.tf

This file was deleted.

0 comments on commit 5c72ece

Please sign in to comment.