Skip to content

Commit

Permalink
- adding logging in order to have better debug info
Browse files Browse the repository at this point in the history
- adding a docker example
  • Loading branch information
ltamaster committed Apr 17, 2019
1 parent 3a53b23 commit 75cdd3a
Show file tree
Hide file tree
Showing 20 changed files with 445 additions and 23 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,7 @@ If the result is Restricted, please run the following command:
```
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
```


## Docker example
Check these [instructions](docker/README.md) for docker test
40 changes: 40 additions & 0 deletions contents/colored_formatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import logging

BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)

RESET_SEQ = "\033[0m"
COLOR_SEQ = "\033[1;%dm"
BOLD_SEQ = "\033[1m"

def formatter_message(message, use_color = True):
if use_color:
message = message.replace("$RESET", RESET_SEQ).replace("$BOLD", BOLD_SEQ)
else:
message = message.replace("$RESET", "").replace("$BOLD", "")
return message

COLORS = {
'WARNING': YELLOW,
'INFO': WHITE,
'DEBUG': BLUE,
'CRITICAL': YELLOW,
'ERROR': RED
}


def format():
FORMAT = "[%(levelname)-18s] %(message)s ($BOLD%(filename)s$RESET:%(lineno)d)[$BOLD%(name)s$RESET]"
return formatter_message(FORMAT, True)


class ColoredFormatter(logging.Formatter):
def __init__(self, msg, use_color = True):
logging.Formatter.__init__(self, msg)
self.use_color = use_color

def format(self, record):
levelname = record.levelname
if self.use_color and levelname in COLORS:
levelname_color = COLOR_SEQ % (30 + COLORS[levelname]) + levelname + RESET_SEQ
record.levelname = levelname_color
return logging.Formatter.format(self, record)
41 changes: 29 additions & 12 deletions contents/winrm-check.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@
import argparse
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
import logging
import colored_formatter
from colored_formatter import ColoredFormatter

log_level = 'INFO'
if os.environ.get('RD_JOB_LOGLEVEL') == 'DEBUG':
log_level = 'DEBUG'
else:
log_level = 'ERROR'

console = logging.StreamHandler()
console.setFormatter(ColoredFormatter(colored_formatter.format()))
console.stream=sys.stdout

log = logging.getLogger()
log.addHandler(console)
log.setLevel(log_level)

parser = argparse.ArgumentParser(description='Run Bolt command.')
parser.add_argument('--username', help='the username')
Expand All @@ -18,7 +35,7 @@
parser.add_argument('--port', help='port',default="5985")
parser.add_argument('--nossl', help='nossl',default="False")
parser.add_argument('--diabletls12', help='diabletls12',default="False")
parser.add_argument('--debug', help='nossl',default="False")
parser.add_argument('--debug', help='debug',default="False")
parser.add_argument('--certpath', help='certpath')

args = parser.parse_args()
Expand Down Expand Up @@ -85,17 +102,17 @@

endpoint=transport+'://'+hostname+':'+port

if(debug):
print("------------------------------------------")
print("endpoint:" +endpoint)
print("authentication:" +authentication)
print("username:" +username)
print("nossl:" + str(nossl))
print("diabletls12:" + str(diabletls12))
print("transport:" + transport)
if(certpath):
print("certpath:" + certpath)
print("------------------------------------------")
log.debug("------------------------------------------")
log.debug("endpoint:" + endpoint)
log.debug("authentication:" + authentication)
log.debug("username:" + username)
log.debug("nossl:" + str(nossl))
log.debug("transport:" + str(transport))
log.debug("diabletls12:" + str(diabletls12))
if(certpath):
log.debug("certpath:" + certpath)
log.debug("------------------------------------------")


arguments={}
arguments["transport"] = authentication
Expand Down
39 changes: 29 additions & 10 deletions contents/winrm-exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,24 @@
import threading
import traceback
import winrm

import logging
import colored_formatter
from colored_formatter import ColoredFormatter
requests.packages.urllib3.disable_warnings()

log_level = 'INFO'
if os.environ.get('RD_JOB_LOGLEVEL') == 'DEBUG':
log_level = 'DEBUG'
else:
log_level = 'ERROR'

console = logging.StreamHandler()
console.setFormatter(ColoredFormatter(colored_formatter.format()))
console.stream=sys.stdout
log = logging.getLogger()
log.addHandler(console)
log.setLevel(log_level)

parser = argparse.ArgumentParser(description='Run Bolt command.')
parser.add_argument('hostname', help='the hostname')
args = parser.parse_args()
Expand Down Expand Up @@ -62,33 +77,37 @@
endpoint=transport+'://'+args.hostname+':'+port

if "RD_OPTION_USERNAME" in os.environ and os.getenv("RD_OPTION_USERNAME"):
log.debug('Using option.username: %s' % os.environ['RD_OPTION_USERNAME'])
#take user from job
username = os.getenv("RD_OPTION_USERNAME").strip('\'')
else:
# take user from node
if "RD_NODE_USERNAME" in os.environ and os.getenv("RD_NODE_USERNAME"):
log.debug('Using username from node definition: %s' % os.environ['RD_NODE_USERNAME'])
username = os.getenv("RD_NODE_USERNAME").strip('\'')
else:
# take user from project
if "RD_CONFIG_USERNAME" in os.environ and os.getenv("RD_CONFIG_USERNAME"):
log.debug('Using username from project definition: %s' % os.environ['RD_CONFIG_USERNAME'])
username = os.getenv("RD_CONFIG_USERNAME").strip('\'')

if "RD_OPTION_WINRMPASSWORD" in os.environ and os.getenv("RD_OPTION_WINRMPASSWORD"):
log.debug('Using option.winrmpassword')
#take password from job
password = os.getenv("RD_OPTION_WINRMPASSWORD").strip('\'')
else:
if "RD_CONFIG_PASSWORD_STORAGE_PATH" in os.environ:
log.debug('Using password from node')
password = os.getenv("RD_CONFIG_PASSWORD_STORAGE_PATH")

if debug:
print("------------------------------------------")
print("endpoint:" + endpoint)
print("authentication:" + authentication)
print("username:" + username)
print("nossl:" + str(nossl))
print("diabletls12:" + str(diabletls12))
print("shell:" + shell)
print("------------------------------------------")
log.debug("------------------------------------------")
log.debug("endpoint:" + endpoint)
log.debug("authentication:" + authentication)
log.debug("username:" + username)
log.debug("nossl:" + str(nossl))
log.debug("diabletls12:" + str(diabletls12))
log.debug("shell:" + shell)
log.debug("------------------------------------------")

arguments = {}
arguments["transport"] = authentication
Expand Down
15 changes: 15 additions & 0 deletions contents/winrm-filecopier.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,22 @@
import logging
import ntpath
import xml.etree.ElementTree as ET
import colored_formatter
from colored_formatter import ColoredFormatter

log_level = 'INFO'
if os.environ.get('RD_JOB_LOGLEVEL') == 'DEBUG':
log_level = 'DEBUG'
else:
log_level = 'ERROR'

console = logging.StreamHandler()
console.setFormatter(ColoredFormatter(colored_formatter.format()))
console.stream=sys.stdout

log = logging.getLogger()
log.addHandler(console)
log.setLevel(log_level)

def _clean_error_msg(self, msg):
"""converts a Powershell CLIXML message to a more human readable string
Expand Down
1 change: 0 additions & 1 deletion contents/winrm_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,5 @@ def get_response(self):
self.stat = response.status_code

except Exception as e:
print(e)
self.e_std = e
self.stat=-1
25 changes: 25 additions & 0 deletions docker/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
all:

#env vars
DOCKER_COMPOSE_SPEC="docker-compose.yml"
build:
set -e
# re-build docker env
docker-compose -f $(DOCKER_COMPOSE_SPEC) build

start:
# run docker
docker-compose -f $(DOCKER_COMPOSE_SPEC) up -d

clean:
# clean up docker env
docker-compose -f $(DOCKER_COMPOSE_SPEC) down --volumes --remove-orphans

build_and_start:
set -e
# re-build docker env
docker-compose -f $(DOCKER_COMPOSE_SPEC) build
# clean up docker env
docker-compose -f $(DOCKER_COMPOSE_SPEC) down --volumes --remove-orphans
# run docker
docker-compose -f $(DOCKER_COMPOSE_SPEC) up -d
34 changes: 34 additions & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Rundeck image with winrm python plugin

This is an official Rundeck OSS image for testing winrm plugin.
It has a default project example

## Set python version
On docker-compose.yml set python version you want to try (`PYTHON_VERSION`)

## Set windows resources model
On `docker/resources` put the Rundeck resource files (xml or yaml) with the windows nodes.
Also, you will need to add the password key storage to connect to the nodes.

Rundeck will be available on `http://localhost:4441`

![instructions](images/nodes-example.png)

## Build process

Use `start-docker-example.sh` to build and copy the plugin to the docker image

## Make commands available

```
make build
```

```
make start
```

```
make clean
```

30 changes: 30 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: '3'

services:
rundeck:
hostname: rundeck
build:
context: rundeck
args:
PYTHON_VERSION: "3.7.3"
environment:
- RUNDECK_GRAILS_URL=http://localhost:4441
ports:
- "4441:4440"
volumes:
- ${PWD}/resources:/home/rundeck/resources
rundeck-ops:
build:
context: rundeck-ops
links:
- rundeck
environment:
- RUNDECK_URL=http://localhost:4441
- RUNDECK_NODE_URL=http://rundeck:4440
- RUNDECK_USER=admin
- RUNDECK_PASSWORD=admin
# import projects
- CONFIG_SCRIPT_POSTSTART=scripts/import_project.sh
#- PROJECTS_LIST=WINRM-DEMO
volumes:
- ${PWD}/projects:/projects
Binary file added docker/images/nodes-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
45 changes: 45 additions & 0 deletions docker/rundeck-ops/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
FROM ubuntu:16.04

## General package configuration
RUN apt-get -y update && \
apt-get -y install \
sudo \
unzip \
zip \
curl \
xmlstarlet \
netcat-traditional \
software-properties-common \
debconf-utils \
ncurses-bin \
iputils-ping \
net-tools \
apt-transport-https \
git

## Install Java
RUN \
add-apt-repository -y ppa:openjdk-r/ppa && \
apt-get update && \
apt-get install -y openjdk-8-jdk


# add GPG key
RUN curl "https://bintray.com/user/downloadSubjectPublicKey?username=bintray" > /tmp/bintray.gpg.key
RUN apt-key add - < /tmp/bintray.gpg.key

#Install Rundeck CLI tool
RUN echo "deb https://dl.bintray.com/rundeck/rundeck-deb /" | sudo tee -a /etc/apt/sources.list
RUN curl "https://bintray.com/user/downloadSubjectPublicKey?username=bintray" > /tmp/bintray.gpg.key
RUN apt-key add - < /tmp/bintray.gpg.key
RUN apt-get -y install apt-transport-https
RUN apt-get -y update
RUN apt-get -y install rundeck-cli


RUN mkdir -p scripts data
COPY scripts scripts

RUN chmod -R a+x scripts/*

CMD scripts/run.sh
39 changes: 39 additions & 0 deletions docker/rundeck-ops/scripts/import_project.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

#trap 'echo "rd fail" ; exit 0' EXIT
set -eu

echo "Sleeping 10s ..."
sleep 10

shopt -s nullglob
ARCHIVES=(/projects/*.{jar,zip,groovy})
shopt -u nullglob

echo "There are ${#ARCHIVES[*]} project archives to import"

#default rundeck user password for key storage
echo "rundeck" > tmp.password

for file in "${ARCHIVES[@]}"
do
echo "Importing archive file $file ..."

PROJECT_NAME=$(unzip -p "$file" META-INF/MANIFEST.MF|awk '/Rundeck-Archive-Project-Name:/ {print $2}' | tr -d '\r' )
if ! rd projects info -p "$PROJECT_NAME" >/dev/null 2>&1
then
echo "Creating project: $PROJECT_NAME ..."
if ! rd projects create -p "$PROJECT_NAME"
then
rc=$?
echo >&2 "WARN: Failed to create project $PROJECT_NAME"
exit $rc
fi
else
echo >&2 "WARN: Project already exists: '$PROJECT_NAME' (skipping)"
fi

rd projects archives import -f "$file" -p "$PROJECT_NAME" --remove --noExecutions --include-config --include-acl

done

Loading

0 comments on commit 75cdd3a

Please sign in to comment.