summaryrefslogtreecommitdiff
path: root/cloudinit/DataSourceCS.py
blob: d31d0445eb3a47e0854f331a514f88a4ca8bc434 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# vi: ts=4 expandtab
#
#    Copyright (C) 2009-2010 Canonical Ltd.
#    Copyright (C) 2012 Hewlett-Packard Development Company, L.P.
#
#    Author: Scott Moser <scott.moser@canonical.com>
#    Author: Juerg Hafliger <juerg.haefliger@hp.com>
#    Author: Cosmin Luta <cosmin.luta@avira.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License version 3, as
#    published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import cloudinit.DataSource as DataSource

from cloudinit import seeddir as base_seeddir
from cloudinit import log
import cloudinit.util as util
from socket import inet_ntoa
import time
import boto.utils as boto_utils
from struct import pack

class DataSourceCS(DataSource.DataSource):
    api_ver = 'latest'
    seeddir = base_seeddir + '/cs'
    metadata_address = None

    def __init__(self, sys_cfg=None):
        DataSource.DataSource.__init__(self, sys_cfg)
        # Cloudstack has its metadata/userdata URLs located at http://<default-gateway-ip>/latest/
        self.metadata_address = "http://%s/" % self.get_default_gateway()
        
    def get_default_gateway(self):
        """ Returns the default gateway ip address in the dotted format
        """
        with open("/proc/net/route", "r") as f:
            for line in f.readlines():
                items = line.split("\t")
                if items[1] == "00000000":
                    # found the default route, get the gateway
                    gw = inet_ntoa(pack("<L", int(items[2], 16)))
                    log.debug("found default route, gateway is %s" % gw)
                    return gw

    def __str__(self):
        return "DataSourceCS"

    def get_data(self):
        seedret = {}
        if util.read_optional_seed(seedret, base=self.seeddir + "/"):
            self.userdata_raw = seedret['user-data']
            self.metadata = seedret['meta-data']
            log.debug("using seeded cs data in %s" % self.seeddir)
            return True

        try:
            start = time.time()
            self.userdata_raw = boto_utils.get_instance_userdata(self.api_ver,
                None, self.metadata_address)
            self.metadata = boto_utils.get_instance_metadata(self.api_ver,
                self.metadata_address)
            log.debug("crawl of metadata service took %ds" % (time.time() - start))
            return True
        except Exception as e:
            log.exception(e)
            return False

    def get_instance_id(self):
        return self.metadata['instance-id']

    def get_availability_zone(self):
        return self.metadata['availability-zone']

datasources = [
  (DataSourceCS, (DataSource.DEP_FILESYSTEM, DataSource.DEP_NETWORK)),
]

# return a list of data sources that match this set of dependencies
def get_datasource_list(depends):
    return DataSource.list_from_depends(depends, datasources)