File: //proc/self/root/lib/python3/dist-packages/cloudinit/config/cc_customize_project.py
# Copyright (C) 2021-2030 Baidu Ltd.
#
# Author: dudeli<dudeli@baidu.com>
#
# This file is part of cloud-init. See LICENSE file for license information.
"""
Custom project
-------------
**Summary:** Set customize project
Set repo/ntp/bcm for specified project.
This a function for baidu cloud custom.
**Internal name:** ``cc_customize_project``
**Module frequency:** per instance
**Supported distros:** all
**Config keys**::
project_info: repourl|ntpurl|bcmurl
project:
repo_url: mirrors.xxx.com
ntp_url: ntp.xxx.com
bcm_url: bcm.xxx.com
"""
import os
from cloudinit import log as logging
from cloudinit import util
from cloudinit.settings import PER_INSTANCE
frequency = PER_INSTANCE
LOG = logging.getLogger(__name__)
def read_project_name(filename):
"""
read project name from filename
Args:
filename: filename string
Returns:
string contains project
"""
try:
project_name = util.load_file(filename)
except IOError:
project_name = ""
return project_name
def replace_file_str(filename, old, new):
"""
replace old strings to new
Args:
filename: filename string
old: string to be replaced
new: target string
Returns:
"""
if not os.path.exists(filename):
LOG.debug("No %s found!!", filename)
return
else:
LOG.debug("%s found. change %s to %s", filename, old, new)
c = None
with open(filename, 'r') as f:
c = f.read()
c = c.replace(old, new)
with open(filename, 'w') as f:
f.write(c)
def set_project_repo(url):
"""
set repo url
Args:
url: url to be replaced
Returns:
"""
org_url = 'mirrors.baidubce.com'
repo_path = ''
repo_paths = ['/etc/yum.repos.d',
'/etc/apt',
'/etc/zypp/repos.d']
for p in repo_paths:
if os.path.exists(p):
repo_path = p
if not repo_path:
LOG.debug('No repo path found!! set skipped.')
return
with util.SeLinuxGuard(repo_path, recursive=True):
for f in os.listdir(repo_path):
if f.endswith('repo') or f.endswith('list'):
replace_file_str(os.path.join(repo_path, f), org_url, url)
def set_project_bcm(url):
"""
set bcm update url
Args:
url: url to be replaced
Returns:
"""
org_url = 'download.bcm.baidubce.com'
config_file = '/etc/config/bcm-agent.conf'
if os.path.exists(config_file):
with util.SeLinuxGuard(os.path.dirname(config_file), recursive=True):
replace_file_str(config_file, org_url, url)
service_file = '/etc/init.d/bcm-agent'
with util.SeLinuxGuard(os.path.dirname(service_file), recursive=True):
replace_file_str(service_file, org_url, url)
def set_project_ntp(url):
"""
set ntp url
Args:
url: url to be replaced
Returns:
"""
org_url = 'ntpsr.baidubce.com'
ntp_files = ['/etc/chrony.conf',
'/etc/chrony/chrony.conf',
'/etc/systemd/timesyncd.conf',
'/etc/ntp.conf',
'/etc/ntp/step-tickers']
for f in ntp_files:
with util.SeLinuxGuard(os.path.dirname(f), recursive=True):
replace_file_str(f, org_url, url)
def convert_str_to_cfg(project_str):
"""
convert str to dict cfg
Args:
project_str: cfg string
Returns:
a dict of cfg
"""
if not project_str:
return None
project_dict = {}
key_list = ['project_name', 'repo_url', 'ntp_url', 'bcm_url']
strs = project_str.split('|')
for i in range(0, len(strs)):
project_dict[key_list[i]] = strs[i]
return project_dict
def handle(_name, cfg, cloud, log, args):
"""
handle customize project module
Args:
_name: module name
cfg: cloud config
cloud: cloudinit object wrapper
log: logger
args: extra args
Returns:
"""
if 'project_info' in cfg:
project_str = util.get_cfg_option_str(cfg, "project_info", None)
project_cfg = convert_str_to_cfg(project_str)
elif 'project' in cfg:
project_cfg = cfg['project']
else:
log.debug(("Skipping module named %s,"
" no 'project/project_info' key in configuration"), _name)
return
project_fn = os.path.join(cloud.get_cpath('data'), "project")
project_name = read_project_name(project_fn)
if 'project_name' in project_cfg:
new_project_name = project_cfg['project_name']
if project_name and project_name != new_project_name:
LOG.debug('Project name changed! %s to %s', project_name,
new_project_name)
else:
util.write_file(project_fn, new_project_name, '0644')
if 'repo_url' in project_cfg:
set_project_repo(project_cfg['repo_url'])
if 'ntp_url' in project_cfg:
set_project_ntp(project_cfg['ntp_url'])
if 'bcm_url' in project_cfg:
set_project_bcm(project_cfg['bcm_url'])
# vi: ts=4 expandtab