# 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
        print(
            colors.WARNING + 'Destroying created AWS resources because of failure' + colors.ENDC)
        tee_check_output(['terraform', 'destroy', '--auto-approve'], workdir=get_terraform_folder())
    else:
        print(colors.OKGREEN + datetime.datetime.now().strftime('%c') + '\n' + colors.ENDC)
        print(colors.OKGREEN + 'Install succeded, generating stack_details.json...' + '\n' + colors.ENDC)
        generate_stack_details()


def exec_terraform_destroy():
    tfCommand = [
            'terraform', 'destroy', '--auto-approve'
    ]

    if not tee_check_output(tfCommand, workdir=get_terraform_folder()):
        print(
            colors.FAIL + datetime.datetime.now().strftime('%c') + '\n' + colors.ENDC)

        print(
            colors.FAIL + 'Terraform destroy failed! You can try re-running the uninstall' + colors.ENDC)
    else:
        print(
            colors.OKGREEN + datetime.datetime.now().strftime('%c') + '\n' + colors.ENDC)

        print(
            colors.OKGREEN + 'Terraform finished! AWS resources destroyed\n' + colors.ENDC)


def get_terraform_output_var(varname):
    try:

# 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

        print(
            colors.WARNING + 'Destroying created AWS resources because of failure' + colors.ENDC)
        tee_check_output(['terraform', 'destroy', '--auto-approve'], workdir=get_terraform_folder())
    else:
        print(colors.OKGREEN + datetime.datetime.now().strftime('%c') + '\n' + colors.ENDC)
        print(colors.OKGREEN + 'Install succeded, generating stack_details.json...' + '\n' + colors.ENDC)
        generate_stack_details()


def exec_terraform_destroy():
    tfCommand = [
            'terraform', 'destroy', '--auto-approve'
    ]

    if not tee_check_output(tfCommand, workdir=get_terraform_folder()):
        print(
            colors.FAIL + datetime.datetime.now().strftime('%c') + '\n' + colors.ENDC)

        print(
            colors.FAIL + 'Terraform destroy failed! You can try re-running the uninstall' + colors.ENDC)
    else:
        print(
            colors.OKGREEN + datetime.datetime.now().strftime('%c') + '\n' + colors.ENDC)

        print(
            colors.OKGREEN + 'Terraform finished! AWS resources destroyed\n' + colors.ENDC)


def get_terraform_output_var(varname):
    try:

        return check_output(['terraform', 'output', varname], workdir=get_terraform_folder()).rstrip().decode('utf8')