# 需要导入模块: import requests [as 别名]
# 或者: from requests import packages [as 别名]
def get_cloud_init_data(self, radl=None, vm=None, public_key=None, user=None):
"""
Get the cloud init data specified by the user in the RADL
"""
cloud_config = {}
if radl:
configure_name = None
if radl.contextualize.items:
system_name = radl.systems[0].name
for item in radl.contextualize.items.values():
if item.system == system_name and item.get_ctxt_tool() == "cloud_init":
configure_name = item.configure
if configure_name:
cloud_config = yaml.safe_load(radl.get_configure_by_name(configure_name).recipes)
if not isinstance(cloud_config, dict):
# The cloud_init data is a shell script
cloud_config = radl.get_configure_by_name(configure_name).recipes.strip()
self.log_debug(cloud_config)
return cloud_config
# Only for those VMs with private IP
if Config.SSH_REVERSE_TUNNELS and vm and not vm.hasPublicNet():
if 'packages' not in cloud_config:
cloud_config['packages'] = []
cloud_config['packages'].extend(["curl", "sshpass"])
curl_command = vm.get_boot_curl_commands()
if 'bootcmd' not in cloud_config:
cloud_config['bootcmd'] = []
cloud_config['bootcmd'].extend(curl_command)
if vm and vm.getSSHPort() != 22:
if 'bootcmd' not in cloud_config:
cloud_config['bootcmd'] = []
cloud_config['bootcmd'].append("sed -i '/Port 22/c\\Port %s' /etc/ssh/sshd_config" % vm.getSSHPort())
cloud_config['bootcmd'].append("service sshd restart")
if public_key:
user_data = {}
user_data['name'] = user
user_data['sudo'] = "ALL=(ALL) NOPASSWD:ALL"
user_data['lock-passwd'] = True
user_data['ssh-import-id'] = user
# avoid to use default /home dir
user_data['homedir'] = "/opt/%s" % user
user_data['ssh-authorized-keys'] = [public_key.strip()]
if 'users' not in cloud_config:
cloud_config['users'] = []
cloud_config['users'].append(user_data)
if cloud_config:
if 'merge_how' not in cloud_config:
cloud_config['merge_how'] = 'list(append)+dict(recurse_array,no_replace)+str()'
res = yaml.safe_dump(cloud_config, default_flow_style=False, width=512)
self.log_debug("#cloud-config\n%s" % res)
return "#cloud-config\n%s" % res
else:
return None