- 
                Notifications
    You must be signed in to change notification settings 
- Fork 168
new scp file #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Closed
      
      
    
  
     Closed
                    new scp file #177
Changes from all commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      c55d11f
              
                new_scp_file
              
              
                renetbo eca3a0d
              
                scp_file
              
              
                renetbo ed213c2
              
                Update to handle get and push
              
              
                renetbo a9fc440
              
                Update junos_scp_file
              
              
                renetbo 51fb38e
              
                updated junos_scp_file with md5, put , get
              
              
                renetbo 01ab7e7
              
                junos_scp_file update
              
              
                renetbo File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,293 @@ | ||
| #!/usr/bin/env python | ||
|  | ||
|  | ||
| # Copyright (c) 1999-2016, Juniper Networks Inc. | ||
| # | ||
| # All rights reserved. | ||
| # | ||
| # License: Apache 2.0 | ||
| # | ||
| # Redistribution and use in source and binary forms, with or without | ||
| # modification, are permitted provided that the following conditions are met: | ||
| # | ||
| # * Redistributions of source code must retain the above copyright | ||
| # notice, this list of conditions and the following disclaimer. | ||
| # | ||
| # * Redistributions in binary form must reproduce the above copyright | ||
| # notice, this list of conditions and the following disclaimer in the | ||
| # documentation and/or other materials provided with the distribution. | ||
| # | ||
| # * Neither the name of the Juniper Networks nor the | ||
| # names of its contributors may be used to endorse or promote products | ||
| # derived from this software without specific prior written permission. | ||
| # | ||
| # THIS SOFTWARE IS PROVIDED BY Juniper Networks, Inc. ''AS IS'' AND ANY | ||
| # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| # DISCLAIMED. IN NO EVENT SHALL Juniper Networks, Inc. BE LIABLE FOR ANY | ||
| # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
| # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
| # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|  | ||
| # <******************* | ||
| # | ||
| # Copyright 2016 Juniper Networks, Inc. All rights reserved. | ||
| # Licensed under the Juniper Networks Script Software License (the "License"). | ||
| # You may not use this script file except in compliance with the License, which is located at | ||
| # http://www.juniper.net/support/legal/scriptlicense/ | ||
| # Unless required by applicable law or otherwise agreed to in writing by the parties, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # | ||
| # *******************> | ||
|  | ||
|  | ||
| DOCUMENTATION = ''' | ||
| --- | ||
| module: junos_scp_file | ||
| author: Boris Renet, Juniper Networks from sw.py | ||
| version_added: "1.1.0" | ||
| short_description: scp a file to from a device. | ||
| description: | ||
| - scp a file to/from local directory to/from remote directory. | ||
| requirements: | ||
| - py-junos-eznc >= 1.2.3 | ||
| options: | ||
| host: | ||
| description: | ||
| - Set to {{ inventory_hostname }} | ||
| required: true | ||
| user: | ||
| description: | ||
| - Login username | ||
| required: false | ||
| default: $USER | ||
| passwd: | ||
| description: | ||
| - Login password | ||
| required: false | ||
| default: assumes ssh-key active | ||
| file: | ||
| description: | ||
| - Name of the file to scp | ||
| required: true | ||
| type: | ||
| description: | ||
| - put or get depending on the direction of the scp | ||
| local_dir: | ||
| description: | ||
| - directory (without the last /) on the source of the scp | ||
| remote_dir: | ||
| description: | ||
| - directory (without the last /) on the destination of the scp | ||
| logfile: | ||
| description: | ||
| - Path on the local server where the progress status is logged | ||
| for debugging purposes | ||
| required: false | ||
| default: None | ||
| ''' | ||
|  | ||
| EXAMPLES = ''' | ||
| Copy a file from the source to the destination: | ||
| - junos_scp_file: | ||
| host={{ inventory_hostname }} | ||
| user={{ ansible_user }} | ||
| passwd={{ ansible_ssh_pass }} | ||
| local_dir="/var/tmp/junos" | ||
| remote_dir="/var/tmp" | ||
| type="put" | ||
| file={{ OS_satellite_package }} | ||
| logfile={{ log }} | ||
|  | ||
| Copy a file from a destination to a source: | ||
| - junos_scp_file: | ||
| host={{ inventory_hostname }} | ||
| user={{ ansible_user }} | ||
| passwd={{ ansible_ssh_pass }} | ||
| local_dir="/var/tmp" | ||
| remote_dir="/var/log" | ||
| type="get" | ||
| file="messages" | ||
| logfile={{ log }} | ||
|  | ||
| ''' | ||
| import logging | ||
| import re | ||
| import os | ||
| import hashlib | ||
| from distutils.version import LooseVersion | ||
| from ansible.module_utils.basic import * | ||
|  | ||
|  | ||
| def _hashfile(afile, hasher, blocksize=65536): | ||
| buf = afile.read(blocksize) | ||
| while len(buf) > 0: | ||
| hasher.update(buf) | ||
| buf = afile.read(blocksize) | ||
| return hasher.hexdigest() | ||
|  | ||
| def local_md5(package,type): | ||
| """ | ||
| Computes the MD5 checksum value on the local package file. | ||
|  | ||
| :param str package: | ||
| File-path to the package (\*.tgz) file on the local server | ||
|  | ||
| :returns: MD5 checksum (str) | ||
| :raises IOError: when **package** file does not exist | ||
| """ | ||
| try: | ||
| checksum=_hashfile(open(package, 'rb'), hashlib.md5()) | ||
| except Exception as err: | ||
| logging.error("unable to get rpc due to:{0}".format(err)) | ||
| if (("No such file" in format(err)) and (type=="get")): | ||
| checksum="no_file" | ||
| else: | ||
| raise err | ||
| return checksum | ||
| logging.info("rpc reponse recvd") | ||
| return checksum | ||
|  | ||
| def remote_md5(file, dev, type): | ||
| try: | ||
| rpc_reply=dev.rpc.get_checksum_information(path=file) | ||
| checksum=rpc_reply.findtext('.//checksum').strip() | ||
| except Exception as err: | ||
| logging.error("unable to get rpc due to:{0}".format(err.message)) | ||
| if (("No such file or directory" in err.message) and (type=="put")): | ||
| checksum="no_file" | ||
| else: | ||
| raise err | ||
| return checksum | ||
| logging.info("rpc reponse recvd") | ||
| return checksum | ||
|  | ||
| try: | ||
| from jnpr.junos import Device | ||
| from jnpr.junos.version import VERSION | ||
| if not LooseVersion(VERSION) >= LooseVersion('1.2.3'): | ||
| HAS_PYEZ = False | ||
| else: | ||
| HAS_PYEZ = True | ||
| except ImportError: | ||
| HAS_PYEZ = False | ||
|  | ||
| from jnpr.junos import Device | ||
| from jnpr.junos.utils.scp import SCP | ||
| from lxml import etree | ||
| def main(): | ||
| module = AnsibleModule( | ||
| argument_spec=dict( | ||
| host=dict(required=True), | ||
| file=dict(required=True), | ||
| local_dir=dict(required=True), | ||
| remote_dir=dict(required=True), | ||
| user=dict(required=False, default=os.getenv('USER')), | ||
| passwd=dict(required=False, default=None), | ||
| type=dict(required=True, default=None), | ||
| logfile=dict(required=False, default=None), | ||
| ), | ||
| supports_check_mode=True | ||
| ) | ||
|  | ||
| if not HAS_PYEZ: | ||
| module.fail_json(msg='junos-eznc >= 1.2.3 is required for this module') | ||
|  | ||
| args = module.params | ||
|  | ||
| # ------------------------------------------------------------------------- | ||
| # logging | ||
| # ------------------------------------------------------------------------- | ||
|  | ||
| logfile = args['logfile'] | ||
| if logfile is not None: | ||
| logging.basicConfig(filename=logfile, level=logging.INFO, | ||
| format='%(asctime)s:%(name)s:%(message)s') | ||
| logging.getLogger().name = args['host'] | ||
|  | ||
| def do_log(msg, level='info'): | ||
| getattr(logging, level)(msg) | ||
| else: | ||
| def do_log(msg): | ||
| pass | ||
|  | ||
|  | ||
| dev = Device(args['host'], user=args['user'], password=args['passwd']) | ||
|  | ||
| do_log("Starting the file transfer: {0}".format(args['file'])) | ||
|  | ||
| def update_my_progress(dev, report): | ||
| do_log(report) | ||
|  | ||
|  | ||
| try: | ||
| dev.open() | ||
| remote_path=args['remote_dir'] | ||
| type=args['type'] | ||
| local_file=args['local_dir']+"/"+args['file'] | ||
| remote_file=args['remote_dir']+"/"+args['file'] | ||
| if (type == "put"): | ||
| do_log('computing local MD5 checksum on: %s' % local_file) | ||
| local_checksum = local_md5(local_file,type) | ||
| do_log('Local checksum: %s' % local_checksum) | ||
| remote_checksum = remote_md5(remote_file,dev,type) | ||
| if ((remote_checksum == "no_file") or (remote_checksum != local_checksum)): | ||
| with SCP(dev,progress=update_my_progress) as scp1: | ||
| scp1.put(local_file,remote_path) | ||
| # validate checksum: | ||
| do_log('computing remote MD5 checksum on: %s' % remote_file) | ||
| remote_checksum = remote_md5(remote_file,dev,type) | ||
| do_log('Remote checksum: %s' % remote_checksum) | ||
| if remote_checksum != local_checksum: | ||
| do_log("checksum check failed.") | ||
| status="Transfer failed (different MD5 between source and destination)" | ||
| module.fail_json(msg=status) | ||
| return False | ||
| else: | ||
| do_log("checksum check passed.") | ||
| status="File pushed OK" | ||
| else: | ||
| do_log("File already present, skipping the scp") | ||
| status="File already present, skipping the scp" | ||
|  | ||
| if (type == "get"): | ||
| do_log('computing remote MD5 checksum on: %s' % remote_file) | ||
| remote_checksum = remote_md5(remote_file,dev,type) | ||
| do_log('Remote checksum: %s' % remote_checksum) | ||
| local_checksum = local_md5(local_file,type) | ||
| if ((local_checksum == "no_file") or (remote_checksum != local_checksum)): | ||
| with SCP(dev,progress=update_my_progress) as scp1: | ||
| scp1.get(remote_file,local_file) | ||
| # validate checksum: | ||
| do_log('computing local MD5 checksum on: %s' % local_file) | ||
| local_checksum = local_md5(local_file,type) | ||
| do_log('Local checksum: %s' % local_checksum) | ||
| if remote_checksum != local_checksum: | ||
| do_log("checksum check failed.") | ||
| status="Transfer failed (different MD5 between source and destination)" | ||
| module.fail_json(msg=status) | ||
| return False | ||
| else: | ||
| do_log("checksum check passed.") | ||
| status="File retrieved OK" | ||
| else: | ||
| do_log("File already present, skipping the scp") | ||
| status="File already present, skipping the scp" | ||
|  | ||
| except Exception as err: | ||
| if (type=="put"): | ||
| msg = 'unable to scp the file to {0}: {1}'.format(args['host'], str(err)) | ||
| else: | ||
| msg = 'unable to scp the file from {0}: {1}'.format(args['host'], str(err)) | ||
| module.fail_json(msg=msg) | ||
| return | ||
| else: | ||
| dev.close() | ||
|  | ||
| module.exit_json(msg=status) | ||
|  | ||
| if __name__ == "__main__": | ||
| main() | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don;t think you can have 2 licenses for the same file :
apache 2.0andJuniper Networks Script Software License