Esempio n. 1
0
def get_from_env(wanted):
    """Get environment variables from the env"""
    env = sb.listof(env_spec()).normalise(Meta({}, []), wanted)
    missing = [e.env_name for e in env if e.missing]
    if missing:
        raise BespinError("Missing environment variables", missing=missing)
    return dict(e.pair for e in env)
Esempio n. 2
0
def get_from_env(wanted):
    """Get environment variables from the env"""
    env = sb.listof(env_spec()).normalise(Meta({}, []), wanted)
    missing = [e.env_name for e in env if e.missing]
    if missing:
        raise BespinError("Missing environment variables", missing=missing)
    return dict(e.pair for e in env)
Esempio n. 3
0
def downtime(collector, stack, method="downtime", **kwargs):
    """Downtime this stack in alerting systems"""
    if stack.downtimer_options is NotSpecified:
        raise BespinError("Nothing to downtime!")

    env = sb.listof(env_spec()).normalise(Meta({}, []), ["USER", "DURATION", "COMMENT"])
    missing = [e.env_name for e in env if e.missing]
    if missing:
        raise BespinError("Missing environment variables", missing=missing)
    provided_env = dict(e.pair for e in env)

    author = provided_env["USER"]
    comment = provided_env["COMMENT"]
    duration = provided_env["DURATION"]

    downtimer = Downtimer(stack.downtimer_options, dry_run=collector.configuration["bespin"].dry_run)
    for system, options in stack.alerting_systems.items():
        downtimer.register_system(system, options)

    getattr(downtimer, method)(duration, author, comment)
Esempio n. 4
0
def downtime(collector, stack, method="downtime", **kwargs):
    """Downtime this stack in alerting systems"""
    if stack.downtimer_options is NotSpecified:
        raise BespinError("Nothing to downtime!")

    env = sb.listof(env_spec()).normalise(Meta({}, []), ["USER", "DURATION", "COMMENT"])
    missing = [e.env_name for e in env if e.missing]
    if missing:
        raise BespinError("Missing environment variables", missing=missing)
    provided_env = dict(e.pair for e in env)

    author = provided_env["USER"]
    comment = provided_env["COMMENT"]
    duration = provided_env["DURATION"]

    downtimer = Downtimer(stack.downtimer_options, dry_run=collector.configuration["bespin"].dry_run)
    for system, options in stack.alerting_systems.items():
        downtimer.register_system(system, options)

    getattr(downtimer, method)(duration, author, comment)
Esempio n. 5
0
 def stack_spec(self):
     """Spec for each stack"""
     return create_spec(
         stack_objs.Stack,
         validators.deprecated_key(
             "url_checker", "Use ``confirm_deployment.url_checker1``"),
         validators.deprecated_key(
             "deploys_s3_path",
             "Use ``confirm_deployment.deploys_s3_path``"),
         validators.deprecated_key(
             "sns_confirmation",
             "Use ``confirm_deployment.sns_confirmation``"),
         validators.deprecated_key("autoscaling_group_id",
                                   "Use ``auto_scaling_group_name``"),
         validators.deprecated_key(
             "instance_count_limit",
             "Use ``scaling_options.instance_count_limit``"),
         bespin=any_spec(),
         name=formatted(defaulted(string_spec(), "{_key_name_1}"),
                        formatter=MergedOptionStringFormatter),
         key_name=formatted(overridden("{_key_name_1}"),
                            formatter=MergedOptionStringFormatter),
         stack_name=formatted(defaulted(string_spec(), "{_key_name_1}"),
                              formatter=MergedOptionStringFormatter),
         environment=formatted(overridden("{environment}"),
                               formatter=MergedOptionStringFormatter),
         env=listof(stack_specs.env_spec(),
                    expect=stack_objs.EnvironmentVariable),
         build_env=listof(stack_specs.env_spec(),
                          expect=stack_objs.EnvironmentVariable),
         stack_name_env=listof(stack_specs.env_spec(),
                               expect=stack_objs.EnvironmentVariable),
         tags=self.tags_spec,
         termination_protection=defaulted(boolean(), False),
         stack_json=valid_stack_json(
             default="{config_root}/{_key_name_1}.json"),
         stack_yaml=valid_stack_yaml(
             default="{config_root}/{_key_name_1}.yaml"),
         params_json=valid_params_json(
             default="{config_root}/{environment}/{_key_name_1}-params.json"
         ),
         params_yaml=valid_params_yaml(
             default="{config_root}/{environment}/{_key_name_1}-params.yaml"
         ),
         stack_policy=valid_policy_json(
             default="{config_root}/{_key_name_1}-policy.json"),
         role_name=formatted(string_spec(),
                             formatter=MergedOptionStringFormatter),
         build_first=listof(
             formatted(string_spec(),
                       formatter=MergedOptionStringFormatter)),
         build_after=listof(
             formatted(string_spec(),
                       formatter=MergedOptionStringFormatter)),
         build_timeout=defaulted(integer_spec(), 1200),
         ignore_deps=defaulted(boolean(), False),
         vars=delayed(
             dictof(string_spec(), stack_specs.var_spec(), nested=True)),
         skip_update_if_equivalent=listof(stack_specs.skipper_spec()),
         suspend_actions=defaulted(boolean(), False),
         auto_scaling_group_name=optional_spec(
             formatted(string_spec(),
                       formatter=MergedOptionStringFormatter)),
         artifact_retention_after_deployment=defaulted(boolean(), False),
         command=optional_spec(string_spec()),
         netscaler=optional_spec(self.netscaler_spec),
         notify_stackdriver=defaulted(boolean(), False),
         stackdriver=optional_spec(
             create_spec(
                 stack_objs.Stackdriver,
                 api_key=required(
                     formatted(string_spec(),
                               formatter=MergedOptionStringFormatter)),
                 deployment_version=defaulted(
                     formatted(string_spec(),
                               formatter=MergedOptionStringFormatter),
                     "<version>"))),
         dns=optional_spec(
             stack_specs.dns_spec(
                 create_spec(
                     stack_objs.DNS,
                     vars=dictof(
                         string_spec(),
                         formatted(string_spec(),
                                   formatter=MergedOptionStringFormatter),
                         nested=True),
                     providers=dictof(string_spec(),
                                      stack_specs.dns_provider_spec()),
                     sites=delayed(
                         dictof(string_spec(),
                                stack_specs.dns_site_spec()))))),
         scaling_options=create_spec(
             ScalingOptions,
             highest_min=defaulted(integer_spec(), 2),
             instance_count_limit=defaulted(integer_spec(), 10)),
         artifacts=container_spec(
             artifact_objs.ArtifactCollection,
             dictof(
                 string_spec(),
                 create_spec(
                     artifact_objs.Artifact,
                     not_created_here=defaulted(boolean(), False),
                     compression_type=string_choice_spec(["gz", "xz"]),
                     history_length=integer_spec(),
                     cleanup_prefix=optional_spec(string_spec()),
                     upload_to=formatted(
                         string_spec(),
                         formatter=MergedOptionStringFormatter),
                     commands=listof(stack_specs.artifact_command_spec(),
                                     expect=artifact_objs.ArtifactCommand),
                     paths=listof(stack_specs.artifact_path_spec(),
                                  expect=artifact_objs.ArtifactPath),
                     files=listof(
                         create_spec(
                             artifact_objs.ArtifactFile,
                             validators.has_either(["content", "task"]),
                             content=optional_spec(
                                 formatted(
                                     string_spec(),
                                     formatter=MergedOptionStringFormatter)
                             ),
                             task=optional_spec(
                                 formatted(
                                     string_spec(),
                                     formatter=MergedOptionStringFormatter)
                             ),
                             path=formatted(
                                 string_spec(),
                                 formatter=MergedOptionStringFormatter),
                             task_runner=formatted(
                                 always_same_spec("{task_runner}"),
                                 formatter=MergedOptionStringFormatter)))))
         ),
         newrelic=optional_spec(
             create_spec(
                 stack_objs.NewRelic,
                 api_key=required(
                     formatted(string_spec(),
                               formatter=MergedOptionStringFormatter)),
                 account_id=required(
                     formatted(string_spec(),
                               formatter=MergedOptionStringFormatter)),
                 application_id=required(
                     formatted(string_spec(),
                               formatter=MergedOptionStringFormatter)),
                 env=listof(stack_specs.env_spec(),
                            expect=stack_objs.EnvironmentVariable),
                 deployed_version=required(
                     formatted(string_spec(),
                               formatter=MergedOptionStringFormatter)))),
         downtimer_options=optional_spec(
             dictof(
                 valid_string_spec(valid_alerting_system()),
                 create_spec(
                     stack_objs.DowntimerOptions,
                     hosts=listof(
                         formatted(
                             string_spec(),
                             formatter=MergedOptionStringFormatter))))),
         alerting_systems=optional_spec(
             dictof(string_spec(), self.alerting_system_spec)),
         ssh=optional_spec(
             create_spec(
                 stack_objs.SSH,
                 validators.deprecated_key(
                     "autoscaling_group_id",
                     "Use ``auto_scaling_group_name``"),
                 user=required(
                     formatted(string_spec(),
                               formatter=MergedOptionStringFormatter)),
                 bastion=optional_spec(
                     formatted(string_spec(),
                               formatter=MergedOptionStringFormatter)),
                 bastion_user=required(
                     formatted(string_spec(),
                               formatter=MergedOptionStringFormatter)),
                 bastion_key_location=optional_spec(
                     formatted(string_spec(),
                               formatter=MergedOptionStringFormatter)),
                 instance_key_location=optional_spec(
                     formatted(string_spec(),
                               formatter=MergedOptionStringFormatter)),
                 address=optional_spec(
                     formatted(string_spec(),
                               formatter=MergedOptionStringFormatter)),
                 instance=optional_spec(
                     listof(
                         formatted(string_spec(),
                                   formatter=MergedOptionStringFormatter))),
                 auto_scaling_group_name=optional_spec(
                     formatted(string_spec(),
                               formatter=MergedOptionStringFormatter)),
                 bastion_key_path=formatted(
                     defaulted(
                         string_spec(),
                         "{config_root}/{environment}/bastion_ssh_key.pem"),
                     formatter=MergedOptionStringFormatter),
                 instance_key_path=formatted(
                     defaulted(string_spec(),
                               "{config_root}/{environment}/ssh_key.pem"),
                     formatter=MergedOptionStringFormatter),
                 storage_type=formatted(
                     defaulted(string_choice_spec(["url", "rattic"]),
                               "url"),
                     formatter=MergedOptionStringFormatter),
                 storage_host=optional_spec(
                     formatted(string_spec(),
                               formatter=MergedOptionStringFormatter)))),
         confirm_deployment=optional_spec(self.confirm_deployment_spec))
Esempio n. 6
0
    def stack_spec(self):
        """Spec for each stack"""
        return create_spec(stack_objs.Stack
            , validators.deprecated_key("url_checker", "Use ``confirm_deployment.url_checker1``")
            , validators.deprecated_key("deploys_s3_path", "Use ``confirm_deployment.deploys_s3_path``")
            , validators.deprecated_key("sns_confirmation", "Use ``confirm_deployment.sns_confirmation``")
            , validators.deprecated_key("autoscaling_group_id", "Use ``auto_scaling_group_name``")
            , validators.deprecated_key("instance_count_limit", "Use ``scaling_options.instance_count_limit``")

            , bespin = any_spec()

            , name = formatted(defaulted(string_spec(), "{_key_name_1}"), formatter=MergedOptionStringFormatter)
            , key_name = formatted(overridden("{_key_name_1}"), formatter=MergedOptionStringFormatter)
            , stack_name = formatted(defaulted(string_spec(), "{_key_name_1}"), formatter=MergedOptionStringFormatter)
            , environment = formatted(overridden("{environment}"), formatter=MergedOptionStringFormatter)

            , env = listof(stack_specs.env_spec(), expect=stack_objs.Environment)
            , build_env = listof(stack_specs.env_spec(), expect=stack_objs.Environment)
            , stack_name_env = listof(stack_specs.env_spec(), expect=stack_objs.Environment)

            , tags = dictionary_spec()

            , stack_json = valid_stack_json(default="{config_root}/{_key_name_1}.json")

            , params_json = valid_params_json(default="{config_root}/{environment}/{_key_name_1}-params.json")
            , params_yaml = valid_params_yaml(default="{config_root}/{environment}/{_key_name_1}-params.yaml")

            , build_first = listof(formatted(string_spec(), formatter=MergedOptionStringFormatter))
            , build_after = listof(formatted(string_spec(), formatter=MergedOptionStringFormatter))
            , build_timeout = defaulted(integer_spec(), 1200)
            , ignore_deps = defaulted(boolean(), False)

            , vars = dictof(string_spec(), stack_specs.var_spec(), nested=True)

            , skip_update_if_equivalent = listof(stack_specs.skipper_spec())

            , suspend_actions = defaulted(boolean(), False)
            , auto_scaling_group_name = optional_spec(formatted(string_spec(), formatter=MergedOptionStringFormatter))

            , artifact_retention_after_deployment = defaulted(boolean(), False)

            , command = optional_spec(string_spec())

            , netscaler = optional_spec(self.netscaler_spec)

            , dns = optional_spec(stack_specs.dns_spec(create_spec(stack_objs.DNS
                , vars = dictof(string_spec(), formatted(string_spec(), formatter=MergedOptionStringFormatter), nested=True)
                , providers = dictof(string_spec(), stack_specs.dns_provider_spec())
                , sites = delayed(dictof(string_spec(), stack_specs.dns_site_spec()))
                )))

            , scaling_options = create_spec(ScalingOptions
                , highest_min = defaulted(integer_spec(), 2)
                , instance_count_limit = defaulted(integer_spec(), 10)
                )

            , artifacts = container_spec(artifact_objs.ArtifactCollection, dictof(string_spec(), create_spec(artifact_objs.Artifact
                , not_created_here = defaulted(boolean(), False)
                , compression_type = string_choice_spec(["gz", "xz"])
                , history_length = integer_spec()
                , cleanup_prefix = optional_spec(string_spec())
                , upload_to = formatted(string_spec(), formatter=MergedOptionStringFormatter)
                , commands = listof(stack_specs.artifact_command_spec(), expect=artifact_objs.ArtifactCommand)
                , paths = listof(stack_specs.artifact_path_spec(), expect=artifact_objs.ArtifactPath)
                , files = listof(create_spec(artifact_objs.ArtifactFile, validators.has_either(["content", "task"])
                    , content = optional_spec(formatted(string_spec(), formatter=MergedOptionStringFormatter))
                    , task = optional_spec(formatted(string_spec(), formatter=MergedOptionStringFormatter))
                    , path = formatted(string_spec(), formatter=MergedOptionStringFormatter)
                    , task_runner = formatted(always_same_spec("{task_runner}"), formatter=MergedOptionStringFormatter)
                    ))
                )))

            , newrelic = optional_spec(create_spec(stack_objs.NewRelic
                , api_key = required(formatted(string_spec(), formatter=MergedOptionStringFormatter))
                , account_id = required(formatted(string_spec(), formatter=MergedOptionStringFormatter))
                , application_id = required(formatted(string_spec(), formatter=MergedOptionStringFormatter))

                , env = listof(stack_specs.env_spec(), expect=stack_objs.Environment)
                , deployed_version = required(formatted(string_spec(), formatter=MergedOptionStringFormatter))
                ))

            , downtimer_options = optional_spec(dictof(valid_string_spec(valid_alerting_system())
                , create_spec(stack_objs.DowntimerOptions
                    , hosts = listof(formatted(string_spec(), formatter=MergedOptionStringFormatter))
                    )
                ))

            , alerting_systems = optional_spec(dictof(string_spec(), self.alerting_system_spec))

            , ssh = optional_spec(create_spec(stack_objs.SSH
                , validators.deprecated_key("autoscaling_group_id", "Use ``auto_scaling_group_name``")

                , user = required(formatted(string_spec(), formatter=MergedOptionStringFormatter))
                , bastion = optional_spec(formatted(string_spec(), formatter=MergedOptionStringFormatter))
                , bastion_user = required(formatted(string_spec(), formatter=MergedOptionStringFormatter))
                , bastion_key_location = optional_spec(formatted(string_spec(), formatter=MergedOptionStringFormatter))
                , instance_key_location = optional_spec(formatted(string_spec(), formatter=MergedOptionStringFormatter))

                , address = optional_spec(formatted(string_spec(), formatter=MergedOptionStringFormatter))
                , instance = optional_spec(listof(formatted(string_spec(), formatter=MergedOptionStringFormatter)))
                , auto_scaling_group_name = optional_spec(formatted(string_spec(), formatter=MergedOptionStringFormatter))

                , bastion_key_path = formatted(defaulted(string_spec(), "{config_root}/{environment}/bastion_ssh_key.pem"), formatter=MergedOptionStringFormatter)
                , instance_key_path = formatted(defaulted(string_spec(), "{config_root}/{environment}/ssh_key.pem"), formatter=MergedOptionStringFormatter)

                , storage_type = formatted(defaulted(string_choice_spec(["url", "rattic"]), "url"), formatter=MergedOptionStringFormatter)
                , storage_host = optional_spec(formatted(string_spec(), formatter=MergedOptionStringFormatter))
                ))

            , confirm_deployment = optional_spec(self.confirm_deployment_spec)
            )
Esempio n. 7
0
    it "creates an artifact_path from the two items":
        host_path = self.unique_val()
        artifact_path = self.unique_val()
        self.assertEqual(specs.artifact_path_spec().normalise(self.meta, [host_path, artifact_path]), artifact_objs.ArtifactPath(host_path, artifact_path))

describe BespinCase, "Env spec":
    before_each:
        self.meta = mock.Mock(name="meta", spec=Meta)
        self.env_name = self.unique_val()
        self.fallback_val = self.unique_val()

    it "takes in just the env_name":
        assert ":" not in self.env_name
        assert "=" not in self.env_name

        made = specs.env_spec().normalise(self.meta, self.env_name)
        self.assertEqual(made.env_name, self.env_name)
        self.assertEqual(made.set_val, None)
        self.assertEqual(made.default_val, None)

    it "takes in env as a list with 1 item":
        assert ":" not in self.env_name
        assert "=" not in self.env_name

        made = specs.env_spec().normalise(self.meta, [self.env_name])
        self.assertEqual(made.env_name, self.env_name)
        self.assertEqual(made.set_val, None)
        self.assertEqual(made.default_val, None)

    it "takes in env as a list with 2 items":
        assert ":" not in self.env_name