# Path: installer/configurators/common.py
# def get_installer_root():
#     # Set the repo root path as an env var here,
#     # so subsequent scripts don't need to hardcode absolute paths.
#     return os.environ.get('JAZZ_INSTALLER_ROOT', str(Path('.').absolute()))
# 
# def get_tfvars_file():
#     return get_terraform_folder() + "terraform.tfvars"
# 
# def replace_tfvars(key, value, fileName, quoteVal=True):
#     with in_place.InPlace(fileName) as fileContent:
#         for line in fileContent:
#             if quoteVal:
#                 fileContent.write(re.sub(r'({0} = )(.*)'.format(key), r'\1"{0}"'.format(value), line))
#             else:
#                 fileContent.write(re.sub(r'({0} = )(.*)'.format(key), r'\1{0}'.format(value), line))


import subprocess
import os
import sys
from pathlib import Path
from installer.configurators.common import get_installer_root, get_tfvars_file, replace_tfvars


def check_jenkins_user(url, defaultport, usernamepw):
    """
        Check if the jenkins user is present in Jenkins server
    """
    jenkins_url = 'http://{0}:{1}'.format(url, defaultport)
    # Download the CLI jar from the jenkins server
    subprocess.call([
        'curl', '-sL', jenkins_url + '/jnlpJars/jenkins-cli.jar', '-o',
        'jenkins-cli.jar'
    ])

    # Call the server and make sure user exists
    cmd = [
        'java', '-jar', 'jenkins-cli.jar', '-s', jenkins_url, 'who-am-i',
        '--username', usernamepw[0], '--password', usernamepw[1]
    ]
    subprocess.call(
        cmd, stdout=open("output", 'w'), stderr=open("output", 'w'))

    if 'authenticated' in open('output').read():
        os.remove('output')
        return 1
    else:
        os.remove('output')
        return 0


def update_jenkins_terraform(endpoint, defaultport, userpw, sshuser, ssh_port, secgrp, subnet):

# Path: installer/configurators/common.py
# def get_installer_root():
#     # Set the repo root path as an env var here,
#     # so subsequent scripts don't need to hardcode absolute paths.
#     return os.environ.get('JAZZ_INSTALLER_ROOT', str(Path('.').absolute()))
# 
# def get_tfvars_file():
#     return get_terraform_folder() + "terraform.tfvars"
# 
# def replace_tfvars(key, value, fileName, quoteVal=True):
#     with in_place.InPlace(fileName) as fileContent:
#         for line in fileContent:
#             if quoteVal:
#                 fileContent.write(re.sub(r'({0} = )(.*)'.format(key), r'\1"{0}"'.format(value), line))
#             else:
#                 fileContent.write(re.sub(r'({0} = )(.*)'.format(key), r'\1{0}'.format(value), line))


# Path: installer/configurators/jenkins.py
import subprocess
import os
import sys
from pathlib import Path
from installer.configurators.common import get_installer_root, get_tfvars_file, replace_tfvars



def check_jenkins_user(url, defaultport, usernamepw):
    """
        Check if the jenkins user is present in Jenkins server
    """
    jenkins_url = 'http://{0}:{1}'.format(url, defaultport)
    # Download the CLI jar from the jenkins server
    subprocess.call([
        'curl', '-sL', jenkins_url + '/jnlpJars/jenkins-cli.jar', '-o',
        'jenkins-cli.jar'
    ])

    # Call the server and make sure user exists
    cmd = [
        'java', '-jar', 'jenkins-cli.jar', '-s', jenkins_url, 'who-am-i',
        '--username', usernamepw[0], '--password', usernamepw[1]
    ]
    subprocess.call(
        cmd, stdout=open("output", 'w'), stderr=open("output", 'w'))

    if 'authenticated' in open('output').read():
        os.remove('output')
        return 1
    else:
        os.remove('output')
        return 0


def update_jenkins_terraform(endpoint, defaultport, userpw, sshuser, ssh_port, secgrp, subnet):

    replace_tfvars('jenkins_elb', '{0}:{1}'.format(endpoint, defaultport), get_tfvars_file())