Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration Aides Second Release #52

Merged
merged 9 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/vapi/accessors/ClusterAccessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def setOcpProject(self):
process = subprocess.Popen(cmdArgs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
if process.returncode != 0:
logging.error(f"Failed to login to cluster {cmdArgs}")
logging.error(f"Failed to set OCP Project {cmdArgs}")
logging.error(f"output = {stderr.decode('utf-8')}")
raise OcpException(f"Failed to connect to project {self.project}")

Expand Down
50 changes: 38 additions & 12 deletions lib/vapi/accessors/MongoAccessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#
# IBM_PROLOG_END_TAG
import logging
import re
import subprocess
import signal
import json
Expand All @@ -36,7 +37,9 @@ class MviMongoException(Exception):

class MongoAccessor:
""" Class to provide a "direct" connection to an MVI Mongo DB instance.
Supports connections in standalone and OCP environments depending upon inputs.
Supports connections within a cluster and to external OCP clusters depending upon inputs.
For local cluster, 'mongoService' and 'creds' must be supplied on input.
For external cluster, a 'cluster' object is required to access the external cluster.
"""

def __init__(self, creds=None, mongoService=None, cluster=None):
Expand Down Expand Up @@ -72,21 +75,44 @@ def getMongoDbCredentials(self):
return self.mongoDbCreds["userName"], self.mongoDbCreds["password"]

# otherwise get credentials from the cluster
cmdArgs = ["oc", "get", "secret", "vision-secrets", "-o", "json"]
process = subprocess.Popen(cmdArgs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
if process.returncode == 0:
jsonData = json.loads(stdout.decode('utf-8'))
userName = base64.b64decode(jsonData["data"]["mongodb-admin-username"]).decode("utf-8")
password = base64.b64decode(jsonData["data"]["mongodb-admin-password"]).decode("utf-8")
logging.debug(f"user={userName}, pw={password}")
secretName = self._getMVISecretName()

if secretName is not None:
cmdArgs = ["oc", "get", "secret", secretName, "-o", "json"]
process = subprocess.Popen(cmdArgs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
if process.returncode == 0:
jsonData = json.loads(stdout.decode('utf-8'))
userName = base64.b64decode(jsonData["data"]["mongodb-admin-username"]).decode("utf-8")
password = base64.b64decode(jsonData["data"]["mongodb-admin-password"]).decode("utf-8")
logging.debug(f"user={userName}, pw={password}")
else:
logging.error(f"Failed to get Mongo info -- {cmdArgs}")
logging.error(f"output = {process.stderr}")
raise MviMongoException(f"Could not get Mongo connection info.")
else:
logging.error(f"Failed to get Mongo info -- {cmdArgs}")
logging.error(f"output = {process.stderr}")
raise MviMongoException(f"Could not get Mongo connection info.")
msg = "Could not find an expected secretName"
logging.error(f"ERROR: {msg}!")
raise MviMongoException(f"{msg}!")

return userName, password

def _getMVISecretName(self):
"""Searches the secrets in the project and returns the MVI internal secrets name."""

secretName = None
cmdArgs = ["oc", "get", "secrets"]
process = subprocess.Popen(cmdArgs, stdout=subprocess.PIPE)
for line in process.stdout:
string = line.decode('utf-8')
logging.debug(string)
secret = string.split()[0]
if secret == "vision-secrets" or re.search(r"-credentials-internal-visualinspection", secret):
secretName = secret
logging.debug(f"Matched secret name {secretName}")
break
return secretName

def getMongoHostName(self):
""" getMongoHostName returns the host name to use for the mongo connection.
The host name depends upon the type of execution environment and the target mongo.
Expand Down
2 changes: 1 addition & 1 deletion misc/migrationAides/build_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ then
fi

ARCH=$(uname -m)
IMAGE_NAME="mvi-migration_${ARCH}"
IMAGE_NAME="mvi-migration-tool_${ARCH}"

mkdir -p "${WORK_DIR}"
cd "${WORK_DIR}"
Expand Down
6 changes: 2 additions & 4 deletions misc/migrationAides/cmds/backupMviDb.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
# IBM_PROLOG_END_TAG

"""
This script will backup a Mongod DB. It is designed to work with pre-8.0.0
This script will backup a MongoDB. It is designed to work with pre-8.0.0
version of MVI (pre-MAS) as well as post 8.0.0. The difference is in the
way the connection is made to Mongo.

With pre-8.0.0, the script requires
1. An SSH tunnel has been establish to the MVI mongoDB pod
(and is still active when this script is run).
2. The mongoDB login credentials are provided as parameters to this script.
1. The mongoDB login credentials are provided as parameters to this script.
The presence (or absence) of these creds is used to determine whether
the script is backing a pre-8.0.0 MVI MongoDB or not.

Expand Down
Loading