forked from devopsloft/devopsloft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspin-docker.py
More file actions
61 lines (51 loc) · 2.17 KB
/
spin-docker.py
File metadata and controls
61 lines (51 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
import dotenv
import os
import subprocess
import click
from createPemFiles import SelfSignedCertificate, IsCertExist
print_debug = 'No'
def print_info(message):
global print_debug
if print_debug == 'yes':
print("--- python debug ---> ", message)
def PrepareEnvironmentVars(environmentName, action):
# Reads the .env file from the repository
# Returns an array with all the env vars, inclduing modificatoins per env
devwebsport = 'DEV_WEB_GUEST_SECURE_PORT'
dotenv.load_dotenv()
envArray = os.environ.copy()
envArray['RUN_BY_PYTHON'] = 'yes'
envArray['ENVIRONMENT'] = environmentName
envArray['HOMEPATH'] = '/home'
if (environmentName == 'dev'):
envArray['WEB_HOST_PORT'] = envArray['DEV_WEB_HOST_PORT']
envArray['WEB_GUEST_PORT'] = envArray['DEV_WEB_GUEST_PORT']
envArray['WEB_HOST_SECURE_PORT'] = envArray['DEV_WEB_HOST_SECURE_PORT']
envArray['WEB_GUEST_SECURE_PORT'] = envArray[devwebsport]
return envArray
@click.command()
@click.option("-e", "--environment", required=False, default="dev",
type=click.Choice(["dev", "prod", "stage"]))
@click.option("-a", "--action", required=False, default="up",
type=click.Choice(["up", "destroy"]))
@click.option("-d", "--debug", required=False, default="no",
type=click.Choice(["yes", "no"]))
def main(environment, action, debug):
machineName = environment
envVars = machineName
envVars = PrepareEnvironmentVars(envVars, action)
if not (IsCertExist()):
SelfSignedCertificate()
if(action == "up"):
command = "docker-compose up -d"
vault_command = "j2 vault/config.hcl.j2 -o vault/config/config.hcl"
vault_copy = "docker cp vault/config/config.hcl vault:/vault/config"
subprocess.call(command, env=envVars, shell=True)
subprocess.call(vault_command, env=envVars, shell=True)
subprocess.call(vault_copy, env=envVars, shell=True)
if(action == "destroy"):
command = "docker-compose down -v --rmi all --remove-orphans"
subprocess.Popen(command, env=envVars, shell=True)
if __name__ == '__main__':
main()