-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of https://github.com/Teradata/stacki into fea…
…ture/test_sles_patches
- Loading branch information
Showing
5 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,50 @@ | ||
# @copyright@ | ||
# Copyright (c) 2006 - 2020 Teradata | ||
# All rights reserved. Stacki(r) v5.x stacki.com | ||
# https://github.com/Teradata/stacki/blob/master/LICENSE.txt | ||
# @copyright@ | ||
|
||
import json | ||
import subprocess | ||
|
||
|
||
class StackCommandError(Exception): | ||
"""Exception raised for errors running a stack command.""" | ||
|
||
def __init__(self, message): | ||
self.message = message | ||
|
||
|
||
def run_stack_command(command, args=None): | ||
"""Runs a stack command, returning StackCommandError if there is an error.""" | ||
|
||
# Create our command to run | ||
command_args = ["/opt/stack/bin/stack"] | ||
command_args.extend(command.replace(".", " ").split()) | ||
if args: | ||
command_args.extend(args) | ||
command_args.append("output-format=json") | ||
|
||
try: | ||
# Run the command, throwing a CalledProcessError if something goes wrong | ||
result = subprocess.run( | ||
command_args, capture_output=True, text=True, check=True | ||
) | ||
except subprocess.CalledProcessError as e: | ||
# Get the first line of the stderr | ||
if e.stderr: | ||
message = e.stderr.splitlines()[0] | ||
else: | ||
message = "error - unknown" | ||
|
||
# Raise an exception for the caller | ||
raise StackCommandError(message) | ||
|
||
# Return the result as a dict | ||
try: | ||
data = json.loads(result.stdout) | ||
except json.JSONDecodeError: | ||
# Something invalid was returned, likely an empty string | ||
data = [] | ||
|
||
return data |
Empty file.
This file contains 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