# Path: installer/configurators/common.py
# def get_terraform_folder():
#     return get_installer_root() + "/installer/terraform/"
# 
# Path: installer/helpers/processwrap.py
# def tee_check_output(args, workdir=None):
#     with open(install_logfile, 'a') as output:
#         output.write('Invoking command: {0}\n'.format(' '.join(args)))
#         output.flush()  # Flush to make sure order is maintained
#         p = subprocess.Popen(args, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, bufsize=1, cwd=workdir)
#         while p.poll() is None:
#             for line in p.stdout:
#                 sys.stdout.buffer.write(line)
#                 output.write(line.decode('utf8'))
#         return p.returncode == 0
# 
# def check_output(args, workdir=None):
#     with open(install_logfile, 'a') as output:
#         output.write('Invoking command: {0}\n'.format(' '.join(args)))
#         output.flush()  # Flush to make sure order is maintained
#         return subprocess.check_output(args, stderr=output, cwd=workdir)
# 
# def call(args, workdir=None):
#     with open(install_logfile, 'a') as output:
#         output.write('Invoking command: {0}\n'.format(' '.join(args)))
#         output.flush()  # Flush to make sure order is maintained
#         subprocess.call(args, stderr=output, stdout=output, cwd=workdir)


from pathlib import Path
from installer.configurators.common import get_terraform_folder
from installer.helpers.processwrap import tee_check_output, check_output, call
from collections import OrderedDict
import datetime
import subprocess
import sys
import json
import os
# TODO move this here?


class colors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'


def exec_terraform_apply():
    print(
        colors.OKBLUE + 'Initializing and running Terraform.\n' + colors.ENDC)
    print(
        colors.OKBLUE + datetime.datetime.now().strftime('%c') + '\n' + colors.ENDC)

    call(['terraform', 'init'], workdir=get_terraform_folder())

    # TODO see if we can avoid passing in AWS creds via env vars, if `awscli` is configured Terraform
    # will just use those preconfigured creds without any extra effort
    tfCommand = [
            'terraform', 'apply', '--auto-approve',
            '-var', 'aws_access_key=%s' % (os.environ['AWS_ACCESS_KEY_ID']),
            '-var', 'aws_secret_key=%s' % (os.environ['AWS_SECRET_ACCESS_KEY'])
    ]


# Path: installer/configurators/common.py
# def get_terraform_folder():
#     return get_installer_root() + "/installer/terraform/"
# 
# Path: installer/helpers/processwrap.py
# def tee_check_output(args, workdir=None):
#     with open(install_logfile, 'a') as output:
#         output.write('Invoking command: {0}\n'.format(' '.join(args)))
#         output.flush()  # Flush to make sure order is maintained
#         p = subprocess.Popen(args, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, bufsize=1, cwd=workdir)
#         while p.poll() is None:
#             for line in p.stdout:
#                 sys.stdout.buffer.write(line)
#                 output.write(line.decode('utf8'))
#         return p.returncode == 0
# 
# def check_output(args, workdir=None):
#     with open(install_logfile, 'a') as output:
#         output.write('Invoking command: {0}\n'.format(' '.join(args)))
#         output.flush()  # Flush to make sure order is maintained
#         return subprocess.check_output(args, stderr=output, cwd=workdir)
# 
# def call(args, workdir=None):
#     with open(install_logfile, 'a') as output:
#         output.write('Invoking command: {0}\n'.format(' '.join(args)))
#         output.flush()  # Flush to make sure order is maintained
#         subprocess.call(args, stderr=output, stdout=output, cwd=workdir)


# Path: installer/helpers/terraform.py
from pathlib import Path
from installer.configurators.common import get_terraform_folder
from installer.helpers.processwrap import tee_check_output, check_output, call
from collections import OrderedDict
import datetime
import subprocess
import sys
import json
import os

# TODO move this here?


class colors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'


def exec_terraform_apply():
    print(
        colors.OKBLUE + 'Initializing and running Terraform.\n' + colors.ENDC)
    print(
        colors.OKBLUE + datetime.datetime.now().strftime('%c') + '\n' + colors.ENDC)

    call(['terraform', 'init'], workdir=get_terraform_folder())

    # TODO see if we can avoid passing in AWS creds via env vars, if `awscli` is configured Terraform
    # will just use those preconfigured creds without any extra effort
    tfCommand = [
            'terraform', 'apply', '--auto-approve',
            '-var', 'aws_access_key=%s' % (os.environ['AWS_ACCESS_KEY_ID']),
            '-var', 'aws_secret_key=%s' % (os.environ['AWS_SECRET_ACCESS_KEY'])
    ]


    if not tee_check_output(tfCommand, workdir=get_terraform_folder()):