Ejemplo n.º 1
0
 def __init__(self, zebr0_client: zebr0.Client) -> None:
     self.zebr0_client = zebr0_client
     self.cf_client = boto3.client(
         service_name="cloudformation",
         region_name=zebr0_client.get("aws-region"))
     self.ec2_client = boto3.client(
         service_name="ec2", region_name=zebr0_client.get("aws-region"))
     self.s3_client = boto3.client(
         service_name="s3", region_name=zebr0_client.get("aws-region"))
     self.iam_client = boto3.client(
         service_name="iam", region_name=zebr0_client.get("aws-region"))
Ejemplo n.º 2
0
 def __init__(self, zebr0_client: zebr0.Client,
              sts_client: zebr0_aws.sts.Client) -> None:
     self.zebr0_client = zebr0_client
     self.sts_client = sts_client
     self.iam_client = boto3.client(
         service_name="iam", region_name=zebr0_client.get("aws-region"))
     self.user_name = zebr0_client.get("aws-user-name")
     self.policy_name = zebr0_client.get("aws-policy-name")
     self.policy_arn = "arn:aws:iam::" + sts_client.get_account_id(
     ) + ":policy/" + self.policy_name
     self.bucket_arn = "arn:aws:s3:::" + zebr0_client.get("aws-bucket-name")
Ejemplo n.º 3
0
def fetch_to_disk(*, key: str, target: str, client: zebr0.Client) -> dict:
    """
    Fetches a key from the key-value server and writes its value into a target file.
    Errors will be returned as a list of strings in an execution report.

    :param key: key to look for
    :param target: path to the target file
    :param client: zebr0 Client to the key-value server
    :return: an execution report
    """

    value = client.get(key, strip=False)
    if not value:
        status = Status.FAILURE
        output = [f"key '{key}' not found on server {client.url}"]
    else:
        try:
            target_path = Path(target)
            target_path.parent.mkdir(
                parents=True,
                exist_ok=True)  # make sure the parent directory exists
            target_path.write_text(value, encoding=zebr0.ENCODING)

            status = Status.SUCCESS
            output = []
        except OSError as error:
            status = Status.FAILURE
            output = str(error).splitlines()

    return {KEY: key, TARGET: target, STATUS: status, OUTPUT: output}
Ejemplo n.º 4
0
 def __init__(self, zebr0_client: zebr0.Client) -> None:
     self.zebr0_client = zebr0_client
     self.ec2_client = boto3.client(
         service_name="ec2", region_name=zebr0_client.get("aws-region"))
     self.default_filters = [{
         "Name": "tag:project",
         "Values": [zebr0_client.levels[0]]
     }, {
         "Name": "tag:stage",
         "Values": [zebr0_client.levels[1]]
     }]
Ejemplo n.º 5
0
def recursive_fetch_script(
        client: zebr0.Client, key: str,
        reports_path: Path) -> Iterator[Tuple[dict, Status, Path]]:
    """
    Fetches a script from the key-value server and yields its tasks, their Status and report Path.
    Included scripts are fetched recursively.
    Malformed tasks are ignored.

    :param client: zebr0 Client to the key-value server
    :param key: the script's key
    :param reports_path: Path to the reports' directory
    :return: the script's tasks, their Status and report Path
    """

    value = client.get(key)
    if not value:
        print(f"key '{key}' not found on server {client.url}")
        return

    tasks = yaml.load(value, Loader=yaml.BaseLoader)
    if not isinstance(tasks, list):
        print(
            f"key '{key}' on server {client.url} is not a proper yaml or json list"
        )
        return

    for task in tasks:
        if isinstance(
                task, str
        ):  # transforming command strings into dictionary for internal consistency
            task = {COMMAND: task, VARIANT: VARIANT_DEFAULT}

        if isinstance(task, dict) and task.keys() == {INCLUDE}:
            yield from recursive_fetch_script(client, task.get(INCLUDE),
                                              reports_path)
        elif isinstance(task, dict) and (task.keys() == {KEY, TARGET}
                                         or task.keys() == {COMMAND, VARIANT}):
            md5 = hashlib.md5(json.dumps(task).encode(
                zebr0.ENCODING)).hexdigest()
            report_path = reports_path.joinpath(md5)
            status = Status.PENDING if not report_path.exists(
            ) else json.loads(report_path.read_text(
                encoding=zebr0.ENCODING)).get(STATUS)

            yield task, status, report_path
        else:
            print("malformed task, ignored:", json.dumps(task))
Ejemplo n.º 6
0
 def __init__(self, zebr0_client: zebr0.Client) -> None:
     self.zebr0_client = zebr0_client
     self.sts_client = boto3.client(
         service_name="sts", region_name=zebr0_client.get("aws-region"))
Ejemplo n.º 7
0
 def __init__(self, zebr0_client: zebr0.Client) -> None:
     self.zebr0_client = zebr0_client
     self.route53_client = boto3.client(
         service_name="route53", region_name=zebr0_client.get("aws-region"))
     self.domain_name = zebr0_client.get("domain-name") + "."
     self.fqdn = zebr0_client.get("fqdn") + "."