Skip to content
This repository has been archived by the owner on Feb 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #73 from oVirt/convert_python_scripts_to_python3
Browse files Browse the repository at this point in the history
Convert python scripts to python3

Ignoring travis yamllint
  • Loading branch information
shenitzky authored Jul 22, 2019
2 parents 745b254 + 44f5ab3 commit 54f4bed
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 215 deletions.
80 changes: 39 additions & 41 deletions files/fail_back.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#!/usr/bin/python
from bcolors import bcolors
from ConfigParser import SafeConfigParser
try:
from ConfigParser import SafeConfigParser
except ModuleNotFoundError:
from configparser import SafeConfigParser
import logging
import os.path
import shlex
import subprocess
from subprocess import call
import sys
import time

from subprocess import call
from six.moves import input

from bcolors import bcolors


INFO = bcolors.OKGREEN
INPUT = bcolors.OKGREEN
Expand Down Expand Up @@ -36,12 +41,12 @@ def run(self, conf_file, log_file, log_level):
"vault: %s \n"
"ansible_play: %s \n"
"report log: /tmp/%s \n"
% (target_host,
, target_host,
source_map,
var_file,
vault,
ansible_play,
report))
report)

cmd = []
cmd.append("ansible-playbook")
Expand Down Expand Up @@ -76,34 +81,34 @@ def run(self, conf_file, log_file, log_level):
cmd_fb.append("-vvv")

# Setting vault password
vault_pass = raw_input(
vault_pass = input(
INPUT + PREFIX + "Please enter vault password (In case of "
"plain text please press ENTER): " + END)
os.system("export vault_password=\"" + vault_pass + "\"")
log.info("Starting cleanup process of setup %s"
" for oVirt ansible disaster recovery" % target_host)
" for oVirt ansible disaster recovery", target_host)
print("\n%s%sStarting cleanup process of setup '%s'"
" for oVirt ansible disaster recovery%s"
% (INFO,
PREFIX,
target_host,
END))
log.info("Executing cleanup command: %s" % ' '.join(map(str, cmd)))
log.info("Executing cleanup command: %s", ' '.join(map(str, cmd)))
if log_file is not None and log_file != '':
self._log_to_file(log_file, cmd)
else:
self._log_to_console(cmd, log)

log.info("Finished cleanup of setup %s"
" for oVirt ansible disaster recovery" % source_map)
" for oVirt ansible disaster recovery", source_map)
print("\n%s%sFinished cleanup of setup '%s'"
" for oVirt ansible disaster recovery%s"
% (INFO,
PREFIX,
source_map,
END))

log.info("Start failback DR from setup '%s'" % target_host)
log.info("Start failback DR from setup '%s'", target_host)
print("\n%s%sStarting fail-back process to setup '%s'"
" from setup '%s' for oVirt ansible disaster recovery%s"
% (INFO,
Expand All @@ -112,8 +117,8 @@ def run(self, conf_file, log_file, log_level):
source_map,
END))

log.info("Executing failback command: %s"
% ' '.join(map(str, cmd_fb)))
log.info("Executing failback command: %s",
' '.join(map(str, cmd_fb)))
if log_file is not None and log_file != '':
self._log_to_file(log_file, cmd_fb)
else:
Expand All @@ -130,7 +135,8 @@ def _log_to_file(self, log_file, cmd):
with open(log_file, "a") as f:
proc = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stderr=subprocess.PIPE,
universal_newlines=True)
for line in iter(proc.stdout.readline, ''):
if 'TASK [' in line:
print("\n%s%s%s\n" % (INFO,
Expand All @@ -148,7 +154,8 @@ def _log_to_file(self, log_file, cmd):
def _log_to_console(self, cmd, log):
proc = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stderr=subprocess.PIPE,
universal_newlines=True)
for line in iter(proc.stdout.readline, ''):
if "[Failback Replication Sync]" in line:
print("%s%s%s" % (INFO, line, END))
Expand All @@ -162,7 +169,7 @@ def _handle_result(self, subprocess, cmd):
try:
proc = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
# do something with output
except subprocess.CalledProcessError, e:
except subprocess.CalledProcessError as e:
print("%sException: %s\n\n"
"failback operation failed, please check log file for "
"further details.%s"
Expand Down Expand Up @@ -223,41 +230,32 @@ def _init_vars(self, conf_file):
_SECTION,
ansible_play=None))
while target_host not in setups:
target_host = raw_input(
target_host = input(
INPUT + PREFIX + "The target setup was not defined. "
"Please provide the setup which it is failback to "
"(primary or secondary): " + END)
while source_map not in setups:
source_map = raw_input(
source_map = input(
INPUT + PREFIX + "The source mapping was not defined. "
"Please provide the source mapping "
"(primary or secondary): " + END)
while not os.path.isfile(var_file):
var_file = raw_input("%s%svar file mapping '%s' does not exist. "
"Please provide a valid mapping var file: %s"
% (INPUT,
PREFIX,
var_file,
END))
var_file = input("%s%svar file mapping '%s' does not exist. "
"Please provide a valid mapping var file: %s"
% (INPUT, PREFIX, var_file, END))
while not os.path.isfile(vault):
vault = raw_input("%s%spassword file '%s' does not exist."
" Please provide a valid password file:%s "
% (INPUT,
PREFIX,
vault,
END))
vault = input("%s%spassword file '%s' does not exist."
" Please provide a valid password file:%s "
% (INPUT, PREFIX, vault, END))
while (not ansible_play) or (not os.path.isfile(ansible_play)):
ansible_play = raw_input("%s%sansible play '%s' "
"is not initialized. "
"Please provide the ansible play file "
"to generate the mapping var file "
"with ('%s'):%s "
% (INPUT,
PREFIX,
str(ansible_play),
PLAY_DEF,
END) or PLAY_DEF)
return (target_host, source_map, var_file, vault, ansible_play)
ansible_play = input("%s%sansible play '%s' "
"is not initialized. "
"Please provide the ansible play file "
"to generate the mapping var file "
"with ('%s'):%s "
% (INPUT, PREFIX, str(ansible_play),
PLAY_DEF, END) or PLAY_DEF)
return target_host, source_map, var_file, vault, ansible_play

def _set_log(self, log_file, log_level):
logger = logging.getLogger(PREFIX)
Expand Down
78 changes: 38 additions & 40 deletions files/fail_over.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#!/usr/bin/python
from bcolors import bcolors
from ConfigParser import SafeConfigParser
try:
from ConfigParser import SafeConfigParser
except ModuleNotFoundError:
from configparser import SafeConfigParser
import logging
import os.path
import shlex
import subprocess
from subprocess import call
import sys
import time

from subprocess import call
from six.moves import input

from bcolors import bcolors


INFO = bcolors.OKGREEN
INPUT = bcolors.OKGREEN
Expand All @@ -35,12 +40,12 @@ def run(self, conf_file, log_file, log_level):
"vault: %s \n"
"ansible_play: %s \n"
"report file: /tmp/%s "
% (target_host,
source_map,
var_file,
vault,
ansible_play,
report))
, target_host,
source_map,
var_file,
vault,
ansible_play,
report)

cmd = []
cmd.append("ansible-playbook")
Expand All @@ -58,10 +63,10 @@ def run(self, conf_file, log_file, log_level):
cmd.append("--vault-password-file")
cmd.append("vault_secret.sh")
cmd.append("-vvv")
vault_pass = raw_input(
vault_pass = input(
INPUT + PREFIX + "Please enter the vault password: " + END)
os.system("export vault_password=\"" + vault_pass + "\"")
log.info("Executing failover command: %s" % ' '.join(map(str, cmd)))
log.info("Executing failover command: %s", ' '.join(map(str, cmd)))
if log_file is not None and log_file != '':
self._log_to_file(log_file, cmd)
else:
Expand All @@ -77,7 +82,8 @@ def _log_to_file(self, log_file, cmd):
with open(log_file, "a") as f:
proc = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stderr=subprocess.PIPE,
universal_newlines=True)
for line in iter(proc.stdout.readline, ''):
if 'TASK [' in line:
print("\n%s%s%s\n" % (INFO,
Expand All @@ -94,7 +100,7 @@ def _log_to_file(self, log_file, cmd):
def _handle_result(self, cmd):
try:
proc = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError, e:
except subprocess.CalledProcessError as e:
print("%sException: %s\n\n"
"failover operation failed, please check log file for "
"further details.%s"
Expand All @@ -106,7 +112,8 @@ def _handle_result(self, cmd):
def _log_to_console(self, cmd, log):
proc = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stderr=subprocess.PIPE,
universal_newlines=True)
for line in iter(proc.stdout.readline, ''):
log.debug(line)
for line in iter(proc.stderr.readline, ''):
Expand Down Expand Up @@ -160,41 +167,32 @@ def _init_vars(self, conf_file):
_SECTION,
ansible_play=None))
while target_host not in setups:
target_host = raw_input(
target_host = input(
INPUT + PREFIX + "target host was not defined. "
"Please provide the target host "
"(primary or secondary): " + END)
while source_map not in setups:
source_map = raw_input(
source_map = input(
INPUT + PREFIX + "source mapping was not defined. "
"Please provide the source mapping "
"(primary or secondary): " + END)
while not os.path.isfile(var_file):
var_file = raw_input("%s%svar file mapping '%s' does not exist. "
"Please provide a valid mapping var file: %s"
% (INPUT,
PREFIX,
var_file,
END))
var_file = input("%s%svar file mapping '%s' does not exist. "
"Please provide a valid mapping var file: %s"
% (INPUT, PREFIX, var_file, END))
while not os.path.isfile(vault):
vault = raw_input("%s%spassword file '%s' does not exist. "
"Please provide a valid password file:%s "
% (INPUT,
PREFIX,
vault,
END))
vault = input("%s%spassword file '%s' does not exist. "
"Please provide a valid password file:%s "
% (INPUT, PREFIX, vault, END))
while (not ansible_play) or (not os.path.isfile(ansible_play)):
ansible_play = raw_input("%s%sansible play '%s' "
"is not initialized. "
"Please provide the ansible play file "
"to generate the mapping var file "
"with ('%s'):%s "
% (INPUT,
PREFIX,
str(ansible_play),
PLAY_DEF,
END) or PLAY_DEF)
return (target_host, source_map, var_file, vault, ansible_play)
ansible_play = input("%s%sansible play '%s' "
"is not initialized. "
"Please provide the ansible play file "
"to generate the mapping var file "
"with ('%s'):%s "
% (INPUT, PREFIX, str(ansible_play),
PLAY_DEF, END) or PLAY_DEF)
return target_host, source_map, var_file, vault, ansible_play

def _set_log(self, log_file, log_level):
logger = logging.getLogger(PREFIX)
Expand Down
Loading

0 comments on commit 54f4bed

Please sign in to comment.