Skip to content

Commit

Permalink
Make scripts PEP8 compliant (mostly)
Browse files Browse the repository at this point in the history
- Update docs to not include private members
- Make codeclimate less annoyed about minor issues
  • Loading branch information
GhostofGoes committed May 20, 2017
1 parent cb4cf87 commit 7056ed3
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 83 deletions.
4 changes: 2 additions & 2 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ engines:
config:
languages:
python:
mass_threshold: 40
mass_threshold: 50
python_version: 3
fixme:
enabled: false
Expand All @@ -14,7 +14,7 @@ engines:
config:
threshold: "C"
pep8:
enabled: false
enabled: true
markdownlint:
enabled: true
shellcheck:
Expand Down
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Documentation for vSphere is [here](https://pubs.vmware.com/vsphere-60/index.jsp

# Documentation
* Development guide: how to setup development environment, contribute, etc.
* ~~PEP8 compliance~~
* ~~Generate Sphinx docs~~
* ~~Mirror Sphinx docs to ReadTheDocs automatically~~
* Put READMEs in sub-directories with code, e.g for Vsphere, Interfaces
Expand Down
115 changes: 72 additions & 43 deletions adles/scripts/adles_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@
adles [options] (--cleanup-masters | --cleanup-enviro) [--nets] -s SPEC [-]
Options:
-n, --no-color Do not color terminal output
-v, --verbose Emit debugging logs to terminal
-c, --validate SPEC Validates syntax of an exercise specification
-t, --type TYPE Type of specification to validate: exercise, package, infra
-s, --spec SPEC Name of a YAML specification file
-i, --infra FILE Specifies the infrastructure configuration file to use
-p, --package Build environment from package specification
-m, --masters Master creation phase of specification
-d, --deploy Environment deployment phase of specification
--cleanup-masters Cleanup masters created by a specification
--cleanup-enviro Cleanup environment created by a specification
--nets Cleanup networks created during either phase
--print-spec NAME Prints the named specification: exercise, package, infrastructure
--list-examples Prints the list of examples available
--print-example NAME Prints the named example
-h, --help Shows this help
--version Prints current version
-n, --no-color Do not color terminal output
-v, --verbose Emit debugging logs to terminal
-c, --validate SPEC Validates syntax of an exercise specification
-t, --type TYPE Type of specification to validate: exercise, package, infra
-s, --spec SPEC Name of a YAML specification file
-i, --infra FILE Override infra spec with the one in FILE
-p, --package Build environment from package specification
-m, --masters Master creation phase of specification
-d, --deploy Environment deployment phase of specification
--cleanup-masters Cleanup masters created by a specification
--cleanup-enviro Cleanup environment created by a specification
--nets Cleanup networks created during either phase
--print-spec NAME Prints the named specification: exercise, package, infrastructure
--list-examples Prints the list of examples available
--print-example NAME Prints the named example
-h, --help Shows this help
--version Prints current version
Examples:
adles --list-examples
Expand Down Expand Up @@ -70,54 +70,75 @@


def main():
args = docopt(__doc__, version=__version__, help=True) # Get commandline arguments
colors = (False if args["--no-color"] else True) # Configure console coloration
setup_logging(filename='adles.log', colors=colors, console_verbose=args["--verbose"])
# Get commandline arguments
args = docopt(__doc__, version=__version__, help=True)

# Set if console output should be colored
colors = (False if args["--no-color"] else True)

# Configure logging
setup_logging(filename='adles.log',
colors=colors,
console_verbose=args["--verbose"])

if args["--spec"]: # If there's a specification
override = None
if args["--package"]: # Package specification
package_spec = check_syntax(args["--spec"], spec_type="package")
if package_spec is None: # Ensure it passed the check
exit(1)
spec_filename = package_spec["contents"]["environment"] # Extract exercise spec file
# Extract exercise spec filename
spec_filename = package_spec["contents"]["environment"]
if "infrastructure" in package_spec["contents"]:
override = package_spec["contents"]["infrastructure"] # Extract infra file
# Extract infra spec filename
override = package_spec["contents"]["infrastructure"]
else:
spec_filename = args["--spec"]
spec = check_syntax(spec_filename) # Validate specification syntax before proceeding
# Validate specification syntax before proceeding
spec = check_syntax(spec_filename)
if spec is None: # Ensure it passed the check
exit(1)
if "name" not in spec["metadata"]: # Default name is the filename of the specification
if "name" not in spec["metadata"]:
# Default name is the filename of the specification
spec["metadata"]["name"] = splitext(basename(args["--spec"]))[0]

if args["--infra"]: # Override the infra file defined in exercise/package specification
# Override the infra file defined in exercise/package specification
if args["--infra"]:
infra_file = args["--infra"]
if not exists(infra_file):
logging.error("Could not find infra file '%s' to override with", infra_file)
logging.error("Could not find infra file '%s' to override with",
infra_file)
else:
override = infra_file

if override is not None: # Override infra file in exercise config
logging.info("Overriding infrastructure config file with '%s'", override)
logging.info("Overriding infrastructure config file with '%s'",
override)
spec["metadata"]["infra-file"] = override

try: # Instantiate the interface and call the functions for the specified phase
interface = Interface(infra=parse_yaml(spec["metadata"]["infra-file"]), spec=spec)
# Instantiate the Interface and call functions for the specified phase
try:
interface = Interface(infra=parse_yaml(
spec["metadata"]["infra-file"]), spec=spec)
if args["--masters"]:
interface.create_masters()
logging.info("Finished Master creation for %s", spec["metadata"]["name"])
logging.info("Finished Master creation for %s",
spec["metadata"]["name"])
elif args["--deploy"]:
interface.deploy_environment()
logging.info("Finished deployment of %s", spec["metadata"]["name"])
logging.info("Finished deployment of %s",
spec["metadata"]["name"])
elif args["--cleanup-masters"]:
interface.cleanup_masters(args["--nets"])
logging.info("Finished master cleanup of %s", spec["metadata"]["name"])
logging.info("Finished master cleanup of %s",
spec["metadata"]["name"])
elif args["--cleanup-enviro"]:
interface.cleanup_environment(args["--nets"])
logging.info("Finished cleanup of %s", spec["metadata"]["name"])
logging.info("Finished cleanup of %s",
spec["metadata"]["name"])
else:
logging.critical("Invalid flags for --spec. Argument dump:\n%s", str(args))
logging.critical("Invalid flags for --spec. "
"Argument dump:\n%s", str(args))
except vim.fault.NoPermission as e: # Log permission errors
logging.error("Permission error: \n%s", str(e))
exit(1)
Expand All @@ -134,23 +155,29 @@ def main():
if check_syntax(args["--validate"], spec_type) is None:
logging.error("Syntax check failed")

elif args["--list-examples"] or args["--print-example"]: # Show examples on commandline
# Show examples on commandline
elif args["--list-examples"] or args["--print-example"]:
from pkg_resources import Requirement, resource_filename
from os import listdir
example_dir = resource_filename(Requirement.parse("ADLES"), "examples")
examples = [x[:-5] for x in listdir(example_dir) if ".yaml" in x] # Filter non-yaml
# Filter non-YAML files from the listdir output
examples = [x[:-5] for x in listdir(example_dir) if ".yaml" in x]
if args["--list-examples"]: # List all examples and their metadata
print("Example scenarios that can be printed using --print-example <name>")
print("Name".ljust(25) + "Version".ljust(10) + "Description") # Print header
print("Example scenarios that can be printed "
"using --print-example <name>")
# Print header for the output
print("Name".ljust(25) + "Version".ljust(10) + "Description")
for example in examples:
metadata = parse_yaml(join(example_dir, example + ".yaml"))["metadata"]
metadata = parse_yaml(
join(example_dir, example + ".yaml"))["metadata"]
name = str(example).ljust(25)
ver = str(metadata["version"]).ljust(10)
desc = str(metadata["description"])
print(name + ver + desc)
else:
example = args["--print-example"]
if example in examples: # Print out the complete content of a named example
if example in examples:
# Print out the complete content of a named example
with open(join(example_dir, example + ".yaml")) as file:
print(file.read())
else:
Expand All @@ -161,10 +188,12 @@ def main():
spec = args["--print-spec"]
specs = ["exercise", "package", "infrastructure"]

if spec in specs: # Find spec in package installation directory and print it
# This extracts the specifications from their package installation location
# Find spec in package installation directory and print it
if spec in specs:
# Extract specifications from their package installation location
filename = resource_filename(Requirement.parse("ADLES"),
join("specifications", spec + "-specification.yaml"))
join("specifications",
spec + "-specification.yaml"))
with open(filename) as file:
print(file.read())
else:
Expand Down
30 changes: 20 additions & 10 deletions adles/scripts/cleanup_vms.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def main():

if ask_question("Multiple VMs? ", default="yes"):
folder, folder_name = resolve_path(server, "folder",
"that has the VMs/folders you want to destroy")
"that has the VMs/folders "
"you want to destroy")

# Display folder structure
if ask_question("Display the folder structure? "):
Expand All @@ -65,19 +66,23 @@ def main():
vm_prefix = default_prompt("Prefix of VMs you wish to destroy"
" (CASE SENSITIVE!)", default='')
recursive = ask_question("Recursively descend into folders? ")
destroy_folders = ask_question("Destroy folders in addition to VMs? ")
destroy_folders = ask_question("Destroy folders "
"in addition to VMs? ")
if destroy_folders:
folder_prefix = default_prompt("Prefix of folders you wish to destroy"
folder_prefix = default_prompt("Prefix of folders "
"you wish to destroy"
" (CASE SENSITIVE!)", default='')
destroy_self = ask_question("Destroy the folder itself? ")
else:
folder_prefix = ''
destroy_self = False

# Show user what options they selected
logging.info("Options selected\nVM Prefix: %s\nFolder Prefix: %s\nRecursive: %s\n"
"Folder-destruction: %s\nSelf-destruction: %s", str(vm_prefix),
str(folder_prefix), recursive, destroy_folders, destroy_self)
logging.info("Options selected\nVM Prefix: %s\n"
"Folder Prefix: %s\nRecursive: %s\n"
"Folder-destruction: %s\nSelf-destruction: %s",
str(vm_prefix), str(folder_prefix), recursive,
destroy_folders, destroy_self)

# Show how many items matched the options
v, f = folder.retrieve_items(vm_prefix, folder_prefix, recursive=True)
Expand All @@ -88,20 +93,25 @@ def main():
num_folders += 1
else:
num_folders = 0
logging.info("%d VMs and %d folders match the options", num_vms, num_folders)
logging.info("%d VMs and %d folders match the options",
num_vms, num_folders)

# Confirm and destroy
if ask_question("Continue with destruction? "):
logging.info("Destroying folder '%s'...", folder_name)
folder.cleanup(vm_prefix=vm_prefix, folder_prefix=folder_prefix, recursive=recursive,
destroy_folders=destroy_folders, destroy_self=destroy_self)
folder.cleanup(vm_prefix=vm_prefix,
folder_prefix=folder_prefix,
recursive=recursive,
destroy_folders=destroy_folders,
destroy_self=destroy_self)
else:
logging.info("Destruction cancelled")
else:
vm = resolve_path(server, "vm", "to destroy")[0]

if ask_question("Display VM info? "):
logging.info(vm.get_info(detailed=True, uuids=True, snapshot=True, vnics=True))
logging.info(vm.get_info(detailed=True, uuids=True,
snapshot=True, vnics=True))

if vm.is_template(): # Warn if template
if not ask_question("VM '%s' is a Template. Continue? " % vm.name):
Expand Down
29 changes: 19 additions & 10 deletions adles/scripts/clone_vms.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@

from docopt import docopt

from adles.utils import ask_question, pad, default_prompt, script_setup, resolve_path, is_vm
from adles.utils import ask_question, pad, default_prompt, \
script_setup, resolve_path, is_vm
from adles.vsphere.vm import VM

__version__ = "0.6.1"
Expand All @@ -54,8 +55,10 @@ def main():
vm_names.append(str(input("Base name for instances to be created: ")))
# Multi-VM source
else:
folder_from, from_name = resolve_path(server, "folder", "you want to clone all VMs in")
v = [VM(vm=x) for x in folder_from.childEntity if is_vm(x)] # Get VMs in the folder
folder_from, from_name = resolve_path(server, "folder",
"you want to clone all VMs in")
# Get VMs in the folder
v = [VM(vm=x) for x in folder_from.childEntity if is_vm(x)]
vms.extend(v)
logging.info("%d VMs found in source folder %s", len(v), from_name)
if not ask_question("Keep the same names? "):
Expand All @@ -66,30 +69,36 @@ def main():
names = list(map(lambda x: x.name, v)) # Same names as sources
vm_names.extend(names)

create_in, create_in_name = resolve_path(server, "folder", "in which to create VMs")
create_in, create_in_name = resolve_path(server, "folder",
"in which to create VMs")
instance_folder_base = None
if ask_question("Do you want to create a folder for each instance? "):
instance_folder_base = str(input("Enter instance folder base name: "))

num_instances = int(input("Number of instances to be created: "))

pool_name = server.get_pool().name # Determine what will be default, so we can tell user
pool_name = default_prompt(prompt="Resource pool to assign VMs to", default=pool_name)
pool_name = server.get_pool().name # Determine what will be the default
pool_name = default_prompt(prompt="Resource pool to assign VMs to",
default=pool_name)
pool = server.get_pool(pool_name)

datastore_name = default_prompt(prompt="Datastore to put clones on")
datastore = server.get_datastore(datastore_name)

logging.info("Creating %d instances under folder %s", num_instances, create_in_name)
logging.info("Creating %d instances under folder %s",
num_instances, create_in_name)
for instance in range(num_instances):
for vm, name in zip(vms, vm_names):
if instance_folder_base: # Create instance folders for a nested clone
f = server.create_folder(instance_folder_base + pad(instance), create_in=create_in)
if instance_folder_base:
# Create instance folders for a nested clone
f = server.create_folder(instance_folder_base + pad(instance),
create_in=create_in)
vm_name = name
else:
f = create_in
vm_name = name + pad(instance) # Append instance number
new_vm = VM(name=vm_name, folder=f, resource_pool=pool, datastore=datastore)
new_vm = VM(name=vm_name, folder=f,
resource_pool=pool, datastore=datastore)
new_vm.create(template=vm.get_vim_vm())


Expand Down
3 changes: 2 additions & 1 deletion adles/scripts/vm_power.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def main():

operation = str(input("Enter the power operation you wish to perform"
" [on | off | reset | suspend]: "))
attempt_guest = ask_question("Attempt to use guest OS operations, if available? ")
attempt_guest = ask_question("Attempt to use guest OS operations, "
"if available? ")

if ask_question("Multiple VMs? ", default="yes"):
folder, folder_name = resolve_path(server, "folder", "with VMs")
Expand Down
Loading

0 comments on commit 7056ed3

Please sign in to comment.