1# This file is part of cloud-init. See LICENSE file for license information. 2 3from cloudinit import dmi 4from cloudinit import sources 5from cloudinit.sources import DataSourceEc2 as EC2 6 7ALIYUN_PRODUCT = "Alibaba Cloud ECS" 8 9 10class DataSourceAliYun(EC2.DataSourceEc2): 11 12 dsname = 'AliYun' 13 metadata_urls = ['http://100.100.100.200'] 14 15 # The minimum supported metadata_version from the ec2 metadata apis 16 min_metadata_version = '2016-01-01' 17 extended_metadata_versions = [] 18 19 def get_hostname(self, fqdn=False, resolve_ip=False, metadata_only=False): 20 return self.metadata.get('hostname', 'localhost.localdomain') 21 22 def get_public_ssh_keys(self): 23 return parse_public_keys(self.metadata.get('public-keys', {})) 24 25 def _get_cloud_name(self): 26 if _is_aliyun(): 27 return EC2.CloudNames.ALIYUN 28 else: 29 return EC2.CloudNames.NO_EC2_METADATA 30 31 32def _is_aliyun(): 33 return dmi.read_dmi_data('system-product-name') == ALIYUN_PRODUCT 34 35 36def parse_public_keys(public_keys): 37 keys = [] 38 for _key_id, key_body in public_keys.items(): 39 if isinstance(key_body, str): 40 keys.append(key_body.strip()) 41 elif isinstance(key_body, list): 42 keys.extend(key_body) 43 elif isinstance(key_body, dict): 44 key = key_body.get('openssh-key', []) 45 if isinstance(key, str): 46 keys.append(key.strip()) 47 elif isinstance(key, list): 48 keys.extend(key) 49 return keys 50 51 52# Used to match classes to dependencies 53datasources = [ 54 (DataSourceAliYun, (sources.DEP_FILESYSTEM, sources.DEP_NETWORK)), 55] 56 57 58# Return a list of data sources that match this set of dependencies 59def get_datasource_list(depends): 60 return sources.list_from_depends(depends, datasources) 61 62# vi: ts=4 expandtab 63