#!/usr/bin/env python
# Copyright 2015-2018 Yelp Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Usage: ./setup_tron_namespace.py service [service...] | --all

Deploy a namespace to the local Tron master from a service configuration file.
Reads from the soa_dir /nail/etc/services by default.

The script will load the service configuration file, generate a Tron configuration
file for it, and send the updated file to Tron.
"""
import argparse
import logging
import sys

from paasta_tools import tron_tools

log = logging.getLogger(__name__)


def parse_args():
    parser = argparse.ArgumentParser(
        description='Update the Tron namespace configuration for a service.',
    )
    parser.add_argument(
        'services',
        nargs='*',
        help='Services to update.',
    )
    parser.add_argument(
        '-a',
        '--all',
        dest='all_namepsaces',
        action='store_true',
        help='Update all available Tron namespaces.',
    )
    parser.add_argument(
        '-d',
        '--soa-dir',
        dest="soa_dir",
        metavar="SOA_DIR",
        default=tron_tools.DEFAULT_SOA_DIR,
        help="Use a different soa config directory",
    )
    parser.add_argument(
        '-v',
        '--verbose',
        action='store_true',
        default=False,
    )
    args = parser.parse_args()
    return args


def main():
    args = parse_args()

    if args.verbose:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.WARNING)

        if args.all_namepsaces:
            try:
                services = tron_tools.get_tron_namespaces_for_cluster()
            except Exception as e:
                log.error('Failed to list tron namespaces: {error}'.format(
                    error=str(e),
                ))
                sys.exit(1)
        else:
            services = args.services

        if not services:
            log.warning("No namespaces found")
            sys.exit(0)

        client = tron_tools.get_tron_client()

        updated = []
        failed = []

        for service in services:
            try:
                new_config = tron_tools.create_complete_config(
                    service=args.service,
                    soa_dir=args.soa_dir,
                )
                client.update_namespace(args.service, new_config)
                updated.append(service)
            except Exception as e:
                log.error('Update for {namespace} failed: {error}'.format(
                    namespace=args.service, error=str(e),
                ))
                failed.append(service)

        log.info(
            'Updated following namespaces: {updated}, failed: {failed}'.format(
                updated=updated,
                failed=failed,
            ),
        )

        sys.exit(1 if failed else 0)


if __name__ == "__main__":
    main()

@@ -2015,20 +2015,16 @@
 RNING)%0A%0A
-    
     if a
@@ -2039,20 +2039,16 @@
 name
-p
 s
+p
 aces:%0A
-    
     
@@ -2060,36 +2060,32 @@
 ry:%0A            
-    
 services = tron_
@@ -2124,36 +2124,32 @@
 uster()%0A        
-    
 except Exception
@@ -2159,36 +2159,32 @@
  e:%0A            
- 
-   
 log.error('Faile
@@ -2243,20 +2243,16 @@
         
-    
 error=st
@@ -2249,36 +2249,32 @@
   error=str(e),%0A
-    
             ))%0A 
@@ -2276,36 +2276,32 @@
  ))%0A            
-    
 sys.exit(1)%0A    
@@ -2300,30 +2300,22 @@
 (1)%0A    
-   
- 
 else:%0A
-    
         
@@ -2340,20 +2340,16 @@
 rvices%0A%0A
-    
     if n
@@ -2365,28 +2365,24 @@
 es:%0A        
-    
 log.warning(
@@ -2404,36 +2404,32 @@
 found%22)%0A        
-    
 sys.exit(0)%0A%0A   
@@ -2425,28 +2425,24 @@
 xit(0)%0A%0A    
- 
-   
 client = tro
@@ -2468,20 +2468,16 @@
 ient()%0A%0A
-    
     upda
@@ -2485,28 +2485,24 @@
 ed = %5B%5D%0A    
-  
-  
 failed = %5B%5D%0A
@@ -2502,20 +2502,16 @@
 d = %5B%5D%0A%0A
-    
     for 
@@ -2535,37 +2535,29 @@
 es:%0A        
-   
- 
 try:%0A
-    
             
@@ -2616,28 +2616,24 @@
             
-    
 service=args
@@ -2658,20 +2658,16 @@
         
- 
-   
 soa_dir=
@@ -2692,24 +2692,18 @@
         
-    )%0A  
+)%0A
         
@@ -2706,18 +2706,16 @@
         
-  
 client.u
@@ -2764,28 +2764,24 @@
             
-    
 updated.appe
@@ -2796,28 +2796,24 @@
 ce)%0A        
-   
- 
 except Excep
@@ -2815,36 +2815,32 @@
 Exception as e:%0A
-    
             log.
@@ -2886,36 +2886,32 @@
 error%7D'.format(%0A
-    
                 
@@ -2956,35 +2956,27 @@
             
-    ))%0A    
+))%0A
             
@@ -2999,20 +2999,16 @@
 rvice)%0A%0A
-    
     log.
@@ -3013,20 +3013,16 @@
 g.info(%0A
-    
         
@@ -3097,28 +3097,24 @@
             
-  
-  
 updated=upda
@@ -3126,28 +3126,24 @@
             
-    
 failed=faile
@@ -3157,30 +3157,18 @@
     
-   
- 
 ),%0A
-    
     )%0A%0A
-    
     

fix indentation