#!/usr/bin/env python

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# Copyright (c) 2017 Mozilla Corporation
#
# Contributors:
# Brandon Myers bmyers@mozilla.com

from git import Repo, cmd

import sys
import os
from configlib import getConfig, OptionParser

sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../lib'))
from utilities.logger import logger, initLogger


def download_generic_alerts(repo_url, save_location, deploy_key):
    git_obj = cmd.Git(save_location)
    git_ssh_cmd = 'ssh -i %s' % deploy_key

    with git_obj.custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
        if not os.path.isdir(save_location):
            logger.debug("Cloning " + str(repo_url) + " into " + str(save_location))
            Repo.clone_from(url=repo_url, to_path=save_location, branch='master')
        else:
            logger.debug("Updating " + str(save_location))
            git_obj.pull()


def main():
    logger.debug('Starting')
    logger.debug(options)
    download_generic_alerts(options.alert_repo_url, options.alert_data_location, options.deploy_key_location)


def initConfig():
    # output our log to stdout or syslog
    options.output = getConfig('output', 'stdout', options.configfile)
    options.sysloghostname = getConfig('sysloghostname', 'localhost', options.configfile)
    options.syslogport = getConfig('syslogport', 514, options.configfile)

    options.alert_repo_url = getConfig('alert_repo_url', '', options.configfile)
    options.alert_data_location = getConfig('alert_data_location', '', options.configfile)

    options.deploy_key_location = getConfig('deployment_key_location', '', options.configfile)


if __name__ == '__main__':
    parser = OptionParser()
    parser.add_option(
        "-c",
        dest='configfile',
        default=sys.argv[0].replace('.py', '.conf'),
        help="configuration file to use")
    (options, args) = parser.parse_args()
    initConfig()
    initLogger(options)
    main()

@@ -694,21 +694,16 @@
     
-with 
 git_obj.
 cust
@@ -702,14 +702,14 @@
 obj.
-custom
+update
 _env
@@ -745,22 +745,18 @@
 ssh_cmd)
-:%0A    
+%0A%0A
     if n
@@ -788,20 +788,16 @@
 ation):%0A
-    
         
@@ -877,20 +877,16 @@
         
- 
-   
 Repo.clo
@@ -897,12 +897,8 @@
 rom(
-url=
 repo
@@ -907,16 +907,8 @@
 rl, 
-to_path=
 save
@@ -922,43 +922,56 @@
 on, 
-branch='master')%0A   
+env=%7B'GIT_SSH_COMMAND': git_ssh_cmd%7D)%0A
     
- 
 else:%0A
-    
     
@@ -1017,28 +1017,24 @@
 _location))%0A
-    
         git_

Modify update_generic_alerts to use ssh key