class MultiEnvBasicConfig(Canned): """ A multi environment / stage config settings. **中文文档** 一个常用的 Multi Stage / Environment 的配置模板. """ PROJECT_NAME = Constant() PROJECT_NAME_SLUG = Derivable() @PROJECT_NAME_SLUG.getter def get_PROJECT_NAME_SLUG(self): return self.PROJECT_NAME.get_value().replace("_", "-") STAGE = Constant() ENVIRONMENT_NAME = Derivable() @ENVIRONMENT_NAME.getter def get_ENVIRONMENT_NAME(self): return "{}-{}".format(self.PROJECT_NAME_SLUG.get_value(), self.STAGE.get_value()) STACK_NAME = Derivable() @STACK_NAME.getter def get_STACK_NAME(self): return self.ENVIRONMENT_NAME.get_value() ENV_TAG = Derivable() # the environment tag for orchestration @ENV_TAG.getter def get_ENV_TAG(self): return self.STAGE.get_value() COMMON_TAGS = Derivable() @COMMON_TAGS.getter def get_COMMON_TAGS(self): return dict( Name=self.ENVIRONMENT_NAME.get_value(), Project=self.PROJECT_NAME_SLUG.get_value(), Stage=self.STAGE.get_value(), EnvName=self.ENVIRONMENT_NAME.get_value(), ) def post_create_template_hooker(self, **kwargs): """ Automatically update common tags after the template been created. """ self.template.update_tags( self.COMMON_TAGS.get_value(), overwrite=False)
class Config(ConfigClass): CONFIG_DIR = path.dirname(__file__) PROJECT_NAME = Constant() STAGE = Constant() # specify dont_dump to avoid dump to dict or json PARAM_ENV_NAME = Derivable(dont_dump=True) @PARAM_ENV_NAME.getter def get_PARAM_ENV_NAME(self): return Parameter("{}-{}".format( self.PROJECT_NAME.get_value().replace("_", "-"), self.STAGE.get_value(), ))
class Config(ConfigClass): METADATA = Constant(default=dict()) PROJECT_NAME = Constant() PROJECT_NAME_SLUG = Derivable() @PROJECT_NAME_SLUG.getter def get_project_name_slug(self): return self.PROJECT_NAME.get_value().replace("_", "-") STAGE = Constant() # example dev / test / prod ENVIRONMENT_NAME = Derivable() @ENVIRONMENT_NAME.getter def get_ENVIRONMENT_NAME(self): return "{}-{}".format(self.PROJECT_NAME_SLUG.get_value(self), self.STAGE.get_value())
class Config(ConfigClass): PROJECT_NAME = Constant(default="my_devops") PROJECT_NAME_SLUG = Derivable() @PROJECT_NAME_SLUG.getter def get_PROJECT_NAME_SLUG(self): return self.PROJECT_NAME.get_value().replace("_", "-") STAGE = Constant(default="dev") ENVIRONMENT_NAME = Derivable() @ENVIRONMENT_NAME.getter def get_ENVIRONMENT_NAME(self): return "{}-{}".format(self.PROJECT_NAME_SLUG.get_value(), self.STAGE.get_value()) def is_prod_runtime(self): return False
class Config(ConfigClass): CONFIG_DIR = path.dirname(__file__) PROJECT_NAME = Constant() STAGE = Constant() PROJECT_NAME_SLUG = Derivable() @PROJECT_NAME_SLUG.getter def get_project_name_slug(self): return self.PROJECT_NAME.get_value().replace("_", "-") ENVIRONMENT_NAME = Derivable() @ENVIRONMENT_NAME.getter def get_environment_name(self): return "{}-{}".format( self.PROJECT_NAME_SLUG.get_value(), self.STAGE.get_value(), ) GITHUB_USERNAME = Constant() GITHUB_PASSWORD = Constant(printable=False) @GITHUB_PASSWORD.validator def check_github_password(self, value): if len(value) < 16: raise ValueError( "GITHUB_PASSWORD has to be more than 16 characters!") LAPTOP_PASSWORD = Constant(dont_dump=True, printable=False) RUNTIME_NAME = Derivable() @RUNTIME_NAME.getter def get_runtime_name(self): if "CIRCLECI" in environ: return "circleci" elif "LAMBDA_RUNTIME_DIR" in environ: return "lambda" else: return "local"
class Config(ConfigClass): CONFIG_DIR = os.path.dirname( __file__) # config stores at the ``devops-example`` directory PROJECT_NAME = Constant() # example "MyWebApp" PROJECT_NAME_SLUG = Derivable() @PROJECT_NAME_SLUG.getter def get_PROJECT_NAME_SLUG(self): return self.PROJECT_NAME.get_value().replace("_", "-") @PROJECT_NAME_SLUG.validator def check_PROJECT_NAME_SLUG(self, value): if "_" in value: raise ValueError("you can't use `_` in slugifie name!") STAGE = Constant() # example "dev" ENVIRONMENT_NAME = Derivable() @ENVIRONMENT_NAME.getter def get_ENVIRONMENT_NAME(self): return "{}-{}".format( self.PROJECT_NAME_SLUG.get_value(), self.STAGE.get_value(), ) APP_PUBLIC_URL = Derivable() @APP_PUBLIC_URL.getter def get_APP_PUBLIC_URL(self): return "https://www.{}.com/{}".format( self.PROJECT_NAME.get_value().lower(), self.STAGE.get_value(), ) GITHUB_ACCESS_TOKEN = Constant(dont_dump=True, printable=False) # sensitive config value
class Config(ConfigClass): PROJECT_NAME = Constant() STAGE = Constant() PROJECT_NAME_SLUG = Derivable() @PROJECT_NAME_SLUG.getter def get_PROJECT_NAME_SLUG(self): return self.PROJECT_NAME.get_value().replace("_", "-") @PROJECT_NAME_SLUG.validator def check_PROJECT_NAME_SLUG(self, value): if "_" in value: raise ValueError("you can't use `_` in slugifie name!") PROJECT_NAME_SUPER_SLUG_CACHE_HIT = Constant(default=0) PROJECT_NAME_SUPER_SLUG = Derivable(cache=True) @PROJECT_NAME_SUPER_SLUG.getter def get_PROJECT_NAME_SUPER_SLUG(self, sep): self.PROJECT_NAME_SUPER_SLUG_CACHE_HIT.set_value( self.PROJECT_NAME_SUPER_SLUG_CACHE_HIT.get_value() + 1) return self.PROJECT_NAME.get_value().replace("_", sep) ENVIRONMENT_NAME = Derivable() @ENVIRONMENT_NAME.getter def get_ENVIRONMENT_NAME(self): return "{}-{}".format( self.PROJECT_NAME_SLUG.get_value(), self.STAGE.get_value(), ) GIT_PASSWORD = Constant(printable=False) METADATA = Constant(dont_dump=True)
class Config(ConfigClass): METADATA = Constant(default=dict(), dont_dump=True) PROJECT_NAME = Constant() PROJECT_NAME_SLUG = Derivable() @PROJECT_NAME_SLUG.getter def get_project_name_slug(self): return self.PROJECT_NAME.get_value().replace("_", "-") STAGE = Constant() # example dev / test / prod ENVIRONMENT_NAME = Derivable() @ENVIRONMENT_NAME.getter def get_ENVIRONMENT_NAME(self): return "{}-{}".format(self.PROJECT_NAME_SLUG.get_value(), self.STAGE.get_value()) AWS_PROFILE = Constant() AWS_PROFILE_FOR_BOTO3 = Derivable() @AWS_PROFILE_FOR_BOTO3.getter def get_AWS_PROFILE_FOR_BOTO3(self): if self.is_aws_ec2_runtime(): return None elif self.is_aws_lambda_runtime(): return None elif self.is_circle_ci_runtime(): return None else: # local computer runtime return self.AWS_PROFILE.get_value() AWS_REGION = Constant() AWS_ACCOUNT_ID = Constant(printable=False) S3_BUCKET_FOR_DEPLOY = Constant() ECS_EXAMPLE_ENVIRONMENT_NAME = Derivable() @ECS_EXAMPLE_ENVIRONMENT_NAME.getter def get_ECS_EXAMPLE_ENVIRONMENT_NAME(self): return "{}-ecs-example".format(self.ENVIRONMENT_NAME.get_value())
class Config(ConfigClass): PROJECT_NAME = Constant() STAGE = Constant() PROJECT_NAME_SLUG = Derivable() @PROJECT_NAME_SLUG.getter def get_PROJECT_NAME_SLUG(self): return self.PROJECT_NAME.get_value().replace("_", "-") ENVIRONMENT_NAME = Derivable() @ENVIRONMENT_NAME.getter def get_ENVIRONMENT_NAME(self): return f"{self.PROJECT_NAME_SLUG.get_value()}-{self.STAGE.get_value()}" STACK_NAME = Derivable() @STACK_NAME.getter def get_STACK_NAME(self): return f"{self.ENVIRONMENT_NAME.get_value()}-stack" AWS_PROFILE = Constant() AWS_ACCOUNT_ID = Constant() AWS_REGION = Constant() S3_BUCKET_FOR_DEPLOY = Constant() GITHUB_URL = Constant() # --- Code Build CODE_BUILD_PROJECT_NAME = Derivable() @CODE_BUILD_PROJECT_NAME.getter def get_CODE_BUILD_PROJECT_NAME(self): return self.ENVIRONMENT_NAME.get_value()
class AppConfig(ConfigClass): METADATA = Constant(default=dict()) PROJECT_NAME = Constant() PROJECT_NAME_SLUG = Derivable() @PROJECT_NAME_SLUG.getter def get_project_name_slug(self): return self.PROJECT_NAME.get_value().replace("_", "-") STAGE = Constant() # example dev / test / prod ENVIRONMENT_NAME = Derivable() @ENVIRONMENT_NAME.getter def get_ENVIRONMENT_NAME(self): return "{}-{}".format(self.PROJECT_NAME_SLUG.get_value(self), self.STAGE.get_value()) HANDLER_MODULE_NAME = Constant() """ The root path of the lambda handler folder. lbdrabbit automatically create lambda function for each sub module """ @HANDLER_MODULE_NAME.validator def check_HANDLER_MODULE_NAME(self, value): from importlib import import_module import_module(value) S3_BUCKET_FOR_DEPLOY = Constant() """ A prerequisite for deployment. Need to be manually created. Recommended bucket name would be ``{account_id}-for-everything``. """ AWS_PROFILE_FOR_DEPLOY = Constant() """ The AWS profile name used for your local machine to deploy cloudformation. """ AWS_PROFILE_FOR_BOTO3 = Derivable() @AWS_PROFILE_FOR_BOTO3.getter def get_AWS_PROFILE_FOR_BOTO3(self): if self.is_aws_lambda_runtime(): return None else: return self.AWS_PROFILE_FOR_DEPLOY.get_value() LAMBDA_CODE_S3_KEY = Constant() LAMBDA_CODE_S3_BUCKET = Constant() LAMBDA_LAYER_ARN = Constant() STACK_NAME = Derivable() @STACK_NAME.getter def get_STACK_NAME(self): return self.ENVIRONMENT_NAME.get_value() # --- lbdrabbit framework related --- DEFAULT_LBD_FUNC_CONFIG_FIELD = Constant( default=DEFAULT_LBD_FUNC_CONFIG_FIELD) DEFAULT_LBD_HANDLER_FUNC_NAME = Constant( default=DEFAULT_LBD_HANDLER_FUNC_NAME) VALID_LBD_HANDLER_FUNC_NAME_LIST = Constant( default=VALID_LBD_HANDLER_FUNC_NAME_LIST)
class Config(ConfigClass): METADATA = Constant(default=dict(), dont_dump=True) PROJECT_NAME = Constant() PROJECT_NAME_SLUG = Derivable() @PROJECT_NAME_SLUG.getter def get_project_name_slug(self): return self.PROJECT_NAME.get_value().replace("_", "-") STAGE = Constant() # example dev / test / prod ENVIRONMENT_NAME = Derivable() @ENVIRONMENT_NAME.getter def get_ENVIRONMENT_NAME(self): return "{}-{}".format(self.PROJECT_NAME_SLUG.get_value(self), self.STAGE.get_value()) LBD_APP_ENVIRONMENT_NAME = Derivable() @LBD_APP_ENVIRONMENT_NAME.getter def get_LBD_APP_ENVIRONMENT_NAME(self): return "lbd-app-{}".format(self.STAGE.get_value()) AWS_PROFILE = Constant() AWS_PROFILE_FOR_BOTO3 = Derivable() @AWS_PROFILE_FOR_BOTO3.getter def get_AWS_PROFILE_FOR_BOTO3(self): if self.is_aws_code_build_runtime(): return None else: # local computer runtime return self.AWS_PROFILE.get_value() AWS_REGION = Constant() AWS_ACCOUNT_ID = Constant(printable=False) S3_BUCKET_FOR_DEPLOY = Constant() ECR_ENDPOINT = Derivable() @ECR_ENDPOINT.getter def get_ECR_ENDPOINT(self): return "{}.dkr.ecr.{}.amazonaws.com".format( self.AWS_ACCOUNT_ID.get_value(), self.AWS_REGION.get_value() ) ECR_REPO_NAME_WEBAPP = Derivable() @ECR_REPO_NAME_WEBAPP.getter def get_ECR_REPO_NAME_WEBAPP(self): return "{}-webapp".format(self.PROJECT_NAME_SLUG.get_value()) APP_VERSION = Derivable() @APP_VERSION.getter def get_APP_VERSION(self): return __version__ VPC_ID = Constant() PUBLIC_SUBNET_ID_AZ1 = Constant() PUBLIC_SUBNET_ID_AZ2 = Constant() SUBNETS = Derivable() @SUBNETS.getter def get_SUBNETS(self): return [ self.PUBLIC_SUBNET_ID_AZ1.get_value(), self.PUBLIC_SUBNET_ID_AZ2.get_value(), ] AWS_CFT_DYNAMIC_DELETEION_POLICY = Derivable() @AWS_CFT_DYNAMIC_DELETEION_POLICY.getter def get_AWS_CFT_DYNAMIC_DELETEION_POLICY(self): if self.STAGE.get_value() == "prod": return "Retain" else: return "Delete" #--- derivable object --- _boto_ses = None # type: boto3.Session @property def boto_ses(self) -> boto3.Session: if self._boto_ses is None: self._boto_ses = boto3.session.Session( profile_name=self.AWS_PROFILE_FOR_BOTO3.get_value(), region_name=self.AWS_REGION.get_value(), ) return self._boto_ses _blue_green_model = None # type: Type[Model] @property def blue_green_model(self) -> Type[Model]: if self._blue_green_model is None: class BlueGreenModel(Model): class Meta: table_name = "tf-blue-green-deploy" region = self.AWS_REGION.get_value() env = UnicodeAttribute(hash_key=True) logic_id = UnicodeAttribute() self._blue_green_model = BlueGreenModel return self._blue_green_model #--- Blue Green Deployment --- LBD_APP_ACTIVE_FLAG = Derivable() @LBD_APP_ACTIVE_FLAG.getter def get_LBD_APP_ACTIVE_FLAG(self): return None # LBD_APP_ACTIVE_LOGIC_ID = Derivable() # LBD_APP_ACTIVE_PORT = Derivable() # # LBD_APP_INACTIVE_FLAG = Derivable() # LBD_APP_INACTIVE_LOGIC_ID = Derivable() # LBD_APP_INACTIVE_PORT = Derivable() # # LBD_APP_STAGING_FLAG = Derivable() # LBD_APP_STAGING_LOGIC_ID = Derivable() # LBD_APP_STAGING_PORT = Derivable() def to_terraform_ecs_service_config_data(self): tf_keys = [ self.STAGE.name, self.ENVIRONMENT_NAME.name, self.VPC_ID.name, self.SUBNETS.name, ] data = self.to_dict() config_data = { key: data[key] for key in tf_keys } return config_data def to_terraform_lbd_app_config_data(self): tf_keys = [ self.STAGE.name, self.LBD_APP_ENVIRONMENT_NAME.name, self.VPC_ID.name, self.SUBNETS.name, ] data = self.to_dict() config_data = { key: data[key] for key in tf_keys } return config_data def to_terraform_lbd_app_blue_green_config_data(self): tf_keys = [ self.STAGE.name, self.LBD_APP_ENVIRONMENT_NAME.name, ] data = self.to_dict() config_data = { key: data[key] for key in tf_keys } return config_data
class Config(ConfigClass): PROJECT_NAME = Constant() PROJECT_NAME_SLUG = Derivable()
class Config(ConfigClass): PROJECT_NAME = Constant(default="my_project")
class Config(ConfigClass): CONFIG_DIR = Path(__file__).parent.parent.parent.append_parts("config") METADATA = Constant(default=dict()) PROJECT_NAME = Constant() PROJECT_NAME_SLUG = Derivable() @PROJECT_NAME_SLUG.getter def get_project_name_slug(self): return self.PROJECT_NAME.get_value().replace("_", "-") STAGE = Constant() # example dev / test / prod ENVIRONMENT_NAME = Derivable() @ENVIRONMENT_NAME.getter def get_ENVIRONMENT_NAME(self): return "{}-{}".format(self.PROJECT_NAME_SLUG.get_value(self), self.STAGE.get_value()) AWS_PROFILE_FOR_DEPLOY = Constant() AWS_PROFILE_FOR_PYTHON = Derivable() @AWS_PROFILE_FOR_PYTHON.getter def get_AWS_PROFILE_FOR_PYTHON(self): if self.is_aws_lambda_runtime(): return None else: return self.AWS_PROFILE_FOR_DEPLOY.get_value() AWS_ACCOUNT_ID = Constant() AWS_REGION = Constant() S3_BUCKET_FOR_DEPLOY = Constant() DYNAMODB_TABLE_STARTUP = Derivable() @DYNAMODB_TABLE_STARTUP.getter def get_DYNAMODB_TABLE_STARTUP(self): return "{}-startup".format(self.ENVIRONMENT_NAME.get_value()) SQS_QUEUE_URL = Derivable() @SQS_QUEUE_URL.getter def get_SQS_QUEUE_URL(self): return f"https://sqs.{self.AWS_REGION.get_value()}.amazonaws.com/{self.AWS_ACCOUNT_ID.get_value()}/{self.ENVIRONMENT_NAME.get_value()}-download-job" LAMBDA_CODE_S3_KEY = Derivable() @LAMBDA_CODE_S3_KEY.getter def get_LAMBDA_CODE_S3_KEY(self): from seedinvest_monitor._version import __version__ return f"lambda/MacHu-GWU/seedinvest_monitor-project/{__version__}/source.zip" LAMBDA_LAYER_VERSION = Constant() LAMBDA_LAYERS = Derivable() @LAMBDA_LAYERS.getter def get_LAMBDA_LAYERS(self): return [ f"arn:aws:lambda:us-east-1:110330507156:layer:seedinvest_monitor:{self.LAMBDA_LAYER_VERSION.get_value()}" ] UPDATE_NEW_PROJECT_RATE = Constant() HTML_UPDATE_INTERVAL_IN_SECONDS = Constant() START_CRAWLER_RATE = Constant()
class Config(ConfigClass): CONFIG_DIR = Path(__file__).parent.parent.parent.append_parts("config").abspath METADATA = Constant(default=dict()) PROJECT_NAME = Constant() PROJECT_NAME_SLUG = Derivable() @PROJECT_NAME_SLUG.getter def get_project_name_slug(self): return self.PROJECT_NAME.get_value().replace("_", "-") STAGE = Constant() # example dev / test / prod ENVIRONMENT_NAME = Derivable() @ENVIRONMENT_NAME.getter def get_ENVIRONMENT_NAME(self): return "{}-{}".format(self.PROJECT_NAME_SLUG.get_value(self), self.STAGE.get_value()) AWS_ACCOUNT_ID = Constant() AWS_REGION = Constant() AWS_PROFILE_FOR_DEPLOY = Constant() AWS_PROFILE_FOR_BOTO3 = Derivable() @AWS_PROFILE_FOR_BOTO3.getter def get_AWS_PROFILE_FOR_BOTO3(self): if self.is_aws_lambda_runtime(): return None else: return self.AWS_PROFILE_FOR_DEPLOY.get_value() S3_BUCKET_FOR_DEPLOY = Constant() S3_BUCKET_FOR_CLOUDTRAIL = Constant() LAMBDA_ENV_VAR_PREFIX = Constant(default="TAT_AWS_CREATOR_AUTO_TAG_") IAM_ROLE_NAME = Constant() IAM_ROLE_ARN = Derivable() @IAM_ROLE_ARN.getter def get_IAM_ROLE_ARN(self): return "arn:aws:iam::{}:role/{}".format( self.AWS_ACCOUNT_ID.get_value(), self.IAM_ROLE_NAME.get_value(), ) LATEST_LAYER_ARN = Derivable() @LATEST_LAYER_ARN.getter def get_LATEST_LAYER_ARN(self): """ """ import boto3 boto_ses = boto3.Session(profile_name=self.AWS_PROFILE_FOR_BOTO3.get_value()) lbd_client = boto_ses.client("lambda") res_list_layers = lbd_client.list_layers( CompatibleRuntime="python3.6" ) latest_layer_arn = None for layer_data in res_list_layers["Layers"]: layer_name = layer_data["LayerName"] if layer_name == self.PROJECT_NAME.get_value(): latest_layer_arn = layer_data["LatestMatchingVersion"]["LayerVersionArn"] if latest_layer_arn is None: raise ValueError return latest_layer_arn
class ServerlessConfig(MultiEnvBasicConfig): # pragma: no cover """ Serverless application config settings for common fields. """ AWS_PROFILE_FOR_DEPLOY = Constant() AWS_PROFILE_FOR_BOTO3 = Derivable() @AWS_PROFILE_FOR_BOTO3.getter def get_AWS_PROFILE_FOR_BOTO3(self): if self.is_aws_lambda_runtime(): return None elif self.is_aws_ec2_runtime(): return None elif self.is_ci_runtime(): return None else: return self.AWS_PROFILE_FOR_DEPLOY.get_value() S3_BUCKET_FOR_DEPLOY = Constant() S3_PREFIX_LAMBDA_ARTIFACT = Constant() """ usually it is ``lambda/{github_account_username}/{github_repo_name}``. The final source code s3 key is ``lambda/{github_account_username}/{github_repo_name}/{version}/source.zip`` run ``make lbd-info`` command to get more information """ LAMBDA_LAYER_ARNS = Constant() LAMBDA_LATEST_LAYER_ARNS = Derivable(dont_dump=True) """ Latest lambda layer arn for ``awslambda.Function.Layers`` property """ @LAMBDA_LATEST_LAYER_ARNS.getter def get_LAMBDA_LATEST_LAYER_ARNS(self): import boto3 boto_ses = boto3.session.Session(profile_name=self.AWS_PROFILE_FOR_BOTO3.get_value()) lbd_client = boto_ses.client("lambda") latest_layer_arn = None list_layers_response = lbd_client.list_layers() for layer_data in list_layers_response["Layers"]: if layer_data["LayerName"] == self.PROJECT_NAME_SLUG.get_value(): latest_layer_arn = layer_data["LatestMatchingVersion"]["LayerVersionArn"] if latest_layer_arn is None: raise ValueError return [ latest_layer_arn, ] API_GATEWAY_REST_API_NAME = Derivable() @API_GATEWAY_REST_API_NAME.getter def get_API_GATEWAY_REST_API_NAME(self): return self.ENVIRONMENT_NAME.get_value() API_GATEWAY_REST_API_DEPLOYMENT_STAGE_NAME = Derivable() @API_GATEWAY_REST_API_DEPLOYMENT_STAGE_NAME.getter def get_API_GATEWAY_REST_API_DEPLOYMENT_STAGE_NAME(self): return self.STAGE.get_value() S3_URI_ATHENA_RESULT = Derivable() @S3_URI_ATHENA_RESULT.getter def get_S3_URI_ATHENA_RESULT(self): return "s3://{}/athena/result".format(self.S3_BUCKET_FOR_DEPLOY.get_value()) ATHENA_DATABASE_NAME = Derivable() @ATHENA_DATABASE_NAME.getter def get_ATHENA_DATABASE_NAME(self): return self.ENVIRONMENT_NAME.get_value().replace("-", "_") LABELS_TO_DEPLOY = Constant() LABELS_TO_IGNORE = Constant()
class Config(ConfigClass): my_field = Constant()
class Config1(ConfigClass): CONFIG_DIR = path.dirname(__file__) PROJECT_NAME = Constant() STAGE = Constant()