Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/arista-eosplus/ztpserver
Browse files Browse the repository at this point in the history
…into develop
  • Loading branch information
jerearista committed Feb 2, 2015
2 parents 6d947ed + b25481e commit d48d825
Show file tree
Hide file tree
Showing 27 changed files with 1,237 additions and 239 deletions.
37 changes: 15 additions & 22 deletions actions/add_config
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,21 @@
#pylint: disable=W0703,W0402,E1103

import os
from string import Template

TEMP_CONFIG = '/tmp/tmp-config'
TEMP_CONFIG = '/tmp/tmp-add-config'

def main(attributes):
'''Adds startup-config section.
Appends config section to startup config based on the value of the
'url' attribute.
''' Appends config section to startup-config.
This action is dual-supervisor compatible.
Accepts:
A list of attributes; use attributes.get(<ATTRIBUTE_NAME>) to read attribute values
Args:
url: path to config snippet/template
substitution_mode: loose|strict Default: loose
variables: A list of variable: value substitutions
Special_attributes: node: attributes.get('NODE') API: see documentation
Attributes:
url: path to source config/template
substitution_mode: loose|strict (default: loose)
variables: list of value substitutions
Special_attributes:
NODE: API object - see documentation for details
Example:
::
Expand All @@ -73,6 +68,7 @@ def main(attributes):
if not url:
raise Exception('Missing attribute(\'url\')')


substitution_mode = attributes.get('substitution_mode', 'loose')
if substitution_mode not in ['loose', 'strict']:
raise Exception('Invalid option specified for substitution_mode '
Expand All @@ -94,14 +90,11 @@ def main(attributes):
node.log_msg('Variables: %s' % variables)
raise Exception('Unable to perform variable substitution - '
'invalid variables')
try:
if substitution_mode == 'strict':
contents = Template(contents).substitute(variables)
else:
contents = Template(contents).safe_substitute(variables)
except KeyError:
raise Exception('Unable to perform variable substitution - '
'missing variable')

contents = node.substitute(contents, variables,
substitution_mode == 'strict')
node.append_startup_config_lines(contents.split('\n'))

# Only remove this in case everything else succeeded
# for debugging purposes
os.remove(TEMP_CONFIG)
43 changes: 20 additions & 23 deletions actions/copy_file
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,29 @@ def main(attributes):
Copies file based on the values of 'src_url' and 'dst_url'
attributes ('dst_url' should point to the destination folder).
If 'overwrite' is set to:
* 'replace': the file is copied to the switch regardless
of whether there is already a file with the same name at the
destination;
* 'if-missing': the file is copied to the switch only if
there is not already a file with the same name at the
destination; if there is, then the action is a no-op;
* 'backup': the file is copied to the switch; if there is
already another file at the destination, that file is renamed
by appending the '.backup' suffix
If 'overwrite' is not set, then 'replace' is the default
behaviour.
This action is NOT dual-supervisor compatible.
Args:
attributes: list of attributes; use attributes.get(<ATTRIBUTE_NAME>)
to read attribute values
node (internernal): attributes.get('NODE') API: see documentation
attributes:
src_url: Source location
dst_url: Destination
mode: Octal mode
overwrite: Overwrite existing files? <replace|if-missing|backup> Default: replace
Attributes:
src_url: path to source file
dst_url: path to destination
mode: octal mode for destination path
overwrite: replace|if-missing|backup (default: replace)
'overwrite' values:
* 'replace': the file is copied to the switch regardless
of whether there is already a file with the same name at the
destination;
* 'if-missing': the file is copied to the switch only if
there is not already a file with the same name at the
destination; if there is, then the action is a no-op;
* 'backup': the file is copied to the switch; if there is
already another file at the destination, that file is renamed
by appending the '.backup' suffix
Special_attributes:
NODE: API object - see documentation for details
Example:
::
Expand Down
14 changes: 6 additions & 8 deletions actions/install_cli_plugin
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,15 @@ PERSISTENT_PLUGIN_DIR = '/mnt/flash/.ztp-plugins'
import os

def main(attributes):
'''Installs EOS CliPlugin.
Installs CliPlugin based on the value of the
'url' attribute.
'''Installs CliPlugin.
This action is NOT dual-supervisor compatible.
Args:
attributes (list): list of attributes; use attributes.get(<ATTRIBUTE_NAME>) to read attribute values
node (internal): attributes.get('NODE') API: see documentation
url: path to the cli plugin
Attributes:
url: path to the CliPlugin
Special_attributes:
NODE: API object - see documentation for details
Example:
::
Expand Down
16 changes: 8 additions & 8 deletions actions/install_extension
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ import ast
import os

def main(attributes):
'''Installs EOS extension.
'''Installs extension.
Installs extension based on the value of the
'url' attribute. If 'force' is
If 'force' is
set, then the dependency checks are overridden.
This action is NOT dual-supervisor compatible.
Args:
attributes (list): list of attributes; use attributes.get(<ATTRIBUTE_NAME>) to read attribute values
node (internal): attributes.get('NODE') API: see documentation
url: path to the rpm or swix extension
force: Force installation regardless of checks. Default: False
Attributes:
url: path to source extension file
force: ignore validation errors (default: false)
Special_attributes:
NODE: API object - see documentation for details
Example:
::
Expand Down
13 changes: 7 additions & 6 deletions actions/install_image
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
BOOT_CONFIG = '/mnt/flash/boot-config'

def main(attributes):
'''Installs software image on the switch.
'''Installs new software image.
If the current software image is the same as the 'version'
attribute value, then this action is a no-op. Otherwise, the
Expand All @@ -43,11 +43,12 @@ def main(attributes):
This action is dual-supervisor compatible.
Args:
attributes (list): list of attributes; use attributes.get(<ATTRIBUTE_NAME>) to read attribute values
node (internal): attributes.get('NODE') API: see documentation
url: path to .swi file
version: target version of the .swi file.
Attributes:
url: path to source image file
version: EOS version of new image file
Special_attributes:
NODE: API object - see documentation for details
Example:
::
Expand Down
23 changes: 15 additions & 8 deletions actions/replace_config
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,24 @@
#pylint: disable=W0703,E1103

def main(attributes):
'''Replaces stratup-config on the switch.
Replaces/adds /mnt/flash/startup-config based on the value of the
'url' attribute.
'''Replaces /mnt/flash/startup-config with new file.
This action is dual-supervisor compatible.
Args:
attributes (list): list of attributes; use attributes.get(<ATTRIBUTE_NAME>) to read attribute values
node (internal): attributes.get('NODE') API: see documentation
url: path to config/template
Attributes:
url: path to source config/template
Special_attributes:
NODE: API object - see documentation for details
Example:
::
-
action: replace_config
attributes:
url: files/configs/tor-startup-config
name: "tor config"
'''

node = attributes.get('NODE')
Expand Down
103 changes: 103 additions & 0 deletions actions/run_bash_script
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/usr/bin/env python
#
# Copyright (c) 2014, Arista Networks, Inc.
# All rights reserved.
#
# 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 Arista 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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "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 ARISTA NETWORKS
# 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.

#pylint: disable=W0703,E1103

import os

TEMP_FILE = '/tmp/tmp-run-script'

def main(attributes):
''' Runs a script in EOS from bash.
This action is dual-supervisor compatible.
Attributes:
url: path to source script/template
variables (optional):
list of value substitutions (for a script template)
Special_attributes:
NODE: API object - see documentation for details
Example:
::
-
action: run_bash_script
attributes:
url: files/templates/ma1.template
variables:
version: 1.2.3
name: "install temp package"
'''

node = attributes.get('NODE')
url = attributes.get('url')

if not url:
raise Exception('Missing attribute(\'url\')')


try:
node.retrieve_url(url, TEMP_FILE)
except Exception as exc:
raise Exception('Unable to retrieve config from URL (%s)' %
exc)

exec_file = open(TEMP_FILE, 'r')
contents = exec_file.read()
exec_file.close()

variables = attributes.get('variables')
if not variables:
variables = {}

if not isinstance(variables, dict):
node.log_msg('Variables: %s' % variables)
raise Exception('Unable to perform variable substitution - '
'invalid variables')

contents = node.substitute(contents, variables)

exec_file = open(TEMP_FILE, 'w')
exec_file.write(contents)
exec_file.close()

os.chmod(TEMP_FILE, 0777)
(cmd, code, out, err) = node.bash_cmds([TEMP_FILE])

if code or err:
raise Exception('Running %s in bash failed '
'return_code=%s, stdout=%s, stderr=%s)' %
(cmd, code, out, err))

# Only remove this in case everything else succeeded
# for debugging purposes
os.remove(TEMP_FILE)

0 comments on commit d48d825

Please sign in to comment.