Exemplo n.º 1
0
def test_call_action_pass_unknown_args():
    actions_manager = ActionsManager()
    parser = argparse.ArgumentParser()
    parser.add_argument("--target", required=True, type=str, help="Object type to init")
    action_function = Mock()

    actions_manager.register_action("test_action", lambda: parser, action_function)

    testargs = ["asd", "--action", "test_action", "--target", "target_test", "--region", "us-east-1", "--configuration_file_name", "test.json"]
    with patch("sys.argv", testargs):
       actions_manager.call_action(pass_unknown_args=True)

    action_function.assert_called_with(parser.parse_args(["--target", "target_test"]), {'region': 'us-east-1', 'configuration_file_name': 'test.json'})
Exemplo n.º 2
0
def test_call_action_do_not_pass_unknown_args():
    actions_manager = ActionsManager()
    parser = argparse.ArgumentParser()
    parser.add_argument("--target", required=True, type=str, help="Object type to init")
    action_function = Mock()

    actions_manager.register_action("test_action", lambda: parser, action_function)

    testargs = ["asd", "--action", "test_action", "--target", "target_test"]
    with patch("sys.argv", testargs):
       actions_manager.call_action()

    action_function.assert_called_once()
Exemplo n.º 3
0
class SystemFunctionCommon:
    ACTION_MANAGER = ActionsManager()

    def __init__(self):
        return

    @staticmethod
    def empty_parser():
        return argparse.ArgumentParser()

    @staticmethod
    def current_subpath(subpath=None):
        cur_dir = os.path.dirname(os.path.abspath(__file__))
        if subpath is None:
            return cur_dir
        return os.path.join(cur_dir, *(subpath.split("/")))

    @staticmethod
    def run_bash(command):
        file_name = f"tmp-{str(uuid.uuid4())}.sh"
        with open(file_name, "w") as file_handler:
            file_handler.write(command)
            command = f"/bin/bash {file_name}"
        ret = subprocess.run([command], capture_output=True, shell=True)
        return ret.stdout.decode().strip("\n")

    @staticmethod
    def check_files_exist_parser():
        description = "Returns true if all files exist"
        parser = argparse.ArgumentParser(description=description)
        parser.add_argument("--files_paths", required=True, type=str, help=f"Comma separated string file list")
        return parser

    @staticmethod
    def check_files_exist(arguments) -> None:
        errors = []
        for file_path in arguments.files_paths.split(","):
            if not os.path.exists(file_path):
                errors.append(f"File '{file_path}' does not exist")
                continue
                
            if not os.path.isfile(file_path):
                errors.append(f"Path '{file_path}' is not a file")

        if errors:
            raise SystemFunctionCommon.FailedCheckError("\n".join(errors))

    @staticmethod
    def action_add_line_to_file_parser():
        description = "add_line_to_file"
        parser = argparse.ArgumentParser(description=description)
        parser.add_argument("--line", required=True, type=str, help="Line to be added")
        parser.add_argument("--file_path", required=True, type=str, help="Path to the file")

        parser.epilog = f"Usage: python3 {__file__} [options]"
        return parser

    @staticmethod
    def action_add_line_to_file(arguments):
        arguments_dict = vars(arguments)
        SystemFunctionCommon.add_line_to_file(**arguments_dict)

    @staticmethod
    def add_line_to_file(line=None, file_path=None):
        if not line.endswith("\n"):
            line = line + "\n"

        try:
            with open(file_path, "r") as file_handler:
                lines = file_handler.readlines()
                if line in lines:
                    return
        except FileNotFoundError:
            pass

        with open(file_path, "a+") as file_handler:
            file_handler.write(line)

    @staticmethod
    def provision_file(src_file_path, dst_location):
        """

        @param src_file_path:
        @param dst_location:
        @return: True if copied else False
        """
        if os.path.isdir(dst_location):
            dst_location = os.path.join(dst_location, os.path.basename(src_file_path))
        if os.path.exists(dst_location):
            with open(dst_location, "r") as file_handler:
                dst_content = file_handler.read()
            with open(src_file_path, "r") as file_handler:
                src_content = file_handler.read()

            if src_content == dst_content:
                return False

        shutil.copy(src_file_path, dst_location)
        return True

    def check_local_port(self):
        pdb.set_trace()
        
    def check_remote_port(self):
        pdb.set_trace()
        """
    import socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    result = sock.connect_ex(('127.0.0.1',80))
    if result == 0:
    print "Port is open"
    else:
    print "Port is not open"
    sock.close()
    """
        
    class FailedCheckError(RuntimeError):
        pass
Exemplo n.º 4
0
import pdb
import argparse
import json

from horey.jenkins_manager.jenkins_manager import JenkinsManager
from horey.jenkins_manager.jenkins_configuration_policy import JenkinsConfigurationPolicy
from horey.jenkins_manager.jenkins_job import JenkinsJob
from horey.common_utils.actions_manager import ActionsManager

action_manager = ActionsManager()


# region run_job
def run_job_parser():
    description = "Run single job"
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument("--build_info_file",
                        required=True,
                        type=str,
                        help="build_info_file")

    return parser


def run_job(arguments, configs_dict) -> None:
    configuration = JenkinsConfigurationPolicy()
    configuration.init_from_dictionary(configs_dict)
    configuration.init_from_file()

    manager = JenkinsManager(configuration)
    with open(arguments.build_info_file) as file_handler:
Exemplo n.º 5
0
    description = "add_line_to_file"
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument("--line",
                        required=True,
                        type=str,
                        help="Line to be added")
    parser.add_argument("--file_path",
                        required=True,
                        type=str,
                        help="Path to the file")

    parser.epilog = f"Usage: python3 {__file__} [options]"
    return parser


def add_line_to_file(arguments) -> None:
    arguments_dict = vars(arguments)
    SystemFunctionCommon.add_line_to_file(**arguments_dict)


action_manager = ActionsManager()

# region add_line_to_file

action_manager.register_action("add_line_to_file", add_line_to_file_parser,
                               add_line_to_file)
# endregion

if __name__ == "__main__":
    action_manager.call_action()
Exemplo n.º 6
0
import sys
import pdb
import argparse
import json
import docker

sys.path.insert(0, "~/private/aws_api/src/aws_clients")
sys.path.insert(0, "~/private/aws_api/ignore")

import logging
logger = logging.Logger(__name__)

from horey.common_utils.actions_manager import ActionsManager

docker_client = docker.from_env()
action_manager = ActionsManager()


# region create_repository
def build_parser():
    description = "Build docker image"
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument("--tag",
                        required=True,
                        type=str,
                        help="Name repository to create")
    return parser


def build(arguments) -> None:
    docker_client.images.build(path=".", tag=arguments.tag)
Exemplo n.º 7
0
import sys
import pdb
import argparse
import json


import ignore_me
from codeartifact_client import CodeartifactClient
import logging
logger = logging.Logger(__name__)
from horey.aws_api.base_entities.aws_account import AWSAccount
from horey.common_utils.actions_manager import ActionsManager

AWSAccount.set_aws_account(ignore_me.acc_mgmt)
action_manager = ActionsManager()


# region create_key
def create_domain_parser():
    description = "Create codeartifact domain"
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument("--name", required=True, type=str, help="Domain name")
    parser.add_argument("--encryption_key_alias", required=True, type=str, help="Key to use")
    return parser


def create_domain(arguments) -> None:
    CodeartifactClient().create_domain(arguments.name, arguments.encryption_key_alias)


action_manager.register_action("create_domain", create_domain_parser, create_domain)
Exemplo n.º 8
0
import sys
import pdb
import argparse
import json

import ignore_me
from ecr_client import ECRClient
import logging
logger = logging.Logger(__name__)
from horey.aws_api.base_entities.aws_account import AWSAccount

from horey.common_utils.actions_manager import ActionsManager

AWSAccount.set_aws_account(ignore_me.acc_default)
action_manager = ActionsManager()


# region get_authorization_information
def get_authorization_information_parser():
    description = "Fetch authorization ECR repository information and write it to file"
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument("--output_file_name",
                        required=False,
                        type=str,
                        default="aws_auth_info.json",
                        help="Name of the output file")
    return parser


def get_authorization_information(arguments) -> None:
    lst_info = ECRClient().get_authorization_info()
Exemplo n.º 9
0
import sys
import pdb
import argparse
import json


import ignore_me
from kms_client import KMSClient
from horey.h_logger import get_logger
logger = get_logger()
from horey.aws_api.base_entities.aws_account import AWSAccount

from horey.common_utils.actions_manager import ActionsManager

AWSAccount.set_aws_account(ignore_me.acc_mgmt)
action_manager = ActionsManager()


# region create_key
def create_key_parser():
    description = "Create KMS key"
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument("--name", required=True, type=str, help="Tag name will be added, description and alias set")
    return parser


def create_key(arguments) -> None:
    KMSClient().create_key(name=arguments.name)


action_manager.register_action("create_key", create_key_parser, create_key)
Exemplo n.º 10
0
import sys
import pdb
import argparse
import json

import logging

from horey.common_utils.actions_manager import ActionsManager
logger = logging.Logger(__name__)

action_manager = ActionsManager()


# region get_authorization_information
def extract_parser():
    description = "Fetch authorization ECR repository information and write it to file"
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument("--path",
                        required=True,
                        type=str,
                        help="Path to the element")
    parser.add_argument("--file_name",
                        required=True,
                        type=str,
                        help="Json file name")
    return parser


def extract(arguments) -> None:
    with open(arguments.file_name) as file_handler:
        json_src = json.load(file_handler)
Exemplo n.º 11
0
        self.check_swappiness_config()
        self.check_swappiness_proc()

    def check_swappiness_proc(self):
        ret = self.run_bash("cat /proc/sys/vm/swappiness")
        if ret != "1":
            raise RuntimeError(f"cat /proc/sys/vm/swappiness = {ret} != 1")

    def check_swappiness_config(self):
        file_content = "vm.swappiness=1"
        ret = self.run_bash(f"cat /etc/sysctl.conf | grep {file_content}")
        if ret != file_content:
            raise RuntimeError(f"can not find {file_content} in /etc/sysctl.conf: {ret}")


action_manager = ActionsManager()


# region check_swappiness
def check_swappiness_parser():
    description = "check_swappiness"
    parser = argparse.ArgumentParser(description=description)
    parser.epilog = f"Usage: python3 {__file__} [options]"
    return parser


def check_swappiness(arguments) -> None:
    Check().check_swappiness()


action_manager.register_action("check_swappiness", check_swappiness_parser, check_swappiness)
Exemplo n.º 12
0
import pdb
import argparse
import os

import logging
logger = logging.Logger(__name__)

from horey.aws_api.aws_api import AWSAPI
from horey.aws_api.aws_api_configuration_policy import AWSAPIConfigurationPolicy
from horey.common_utils.actions_manager import ActionsManager

#AWSAccount.set_aws_account(ignore_me.acc_default)
action_manager = ActionsManager()


# region cleanup
def cleanup_parser():
    description = "Cleanup AWS account"
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument("--target",
                        required=True,
                        type=str,
                        help="Object type to cleanup")
    parser.add_argument("--configuration_file_full_path",
                        required=True,
                        type=str,
                        help="Configuration file full path")

    return parser

Exemplo n.º 13
0
import pdb
import argparse
import os

import logging
logger = logging.Logger(__name__)

from horey.aws_api.aws_api import AWSAPI
from horey.aws_api.aws_api_configuration_policy import AWSAPIConfigurationPolicy
from horey.common_utils.actions_manager import ActionsManager

#AWSAccount.set_aws_account(ignore_me.acc_default)
action_manager = ActionsManager()


# region cleanup
def cleanup_parser():
    description = "Cleanup AWS account"
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument("--target",
                        required=True,
                        type=str,
                        help="Object type to cleanup")
    parser.add_argument("--configuration_file_full_path",
                        required=True,
                        type=str,
                        help="Configuration file full path")

    return parser

Exemplo n.º 14
0
import pdb
import argparse
import json
import sys

sys.path.insert(0, "/Users/alexey.beley/private/horey/environment_bootstrap")

from horey.environment_bootstrap.jenkins.jenkins_deployer import JenkinsDeployer
from horey.environment_bootstrap.jenkins.jenkins_deployer_configuration_policy import JenkinsDeployerConfigurationPolicy
#from horey.jenkins_manager.jenkins_configuration_policy import JenkinsConfigurationPolicy
#from horey.jenkins_manager.jenkins_job import JenkinsJob
from horey.common_utils.actions_manager import ActionsManager

action_manager = ActionsManager()


# region run_job
def deploy_infrastructure_parser():
    description = "Deploy jenkins infra"
    parser = argparse.ArgumentParser(description=description)
    return parser


def deploy_infrastructure(arguments, configs_dict) -> None:
    configuration = JenkinsDeployerConfigurationPolicy()
    configuration.init_from_dictionary(configs_dict)
    configuration.init_from_file()
    jenkins_deployer = JenkinsDeployer(configuration)
    jenkins_deployer.deploy_infrastructure()

Exemplo n.º 15
0
import pdb
import argparse

from horey.aws_api.aws_clients.sessions_manager import SessionsManager
from horey.aws_api.aws_api_configuration_policy import AWSAPIConfigurationPolicy
from horey.common_utils.actions_manager import ActionsManager
from horey.common_utils.common_utils import CommonUtils
from horey.aws_api.base_entities.aws_account import AWSAccount

action_manager = ActionsManager()


# region run_job
def set_session_credentials_parser():
    # export AWS_PROFILE=user1d
    description = "Get session credentials"
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument("--profile_name",
                        required=True,
                        type=str,
                        help="profile name in .aws/credentials")

    return parser


def set_session_credentials(arguments, configs_dict) -> None:
    configuration = AWSAPIConfigurationPolicy()
    configuration.configuration_file_full_path = "~/Desktop/tmp/configuration_values.py"
    configuration.init_from_file()

    accounts = CommonUtils.load_object_from_module(configuration.accounts_file,
Exemplo n.º 16
0
import sys
import argparse

from horey.aws_api.aws_clients.sns_client import SNSClient
from horey.h_logger import get_logger

from horey.aws_api.base_entities.aws_account import AWSAccount
from horey.common_utils.actions_manager import ActionsManager
from horey.common_utils.common_utils import CommonUtils

logger = get_logger()
action_manager = ActionsManager()


# region create_key
def publish_parser():
    description = "Create codeartifact domain"
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument("--topic_arn",
                        required=True,
                        type=str,
                        help="Topic Arn")
    parser.add_argument("--message", required=True, type=str, help="Message")
    parser.add_argument("--subject",
                        required=True,
                        type=str,
                        help="Message subject")
    parser.add_argument("--accounts_file",
                        required=True,
                        type=str,
                        help="Message")
Exemplo n.º 17
0
import sys
import argparse

import ignore_me
from route53_client import Route53Client
from horey.h_logger import get_logger

from horey.aws_api.base_entities.aws_account import AWSAccount
from horey.common_utils.actions_manager import ActionsManager

logger = get_logger()
AWSAccount.set_aws_account(ignore_me.acc_mgmt)
action_manager = ActionsManager()


# region create_key
def create_record_parser():
    description = "Create codeartifact domain"
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument("--name", required=True, type=str, help="Domain name")
    return parser


def create_record(arguments) -> None:
    Route53Client().change_resource_record_sets(arguments.name)


action_manager.register_action("create_record", create_record_parser,
                               create_record)
# endregion