2016-02-01 12:59:10 +01:00
#!/usr/bin/python
2016-04-15 11:23:40 +02:00
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals # We require Python 2.6 or later
2016-02-01 12:59:10 +01:00
from string import Template
2016-05-27 10:21:32 +02:00
import random
2016-04-15 11:23:40 +02:00
import os
2016-04-18 10:01:44 +02:00
import sys
2018-06-13 12:13:47 +02:00
import string
2016-08-24 08:24:13 +02:00
import argparse
2016-09-06 06:44:17 +02:00
import subprocess
2016-10-14 11:13:15 +02:00
import shutil
2016-04-15 11:23:40 +02:00
from io import open
2016-04-18 10:01:44 +02:00
if sys.version_info[:3][0] == 2:
2016-04-15 11:23:40 +02:00
import ConfigParser as ConfigParser
import StringIO as StringIO
2016-04-18 10:01:44 +02:00
if sys.version_info[:3][0] == 3:
2016-04-15 11:23:40 +02:00
import configparser as ConfigParser
import io as StringIO
2016-02-22 03:00:42 +01:00
2018-04-08 17:43:45 +02:00
DATA_VOL = "/data"
2018-08-09 09:27:46 +02:00
def validate(conf, args):
2016-10-14 11:13:15 +02:00
protocol = rcp.get("configuration", "ui_url_protocol")
2017-02-20 11:40:05 +01:00
if protocol != "https" and args.notary_mode:
raise Exception("Error: the protocol must be https when Harbor is deployed with Notary")
2016-10-14 11:13:15 +02:00
if protocol == "https":
if not rcp.has_option("configuration", "ssl_cert"):
raise Exception("Error: The protocol is https but attribute ssl_cert is not set")
cert_path = rcp.get("configuration", "ssl_cert")
if not os.path.isfile(cert_path):
raise Exception("Error: The path for certificate: %s is invalid" % cert_path)
if not rcp.has_option("configuration", "ssl_cert_key"):
raise Exception("Error: The protocol is https but attribute ssl_cert_key is not set")
cert_key_path = rcp.get("configuration", "ssl_cert_key")
if not os.path.isfile(cert_key_path):
raise Exception("Error: The path for certificate key: %s is invalid" % cert_key_path)
2016-11-16 13:31:04 +01:00
project_creation = rcp.get("configuration", "project_creation_restriction")
if project_creation != "everyone" and project_creation != "adminonly":
raise Exception("Error invalid value for project_creation_restriction: %s" % project_creation)
2018-07-19 10:47:05 +02:00
valid_storage_drivers = ["filesystem", "azure", "gcs", "s3", "swift", "oss"]
storage_provider_name = rcp.get("configuration", "registry_storage_provider_name").strip()
if storage_provider_name not in valid_storage_drivers:
raise Exception("Error: storage driver %s is not supported, only the following ones are supported: %s" % (storage_provider_name, ",".join(valid_storage_drivers)))
storage_provider_config = rcp.get("configuration", "registry_storage_provider_config").strip()
if storage_provider_name != "filesystem":
if storage_provider_config == "":
raise Exception("Error: no provider configurations are provided for provider %s" % storage_provider_name)
2016-10-14 11:13:15 +02:00
2018-08-09 09:27:46 +02:00
redis_host = rcp.get("configuration", "redis_host")
if redis_host is None or len(redis_host) < 1:
raise Exception("Error: redis_host in harbor.cfg needs to point to an endpoint of Redis server or cluster.")
redis_port = rcp.get("configuration", "redis_port")
if len(redis_port) < 1:
raise Exception("Error: redis_port in harbor.cfg needs to point to the port of Redis server or cluster.")
redis_db_index = rcp.get("configuration", "redis_db_index").strip()
if len(redis_db_index.split(",")) != 3:
raise Exception("Error invalid value for redis_db_index: %s. please set it as 1,2,3" % redis_db_index)
2018-04-08 17:43:45 +02:00
#To meet security requirement
#By default it will change file mode to 0600, and make the owner of the file to 10000:10000
def mark_file(path, mode=0o600, uid=10000, gid=10000):
os.chmod(path, mode)
os.chown(path, uid, gid)
2016-10-18 12:06:47 +02:00
def get_secret_key(path):
2017-03-24 13:05:13 +01:00
secret_key = _get_secret(path, "secretkey")
if len(secret_key) != 16:
raise Exception("secret key's length has to be 16 chars, current length: %d" % len(secret_key))
return secret_key
def get_alias(path):
alias = _get_secret(path, "defaultalias", length=8)
return alias
def _get_secret(folder, filename, length=16):
key_file = os.path.join(folder, filename)
2016-10-18 12:06:47 +02:00
if os.path.isfile(key_file):
with open(key_file, 'r') as f:
key = f.read()
2017-03-24 13:05:13 +01:00
print("loaded secret from file: %s" % key_file)
2018-06-08 10:13:37 +02:00
mark_file(key_file)
2016-10-18 12:06:47 +02:00
return key
2017-03-24 13:05:13 +01:00
if not os.path.isdir(folder):
2018-04-08 17:43:45 +02:00
os.makedirs(folder)
2017-03-24 13:05:13 +01:00
key = ''.join(random.choice(string.ascii_letters+string.digits) for i in range(length))
2016-10-18 12:06:47 +02:00
with open(key_file, 'w') as f:
f.write(key)
2017-03-24 13:05:13 +01:00
print("Generated and saved secret to file: %s" % key_file)
2018-04-08 17:43:45 +02:00
mark_file(key_file)
2016-10-18 12:06:47 +02:00
return key
2016-10-19 08:32:00 +02:00
2018-04-08 17:43:45 +02:00
def prep_conf_dir(root, *name):
absolute_path = os.path.join(root, *name)
2017-02-20 11:40:05 +01:00
if not os.path.exists(absolute_path):
os.makedirs(absolute_path)
return absolute_path
def render(src, dest, **kw):
t = Template(open(src, 'r').read())
with open(dest, 'w') as f:
f.write(t.substitute(**kw))
print("Generated configuration file: %s" % dest)
2016-10-19 08:32:00 +02:00
base_dir = os.path.dirname(__file__)
config_dir = os.path.join(base_dir, "common/config")
templates_dir = os.path.join(base_dir, "common/templates")
2017-02-20 11:40:05 +01:00
def delfile(src):
if os.path.isfile(src):
try:
os.remove(src)
print("Clearing the configuration file: %s" % src)
except:
pass
elif os.path.isdir(src):
for item in os.listdir(src):
itemsrc=os.path.join(src,item)
delfile(itemsrc)
2016-10-18 12:06:47 +02:00
2016-08-24 08:24:13 +02:00
parser = argparse.ArgumentParser()
2017-02-20 04:16:55 +01:00
parser.add_argument('--conf', dest='cfgfile', default=base_dir+'/harbor.cfg',type=str,help="the path of Harbor configuration file")
2017-02-20 11:40:05 +01:00
parser.add_argument('--with-notary', dest='notary_mode', default=False, action='store_true', help="the Harbor instance is to be deployed with notary")
2017-04-27 13:01:45 +02:00
parser.add_argument('--with-clair', dest='clair_mode', default=False, action='store_true', help="the Harbor instance is to be deployed with clair")
2018-07-19 10:47:05 +02:00
parser.add_argument('--with-chartmuseum', dest='chart_mode', default=False, action='store_true', help="the Harbor instance is to be deployed with chart repository supporting")
2016-08-24 08:24:13 +02:00
args = parser.parse_args()
2017-02-20 11:40:05 +01:00
delfile(config_dir)
2016-02-22 03:00:42 +01:00
#Read configurations
conf = StringIO.StringIO()
conf.write("[configuration]\n")
2016-08-24 08:24:13 +02:00
conf.write(open(args.cfgfile).read())
2016-02-22 03:00:42 +01:00
conf.seek(0, os.SEEK_SET)
2016-04-15 11:23:40 +02:00
rcp = ConfigParser.RawConfigParser()
rcp.readfp(conf)
2017-02-20 11:40:05 +01:00
validate(rcp, args)
2016-09-06 06:44:17 +02:00
2018-01-16 05:38:53 +01:00
reload_config = rcp.get("configuration", "reload_config") if rcp.has_option(
"configuration", "reload_config") else "false"
2016-04-15 11:23:40 +02:00
hostname = rcp.get("configuration", "hostname")
2016-10-14 11:13:15 +02:00
protocol = rcp.get("configuration", "ui_url_protocol")
2018-03-15 07:21:53 +01:00
public_url = protocol + "://" + hostname
2016-11-06 23:16:59 +01:00
email_identity = rcp.get("configuration", "email_identity")
2017-01-12 11:29:02 +01:00
email_host = rcp.get("configuration", "email_server")
email_port = rcp.get("configuration", "email_server_port")
email_usr = rcp.get("configuration", "email_username")
email_pwd = rcp.get("configuration", "email_password")
2016-04-15 11:23:40 +02:00
email_from = rcp.get("configuration", "email_from")
2016-04-21 07:03:12 +02:00
email_ssl = rcp.get("configuration", "email_ssl")
2017-10-31 06:51:49 +01:00
email_insecure = rcp.get("configuration", "email_insecure")
2016-04-15 11:23:40 +02:00
harbor_admin_password = rcp.get("configuration", "harbor_admin_password")
auth_mode = rcp.get("configuration", "auth_mode")
ldap_url = rcp.get("configuration", "ldap_url")
2016-08-18 12:31:41 +02:00
# this two options are either both set or unset
if rcp.has_option("configuration", "ldap_searchdn"):
ldap_searchdn = rcp.get("configuration", "ldap_searchdn")
ldap_search_pwd = rcp.get("configuration", "ldap_search_pwd")
else:
ldap_searchdn = ""
ldap_search_pwd = ""
2016-04-15 11:23:40 +02:00
ldap_basedn = rcp.get("configuration", "ldap_basedn")
2016-08-18 12:31:41 +02:00
# ldap_filter is null by default
if rcp.has_option("configuration", "ldap_filter"):
ldap_filter = rcp.get("configuration", "ldap_filter")
else:
ldap_filter = ""
ldap_uid = rcp.get("configuration", "ldap_uid")
ldap_scope = rcp.get("configuration", "ldap_scope")
2017-01-12 11:29:02 +01:00
ldap_timeout = rcp.get("configuration", "ldap_timeout")
2017-12-27 02:37:00 +01:00
ldap_verify_cert = rcp.get("configuration", "ldap_verify_cert")
2018-03-14 12:07:06 +01:00
ldap_group_basedn = rcp.get("configuration", "ldap_group_basedn")
ldap_group_filter = rcp.get("configuration", "ldap_group_filter")
ldap_group_gid = rcp.get("configuration", "ldap_group_gid")
ldap_group_scope = rcp.get("configuration", "ldap_group_scope")
2016-04-15 11:23:40 +02:00
db_password = rcp.get("configuration", "db_password")
2017-09-22 04:46:02 +02:00
db_host = rcp.get("configuration", "db_host")
db_user = rcp.get("configuration", "db_user")
2017-09-27 06:56:57 +02:00
db_port = rcp.get("configuration", "db_port")
2016-04-15 11:23:40 +02:00
self_registration = rcp.get("configuration", "self_registration")
2016-10-14 11:13:15 +02:00
if protocol == "https":
cert_path = rcp.get("configuration", "ssl_cert")
cert_key_path = rcp.get("configuration", "ssl_cert_key")
2016-04-20 13:40:19 +02:00
customize_crt = rcp.get("configuration", "customize_crt")
2016-05-27 10:21:32 +02:00
max_job_workers = rcp.get("configuration", "max_job_workers")
2016-08-08 05:21:48 +02:00
token_expiration = rcp.get("configuration", "token_expiration")
2016-11-16 13:31:04 +01:00
proj_cre_restriction = rcp.get("configuration", "project_creation_restriction")
2016-12-12 05:39:11 +01:00
secretkey_path = rcp.get("configuration", "secretkey_path")
2017-03-16 09:09:05 +01:00
if rcp.has_option("configuration", "admiral_url"):
admiral_url = rcp.get("configuration", "admiral_url")
else:
admiral_url = ""
2017-12-19 08:25:12 +01:00
clair_db_password = rcp.get("configuration", "clair_db_password")
clair_db_host = rcp.get("configuration", "clair_db_host")
clair_db_port = rcp.get("configuration", "clair_db_port")
clair_db_username = rcp.get("configuration", "clair_db_username")
clair_db = rcp.get("configuration", "clair_db")
2018-07-26 10:02:22 +02:00
clair_updaters_interval = rcp.get("configuration", "clair_updaters_interval")
2017-12-19 08:25:12 +01:00
2017-10-01 23:03:53 +02:00
uaa_endpoint = rcp.get("configuration", "uaa_endpoint")
uaa_clientid = rcp.get("configuration", "uaa_clientid")
uaa_clientsecret = rcp.get("configuration", "uaa_clientsecret")
2017-12-19 07:42:07 +01:00
uaa_verify_cert = rcp.get("configuration", "uaa_verify_cert")
2018-01-03 09:21:29 +01:00
uaa_ca_cert = rcp.get("configuration", "uaa_ca_cert")
2017-09-27 06:56:57 +02:00
2016-12-12 05:39:11 +01:00
secret_key = get_secret_key(secretkey_path)
2017-11-08 06:07:27 +01:00
log_rotate_count = rcp.get("configuration", "log_rotate_count")
log_rotate_size = rcp.get("configuration", "log_rotate_size")
2017-09-27 06:56:57 +02:00
2018-08-09 09:27:46 +02:00
redis_host = rcp.get("configuration", "redis_host")
redis_port = rcp.get("configuration", "redis_port")
redis_password = rcp.get("configuration", "redis_password")
redis_db_index = rcp.get("configuration", "redis_db_index")
db_indexs = redis_db_index.split(',')
redis_db_index_reg = db_indexs[0]
redis_db_index_js = db_indexs[1]
redis_db_index_chart = db_indexs[2]
#redis://[arbitrary_username:password@]ipaddress:port/database_index
redis_url_js = ''
2018-08-09 13:04:35 +02:00
redis_url_reg = ''
2018-08-09 09:27:46 +02:00
if len(redis_password) > 0:
redis_url_js = "redis://anonymous:%s@%s:%s/%s" % (redis_password, redis_host, redis_port, redis_db_index_js)
2018-08-09 13:04:35 +02:00
redis_url_reg = "redis://anonymous:%s@%s:%s/%s" % (redis_password, redis_host, redis_port, redis_db_index_reg)
2017-09-27 06:56:57 +02:00
else:
2018-08-09 09:27:46 +02:00
redis_url_js = "redis://%s:%s/%s" % (redis_host, redis_port, redis_db_index_js)
2018-08-09 13:04:35 +02:00
redis_url_reg = "redis://%s:%s/%s" % (redis_host, redis_port, redis_db_index_reg)
2016-02-22 03:00:42 +01:00
2018-06-27 13:26:15 +02:00
if rcp.has_option("configuration", "skip_reload_env_pattern"):
skip_reload_env_pattern = rcp.get("configuration", "skip_reload_env_pattern")
else:
skip_reload_env_pattern = "$^"
2018-01-16 09:47:10 +01:00
storage_provider_name = rcp.get("configuration", "registry_storage_provider_name").strip()
storage_provider_config = rcp.get("configuration", "registry_storage_provider_config").strip()
2018-01-22 13:09:24 +01:00
# yaml requires 1 or more spaces between the key and value
2018-01-25 10:34:47 +01:00
storage_provider_config = storage_provider_config.replace(":", ": ", 1)
2018-08-16 11:13:44 +02:00
registry_custom_ca_bundle_path = rcp.get("configuration", "registry_custom_ca_bundle").strip()
2016-05-27 10:21:32 +02:00
ui_secret = ''.join(random.choice(string.ascii_letters+string.digits) for i in range(16))
2018-07-16 10:50:28 +02:00
jobservice_secret = ''.join(random.choice(string.ascii_letters+string.digits) for i in range(16))
2016-05-27 10:21:32 +02:00
2017-01-12 11:29:02 +01:00
adminserver_config_dir = os.path.join(config_dir,"adminserver")
if not os.path.exists(adminserver_config_dir):
os.makedirs(os.path.join(config_dir, "adminserver"))
2017-02-20 11:40:05 +01:00
ui_config_dir = prep_conf_dir(config_dir,"ui")
2017-10-16 11:40:29 +02:00
ui_certificates_dir = prep_conf_dir(ui_config_dir,"certificates")
2017-02-20 11:40:05 +01:00
db_config_dir = prep_conf_dir(config_dir, "db")
job_config_dir = prep_conf_dir(config_dir, "jobservice")
registry_config_dir = prep_conf_dir(config_dir, "registry")
2018-07-16 10:50:28 +02:00
registryctl_config_dir = prep_conf_dir(config_dir, "registryctl")
2017-02-20 11:40:05 +01:00
nginx_config_dir = prep_conf_dir (config_dir, "nginx")
nginx_conf_d = prep_conf_dir(nginx_config_dir, "conf.d")
2017-10-13 10:06:25 +02:00
log_config_dir = prep_conf_dir (config_dir, "log")
2016-02-01 12:59:10 +01:00
2017-01-12 11:29:02 +01:00
adminserver_conf_env = os.path.join(config_dir, "adminserver", "env")
2016-02-01 12:59:10 +01:00
ui_conf_env = os.path.join(config_dir, "ui", "env")
2016-04-15 11:23:40 +02:00
ui_conf = os.path.join(config_dir, "ui", "app.conf")
2018-01-03 09:21:29 +01:00
ui_cert_dir = os.path.join(config_dir, "ui", "certificates")
2018-03-22 12:48:22 +01:00
jobservice_conf = os.path.join(config_dir, "jobservice", "config.yml")
2016-02-01 12:59:10 +01:00
registry_conf = os.path.join(config_dir, "registry", "config.yml")
2018-07-16 10:50:28 +02:00
registryctl_conf_env = os.path.join(config_dir, "registryctl", "env")
registryctl_conf_yml = os.path.join(config_dir, "registryctl", "config.yml")
2016-02-22 03:00:42 +01:00
db_conf_env = os.path.join(config_dir, "db", "env")
2016-05-27 10:21:32 +02:00
job_conf_env = os.path.join(config_dir, "jobservice", "env")
2016-10-14 11:13:15 +02:00
nginx_conf = os.path.join(config_dir, "nginx", "nginx.conf")
2017-10-13 10:06:25 +02:00
cert_dir = os.path.join(config_dir, "nginx", "cert")
2017-11-08 06:07:27 +01:00
log_rotate_config = os.path.join(config_dir, "log", "logrotate.conf")
2018-03-15 07:21:53 +01:00
adminserver_url = "http://adminserver:8080"
registry_url = "http://registry:5000"
2018-07-16 10:50:28 +02:00
registry_controller_url = "http://registryctl:8080"
2018-03-15 07:21:53 +01:00
ui_url = "http://ui:8080"
token_service_url = "http://ui:8080/service/token"
jobservice_url = "http://jobservice:8080"
clair_url = "http://clair:6060"
notary_url = "http://notary-server:4443"
2018-07-19 17:50:25 +02:00
chart_repository_url = "http://chartmuseum:9999"
2016-02-01 12:59:10 +01:00
2018-05-06 16:02:19 +02:00
if len(admiral_url) != 0 and admiral_url != "NA":
#VIC overwrites the data volume path, which by default should be same as the value of secretkey_path
DATA_VOL = secretkey_path
JOB_LOG_DIR = os.path.join(DATA_VOL, "job_logs")
if not os.path.exists(JOB_LOG_DIR):
os.makedirs(JOB_LOG_DIR)
mark_file(JOB_LOG_DIR, mode=0o755)
2016-10-14 11:13:15 +02:00
if protocol == "https":
target_cert_path = os.path.join(cert_dir, os.path.basename(cert_path))
2016-11-17 09:14:22 +01:00
if not os.path.exists(cert_dir):
os.makedirs(cert_dir)
2016-10-14 11:13:15 +02:00
shutil.copy2(cert_path,target_cert_path)
target_cert_key_path = os.path.join(cert_dir, os.path.basename(cert_key_path))
shutil.copy2(cert_key_path,target_cert_key_path)
render(os.path.join(templates_dir, "nginx", "nginx.https.conf"),
nginx_conf,
ssl_cert = os.path.join("/etc/nginx/cert", os.path.basename(target_cert_path)),
ssl_cert_key = os.path.join("/etc/nginx/cert", os.path.basename(target_cert_key_path)))
else:
render(os.path.join(templates_dir, "nginx", "nginx.http.conf"),
nginx_conf)
2018-06-27 13:26:15 +02:00
#Use reload_key to avoid reload config after restart harbor
reload_key = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(6)) if reload_config == "true" else ""
2018-07-01 03:22:25 +02:00
ldap_group_admin_dn = rcp.get("configuration", "ldap_group_admin_dn") if rcp.has_option("configuration", "ldap_group_admin_dn") else ""
2017-01-12 11:29:02 +01:00
render(os.path.join(templates_dir, "adminserver", "env"),
2017-03-09 13:12:33 +01:00
adminserver_conf_env,
2018-01-16 05:38:53 +01:00
reload_config=reload_config,
2018-03-15 07:21:53 +01:00
public_url=public_url,
2017-03-09 13:12:33 +01:00
ui_url=ui_url,
auth_mode=auth_mode,
self_registration=self_registration,
2016-02-01 12:59:10 +01:00
ldap_url=ldap_url,
2016-08-18 12:31:41 +02:00
ldap_searchdn =ldap_searchdn,
ldap_search_pwd =ldap_search_pwd,
2016-03-30 12:43:56 +02:00
ldap_basedn=ldap_basedn,
2016-08-18 12:31:41 +02:00
ldap_filter=ldap_filter,
ldap_uid=ldap_uid,
ldap_scope=ldap_scope,
2017-12-27 02:37:00 +01:00
ldap_verify_cert=ldap_verify_cert,
2017-01-12 11:29:02 +01:00
ldap_timeout=ldap_timeout,
2018-03-14 12:07:06 +01:00
ldap_group_basedn=ldap_group_basedn,
ldap_group_filter=ldap_group_filter,
ldap_group_gid=ldap_group_gid,
ldap_group_scope=ldap_group_scope,
2018-07-01 03:22:25 +02:00
ldap_group_admin_dn=ldap_group_admin_dn,
2017-01-12 11:29:02 +01:00
db_password=db_password,
2017-09-22 04:46:02 +02:00
db_host=db_host,
db_user=db_user,
2017-09-27 06:56:57 +02:00
db_port=db_port,
2017-03-09 13:12:33 +01:00
email_host=email_host,
email_port=email_port,
email_usr=email_usr,
email_pwd=email_pwd,
email_ssl=email_ssl,
2017-10-31 06:51:49 +01:00
email_insecure=email_insecure,
2017-03-09 13:12:33 +01:00
email_from=email_from,
email_identity=email_identity,
2017-01-12 11:29:02 +01:00
harbor_admin_password=harbor_admin_password,
2017-03-09 13:12:33 +01:00
project_creation_restriction=proj_cre_restriction,
max_job_workers=max_job_workers,
ui_secret=ui_secret,
jobservice_secret=jobservice_secret,
token_expiration=token_expiration,
admiral_url=admiral_url,
2017-04-27 13:01:45 +02:00
with_notary=args.notary_mode,
2017-07-17 09:00:48 +02:00
with_clair=args.clair_mode,
2017-12-19 08:25:12 +01:00
clair_db_password=clair_db_password,
clair_db_host=clair_db_host,
clair_db_port=clair_db_port,
clair_db_username=clair_db_username,
clair_db=clair_db,
2017-10-01 23:03:53 +02:00
uaa_endpoint=uaa_endpoint,
uaa_clientid=uaa_clientid,
2017-12-19 07:42:07 +01:00
uaa_clientsecret=uaa_clientsecret,
2018-01-16 09:47:10 +01:00
uaa_verify_cert=uaa_verify_cert,
2018-03-15 07:21:53 +01:00
storage_provider_name=storage_provider_name,
registry_url=registry_url,
token_service_url=token_service_url,
jobservice_url=jobservice_url,
clair_url=clair_url,
2018-06-27 13:26:15 +02:00
notary_url=notary_url,
reload_key=reload_key,
2018-07-19 10:47:05 +02:00
skip_reload_env_pattern=skip_reload_env_pattern,
2018-07-19 17:50:25 +02:00
chart_repository_url=chart_repository_url,
registry_controller_url = registry_controller_url,
with_chartmuseum=args.chart_mode
2017-01-12 11:29:02 +01:00
)
2016-02-01 12:59:10 +01:00
2018-07-19 17:50:25 +02:00
# set cache for chart repo server
# default set 'memory' mode, if redis is configured then set to 'redis'
chart_cache_driver = "memory"
2018-08-09 09:27:46 +02:00
if len(redis_host) > 0:
2018-07-19 17:50:25 +02:00
chart_cache_driver = "redis"
2017-01-12 11:29:02 +01:00
render(os.path.join(templates_dir, "ui", "env"),
2017-03-09 13:12:33 +01:00
ui_conf_env,
ui_secret=ui_secret,
2017-07-17 09:00:48 +02:00
jobservice_secret=jobservice_secret,
2018-08-09 09:27:46 +02:00
redis_host=redis_host,
redis_port=redis_port,
redis_password=redis_password,
2018-07-19 17:50:25 +02:00
adminserver_url = adminserver_url,
2018-08-09 13:04:35 +02:00
chart_cache_driver = chart_cache_driver,
redis_url_reg = redis_url_reg)
2018-01-07 10:23:18 +01:00
2018-08-27 12:24:21 +02:00
registry_config_file = "config.yml"
2018-01-07 10:23:18 +01:00
if storage_provider_name == "filesystem":
if not storage_provider_config:
storage_provider_config = "rootdirectory: /storage"
elif "rootdirectory:" not in storage_provider_config:
storage_provider_config = "rootdirectory: /storage" + "," + storage_provider_config
# generate storage configuration section in yaml format
2018-06-13 12:13:47 +02:00
storage_provider_conf_list = [storage_provider_name + ':']
for c in storage_provider_config.split(","):
2018-08-27 10:18:07 +02:00
kvs = c.split(": ")
if len(kvs) == 2:
if kvs[0].strip() == "keyfile":
srcKeyFile = kvs[1].strip()
if os.path.isfile(srcKeyFile):
shutil.copyfile(srcKeyFile, os.path.join(registry_config_dir, "gcs.key"))
storage_provider_conf_list.append("keyfile: %s" % "/etc/registry/gcs.key")
continue
2018-06-13 12:13:47 +02:00
storage_provider_conf_list.append(c.strip())
storage_provider_info = ('\n' + ' ' * 4).join(storage_provider_conf_list)
2018-01-07 10:23:18 +01:00
render(os.path.join(templates_dir, "registry", registry_config_file),
registry_conf,
storage_provider_info=storage_provider_info,
2018-03-15 07:21:53 +01:00
public_url=public_url,
2018-01-07 10:23:18 +01:00
ui_url=ui_url,
2018-08-09 09:27:46 +02:00
redis_host=redis_host,
redis_port=redis_port,
redis_password=redis_password,
redis_db_index_reg=redis_db_index_reg)
2016-02-01 12:59:10 +01:00
2016-02-22 03:00:42 +01:00
render(os.path.join(templates_dir, "db", "env"),
db_conf_env,
db_password=db_password)
2016-05-27 10:21:32 +02:00
render(os.path.join(templates_dir, "jobservice", "env"),
job_conf_env,
2017-02-23 11:02:19 +01:00
ui_secret=ui_secret,
2018-03-15 07:21:53 +01:00
jobservice_secret=jobservice_secret,
adminserver_url=adminserver_url)
2018-04-12 10:17:57 +02:00
render(os.path.join(templates_dir, "jobservice", "config.yml"),
jobservice_conf,
max_job_workers=max_job_workers,
2018-08-09 09:27:46 +02:00
redis_url=redis_url_js)
2017-11-08 06:07:27 +01:00
render(os.path.join(templates_dir, "log", "logrotate.conf"),
log_rotate_config,
log_rotate_count=log_rotate_count,
log_rotate_size=log_rotate_size)
2017-03-09 13:12:33 +01:00
2018-07-16 10:50:28 +02:00
render(os.path.join(templates_dir, "registryctl", "env"),
registryctl_conf_env,
jobservice_secret=jobservice_secret,
ui_secret=ui_secret)
2017-01-12 11:29:02 +01:00
shutil.copyfile(os.path.join(templates_dir, "ui", "app.conf"), ui_conf)
2018-07-16 10:50:28 +02:00
shutil.copyfile(os.path.join(templates_dir, "registryctl", "config.yml"), registryctl_conf_yml)
2018-06-13 12:13:47 +02:00
print("Generated configuration file: %s" % ui_conf)
2017-01-12 11:29:02 +01:00
2018-01-03 09:21:29 +01:00
if auth_mode == "uaa_auth":
if os.path.isfile(uaa_ca_cert):
if not os.path.isdir(ui_cert_dir):
2018-04-08 17:43:45 +02:00
os.makedirs(ui_cert_dir)
2018-01-03 09:21:29 +01:00
ui_uaa_ca = os.path.join(ui_cert_dir, "uaa_ca.pem")
print("Copying UAA CA cert to %s" % ui_uaa_ca)
shutil.copyfile(uaa_ca_cert, ui_uaa_ca)
else:
print("Can not find UAA CA cert: %s, skip" % uaa_ca_cert)
2017-01-12 11:29:02 +01:00
2016-04-25 10:08:16 +02:00
def validate_crt_subj(dirty_subj):
subj_list = [item for item in dirty_subj.strip().split("/") \
if len(item.split("=")) == 2 and len(item.split("=")[1]) > 0]
return "/" + "/".join(subj_list)
FNULL = open(os.devnull, 'w')
from functools import wraps
def stat_decorator(func):
2016-05-09 12:50:04 +02:00
@wraps(func)
2017-03-23 14:00:53 +01:00
def check_wrapper(*args, **kw):
stat = func(*args, **kw)
message = "Generated certificate, key file: %s, cert file: %s" % (kw['key_path'], kw['cert_path']) \
if stat == 0 else "Fail to generate key file: %s, cert file: %s" % (kw['key_path'], kw['cert_path'])
2016-04-25 10:08:16 +02:00
print(message)
if stat != 0:
sys.exit(1)
return check_wrapper
@stat_decorator
2017-03-23 14:00:53 +01:00
def create_root_cert(subj, key_path="./k.key", cert_path="./cert.crt"):
2017-03-23 14:00:53 +01:00
rc = subprocess.call(["openssl", "genrsa", "-out", key_path, "4096"], stdout=FNULL, stderr=subprocess.STDOUT)
2017-03-23 14:00:53 +01:00
if rc != 0:
return rc
return subprocess.call(["openssl", "req", "-new", "-x509", "-key", key_path,\
2017-03-23 14:00:53 +01:00
"-out", cert_path, "-days", "3650", "-subj", subj], stdout=FNULL, stderr=subprocess.STDOUT)
2016-04-25 10:08:16 +02:00
@stat_decorator
2017-03-23 14:00:53 +01:00
def create_cert(subj, ca_key, ca_cert, key_path="./k.key", cert_path="./cert.crt"):
cert_dir = os.path.dirname(cert_path)
csr_path = os.path.join(cert_dir, "tmp.csr")
rc = subprocess.call(["openssl", "req", "-newkey", "rsa:4096", "-nodes","-sha256","-keyout", key_path,\
2017-03-23 14:00:53 +01:00
"-out", csr_path, "-subj", subj], stdout=FNULL, stderr=subprocess.STDOUT)
2017-03-23 14:00:53 +01:00
if rc != 0:
return rc
return subprocess.call(["openssl", "x509", "-req", "-days", "3650", "-in", csr_path, "-CA", \
2017-03-23 14:00:53 +01:00
ca_cert, "-CAkey", ca_key, "-CAcreateserial", "-out", cert_path], stdout=FNULL, stderr=subprocess.STDOUT)
2016-04-25 10:08:16 +02:00
2017-03-23 14:00:53 +01:00
def openssl_installed():
shell_stat = subprocess.check_call(["which", "openssl"], stdout=FNULL, stderr=subprocess.STDOUT)
if shell_stat != 0:
2016-04-25 10:08:16 +02:00
print("Cannot find openssl installed in this computer\nUse default SSL certificate file")
return False
2017-03-23 14:00:53 +01:00
return True
2016-04-25 10:08:16 +02:00
2017-03-23 14:00:53 +01:00
if customize_crt == 'on' and openssl_installed():
2016-04-25 10:08:16 +02:00
shell_stat = subprocess.check_call(["which", "openssl"], stdout=FNULL, stderr=subprocess.STDOUT)
2018-06-27 16:50:26 +02:00
empty_subj = "/"
2017-03-23 14:00:53 +01:00
private_key_pem = os.path.join(config_dir, "ui", "private_key.pem")
root_crt = os.path.join(config_dir, "registry", "root.crt")
create_root_cert(empty_subj, key_path=private_key_pem, cert_path=root_crt)
2018-04-08 17:43:45 +02:00
mark_file(private_key_pem)
mark_file(root_crt)
2016-11-09 11:21:28 +01:00
else:
2017-03-23 14:00:53 +01:00
print("Copied configuration file: %s" % ui_config_dir + "private_key.pem")
2016-11-09 11:21:28 +01:00
shutil.copyfile(os.path.join(templates_dir, "ui", "private_key.pem"), os.path.join(ui_config_dir, "private_key.pem"))
2017-03-23 14:00:53 +01:00
print("Copied configuration file: %s" % registry_config_dir + "root.crt")
2016-11-09 11:21:28 +01:00
shutil.copyfile(os.path.join(templates_dir, "registry", "root.crt"), os.path.join(registry_config_dir, "root.crt"))
2017-09-27 06:56:57 +02:00
2018-08-16 11:13:44 +02:00
if len(registry_custom_ca_bundle_path) > 0 and os.path.isfile(registry_custom_ca_bundle_path):
shutil.copyfile(registry_custom_ca_bundle_path, os.path.join(registry_config_dir, "custom-ca-bundle.crt"))
print("Copied custom ca bundle: %s" % os.path.join(registry_config_dir, "custom-ca-bundle.crt"))
2017-02-20 11:40:05 +01:00
if args.notary_mode:
notary_config_dir = prep_conf_dir(config_dir, "notary")
notary_temp_dir = os.path.join(templates_dir, "notary")
print("Copying sql file for notary DB")
2018-04-27 11:27:12 +02:00
# if os.path.exists(os.path.join(notary_config_dir, "postgresql-initdb.d")):
# shutil.rmtree(os.path.join(notary_config_dir, "postgresql-initdb.d"))
# shutil.copytree(os.path.join(notary_temp_dir, "postgresql-initdb.d"), os.path.join(notary_config_dir, "postgresql-initdb.d"))
2017-03-23 14:00:53 +01:00
if customize_crt == 'on' and openssl_installed():
2017-03-29 10:39:57 +02:00
try:
temp_cert_dir = os.path.join(base_dir, "cert_tmp")
if not os.path.exists(temp_cert_dir):
os.makedirs(temp_cert_dir)
2018-08-10 08:16:59 +02:00
ca_subj = "/C=US/ST=California/L=Palo Alto/O=GoHarbor/OU=Harbor/CN=Self-signed by GoHarbor"
cert_subj = "/C=US/ST=California/L=Palo Alto/O=GoHarbor/OU=Harbor/CN=notarysigner"
2017-03-29 10:39:57 +02:00
signer_ca_cert = os.path.join(temp_cert_dir, "notary-signer-ca.crt")
signer_ca_key = os.path.join(temp_cert_dir, "notary-signer-ca.key")
signer_cert_path = os.path.join(temp_cert_dir, "notary-signer.crt")
signer_key_path = os.path.join(temp_cert_dir, "notary-signer.key")
create_root_cert(ca_subj, key_path=signer_ca_key, cert_path=signer_ca_cert)
create_cert(cert_subj, signer_ca_key, signer_ca_cert, key_path=signer_key_path, cert_path=signer_cert_path)
print("Copying certs for notary signer")
shutil.copy2(signer_cert_path, notary_config_dir)
shutil.copy2(signer_key_path, notary_config_dir)
shutil.copy2(signer_ca_cert, notary_config_dir)
finally:
srl_tmp = os.path.join(os.getcwd(), ".srl")
if os.path.isfile(srl_tmp):
os.remove(srl_tmp)
if os.path.isdir(temp_cert_dir):
shutil.rmtree(temp_cert_dir, True)
2017-03-23 14:00:53 +01:00
else:
print("Copying certs for notary signer")
shutil.copy2(os.path.join(notary_temp_dir, "notary-signer.crt"), notary_config_dir)
shutil.copy2(os.path.join(notary_temp_dir, "notary-signer.key"), notary_config_dir)
shutil.copy2(os.path.join(notary_temp_dir, "notary-signer-ca.crt"), notary_config_dir)
2017-02-20 11:40:05 +01:00
shutil.copy2(os.path.join(registry_config_dir, "root.crt"), notary_config_dir)
2018-04-08 17:43:45 +02:00
mark_file(os.path.join(notary_config_dir, "notary-signer.crt"))
mark_file(os.path.join(notary_config_dir, "notary-signer.key"))
mark_file(os.path.join(notary_config_dir, "notary-signer-ca.crt"))
mark_file(os.path.join(notary_config_dir, "root.crt"))
2017-03-16 09:09:05 +01:00
print("Copying notary signer configuration file")
2018-05-06 16:02:19 +02:00
#Call render instead of copy so the umask will take effect to mark the file as 0644
render(os.path.join(notary_temp_dir, "signer-config.postgres.json"),
os.path.join(notary_config_dir, "signer-config.postgres.json"))
2018-04-27 11:27:12 +02:00
render(os.path.join(notary_temp_dir, "server-config.postgres.json"),
os.path.join(notary_config_dir, "server-config.postgres.json"),
2018-03-15 07:21:53 +01:00
token_endpoint=public_url)
2017-03-16 09:09:05 +01:00
print("Copying nginx configuration file for notary")
shutil.copy2(os.path.join(templates_dir, "nginx", "notary.upstream.conf"), nginx_conf_d)
2017-03-28 04:11:13 +02:00
render(os.path.join(templates_dir, "nginx", "notary.server.conf"),
os.path.join(nginx_conf_d, "notary.server.conf"),
ssl_cert = os.path.join("/etc/nginx/cert", os.path.basename(target_cert_path)),
ssl_cert_key = os.path.join("/etc/nginx/cert", os.path.basename(target_cert_key_path)))
2017-03-24 13:05:13 +01:00
default_alias = get_alias(secretkey_path)
2017-02-20 11:40:05 +01:00
render(os.path.join(notary_temp_dir, "signer_env"), os.path.join(notary_config_dir, "signer_env"), alias = default_alias)
2018-04-27 11:27:12 +02:00
shutil.copy2(os.path.join(notary_temp_dir, "server_env"), notary_config_dir)
2017-02-20 11:40:05 +01:00
2017-04-27 13:01:45 +02:00
if args.clair_mode:
clair_temp_dir = os.path.join(templates_dir, "clair")
clair_config_dir = prep_conf_dir(config_dir, "clair")
2017-06-28 09:45:16 +02:00
if os.path.exists(os.path.join(clair_config_dir, "postgresql-init.d")):
2017-07-17 09:00:48 +02:00
print("Copying offline data file for clair DB")
2017-06-28 09:45:16 +02:00
shutil.rmtree(os.path.join(clair_config_dir, "postgresql-init.d"))
shutil.copytree(os.path.join(clair_temp_dir, "postgresql-init.d"), os.path.join(clair_config_dir, "postgresql-init.d"))
2017-04-27 13:01:45 +02:00
postgres_env = os.path.join(clair_config_dir, "postgres_env")
2017-12-19 08:25:12 +01:00
render(os.path.join(clair_temp_dir, "postgres_env"), postgres_env, password = clair_db_password)
2017-04-27 13:01:45 +02:00
clair_conf = os.path.join(clair_config_dir, "config.yaml")
2017-12-19 08:25:12 +01:00
render(os.path.join(clair_temp_dir, "config.yaml"),
clair_conf,
password = clair_db_password,
username = clair_db_username,
host = clair_db_host,
2018-03-28 10:00:53 +02:00
port = clair_db_port,
2018-07-26 10:02:22 +02:00
dbname = clair_db,
interval = clair_updaters_interval)
2018-03-28 10:00:53 +02:00
# config http proxy for Clair
2018-03-28 12:49:40 +02:00
http_proxy = rcp.get("configuration", "http_proxy").strip()
https_proxy = rcp.get("configuration", "https_proxy").strip()
no_proxy = rcp.get("configuration", "no_proxy").strip()
clair_env = os.path.join(clair_config_dir, "clair_env")
render(os.path.join(clair_temp_dir, "clair_env"), clair_env,
http_proxy = http_proxy,
https_proxy = https_proxy,
no_proxy = no_proxy)
2017-04-27 13:01:45 +02:00
2018-07-19 10:47:05 +02:00
# config chart repository
if args.chart_mode:
chartm_temp_dir = os.path.join(templates_dir, "chartserver")
2018-07-19 17:50:25 +02:00
chartm_config_dir = os.path.join(config_dir, "chartserver")
2018-07-19 10:47:05 +02:00
chartm_env = os.path.join(config_dir, "chartserver", "env")
2018-07-19 17:50:25 +02:00
if not os.path.isdir(chartm_config_dir):
print ("Create config folder: %s" % chartm_config_dir)
os.makedirs(chartm_config_dir)
2018-07-19 10:47:05 +02:00
2018-08-27 10:18:07 +02:00
# handle custom ca bundle
if len(registry_custom_ca_bundle_path) > 0 and os.path.isfile(registry_custom_ca_bundle_path):
shutil.copyfile(registry_custom_ca_bundle_path, os.path.join(chartm_config_dir, "custom-ca-bundle.crt"))
print("Copied custom ca bundle: %s" % os.path.join(chartm_config_dir, "custom-ca-bundle.crt"))
2018-07-19 10:47:05 +02:00
# process redis info
2018-08-09 09:27:46 +02:00
cache_store = "redis"
cache_redis_password = redis_password
cache_redis_addr = redis_host+":"+redis_port
cache_redis_db_index = redis_db_index_chart
2018-07-19 10:47:05 +02:00
# process storage info
#default using local file system
storage_driver = "local"
# storage provider configurations
# please be aware that, we do not check the validations of the values for the specified keys
# convert the configs to config map
storage_provider_configs = storage_provider_config.split(",")
storgae_provider_confg_map = {}
storage_provider_config_options = []
for k_v in storage_provider_configs:
if len(k_v) > 0:
kvs = k_v.split(": ") # add space suffix to avoid existing ":" in the value
if len(kvs) == 2:
#key must not be empty
if kvs[0].strip() != "":
storgae_provider_confg_map[kvs[0].strip()] = kvs[1].strip()
if storage_provider_name == "s3":
# aws s3 storage
storage_driver = "amazon"
storage_provider_config_options.append("STORAGE_AMAZON_BUCKET=%s" % storgae_provider_confg_map.get("bucket", ""))
storage_provider_config_options.append("STORAGE_AMAZON_PREFIX=%s" % storgae_provider_confg_map.get("rootdirectory", ""))
storage_provider_config_options.append("STORAGE_AMAZON_REGION=%s" % storgae_provider_confg_map.get("region", ""))
storage_provider_config_options.append("STORAGE_AMAZON_ENDPOINT=%s" % storgae_provider_confg_map.get("regionendpoint", ""))
2018-08-27 10:18:07 +02:00
storage_provider_config_options.append("AWS_ACCESS_KEY_ID=%s" % storgae_provider_confg_map.get("accesskey", ""))
storage_provider_config_options.append("AWS_SECRET_ACCESS_KEY=%s" % storgae_provider_confg_map.get("secretkey", ""))
2018-07-19 10:47:05 +02:00
elif storage_provider_name == "gcs":
# google cloud storage
storage_driver = "google"
storage_provider_config_options.append("STORAGE_GOOGLE_BUCKET=%s" % storgae_provider_confg_map.get("bucket", ""))
storage_provider_config_options.append("STORAGE_GOOGLE_PREFIX=%s" % storgae_provider_confg_map.get("rootdirectory", ""))
2018-08-27 10:18:07 +02:00
keyFileOnHost = storgae_provider_confg_map.get("keyfile", "")
if os.path.isfile(keyFileOnHost):
shutil.copyfile(keyFileOnHost, os.path.join(chartm_config_dir, "gcs.key"))
targetKeyFile = "/etc/chartserver/gcs.key"
storage_provider_config_options.append("GOOGLE_APPLICATION_CREDENTIALS=%s" % targetKeyFile)
2018-07-19 10:47:05 +02:00
elif storage_provider_name == "azure":
# azure storage
storage_driver = "microsoft"
storage_provider_config_options.append("STORAGE_MICROSOFT_CONTAINER=%s" % storgae_provider_confg_map.get("container", ""))
2018-08-27 10:18:07 +02:00
storage_provider_config_options.append("AZURE_STORAGE_ACCOUNT=%s" % storgae_provider_confg_map.get("accountname", ""))
storage_provider_config_options.append("AZURE_STORAGE_ACCESS_KEY=%s" % storgae_provider_confg_map.get("accountkey", ""))
2018-07-19 10:47:05 +02:00
storage_provider_config_options.append("STORAGE_MICROSOFT_PREFIX=/azure/harbor/charts")
elif storage_provider_name == "swift":
# open stack swift
storage_driver = "openstack"
storage_provider_config_options.append("STORAGE_OPENSTACK_CONTAINER=%s" % storgae_provider_confg_map.get("container", ""))
storage_provider_config_options.append("STORAGE_OPENSTACK_PREFIX=%s" % storgae_provider_confg_map.get("rootdirectory", ""))
storage_provider_config_options.append("STORAGE_OPENSTACK_REGION=%s" % storgae_provider_confg_map.get("region", ""))
2018-08-27 10:18:07 +02:00
storage_provider_config_options.append("OS_AUTH_URL=%s" % storgae_provider_confg_map.get("authurl", ""))
storage_provider_config_options.append("OS_USERNAME=%s" % storgae_provider_confg_map.get("username", ""))
storage_provider_config_options.append("OS_PASSWORD=%s" % storgae_provider_confg_map.get("password", ""))
storage_provider_config_options.append("OS_PROJECT_ID=%s" % storgae_provider_confg_map.get("tenantid", ""))
storage_provider_config_options.append("OS_PROJECT_NAME=%s" % storgae_provider_confg_map.get("tenant", ""))
storage_provider_config_options.append("OS_DOMAIN_ID=%s" % storgae_provider_confg_map.get("domainid", ""))
storage_provider_config_options.append("OS_DOMAIN_NAME=%s" % storgae_provider_confg_map.get("domain", ""))
2018-07-19 10:47:05 +02:00
elif storage_provider_name == "oss":
# aliyun OSS
storage_driver = "alibaba"
storage_provider_config_options.append("STORAGE_ALIBABA_BUCKET=%s" % storgae_provider_confg_map.get("bucket", ""))
storage_provider_config_options.append("STORAGE_ALIBABA_PREFIX=%s" % storgae_provider_confg_map.get("rootdirectory", ""))
storage_provider_config_options.append("STORAGE_ALIBABA_ENDPOINT=%s" % storgae_provider_confg_map.get("endpoint", ""))
2018-08-27 10:18:07 +02:00
storage_provider_config_options.append("ALIBABA_CLOUD_ACCESS_KEY_ID=%s" % storgae_provider_confg_map.get("accesskeyid", ""))
storage_provider_config_options.append("ALIBABA_CLOUD_ACCESS_KEY_SECRET=%s" % storgae_provider_confg_map.get("accesskeysecret", ""))
2018-07-19 10:47:05 +02:00
else:
# use local file system
storage_provider_config_options.append("STORAGE_LOCAL_ROOTDIR=/chart_storage")
# generate storage provider configuration
all_storage_provider_configs = ('\n').join(storage_provider_config_options)
render(os.path.join(chartm_temp_dir, "env"),
chartm_env,
2018-07-19 17:50:25 +02:00
cache_store=cache_store,
2018-07-19 10:47:05 +02:00
cache_redis_addr=cache_redis_addr,
cache_redis_password=cache_redis_password,
cache_redis_db_index=cache_redis_db_index,
ui_secret=ui_secret,
storage_driver=storage_driver,
all_storage_driver_configs=all_storage_provider_configs)
2017-03-23 14:00:53 +01:00
FNULL.close()
2018-05-06 16:02:19 +02:00
print("The configuration files are ready, please use docker-compose to start the service.")