Skip to content

Commit

Permalink
integrate logging lib - debug into rover
Browse files Browse the repository at this point in the history
  • Loading branch information
hattan committed Jun 21, 2021
1 parent 9529fa2 commit 3c17763
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 48 deletions.
1 change: 1 addition & 0 deletions docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Usage: rover <command> <switches>
-ct | --ci-task-name <task name> (ci workflow) CI Tool to invoke. If omitted all tools are run, if provided only that tool is run.
-b | --base-dir (ci workflow) Base directory for paths in symphony.yml.
-d | --debug Show debug (verbose) logs
| --log-degree <degree> This is the desired log degree. It can be set to FATAL,ERROR, WARN, INFO, DEBUG or VERBOSE
-lz | --landingzone Path to a landing zone
-a | --action Terraform action to perform (plan, apply , destroy)
-c | --cloud Name of the Azure Cloud to use (AzurePublic, AzureUSGovernment, AzureChinaCloud, AzureGermanCloud) or specific AzureStack name.
Expand Down
22 changes: 11 additions & 11 deletions scripts/cd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -164,32 +164,32 @@ function execute_cd {
tf_command=$(echo $PARAMS | sed -e 's/^[ \t]*//')


debug @"Starting Deployment"
debug " landingzone_name: $landingzone_name"
debug " TF_VAR_tf_name: $TF_VAR_tf_name"
debug " TF_VAR_tf_plan: $TF_VAR_tf_plan"
debug " TF_VAR_level: $TF_VAR_level"
debug " tf_command: $tf_command"
debug " TF_VAR_workspace: $TF_VAR_workspace"
debug " integration_test_absolute_path: $integration_test_absolute_path"
log_debug @"Starting Deployment"
log_debug " landingzone_name: $landingzone_name"
log_debug " TF_VAR_tf_name: $TF_VAR_tf_name"
log_debug " TF_VAR_tf_plan: $TF_VAR_tf_plan"
log_debug " TF_VAR_level: $TF_VAR_level"
log_debug " tf_command: $tf_command"
log_debug " TF_VAR_workspace: $TF_VAR_workspace"
log_debug " integration_test_absolute_path: $integration_test_absolute_path"

case "${action}" in
run)
export tf_action="apply"
debug " tf_action: $tf_action"
log_debug " tf_action: $tf_action"

deploy "${TF_VAR_workspace}"
set_autorest_environment_variables
run_integration_tests "$integration_test_absolute_path"
;;
plan)
export tf_action="plan"
debug " tf_action: $tf_action"
log_debug " tf_action: $tf_action"
deploy "${TF_VAR_workspace}"
;;
apply)
export tf_action="apply"
debug " tf_action: $tf_action"
log_debug " tf_action: $tf_action"
deploy "${TF_VAR_workspace}"
;;
test)
Expand Down
6 changes: 0 additions & 6 deletions scripts/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ error_message() {
printf >&2 "\e[91m$@\n\e[0m"
}

debug() {
local message=$1
if [ "$debug_mode" == "true" ]; then
echo "$message"
fi
}

information() {
printf "\e[36m$@\n\e[0m"
Expand Down
32 changes: 21 additions & 11 deletions scripts/lib/logger.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env bash



__log_init__() {

# Set Time zone to UTC / Comment out to use local time
Expand All @@ -17,16 +15,25 @@ __log_init__() {

# hash to map loggers to their log levels
# the default logger "default" has INFO as its default log level
_loggers_level_map["default"]=3 # the log level for the default logger is INFO
_loggers_level_map["default"]=1 # the log level for the default logger is INFO

#------------------------------------------------------------------------------
# make sure log directory exists in standard log folder
#------------------------------------------------------------------------------

mkdir -p ~/caflogs/logs/
if [ -z "$log_folder_path" ]; then
error "0" "Log folder path is not set" 1
fi

__create_dir__ "$log_folder_path"

}

__create_dir__() {
local path=$1
mkdir -p $path
}

__set_text_log__() {

# output to file standard error to file
Expand All @@ -36,9 +43,9 @@ __set_text_log__() {
}

#------------------------------------------------------------------------------
# set_log_level
# set_log_degree
#------------------------------------------------------------------------------
set_log_level() {
set_log_degree() {
local logger=default in_level l
[[ $1 = "-l" ]] && { logger=$2; shift 2 2>/dev/null; }
in_level="${1:-INFO}"
Expand All @@ -64,16 +71,18 @@ set_log_level() {
_log() {
local in_level=$1; shift
local logger=default log_level_set log_level
[[ $1 = "-l" ]] && { logger=$2; shift 2; }
# [[ $1 = "-l" ]] && { logger=$2; shift 2; }

log_level="${_log_levels[$in_level]}"
log_level_set="${_loggers_level_map[$logger]}"

if [[ $log_level_set ]]; then
((log_level_set >= log_level)) && {
if [ "$log_level_set" -ge "$log_level" ]; then
printf '%(%Y-%m-%dT%H:%M:%S %Z)T %-7s %s ' -1 "[$in_level]" "[${BASH_SOURCE[2]}:${BASH_LINENO[1]}]"
printf '%s\n' "$@"
}
else
printf '%(%Y-%m-%dT%H:%M:%S %Z)T %-7s %s ' -1 [WARN] "[${BASH_SOURCE[2]}:${BASH_LINENO[1]}] Unknown logger '$logger'"
fi
else
printf '%(%Y-%m-%dT%H:%M:%S %Z)T %-7s %s ' -1 [WARN] "[${BASH_SOURCE[2]}:${BASH_LINENO[1]}] Unknown logger '$logger'"
fi
}

Expand All @@ -97,3 +106,4 @@ log_verbose_enter() { _log VERBOSE "Entering function ${FUNCNAME[1]}"; }
log_info_leave() { _log INFO "Leaving function ${FUNCNAME[1]}"; }
log_debug_leave() { _log DEBUG "Leaving function ${FUNCNAME[1]}"; }
log_verbose_leave() { _log VERBOSE "Leaving function ${FUNCNAME[1]}"; }

13 changes: 13 additions & 0 deletions scripts/rover.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# deploy a landingzone with
# rover -lz [landingzone_folder_name] -a [plan | apply | destroy] [parameters]

source /tf/rover/lib/logger.sh
source /tf/rover/clone.sh
source /tf/rover/walkthrough.sh
source /tf/rover/tfstate_azurerm.sh
Expand Down Expand Up @@ -32,12 +33,14 @@ export skip_permission_check=${skip_permission_check:=false}
export symphony_run_all_tasks=true
export debug_mode=${debug_mode:="false"}
export devops=${devops:="false"}
export log_folder_path=${log_folderpath:="~/caflogs/logs"}

unset PARAMS

current_path=$(pwd)

mkdir -p ${TF_PLUGIN_CACHE_DIR}
__log_init__

while (( "$#" )); do
case "${1}" in
Expand All @@ -56,14 +59,23 @@ while (( "$#" )); do
export TF_VAR_tf_name=${TF_VAR_tf_name:="$(basename ${landingzone_name}).tfstate"}
shift 2
;;
-lp|--log-path)
export log_folder_path=${2}
shift 2
;;
-c|--cloud)
export cloud_name=${2}
shift 2
;;
-d|--debug)
export debug_mode="true"
set_log_degree DEBUG
shift 1
;;
--log-degree)
set_log_degree $2
shift 2
;;
-a|--action)
export tf_action=${2}
shift 2
Expand Down Expand Up @@ -206,6 +218,7 @@ trap 'error ${LINENO}' ERR 1 2 3 6

tf_command=$(echo $PARAMS | sed -e 's/^[ \t]*//')


verify_azure_session
process_target_subscription

Expand Down
18 changes: 9 additions & 9 deletions scripts/task.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ function task_print_debug {
local task_flags=$6
local task_parameters=$7

debug ""
debug " Running task : $task_executable"
debug " sub command : $task_sub_command"
debug " task init required : $task_requires_init"
debug " landing zone folder : $landing_zone_path"
debug " config folder : $config_path"
debug " flags : $task_flags"
debug " parameters : $(format_task_parameters "$task_parameters")"
debug " var files : $PARAMS"
log_debug ""
log_debug " Running task : $task_executable"
log_debug " sub command : $task_sub_command"
log_debug " task init required : $task_requires_init"
log_debug " landing zone folder : $landing_zone_path"
log_debug " config folder : $config_path"
log_debug " flags : $task_flags"
log_debug " parameters : $(format_task_parameters "$task_parameters")"
log_debug " var files : $PARAMS"
}

function run_task {
Expand Down
16 changes: 8 additions & 8 deletions scripts/test_runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ function run_integration_tests {
export ENVIRONMENT=$TF_VAR_environment
export ARM_SUBSCRIPTION_ID=$(echo ${account} | jq -r .id)

debug " Test Directory : $target_directory"
debug " Environment : $ENVIRONMENT"
debug " Subscription Id : $ARM_SUBSCRIPTION_ID"
debug " STATE_FILE_PATH : $STATE_FILE_PATH"
debug " STATE_FILE : $targetStateFile"
debug " Level : $TF_VAR_level"
debug " Prefix : $PREFIX"
log_debug " Test Directory : $target_directory"
log_debug " Environment : $ENVIRONMENT"
log_debug " Subscription Id : $ARM_SUBSCRIPTION_ID"
log_debug " STATE_FILE_PATH : $STATE_FILE_PATH"
log_debug " STATE_FILE : $targetStateFile"
log_debug " Level : $TF_VAR_level"
log_debug " Prefix : $PREFIX"

pushd $target_directory > /dev/null
go test -v -tags $TF_VAR_level
popd > /dev/null

debug "Removing $targetStateFile"
log_debug "Removing $targetStateFile"
rm $targetStateFile
}

Expand Down
1 change: 1 addition & 0 deletions spec/unit/cd/execute_cd_spec.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Describe 'cd.sh'
Include scripts/cd.sh
Include scripts/lib/logger.sh
Include scripts/functions.sh

Describe "execute_cd"
Expand Down
1 change: 1 addition & 0 deletions spec/unit/cd/join_path_spec.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Describe 'cd.sh'
Include scripts/cd.sh
Include scripts/lib/logger.sh
Include scripts/functions.sh

Describe "join_path"
Expand Down
1 change: 1 addition & 0 deletions spec/unit/cd/set_autorest_environment_variables_spec.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Describe 'cd.sh'
Include scripts/cd.sh
Include scripts/lib/logger.sh
Include scripts/functions.sh

Describe "set_autorest_environment_variables"
Expand Down
7 changes: 4 additions & 3 deletions spec/unit/cd/verify_cd_parameters_spec.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Describe 'cd.sh'
Include scripts/cd.sh
Include scripts/lib/logger.sh
Include scripts/functions.sh

Describe "verify_cd_parameters"
Expand Down Expand Up @@ -77,7 +78,7 @@ Describe 'cd.sh'
End
End

Context "rover cd -h & valid Symphony Yaml Provided"
Context "rover deploy -h & valid Symphony Yaml Provided"
setup() {
export symphony_yaml_file="spec/harness/symphony.yml"
export cd_action="-h"
Expand All @@ -88,7 +89,7 @@ Describe 'cd.sh'
When call verify_cd_parameters
The output should include '@Verifying cd parameters'
The error should include 'Usage:'
The error should include 'rover cd <action> <flags>'
The error should include 'rover deploy <action> <flags>'
The status should eq 0
End
End
Expand All @@ -105,7 +106,7 @@ Describe 'cd.sh'
When call verify_cd_parameters
The output should include '@Verifying cd parameters'
The error should include 'Usage:'
The error should include 'rover cd <action> <flags>'
The error should include 'rover deploy <action> <flags>'
The status should eq 0
End
End
Expand Down
44 changes: 44 additions & 0 deletions spec/unit/logger/logger_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Describe 'logger.sh'
Include scripts/functions.sh
Include scripts/lib/logger.sh

Describe "__log_init__"
#Function Mocks
export TEST_DEBUG_CREATE_DIR=true
error() {
local parent_lineno="$1"
local message="$2"
local code="$3"
>&2 echo "Error line:${parent_lineno}: message:${message} status :${code}"
export TEST_DEBUG_CREATE_DIR=false
return ${code}
}
__create_dir__ (){
if [ "$TEST_DEBUG_CREATE_DIR" == "true" ]; then
echo "creating directory $1"
fi
}

Context "Log Path Not Set"
It 'should throw an error and not create directory'
When call __log_init__
The error should include 'Error line:0: message:Log folder path is not set status :1'
End
End

Context "Log Path is Set"
setup() {
export log_folder_path="tmp/$(uuidgen)"
}
BeforeEach 'setup'


It 'should throw an error and not create directory'
When call __log_init__
The error should eq ""
The output should include "creating directory $log_folder_path"
End
End

End
End

0 comments on commit 3c17763

Please sign in to comment.