Ejemplo n.º 1
0
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import sys
from ebi_eva_common_pyutils.mongodb import MongoDatabase
from ebi_eva_common_pyutils.logger import logging_config

logger = logging_config.get_logger(__name__)
logging_config.add_stdout_handler()


def create_indexes(mongo_source: MongoDatabase, mongo_dest: MongoDatabase):
    logger.info(
        f"Creating indexes in the target database {mongo_dest.uri_with_db_name}...."
    )
    try:
        mongo_dest.create_index_on_collections(mongo_source.get_indexes())
    except Exception as ex:
        logger.error(f"Error while creating indexes!\n{ex.__str__()}")
        sys.exit(1)


def main():
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import requests
import subprocess
from retry import retry

from ebi_eva_common_pyutils.logger import logging_config as log_cfg

logger = log_cfg.get_logger(__name__)


def is_port_in_use(port):
    import socket
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        return s.connect_ex(('localhost', port)) == 0


def get_available_local_port(try_starting_with_port):
    for i in range(0, 20):
        port_to_try = try_starting_with_port + i
        logger.info(
            "Attempting to forward remote mongo port to local port {0}...".
            format(port_to_try))
        if is_port_in_use(port_to_try):