Revision d9b25288

b/snf-cyclades-gtools/synnefo/ganeti/eventd.py
99 99
    raise InvalidBackendStatus(status, job)
100 100

  
101 101

  
102
def get_instance_nics(instance, logger):
103
    """Query Ganeti to a get the instance's NICs.
102
def get_instance_attachments(instance, logger):
103
    """Query Ganeti to a get the instance's attachments (NICs and Disks)
104 104

  
105
    Get instance's NICs from Ganeti configuration data. If running on master,
106
    query Ganeti via Ganeti CLI client. Otherwise, get the nics from Ganeti
107
    configuration file.
105
    Get instance's attachments from Ganeti configuration data. If running on
106
    master, query Ganeti via Ganeti CLI client. Otherwise, get attachments
107
    straight from Ganeti's configuration file.
108 108

  
109 109
    @type instance: string
110 110
    @param instance: the name of the instance
111
    @rtype: List of dicts
112
    @return: Dictionary containing the instance's NICs. Each dictionary
113
             contains the following keys: 'network', 'ip', 'mac', 'mode',
114
             'link' and 'firewall'
111
    @rtype: instance's NICs and Disks
112
    @return: Dictionary containing the 'nics' and 'disks' of the instance.
115 113

  
116 114
    """
117 115
    try:
118 116
        client = cli.GetClient()
119
        fields = ["nic.names", "nic.networks.names", "nic.ips", "nic.macs",
120
                  "nic.modes", "nic.links", "tags"]
121
        info = client.QueryInstances([instance], fields, use_locking=False)
122
        names, networks, ips, macs, modes, links, tags = info[0]
123
        nic_keys = ["name", "network", "ip", "mac", "mode", "link"]
124
        nics = zip(names, networks, ips, macs, modes, links)
117
        q_fields = ["nic.names", "nic.networks.names", "nic.ips", "nic.macs",
118
                    "nic.modes", "nic.links", "nic.uuids", "tags",
119
                    "disk.names", "disk.sizes", "disk.uuids"]
120
        info = client.QueryInstances([instance], q_fields, use_locking=False)
121
        # Parse NICs
122
        names, networks, ips, macs, modes, links, uuids, tags = info[0][:-3]
123
        nic_keys = ["name", "network", "ip", "mac", "mode", "link", "uuid"]
124
        nics = zip(names, networks, ips, macs, modes, links, uuids)
125 125
        nics = map(lambda x: dict(zip(nic_keys, x)), nics)
126
        # Parse Disks
127
        names, sizes, uuids = info[0][-3:]
128
        disk_keys = ["name", "size", "uuid"]
129
        disks = zip(names, sizes, uuids)
130
        disks = map(lambda x: dict(zip(disk_keys, x)), disks)
126 131
    except ganeti_errors.OpPrereqError:
127 132
        # Not running on master! Load the conf file
128 133
        raw_data = utils.ReadFile(pathutils.CLUSTER_CONF_FILE)
129 134
        config = serializer.LoadJson(raw_data)
130 135
        i = config["instances"][instance]
136
        # Parse NICs
131 137
        nics = []
132
        for nic in i["nics"]:
138
        for index, nic in enumerate(i["nics"]):
133 139
            params = nic.pop("nicparams")
134 140
            nic["mode"] = params["mode"]
135 141
            nic["link"] = params["link"]
142
            nic["index"] = index
136 143
            nics.append(nic)
144
        # Parse Disks
145
        disks = []
146
        for index, disk in enumerate(i["disks"]):
147
            disks.append({"name": disk.pop("name"),
148
                          "size": disk["size"],
149
                          "uuid": disk["uuid"],
150
                          "index": index})
137 151
        tags = i.get("tags", [])
138 152
    # Get firewall from instance Tags
139 153
    # Tags are of the form synnefo:network:N:firewall_mode
......
147 161
            firewall = t[3]
148 162
            [nic.setdefault("firewall", firewall)
149 163
             for nic in nics if nic["name"] == nic_name]
150
    return nics
164
    attachments = {"nics": nics,
165
                   "disks": disks}
166
    return attachments
151 167

  
152 168

  
153 169
class InvalidBackendStatus(Exception):
......
298 314
             op.status == "success") or
299 315
            (op_id == "OP_INSTANCE_SET_PARAMS" and
300 316
             op.status in ["success", "error", "cancelled"])):
301
                nics = get_instance_nics(msg["instance"], self.logger)
302
                msg["instance_nics"] = nics
317
                attachments = get_instance_attachments(msg["instance"],
318
                                                       self.logger)
319
                msg["instance_nics"] = attachments["nics"]
320
                msg["instance_disks"] = attachments["disks"]
303 321

  
304 322
        routekey = "ganeti.%s.event.op" % prefix_from_name(instances)
305 323

  

Also available in: Unified diff