# coding=utf-8
"""
Django settings for gentelella project.

Generated by 'django-admin startproject' using Django 1.10.

For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""
import os
import ldap
import logging
from django_auth_ldap.config import LDAPSearch, LDAPSearchUnion, GroupOfNamesType

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))

CHART_DIR = os.path.join(BASE_DIR, "charts")
NGINX_TEMPLATE = os.path.join(BASE_DIR, "deploy_template_pro.conf")
NGINX_CONF_DIR = "/etc/nginx/conf.d"

DEVOPS_HOST = "172.31.238.14"
DNS_HOST = "172.31.238.12"
CMDB = "172.31.238.14:8055"
QAAS_CMDB = "172.31.238.13:30556"
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '8*md2t)o**67@*yhc(d=f@j95kl(dnf^rmm4s00$-mh_vurb2b'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ["*"]

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'guardian',
    # 'mama_cas',
    'rest_framework',
    'app',
]

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ],
}
# REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
#     'DEFAULT_PERMISSION_CLASSES': [
#         'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
#     ]
# }

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'flashholdDevops.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.media',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'flashholdDevops.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
    'default': {
        # 'ENGINE': 'django.db.backends.sqlite3',
        # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'devops',
        'HOST': '172.31.238.14',
        'USER': 'root',
        'PORT': 3306,
        'PASSWORD': 'flashhold#123',
    }
}

AUTHENTICATION_BACKENDS = [
    # 'django_auth_ldap.backend.LDAPBackend',
    'django.contrib.auth.backends.ModelBackend',
    'guardian.backends.ObjectPermissionBackend',
]

# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/


LANGUAGE_CODE = 'zh-Hans'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = True

LOGIN_URL = "/login"

GITLAB_URL = "http://git.flashhold.com:10080/"
DEVOPS_URL = "http://127.0.0.1:9000"
CONSOLE_URL = "http://devops.flashhold.com:3000"
GITLAB_PRIVATE_TOKEN = "KMr31MJdt4vXYJZzkokT"

HARBOR_HOST = "172.31.238.10"
HARBOR_USER = "admin"
HARBOR_PASSWORD = "Harbor12345"
HARBOR_URL = "harbor2.flashhold.com"

LDAP_URL = "ldap://ldap.flashhold.com/"

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

STATIC_URL = '/static/'
MEDIA_URL = '/media/'

# STATICFILES_DIRS = [
#     os.path.join(BASE_DIR, "static")
# ]

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# AUTH_LDAP_SERVER_URI = "ldap://ldap.flashhold.com"
# AUTH_LDAP_BIND_DN = "cn=admin,dc=flashhold,dc=com"
# AUTH_LDAP_BIND_PASSWORD = "M2Y3Y2JjNWI2NjUxY2ZlM2JmOWVlODQ1"
#
#
# AUTH_LDAP_USER_DN_TEMPLATE = "cn=%(user)s,ou=People,dc=flashhold,dc=com"
# # OU = 'ou=People,dc=flashhold,dc=com'
# # AUTH_LDAP_USER_SEARCH = LDAPSearch(OU, ldap.SCOPE_SUBTREE, "(&(objectClass=posixAccount)(objectClass=inetOrgPerson))")
#
# # AUTH_LDAP_USER_ATTR_MAP = {
# #     "first_name": "sn",
# #     "last_name": "sn",
# #     "email": "mail"
# # }
# OUg = 'ou=Group2,dc=flashhold,dc=com'
# AUTH_LDAP_GROUP_SEARCH = LDAPSearch(OUg, ldap.SCOPE_SUBTREE, "(objectClass=groupOfNames)")
# AUTH_LDAP_GROUP_TYPE = GroupOfNamesType(name_attr="cn")
# # AUTH_LDAP_REQUIRE_GROUP = OUg
# AUTH_LDAP_MIRROR_GROUPS = True
# AUTH_LDAP_ALWAYS_UPDATE_USER = True

BASE_LOG_DIR = os.path.join(BASE_DIR, "log")
LOGGING = {
    'version': 1,  # 保留字
    'disable_existing_loggers': False,  # 禁用已经存在的logger实例
    # 日志文件的格式
    'formatters': {
        # 详细的日志格式
        'standard': {
            'format': '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]'
                      '[%(levelname)s][%(message)s]'
        },
        # 简单的日志格式
        'simple': {
            'format': '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
        }
    },
    # 过滤器
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    # 处理器
    'handlers': {
        # 在终端打印
        'console': {
            'level': 'DEBUG',
            'filters': ['require_debug_true'],  # 只有在Django debug为True时才在屏幕打印日志
            'class': 'logging.StreamHandler',  #
            'formatter': 'simple'
        },
        # 默认的
        'default': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件，自动切
            'filename': os.path.join(BASE_LOG_DIR, "info.log"),  # 日志文件
            'maxBytes': 1024 * 1024 * 50,  # 日志大小 50M
            'backupCount': 3,  # 最多备份几个
            'formatter': 'standard',
            'encoding': 'utf-8',
        },
        # 专门用来记错误日志
        'error': {
            'level': 'ERROR',
            'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件，自动切
            'filename': os.path.join(BASE_LOG_DIR, "error.log"),  # 日志文件
            'maxBytes': 1024 * 1024 * 50,  # 日志大小 50M
            'backupCount': 5,
            'formatter': 'standard',
            'encoding': 'utf-8',
        },

    },
    'loggers': {
        # 默认的logger应用如下配置
        '': {
            'handlers': ['default', 'console', 'error'],  # 上线之后可以把'console'移除
            'level': 'INFO',
            'propagate': True,  # 向不向更高级别的logger传递
        }
    },
}

LOCAL_HARBOR_LIBRARY_REPO = "http://harbor2.flashhold.com/chartrepo/library"
# commonApp chart need make and push in advance
COMMON_APP = (
    ("mysql", 'mysql'),
    ("mongodb", 'mongodb'),
    ("zookeeper", 'zookeeper'),
    ("rmq", 'rmq'),
    ("redis", 'redis'),
    ("nginx", 'nginx'),
    ("prometheus", 'prometheus'),
    ("grafana", 'grafana'),
)

COMMON_APP_REPO = (
    ("prometheus", "http://harbor2.flashhold.com/library/prometheus:1.2.3"),
    ("grafana", "http://harbor2.flashhold.com/library/grafana:6.1.63"),
    ("mysql", "http://harbor2.flashhold.com/library/mysql:5.7.26"),
    ("mongo", "http://harbor2.flashhold.com/library/mongodb:3.4.9"),
    ("redis", "http://harbor2.flashhold.com/library/redis:4.0.14"),
    ("rmqnamesrv", "http://harbor2.flashhold.com/library/rocketmq_server:4.3.0"),
    ("rmqbroker", "http://harbor2.flashhold.com/library/rocketmq_broker:4.3.0"),
    ("rmqconsole", "http://harbor2.flashhold.com/library/rocketmq-console-ng:4.3.2"),
    ("zookeeper", "http://harbor2.flashhold.com/library/zookeeper:3.4.6"),
    ("nginx", "http://harbor2.flashhold.com/library/nginx:jaeger0.4.6"),

)
COMMON_APP_PORT = {"mysql": 3306, "redis": 6379, "rmp": 80, 'mongodb': 27017, "zookeeper": 2181, "nginx": 1008, "prometheus": 9090, "grafana": 3000}

DATASET = (
    (HARBOR_URL + "/library/flashhold-mysql:v8", "MYSQL"),
    (HARBOR_URL + "library/mongo:3.6", "MONGO3.6"),
)

KUBERNETES_CONFIG = "/etc/kubernetes/admin.conf"
HELM_HOME = "/root/.helm"
KUBERNETES_TOKEN = "eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLXh6N3J0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiI4ZjU0NWVmNy05ZDdlLTExZTktYjA4YS1kODlkNjcyYTgxODgiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZS1zeXN0ZW06YWRtaW4tdXNlciJ9.GGrvRPoteXo67sQsuyNM4VRb89yNFe8adMGk_zXseMT7-JsgxkuCMY6pslPGxk7SHsHn6wW8_8YgJDOfdxsvkYB7qn2f4DeG0ud-5rGepYJdyHcUiyggFRIl1NXpca87Ub4BO0fRAVmg8lhussjDa_1aTsyANjttkkhFu9B1Hbo8CQaAzL4SN82DWta2EfU93EHIqW19zWrMl56hSJtOV80sCmOBh72kmlYveRAyjv8u_Y2oO2xoIyvaOrflzChRmqA37X6URadVffsir6m4jZmCaQxfNN9ClSv4JPEG2akcKhkdcciW2FhBt81K9m1dB7H6kORkhpggAnkB4j79Xw"
KUBERNETES_HOST = "https://172.31.238.88:16443"

# MAMA_CAS_SERVICES = [
#     {
#         'SERVICE': 'http://192.168.21.106/users/auth/cas3',
        # 'SERVICE': 'http://192.168.21.106/users/sign_in',
        # 'CALLBACKS': [
        #     'helper.helper.custom_attributes',
        # ],
    # },
# ]

# MAMA_CAS_LOGIN_TEMPLATE = "app/login.html"

HOST = "192.168.25.74"

GITLAB_VARIABLE = {
    "REGISTRY_USERNAME": "admin",
    "REGISTRY_PASSWORD": "Harbor12345"
}

BASEAPP_ROOT = "evo-baseapp"

PROMETHEUS_URL = "http://prometheus.k8s.flashhold.com"
GRAFANA_URL = "http://grafana.k8s.flashhold.com"
ALERTMANAGER_URL = "http://alertmanager.k8s.flashhold.com"

DC_PATH = os.path.join(BASE_DIR, "docker-compose.yml")

SERVICE_PORT = {
    "evo-interface": 10080,
    "evo-notification": 8405,
    "evo-basic": 8023,
    "evo-auth": 8404,
    "evo-console": 8020,
    "evo-wes": 7777,
    "evo-station": 8090,
    "evo-wcs-engine": 8070,
    "evo-wcs-g2p": 8071,
    "evo-wcs-si": 8073,
    "evo-rcs": 9001,
    "evo-quicktron-web": 7010,
    "evo-basic-web": 7011,
    "evo-console-web": 7014,
    "evo-wcs-web": 7033,
    "evo-wes-web": 7012,
    "evo-station-web": 7013,
    "evo-inventory-web": 7015,
    "evo-report-web": 7017,
    "evo-rcs-web": 7018,
    "evo-interface-web": 7016,
    "registry": 8761,
    "evo-agv-simulation-carrier": 8080,
}

TAR_HOST = "192.168.22.30"
TAR_DIR = "/var/www/html/dockerbuildpackages"

TILLER_SERVER = {"address": "172.31.238.13", "port": 30035}

POD_PERSISTENCE_APP = ["evo-basic","evo-rcs","evo-station","evo-wcs-engine","evo-wcs-g2p","evo-wcs-si","evo-wes"]

DOCKER_SERVER = "172.31.238.14:2375"
#
MYSQL_IMAGE_BASE = 'harbor2.flashhold.com/library/flashhold-mysql:v8'

MYSQLCLIENT_IMAGE_BASE = 'harbor2.flashhold.com/library/mysql-client:v8'

NODEMAP = {"kmaster1.local": "172.31.238.12",
           "kmaster2.local": "172.31.238.13",
           "kmaster3.local": "172.31.238.14",
           "knode1.local": "172.31.238.10",
           "knode2.local": "172.31.238.11",
           "knode3.local": "172.31.238.15",
           "knode4.local": "172.31.238.16",
           "knode5.local": "172.31.238.17",
           "knode6.local": "172.31.238.18",
           "knode7.local": "172.31.238.19",
           "knode8.local": "172.31.238.20",
           "knode9.local": "172.31.238.21",
           "knode10.local": "172.31.238.22",
           "knode11.local": "172.31.238.23",
           "knode12.local": "172.31.238.24",
           "knode13.local": "172.31.238.25"
           }

BASEAPP_CHARTS_ADDR = "http://harbor2.flashhold.com/chartrepo/evo-baseapp"

TCPSERVERMAP = {
    "evo-rcs": [9001, 5009, 7070],
    "evo-basic": [8023, 5004],
    "evo-station": [8090, 5005],
    "evo-wcs-engine": [8070, 5007],
    "evo-wcs-g2p": [8071, 5008],
    "evo-wes": [7777, 5006],
    "evo-interface": [10080, 5005],
    "evo-printer": [8009],
    "evo-auth": [8404],
    "evo-agv-simulation-carrier": [8080, 5010],
    "evo-rcs-log": [1234],
    "registry": [8761]
}
