# Path: installer/cli/click_required.py
# class Required(click.Option):
#     def __init__(self, *args, **kwargs):
#         required_if = kwargs.pop('required_if', None)
#         required_if_not = kwargs.pop('required_if_not', None)
#         assert (required_if or required_if_not), "'required_if/if_not' parameter required"
#         if required_if:
#             self.required_if = required_if
#             kwargs['help'] = (kwargs.get('help', '') +
#                               ' NOTE: This argument must be provided if %s is specified' %
#                               self.required_if).strip()
#         elif required_if_not:
#             self.required_if_not = required_if_not
#             kwargs['help'] = (kwargs.get('help', '') +
#                               ' NOTE: This argument is only required if %s is not specified' %
#                               self.required_if_not).strip()
#         super(Required, self).__init__(*args, **kwargs)
# 
#     def handle_parse_result(self, ctx, opts, args):
#         if hasattr(self, 'required_if'):
#             we_are_not_present = self.name not in opts
#             other_present = self.required_if in opts
# 
#             if other_present:
#                 if we_are_not_present:
#                     raise click.UsageError(
#                         "Illegal usage: `%s` must be provided if `%s` is specified" % (
#                             self.name, self.required_if))
#             else:
#                 self.prompt = None
#         elif hasattr(self, 'required_if_not'):
#             we_are_present = self.name in opts
#             other_present = self.required_if_not in opts
# 
#             if other_present:
#                 if we_are_present:
#                     raise click.UsageError(
#                         "Illegal usage: `%s` is only required if `%s` is not specified" % (
#                             self.name, self.required_if_not))
#                 else:
#                     self.prompt = None
# 
#         return super(Required, self).handle_parse_result(
#             ctx, opts, args)
# 
# Path: installer/configurators/jenkins_container.py
# def configure_jenkins_container(existing_vpc_id, vpc_cidr, ecs_range):
#     """
#         Launch a containerized Jenkins server.
#     """
#     if existing_vpc_id:
#         replace_tfvars('existing_vpc_ecs', existing_vpc_id, get_tfvars_file())
#     else:
#         replace_tfvars("autovpc", "true", get_tfvars_file(), False)
#         replace_tfvars("vpc_cidr_block", vpc_cidr, get_tfvars_file())
# 
#     replace_tfvars('network_range', ecs_range, get_tfvars_file())
#     replace_tfvars('jenkinsuser', "admin", get_tfvars_file())
#     replace_tfvars('jenkinspasswd', passwd_generator(), get_tfvars_file())
# 
# Path: installer/configurators/gitlab_container.py
# def configure_gitlab_container():
#     # TODO having to explicitly disable each other SCM type by name here is not elegant
#     replace_tfvars('scmbb', 'false', get_tfvars_file(), False)
#     replace_tfvars("scmgitlab", "true", get_tfvars_file(), False)
#     replace_tfvars('scm_type', 'gitlab', get_tfvars_file())
#     replace_tfvars('scm_pathext', '/', get_tfvars_file())
# 
# Path: installer/configurators/sonarqube_container.py
# def configure_sonarqube_container():
#     """
#         Configure a containerized Sonar server.
#     """
#     replace_tfvars("dockerizedSonarqube", "true", get_tfvars_file(), False)
#     replace_tfvars('sonar_username', "admin", get_tfvars_file())
#     replace_tfvars('sonar_passwd', passwd_generator(), get_tfvars_file())
#     replace_tfvars('codequality_type', 'sonarqube', get_tfvars_file())
#     replace_tfvars('codeq', 1, get_tfvars_file())
# 
# Path: installer/helpers/terraform.py
# 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()):
#         print(
#             colors.FAIL + datetime.datetime.now().strftime('%c') + '\n' + colors.ENDC)
# 
#         print(
#             colors.FAIL + 'Terraform apply failed!' + colors.ENDC)
# 
#         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()


import click
from installer.cli.click_required import Required
from installer.configurators.jenkins_container import configure_jenkins_container
from installer.configurators.gitlab_container import configure_gitlab_container
from installer.configurators.sonarqube_container import configure_sonarqube_container
from installer.helpers.terraform import exec_terraform_apply


@click.command()
# Sonarqube (container)
@click.option(
    '--sonarqube/--no-sonarqube',
    default=False
)
@click.option(
    "--vpcid",
    help='Specify the ID of an existing VPC to use for ECS configuration',

# Path: installer/cli/click_required.py
# class Required(click.Option):
#     def __init__(self, *args, **kwargs):
#         required_if = kwargs.pop('required_if', None)
#         required_if_not = kwargs.pop('required_if_not', None)
#         assert (required_if or required_if_not), "'required_if/if_not' parameter required"
#         if required_if:
#             self.required_if = required_if
#             kwargs['help'] = (kwargs.get('help', '') +
#                               ' NOTE: This argument must be provided if %s is specified' %
#                               self.required_if).strip()
#         elif required_if_not:
#             self.required_if_not = required_if_not
#             kwargs['help'] = (kwargs.get('help', '') +
#                               ' NOTE: This argument is only required if %s is not specified' %
#                               self.required_if_not).strip()
#         super(Required, self).__init__(*args, **kwargs)
# 
#     def handle_parse_result(self, ctx, opts, args):
#         if hasattr(self, 'required_if'):
#             we_are_not_present = self.name not in opts
#             other_present = self.required_if in opts
# 
#             if other_present:
#                 if we_are_not_present:
#                     raise click.UsageError(
#                         "Illegal usage: `%s` must be provided if `%s` is specified" % (
#                             self.name, self.required_if))
#             else:
#                 self.prompt = None
#         elif hasattr(self, 'required_if_not'):
#             we_are_present = self.name in opts
#             other_present = self.required_if_not in opts
# 
#             if other_present:
#                 if we_are_present:
#                     raise click.UsageError(
#                         "Illegal usage: `%s` is only required if `%s` is not specified" % (
#                             self.name, self.required_if_not))
#                 else:
#                     self.prompt = None
# 
#         return super(Required, self).handle_parse_result(
#             ctx, opts, args)
# 
# Path: installer/configurators/jenkins_container.py
# def configure_jenkins_container(existing_vpc_id, vpc_cidr, ecs_range):
#     """
#         Launch a containerized Jenkins server.
#     """
#     if existing_vpc_id:
#         replace_tfvars('existing_vpc_ecs', existing_vpc_id, get_tfvars_file())
#     else:
#         replace_tfvars("autovpc", "true", get_tfvars_file(), False)
#         replace_tfvars("vpc_cidr_block", vpc_cidr, get_tfvars_file())
# 
#     replace_tfvars('network_range', ecs_range, get_tfvars_file())
#     replace_tfvars('jenkinsuser', "admin", get_tfvars_file())
#     replace_tfvars('jenkinspasswd', passwd_generator(), get_tfvars_file())
# 
# Path: installer/configurators/gitlab_container.py
# def configure_gitlab_container():
#     # TODO having to explicitly disable each other SCM type by name here is not elegant
#     replace_tfvars('scmbb', 'false', get_tfvars_file(), False)
#     replace_tfvars("scmgitlab", "true", get_tfvars_file(), False)
#     replace_tfvars('scm_type', 'gitlab', get_tfvars_file())
#     replace_tfvars('scm_pathext', '/', get_tfvars_file())
# 
# Path: installer/configurators/sonarqube_container.py
# def configure_sonarqube_container():
#     """
#         Configure a containerized Sonar server.
#     """
#     replace_tfvars("dockerizedSonarqube", "true", get_tfvars_file(), False)
#     replace_tfvars('sonar_username', "admin", get_tfvars_file())
#     replace_tfvars('sonar_passwd', passwd_generator(), get_tfvars_file())
#     replace_tfvars('codequality_type', 'sonarqube', get_tfvars_file())
#     replace_tfvars('codeq', 1, get_tfvars_file())
# 
# Path: installer/helpers/terraform.py
# 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()):
#         print(
#             colors.FAIL + datetime.datetime.now().strftime('%c') + '\n' + colors.ENDC)
# 
#         print(
#             colors.FAIL + 'Terraform apply failed!' + colors.ENDC)
# 
#         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()


# Path: installer/cli/scenario3cli.py
import click
from installer.cli.click_required import Required
from installer.configurators.jenkins_container import configure_jenkins_container
from installer.configurators.gitlab_container import configure_gitlab_container
from installer.configurators.sonarqube_container import configure_sonarqube_container
from installer.helpers.terraform import exec_terraform_apply



@click.command()
# Sonarqube (container)
@click.option(
    '--sonarqube/--no-sonarqube',
    default=False
)
@click.option(
    "--vpcid",
    help='Specify the ID of an existing VPC to use for ECS configuration',

    cls=Required,