~cloud-init-dev/cloud-init/trunk

« back to all changes in this revision

Viewing changes to cloudinit/sources/DataSourceDigitalOcean.py

  • Committer: Scott Moser
  • Date: 2016-08-10 15:06:15 UTC
  • Revision ID: smoser@ubuntu.com-20160810150615-ma2fv107w3suy1ma
README: Mention move of revision control to git.

cloud-init development has moved its revision control to git.
It is available at 
  https://code.launchpad.net/cloud-init

Clone with 
  git clone https://git.launchpad.net/cloud-init
or
  git clone git+ssh://git.launchpad.net/cloud-init

For more information see
  https://git.launchpad.net/cloud-init/tree/HACKING.rst

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vi: ts=4 expandtab
2
 
#
3
 
#    Author: Neal Shrader <neal@digitalocean.com>
4
 
#
5
 
#    This program is free software: you can redistribute it and/or modify
6
 
#    it under the terms of the GNU General Public License version 3, as
7
 
#    published by the Free Software Foundation.
8
 
#
9
 
#    This program is distributed in the hope that it will be useful,
10
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
#    GNU General Public License for more details.
13
 
#
14
 
#    You should have received a copy of the GNU General Public License
15
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 
 
17
 
from cloudinit import ec2_utils
18
 
from cloudinit import log as logging
19
 
from cloudinit import sources
20
 
from cloudinit import util
21
 
 
22
 
import functools
23
 
 
24
 
 
25
 
LOG = logging.getLogger(__name__)
26
 
 
27
 
BUILTIN_DS_CONFIG = {
28
 
    'metadata_url': 'http://169.254.169.254/metadata/v1/',
29
 
    'mirrors_url': 'http://mirrors.digitalocean.com/'
30
 
}
31
 
MD_RETRIES = 0
32
 
MD_TIMEOUT = 1
33
 
 
34
 
 
35
 
class DataSourceDigitalOcean(sources.DataSource):
36
 
    def __init__(self, sys_cfg, distro, paths):
37
 
        sources.DataSource.__init__(self, sys_cfg, distro, paths)
38
 
        self.metadata = dict()
39
 
        self.ds_cfg = util.mergemanydict([
40
 
            util.get_cfg_by_path(sys_cfg, ["datasource", "DigitalOcean"], {}),
41
 
            BUILTIN_DS_CONFIG])
42
 
        self.metadata_address = self.ds_cfg['metadata_url']
43
 
 
44
 
        if self.ds_cfg.get('retries'):
45
 
            self.retries = self.ds_cfg['retries']
46
 
        else:
47
 
            self.retries = MD_RETRIES
48
 
 
49
 
        if self.ds_cfg.get('timeout'):
50
 
            self.timeout = self.ds_cfg['timeout']
51
 
        else:
52
 
            self.timeout = MD_TIMEOUT
53
 
 
54
 
    def get_data(self):
55
 
        caller = functools.partial(util.read_file_or_url,
56
 
                                   timeout=self.timeout, retries=self.retries)
57
 
 
58
 
        def mcaller(url):
59
 
            return caller(url).contents
60
 
 
61
 
        md = ec2_utils.MetadataMaterializer(mcaller(self.metadata_address),
62
 
                                            base_url=self.metadata_address,
63
 
                                            caller=mcaller)
64
 
 
65
 
        self.metadata = md.materialize()
66
 
 
67
 
        if self.metadata.get('id'):
68
 
            return True
69
 
        else:
70
 
            return False
71
 
 
72
 
    def get_userdata_raw(self):
73
 
        return "\n".join(self.metadata['user-data'])
74
 
 
75
 
    def get_vendordata_raw(self):
76
 
        return "\n".join(self.metadata['vendor-data'])
77
 
 
78
 
    def get_public_ssh_keys(self):
79
 
        public_keys = self.metadata['public-keys']
80
 
        if isinstance(public_keys, list):
81
 
            return public_keys
82
 
        else:
83
 
            return [public_keys]
84
 
 
85
 
    @property
86
 
    def availability_zone(self):
87
 
        return self.metadata['region']
88
 
 
89
 
    def get_instance_id(self):
90
 
        return self.metadata['id']
91
 
 
92
 
    def get_hostname(self, fqdn=False, resolve_ip=False):
93
 
        return self.metadata['hostname']
94
 
 
95
 
    def get_package_mirror_info(self):
96
 
        return self.ds_cfg['mirrors_url']
97
 
 
98
 
    @property
99
 
    def launch_index(self):
100
 
        return None
101
 
 
102
 
# Used to match classes to dependencies
103
 
datasources = [
104
 
    (DataSourceDigitalOcean, (sources.DEP_FILESYSTEM, sources.DEP_NETWORK)),
105
 
]
106
 
 
107
 
 
108
 
# Return a list of data sources that match this set of dependencies
109
 
def get_datasource_list(depends):
110
 
    return sources.list_from_depends(depends, datasources)