Package: pycons3rt3
pycons3rt3Python3 integration for CONS3RTFeaturesLogging frameworkPython3 SDK for the CONS3RT APIGather CONS3RT deployment info and propertiesRun Linux commands from pythonConfigure networkingIntegrate with AWS S3 and EC2Nexus Artifact Repository downloadsPost to SlackInstallationInstall from pipIf you have Python 3.5+ installed, you can run one of the following:pip3 install pycons3rt3python3 -m pip install pycons3rt3Install from sourcegit clone https://github.com/cons3rt/pycons3rt3
cd pycons3rt3
pip install -r cfg/requirements.txt
python setup.py installDirectoriesWhen installed, pycons3rt determines and creates a system directory (pycons3rt_system_home) and
a local user directory (pycons3rt_user_home).The default locations forpycons3rt_system_homeare here:Linux:~/.cons3rtMacOS:~/.cons3rtWindows:C:\cons3rtThe default locations forpycons3rt_user_homeis:~/.cons3rton all OS types.PYCONS3RT_HOME environment variableTo change the location ofpycons3rt_system_homeandpycons3rt_user_home, set the environment variablePYCONS3RT_HOMEto an existing directory.  This directory will be used for both system and user home directories.To do this on Linux or macOS, add the following to~/.bash_profile:export PYCONS3RT_HOME="/path/to/desired/directory"On Windows, add thePYCONS3RT_HOMEenvironment variable to the system settings.The following directories are also created:Config:pycons3rt_system_home/conf/Logs:pycons3rt_user_home/log/Source:pycons3rt_user_home/src/If installed from source via asset, the asset clones the pycons3rt source code here for installation:pycons3rt_user_home/src/pycons3rtThe logging configuration file is installed here:pycons3rt_system_home/conf/pycons3rt-logging.confBy default, pycons3rt log files will output here:pycons3rt_user_home/log/pycons3rt-info.logpycons3rt_user_home/log/pycons3rt-warn.logpycons3rt_user_home/log/pycons3rt-debug.logCONS3RT ReST API and CLIpycons3rt3 provides a python3 SDK for using the CONS3RT ReST API.To access the ReST API you will need:An active CONS3RT AccountAccess to a projectA CONS3RT project-based ReST API tokenFor sites that require Client Certificate Authentication you will need a valid PKI certificate (ECA)pycons3rt3 doesnotsupport CAC authentication at this timeIf you have an ECA certificate in p12 or pfx format, convert it to a password-less pem file:openssl pkcs12 -in certfile.pfx -out certfile.pem -nodesConfigurationTo configure pycons3rt3 for the CONS3RT ReST API type:cons3rt configAfter entering your info, a config file is created here:pycons3rt_user_home/conf/config.jsonThis configuration will automatically load for ReST API calls.asset CLIThe asset CLI command helps you automatically create and import assets:# Validate an asset structure
asset validate --asset_dir=/path/to/asset

# Create a valid asset zip file
asset create --asset_dir=/path/to/assetCreates an asset zip fileAssetName.zipin your Downloads directorySpecify the destination directory:asset create --asset_dir=/path/to/asset --dest_dir=/path/to/directoryCreates an asset zip fileAssetName.zipin the specified directoryImport an asset into CONS3RT:asset import --asset_dir=/path/to/assetCreates an asset zip, and imports the zip file into CONS3RTAdds anasset.ymlfile to the asset directory with the site info and asset IDImport an asset and set the visibility to the project-level:asset import --asset_dir=/path/to/asset --visibility=OWNING_PROJECTUpdate an existing asset in CONS3RT:asset update --asset_dir=/path/to/assetUses the asset ID in the asset.yml fileCreates an asset zip, and updates the asset IDUpdate an asset and set the visibility to the community-level:asset update --asset_dir=/path/to/asset --visibility=COMMUNITYQuery for software assets:asset query --asset_type=softwareQuery for the latest community container asset containing "nginx":asset query --asset_type=containers --asset_subtype=DOCKER --expanded --community --name nginx --latestUse thequeryidscommand to query for just the latest asset ID for asset with name "nginx":asset queryids --asset_type=containers --latest --name=nginxcons3rt CLIUsage:cons3rt <command> <subcommands> <args>Interactive command to create aconfig.jsonfile:cons3rt configThis creates a config file at the default location:~/.cons3rt/conf/config.jsonIfPYCONS3RT_HOMEenvironment variable is defined, config file location:${PYCONS3RT_HOME}/conf/config.jsonSpecify a config file location:cons3rt --config /path/to/config.json <command> <subcommands> <args>cons3rt cloud CLIPermissions:Team ManagerroleThe--id=1or--ids=1,2,3args indicate cloud IDs# Delete a cloud
cons3rt cloud delete --id=1

# List clouds
cons3rt cloud list

# Retrieve cloud details
cons3rt cloud retrieve --id=1

# Share a template from the cloud template provider to all other cloudspaces in the cloud
cons3rt cloud template --id=1 --share --name 'cons3rt-redhat-8'

# Share all templates from the cloud template provider to all other cloudspaces in the cloud
cons3rt cloud template --id=1 --share --all

# List deployment runs in the cloud(s)
cons3rt cloud run list --id=1 
cons3rt cloud run list --ids=1,2,3,4,5

# list deployment run hosts in the cloud(s)
cons3rt cloud run hosts --id=1 
cons3rt cloud run hosts --ids=1,2,3,4,5

# Added CLI command to list GPU usage in the cloud(s)
cons3rt cloud run gpus --id=1
cons3rt cloud run gpus --ids=1,2,3,4,5

# Use the `--load` flag to load deployment run host details saved from prior commands, this works in these commands:
cons3rt cloud run hosts --id=1 --load
cons3rt cloud run gpus --id=1 --loadcons3rt cloudspace CLIPermissions:Team ManagerroleThe--id=1or--ids=1,2,3args can be used to indicate which cloudspace IDs.# Release active runs from multiple cloudspaces:
cons3rt cloudspace --release_active_runs --ids=123,124

# Delete inactive runs from your cloudspace
cons3rt cloudspace --delete_inactive_runs --id=123

# Deallocate a cloudspace
cons3rt cloudspace deallocate --id 123

# List cloudspaces
cons3rt cloudspace list

# List active runs in a cloudspace:
cons3rt cloudspace --list_active_runs --id=123

# Delete a specific template
cons3rt cloudspace template delete --id=1 --name 'cons3rt-redhat-8'

# Delete all templates in a cloudspace
cons3rt cloudspace template delete --id=1 --all

# List templates in a cloudspace
cons3rt cloudspace template list --id=1

# Register template in a cloudspace
cons3rt cloudspace template register --id=1 --name 'cons3rt-redhat-8'

# Register multiple templates
cons3rt cloudspace template register --id=1 --names 'cons3rt-redhat-8,cons3rt-redhat-9'

# Register all unregistered templates
cons3rt cloudspace template register --all

# Retrieve cloudpace details
cons3rt cloudspace retrieve --id=16

# Share a specific template from one cloudspace to another
cons3rt cloudspace template share --provider_id=1 --ids=2,3,4,5,6 --name 'cons3rt-redhat-8'

# Share multiple templates from one cloudspace to another
cons3rt cloudspace template share --provider_id=1 --ids=2,3,4,5,6 --names 'cons3rt-redhat-8,cons3rt-redhat-9'

# Share all templates from one cloudspace to another
cons3rt cloudspace template share --provider_id=1 --ids=2,3,4,5,6 --all

# Unregister a cloudspace
cons3rt cloudspace unregister --id 123cons3rt user CLIPermissions:Site Adminrole# Get a list of active users
cons3rt user list --state=ACTIVEcons3rt team CLIPermissions:Team ManagerroleThe--id=1or--ids=1,2,3args can be used to indicate which team IDs.# Get a list of teams (site admins only)
cons3rt team list

# Get a count of ACTIVE users using various collabd tools like Jira
cons3rt team collabtools users --id=2

# Get a unique list of ACTIVE users in collab tools projects for a team or list of teams
cons3rt team collabtools users --ids=2,5 --unique

# Get a unique list of ACTIVE + BLOCKED users in collab tools projects for a team or list of teams
cons3rt team collabtools users --ids=2,5 --unique --blocked

# Get a list of active team members, printed by project
cons3rt team members list --id=11

# Get a list of active and blocked team members, printed by project
cons3rt team members list --id=11 --blocked

# Get a unique list of team members for a list of teams
cons3rt team members list --ids=1,2,3,4,5 --unique

# Get a unique list of team members managed by a particular user
cons3rt team members list --username=USERNAME --unique

# Get a list of active team managers
cons3rt team managers

# Get a list of team managers for specific teams
cons3rt team managers --ids=2,5

# Get a list of teams managed by a specific user
cons3rt team managers --username=johndoe

# Get a CSV report of runs in the team
cons3rt team report runs --id=3

# Get a CSV asset report for the team
cons3rt team report assets --id=3

# List runs in team-owned projects
cons3rt team run list --id=3

# Create snapshots for all team runs
cons3rt team run snapshot create --id=3

# Restore snapshots for all team runs
cons3rt team run snapshot restore --id=3

# Delete snapshots for all team runs
cons3rt team run snapshot delete --id=3

# Skip run IDs for any of the snapshot commands with --skip
cons3rt team run snapshot create --id=3 --skip=12345,12346,12347cons3rt project CLIPermissions:Team ManagerroleProject OwnerorProject ManagerroleThe--id=1or--ids=1,2,3args can be used to indicate which project IDs.# List projects
cons3rt project list

# Print project details
cons3rt project get --id=3

# List project members
cons3rt project members list --id=3

# List runs in projects
cons3rt project run list --ids=3,6,9

# Create snapshots for all project runs
cons3rt project run snapshot create --id=3

# Restore snapshots for all project runs
cons3rt project run snapshot restore --id=3

# Delete snapshots for all project runs
cons3rt project run snapshot delete --id=3

# Skip run IDs for any of the snapshot commands with --skip
cons3rt project run snapshot create --id=3 --skip=12345,12346,12347

# Power runs off/on in the project
cons3rt project run on --id=3
cons3rt project run off --id=3

# Delete runs in projects
cons3rt project run delete --ids=3,4,5

# Release runs in projects, note this will prompt for confirmation due to the destructive nature
cons3rt project run release --id=3ractl command -- Controls and queries remote access across the CONS3RT sitePermissions:Site Adminrole# Create a csv file with remote access info for a specific cloudspace
ractl print cloudspace --id=116

# Create a csv file with remote access info for the CONS3RT site
ractl print siteUse pycons3rt3 in pythonLogifyWith the default configuration log files go to:pycons3rt_user_home/log/, and INFO
level is printed to stdout.  To customize pycons3rt logging, modify the
logging configuration file.Logging example:import logging
from pycons3rt3.logify import Logify

mod_logger = Logify.get_name() + '.your_module'
    
    # Then use in a function or class:
    
    class MyClass(object):
        def __init__(self, dep=None):
            self.cls_logger = mod_logger + '.MyCLass'
    	def class_method(self):
    		log = logging.getLogger(self.cls_logger + '.class_method')
    		log.info('Class Method Logging')
    
    def main():
        log = logging.getLogger(mod_logger + '.main')
        log.debug('DEBUG')
    	log.info('INFO')
    	log.warn('WARN')
    	log.error('ERROR')DeploymentThis module provides a set of useful utilities for accessing
deployment related info on deployment run hosts.from pycons3rt3.deployment import Deployment
    
# Create a new Deployment object
dep = new Deployment()
    
# Deployment name
print(dep.deployment_name)
    
# Get the role name
print(dep.cons3rt_role_name)
    
# Deployment properties
print(dep.properties)
    
# Get a specific deployment property value by name
my_value = dep.get_value('cons3rt.user')
    
# Scenario network info
print(dep.scenario_network_info)

# ASSET_DIR
print(dep.asset_dir)SlackThis module provides an interface for posting anything to Slack!from pycons3rt3.slack import SlackMessage
from pycons3rt3.slack import SlackAttachments

# Create a message
slack_msg = SlackMessage(
                my_webhook_url, 
                channel='#DevOps', 
                icon_url='http://cool-icon-url',
                text='This is a Slack message',
                user='@sender_username')

# Create and add an attachment
slack_attachment = SlackAttachment(
                       fallback='This is the fallback text', 
                       color='green', 
                       pretext='Pretext', 
                       text='Moar text!')

slack_msg.add_attachment(slack_attachment)

# Send a message
slack_msg.send()NexusThis module provides simple method of fetching artifacts from a nexus
repository.from pycons3rt3 import nexus

nexus.get_artifact(
    username=nexus_username,
    password=nexus_password,
    suppress_status=True,
    nexus_url=nexus_url,
    timeout_sec=45,
    overwrite=False,
    group_id='com.cons3rt',
    artifact_id='cons3rt-backend-install',
    version=`18.11.1`,
    packaging='zip',
    classifier='package-otto',
    destination_dir=dest_dir)Bash (Linux only)Executes commands on a Linux system.  See the source code for specific available
commands but the most commonly usedrun_commandis shown below.run_command(command, timeout_sec=3600.0, output=True)Parameterscommand: List containing the command and any additional argstimeout_sec: (optional) Float specifying how long to wait before
terminating the command.  Default is 3600.0.output: (boolean) True collects the output of the command.  In some cases
suppressing the command output improves stability.Returns:A dictionary containing "code", the numeric exit code from the command, and
"output" which captures the stdout/strerrif output was setTrue. Sample output:Raises:CommandErrorwhen there is a problem running the command.{
    "code": "0",
    "output": "stdout/stderr from the command"
}Example Usage:from pycons3rt3.bash import run_command
from pycons3rt3.bash import CommandError
command = ['ls', '/root']
try:
    result = run_command(command, timeout_sec=60.0)
    code = result['code']
    output = result['output']
except CommandError:
    raise
if code == 0:
    log.info('Successfully executed command {c}'.format(s=command))
else:
    msg = 'There was a problem running command returned code {c} and produced output: {o}'.format(
                    c=code, o=output)
            log.error(msg)
            raise CommandError(msg)More to come....