blob: 41e1305a0ccdafaba2c8390465041cb98900a345 [file] [log] [blame]
Scott Bakerb63ea792016-08-11 10:24:48 -07001import hashlib
2import os
3import socket
4import sys
5import base64
6import time
7from django.db.models import F, Q
8from xos.config import Config
9from synchronizers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
10from synchronizers.base.syncstep import SyncStep, DeferredException
11from synchronizers.base.ansible import run_template_ssh
12from core.models import Service, Slice, Instance
13from xos.logger import Logger, logging
14
15# hpclibrary will be in steps/..
16parentdir = os.path.join(os.path.dirname(__file__),"..")
17sys.path.insert(0,parentdir)
18
19logger = Logger(level=logging.INFO)
20
21class SyncContainer(SyncInstanceUsingAnsible):
22 provides=[Instance]
23 observes=Instance
24 requested_interval=0
25 template_name = "sync_container.yaml"
26
27 def __init__(self, *args, **kwargs):
28 super(SyncContainer, self).__init__(*args, **kwargs)
29
30 def fetch_pending(self, deletion=False):
31 objs = super(SyncContainer, self).fetch_pending(deletion)
32 objs = [x for x in objs if x.isolation in ["container", "container_vm"]]
33 return objs
34
35 def get_instance_port(self, container_port):
36 for p in container_port.network.links.all():
37 if (p.instance) and (p.instance.isolation=="vm") and (p.instance.node == container_port.instance.node) and (p.mac):
38 return p
39 return None
40
41 def get_parent_port_mac(self, instance, port):
42 if not instance.parent:
43 raise Exception("instance has no parent")
44 for parent_port in instance.parent.ports.all():
45 if parent_port.network == port.network:
46 if not parent_port.mac:
47 raise DeferredException("parent port on network %s does not have mac yet" % parent_port.network.name)
48 return parent_port.mac
49 raise Exception("failed to find corresponding parent port for network %s" % port.network.name)
50
51 def get_ports(self, o):
52 i=0
53 ports = []
54 if (o.slice.network in ["host", "bridged"]):
55 pass # no ports in host or bridged mode
56 else:
57 for port in o.ports.all():
58 if (not port.ip):
59 # 'unmanaged' ports may have an ip, but no mac
60 # XXX: are there any ports that have a mac but no ip?
61 raise DeferredException("Port on network %s is not yet ready" % port.network.name)
62
63 pd={}
64 pd["mac"] = port.mac or ""
65 pd["ip"] = port.ip or ""
66 pd["xos_network_id"] = port.network.id
67
68 if port.network.name == "wan_network":
69 if port.ip:
70 (a, b, c, d) = port.ip.split('.')
71 pd["mac"] = "02:42:%02x:%02x:%02x:%02x" % (int(a), int(b), int(c), int(d))
72
73
74 if o.isolation == "container":
75 # container on bare metal
76 instance_port = self.get_instance_port(port)
77 if not instance_port:
78 raise DeferredException("No instance on slice for port on network %s" % port.network.name)
79
80 pd["snoop_instance_mac"] = instance_port.mac
81 pd["snoop_instance_id"] = instance_port.instance.instance_id
82 pd["src_device"] = ""
83 pd["bridge"] = "br-int"
84 else:
85 # container in VM
86 pd["snoop_instance_mac"] = ""
87 pd["snoop_instance_id"] = ""
88 pd["parent_mac"] = self.get_parent_port_mac(o, port)
89 pd["bridge"] = ""
90
91 for (k,v) in port.get_parameters().items():
92 pd[k] = v
93
94 ports.append(pd)
95
96 # for any ports that don't have a device, assign one
97 used_ports = [x["device"] for x in ports if ("device" in x)]
98 avail_ports = ["eth%d"%i for i in range(0,64) if ("eth%d"%i not in used_ports)]
99 for port in ports:
100 if not port.get("device",None):
101 port["device"] = avail_ports.pop(0)
102
103 return ports
104
105 def get_extra_attributes(self, o):
106 fields={}
107 fields["ansible_tag"] = "container-%s" % str(o.id)
108 if o.image.tag:
109 fields["docker_image"] = o.image.path + ":" + o.image.tag
110 else:
111 fields["docker_image"] = o.image.path
112 fields["ports"] = self.get_ports(o)
113 if o.volumes:
114 fields["volumes"] = [x.strip() for x in o.volumes.split(",")]
115 else:
116 fields["volumes"] = ""
117 fields["network_method"] = o.slice.network or "default"
118 return fields
119
120 def sync_record(self, o):
121 logger.info("sync'ing object %s" % str(o),extra=o.tologdict())
122
123 fields = self.get_ansible_fields(o)
124
125 # If 'o' defines a 'sync_attributes' list, then we'll copy those
126 # attributes into the Ansible recipe's field list automatically.
127 if hasattr(o, "sync_attributes"):
128 for attribute_name in o.sync_attributes:
129 fields[attribute_name] = getattr(o, attribute_name)
130
131 fields.update(self.get_extra_attributes(o))
132
133 self.run_playbook(o, fields)
134
135 o.instance_id = fields["container_name"]
136 o.instance_name = fields["container_name"]
137
138 o.save()
139
140 def delete_record(self, o):
141 logger.info("delete'ing object %s" % str(o),extra=o.tologdict())
142
143 fields = self.get_ansible_fields(o)
144
145 # If 'o' defines a 'sync_attributes' list, then we'll copy those
146 # attributes into the Ansible recipe's field list automatically.
147 if hasattr(o, "sync_attributes"):
148 for attribute_name in o.sync_attributes:
149 fields[attribute_name] = getattr(o, attribute_name)
150
151 fields.update(self.get_extra_attributes(o))
152
153 self.run_playbook(o, fields, "teardown_container.yaml")
154
155 def run_playbook(self, o, fields, template_name=None):
156 if not template_name:
157 template_name = self.template_name
158 tStart = time.time()
159 run_template_ssh(template_name, fields, path="container")
160 logger.info("playbook execution time %d" % int(time.time()-tStart),extra=o.tologdict())
161
162