Scott Baker | 35a9953 | 2015-08-31 16:18:08 -0700 | [diff] [blame] | 1 | import os |
Scott Baker | 714d524 | 2015-08-05 08:20:12 -0700 | [diff] [blame] | 2 | import pdb |
Scott Baker | 35a9953 | 2015-08-31 16:18:08 -0700 | [diff] [blame] | 3 | import json |
Scott Baker | f65591f | 2015-09-28 16:01:21 -0700 | [diff] [blame] | 4 | import subprocess |
Scott Baker | 714d524 | 2015-08-05 08:20:12 -0700 | [diff] [blame] | 5 | |
Scott Baker | 07c71fc | 2015-09-24 15:37:16 -0700 | [diff] [blame] | 6 | from core.models import User |
| 7 | |
Scott Baker | 3841b37 | 2015-08-03 14:20:31 -0700 | [diff] [blame] | 8 | class XOSResource(object): |
| 9 | xos_base_class = "XOSResource" |
Scott Baker | 9fdb39f | 2015-08-04 16:44:18 -0700 | [diff] [blame] | 10 | xos_model = None |
Scott Baker | 2142bd7 | 2015-08-10 17:08:02 -0700 | [diff] [blame] | 11 | name_field = "name" |
| 12 | copyin_props = [] |
Scott Baker | 3841b37 | 2015-08-03 14:20:31 -0700 | [diff] [blame] | 13 | provides = None |
| 14 | |
Scott Baker | 5d43214 | 2015-08-07 17:06:47 -0700 | [diff] [blame] | 15 | def __init__(self, user, nodetemplate, engine): |
Scott Baker | 9fce62e | 2015-08-03 15:43:54 -0700 | [diff] [blame] | 16 | self.dirty = False |
Scott Baker | 404c46d | 2015-08-24 15:50:03 -0700 | [diff] [blame] | 17 | self.deferred_sync = [] |
Scott Baker | 3841b37 | 2015-08-03 14:20:31 -0700 | [diff] [blame] | 18 | self.user = user |
| 19 | self.nodetemplate = nodetemplate |
Scott Baker | 5d43214 | 2015-08-07 17:06:47 -0700 | [diff] [blame] | 20 | self.engine = engine |
Scott Baker | 3841b37 | 2015-08-03 14:20:31 -0700 | [diff] [blame] | 21 | |
Scott Baker | 642dbb5 | 2016-04-25 14:55:22 -0700 | [diff] [blame] | 22 | @property |
| 23 | def full_name(self): |
| 24 | return self.nodetemplate.name |
| 25 | |
| 26 | @property |
| 27 | def obj_name(self): |
| 28 | if "#" in self.nodetemplate.name: |
Scott Baker | 3826522 | 2016-04-25 15:19:03 -0700 | [diff] [blame^] | 29 | return self.nodetemplate.name.split("#",1)[1] |
Scott Baker | 642dbb5 | 2016-04-25 14:55:22 -0700 | [diff] [blame] | 30 | else: |
| 31 | return self.nodetemplate.name |
| 32 | |
Scott Baker | 3279525 | 2015-08-04 23:53:07 -0700 | [diff] [blame] | 33 | def get_all_required_node_names(self): |
| 34 | results = [] |
| 35 | for reqs in self.nodetemplate.requirements: |
| 36 | for (k,v) in reqs.items(): |
| 37 | results.append(v["node"]) |
| 38 | return results |
| 39 | |
Scott Baker | 1415294 | 2015-08-04 10:59:29 -0700 | [diff] [blame] | 40 | def get_requirements(self, relationship_name, throw_exception=False): |
Scott Baker | 9fce62e | 2015-08-03 15:43:54 -0700 | [diff] [blame] | 41 | """ helper to search the list of requirements for a particular relationship |
| 42 | type. |
| 43 | """ |
Scott Baker | 1415294 | 2015-08-04 10:59:29 -0700 | [diff] [blame] | 44 | |
| 45 | results = [] |
Scott Baker | 9fce62e | 2015-08-03 15:43:54 -0700 | [diff] [blame] | 46 | for reqs in self.nodetemplate.requirements: |
| 47 | for (k,v) in reqs.items(): |
| 48 | if (v["relationship"] == relationship_name): |
Scott Baker | 1415294 | 2015-08-04 10:59:29 -0700 | [diff] [blame] | 49 | results.append(v["node"]) |
Scott Baker | 9fce62e | 2015-08-03 15:43:54 -0700 | [diff] [blame] | 50 | |
Scott Baker | 1415294 | 2015-08-04 10:59:29 -0700 | [diff] [blame] | 51 | if (not results) and throw_exception: |
Scott Baker | 642dbb5 | 2016-04-25 14:55:22 -0700 | [diff] [blame] | 52 | raise Exception("Failed to find requirement in %s using relationship %s" % (self.full_name, relationship_name)) |
Scott Baker | 9fce62e | 2015-08-03 15:43:54 -0700 | [diff] [blame] | 53 | |
Scott Baker | 1415294 | 2015-08-04 10:59:29 -0700 | [diff] [blame] | 54 | return results |
| 55 | |
| 56 | def get_requirement(self, relationship_name, throw_exception=False): |
| 57 | reqs = self.get_requirements(relationship_name, throw_exception) |
| 58 | if not reqs: |
| 59 | return None |
| 60 | return reqs[0] |
Scott Baker | 9fce62e | 2015-08-03 15:43:54 -0700 | [diff] [blame] | 61 | |
Scott Baker | 714d524 | 2015-08-05 08:20:12 -0700 | [diff] [blame] | 62 | def get_scalable(self): |
Scott Baker | 502400a | 2015-08-05 10:41:51 -0700 | [diff] [blame] | 63 | scalable = self.nodetemplate.get_capabilities().get("scalable", None) |
| 64 | if scalable: |
| 65 | return {"min_instances": scalable.get_property_value("min_instances"), |
| 66 | "max_instances": scalable.get_property_value("max_instances"), |
| 67 | "default_instances": scalable.get_property_value("default_instances")} |
Scott Baker | 714d524 | 2015-08-05 08:20:12 -0700 | [diff] [blame] | 68 | else: |
| 69 | return {} |
| 70 | |
Scott Baker | 35a9953 | 2015-08-31 16:18:08 -0700 | [diff] [blame] | 71 | def get_property(self, name): |
| 72 | return self.nodetemplate.get_property_value(name) |
Scott Baker | 068b118 | 2015-08-05 18:34:23 -0700 | [diff] [blame] | 73 | |
Scott Baker | 3314287 | 2015-10-08 16:51:34 -0700 | [diff] [blame] | 74 | def get_property_default(self, name, default=None): |
| 75 | props = self.nodetemplate.get_properties() |
Sapan Bhatia | 16be143 | 2016-01-14 11:41:38 -0500 | [diff] [blame] | 76 | if props and name in props.keys(): |
Scott Baker | 3314287 | 2015-10-08 16:51:34 -0700 | [diff] [blame] | 77 | return props[name].value |
| 78 | return default |
| 79 | |
Scott Baker | 5fcce04 | 2015-08-24 16:25:46 -0700 | [diff] [blame] | 80 | def get_xos_object(self, cls, throw_exception=True, **kwargs): |
Scott Baker | 9fce62e | 2015-08-03 15:43:54 -0700 | [diff] [blame] | 81 | objs = cls.objects.filter(**kwargs) |
| 82 | if not objs: |
Scott Baker | 5fcce04 | 2015-08-24 16:25:46 -0700 | [diff] [blame] | 83 | if throw_exception: |
| 84 | raise Exception("Failed to find %s filtered by %s" % (cls.__name__, str(kwargs))) |
| 85 | return None |
Scott Baker | 9fce62e | 2015-08-03 15:43:54 -0700 | [diff] [blame] | 86 | return objs[0] |
| 87 | |
Scott Baker | 9fdb39f | 2015-08-04 16:44:18 -0700 | [diff] [blame] | 88 | def get_existing_objs(self): |
Scott Baker | 642dbb5 | 2016-04-25 14:55:22 -0700 | [diff] [blame] | 89 | return self.xos_model.objects.filter(**{self.name_field: self.obj_name}) |
Scott Baker | 9fdb39f | 2015-08-04 16:44:18 -0700 | [diff] [blame] | 90 | |
Scott Baker | c058150 | 2015-09-01 22:57:46 -0700 | [diff] [blame] | 91 | def get_model_class_name(self): |
| 92 | return self.xos_model.__name__ |
| 93 | |
Scott Baker | 9fdb39f | 2015-08-04 16:44:18 -0700 | [diff] [blame] | 94 | def create_or_update(self): |
| 95 | existing_objs = self.get_existing_objs() |
| 96 | if existing_objs: |
Scott Baker | 3314287 | 2015-10-08 16:51:34 -0700 | [diff] [blame] | 97 | if self.get_property_default("no-update", False): |
Scott Baker | 642dbb5 | 2016-04-25 14:55:22 -0700 | [diff] [blame] | 98 | self.info("%s:%s (%s) already exists. Skipping update due to 'no-update' property" % (self.get_model_class_name(), self.obj_name, self.full_name)) |
Scott Baker | 3314287 | 2015-10-08 16:51:34 -0700 | [diff] [blame] | 99 | else: |
Scott Baker | 642dbb5 | 2016-04-25 14:55:22 -0700 | [diff] [blame] | 100 | self.info("%s:%s (%s) already exists" % (self.get_model_class_name(), self.obj_name, self.full_name)) |
Scott Baker | 3314287 | 2015-10-08 16:51:34 -0700 | [diff] [blame] | 101 | self.update(existing_objs[0]) |
Scott Baker | 9fdb39f | 2015-08-04 16:44:18 -0700 | [diff] [blame] | 102 | else: |
Scott Baker | 3314287 | 2015-10-08 16:51:34 -0700 | [diff] [blame] | 103 | if self.get_property_default("no-create", False): |
Scott Baker | 642dbb5 | 2016-04-25 14:55:22 -0700 | [diff] [blame] | 104 | self.info("%s:%s (%s) does not exist, but 'no-create' is specified" % (self.get_model_class_name(), self.obj_name, self.full_name)) |
Scott Baker | 3314287 | 2015-10-08 16:51:34 -0700 | [diff] [blame] | 105 | else: |
| 106 | self.create() |
Scott Baker | 9fdb39f | 2015-08-04 16:44:18 -0700 | [diff] [blame] | 107 | |
Scott Baker | 61c4311 | 2015-08-12 19:06:16 -0700 | [diff] [blame] | 108 | def can_delete(self, obj): |
Scott Baker | 3314287 | 2015-10-08 16:51:34 -0700 | [diff] [blame] | 109 | if self.get_property_default("no-delete",False): |
Scott Baker | 642dbb5 | 2016-04-25 14:55:22 -0700 | [diff] [blame] | 110 | self.info("%s:%s %s is marked 'no-delete'. Skipping delete." % (self.get_model_class_name(), self.obj_name, self.full_name)) |
Scott Baker | 3314287 | 2015-10-08 16:51:34 -0700 | [diff] [blame] | 111 | return False |
Scott Baker | 61c4311 | 2015-08-12 19:06:16 -0700 | [diff] [blame] | 112 | return True |
Scott Baker | 2142bd7 | 2015-08-10 17:08:02 -0700 | [diff] [blame] | 113 | |
Scott Baker | 07c71fc | 2015-09-24 15:37:16 -0700 | [diff] [blame] | 114 | def postprocess_privileges(self, roleclass, privclass, rolemap, obj, toFieldName): |
Scott Baker | 92362bc | 2015-09-24 15:22:46 -0700 | [diff] [blame] | 115 | for (rel, role) in rolemap: |
| 116 | for email in self.get_requirements(rel): |
Scott Baker | 688c4dd | 2016-01-27 19:00:06 -0800 | [diff] [blame] | 117 | role_obj = self.get_xos_object(roleclass, throw_exception=False, role=role) |
| 118 | if not role_obj: |
| 119 | # if the role doesn't exist, make it |
| 120 | self.info("Creating %s %s" % (roleclass.__name__, role)) |
| 121 | role_obj = roleclass(role=role) |
| 122 | role_obj.save() |
| 123 | |
Scott Baker | 92362bc | 2015-09-24 15:22:46 -0700 | [diff] [blame] | 124 | user = self.get_xos_object(User, email=email) |
Scott Baker | 688c4dd | 2016-01-27 19:00:06 -0800 | [diff] [blame] | 125 | if not privclass.objects.filter(user=user, role=role_obj, **{toFieldName: obj}): |
| 126 | sp = privclass(user=user, role=role_obj, **{toFieldName: obj}) |
Scott Baker | 92362bc | 2015-09-24 15:22:46 -0700 | [diff] [blame] | 127 | sp.save() |
| 128 | self.info("Added privilege on %s role %s for %s" % (str(obj), str(role), str(user))) |
| 129 | |
Scott Baker | 2142bd7 | 2015-08-10 17:08:02 -0700 | [diff] [blame] | 130 | def postprocess(self, obj): |
| 131 | pass |
| 132 | |
Scott Baker | 35a9953 | 2015-08-31 16:18:08 -0700 | [diff] [blame] | 133 | def intrinsic_get_artifact(self, obj=None, name=None, method=None): |
| 134 | if obj!="SELF": |
| 135 | raise Exception("only SELF is supported for get_artifact first arg") |
| 136 | if method!="LOCAL_FILE": |
| 137 | raise Exception("only LOCAL_FILE is supported for get_artifact third arg") |
| 138 | |
| 139 | for (k,v) in self.nodetemplate.entity_tpl.get("artifacts", {}).items(): |
| 140 | if k == name: |
| 141 | if not os.path.exists(v): |
| 142 | raise Exception("Artifact local file %s for artifact %s does not exist" % (v, k)) |
| 143 | return open(v).read() |
| 144 | |
| 145 | raise Exception("artifact %s not found" % name) |
| 146 | |
Scott Baker | f65591f | 2015-09-28 16:01:21 -0700 | [diff] [blame] | 147 | def intrinsic_get_script_env(self, obj=None, name=None, varname=None, method=None): |
| 148 | if obj!="SELF": |
| 149 | raise Exception("only SELF is supported for get_artifact first arg") |
| 150 | if method!="LOCAL_FILE": |
| 151 | raise Exception("only LOCAL_FILE is supported for get_artifact fourth arg") |
| 152 | |
| 153 | for (k,v) in self.nodetemplate.entity_tpl.get("artifacts", {}).items(): |
| 154 | if k == name: |
| 155 | if not os.path.exists(v): |
| 156 | raise Exception("Artifact local file %s for artifact %s does not exist" % (v, k)) |
| 157 | return subprocess.Popen('/bin/bash -c "source %s &> /dev/null; echo \\$%s"' % (v, varname), shell=True, stdout=subprocess.PIPE).stdout.read().strip() |
| 158 | |
| 159 | raise Exception("artifact %s not found" % name) |
| 160 | |
Scott Baker | 35a9953 | 2015-08-31 16:18:08 -0700 | [diff] [blame] | 161 | def try_intrinsic_function(self, v): |
| 162 | try: |
| 163 | jsv = v.replace("'", '"') |
| 164 | jsv = json.loads(jsv) |
| 165 | except: |
| 166 | #import traceback |
| 167 | #traceback.print_exc() |
| 168 | return v |
| 169 | |
| 170 | if type(jsv)!=dict: |
| 171 | return v |
| 172 | |
| 173 | if "get_artifact" in jsv: |
| 174 | return self.intrinsic_get_artifact(*jsv["get_artifact"]) |
Scott Baker | f65591f | 2015-09-28 16:01:21 -0700 | [diff] [blame] | 175 | elif "get_script_env" in jsv: |
| 176 | return self.intrinsic_get_script_env(*jsv["get_script_env"]) |
Scott Baker | 35a9953 | 2015-08-31 16:18:08 -0700 | [diff] [blame] | 177 | |
| 178 | return v |
| 179 | |
Scott Baker | 2142bd7 | 2015-08-10 17:08:02 -0700 | [diff] [blame] | 180 | def get_xos_args(self): |
| 181 | args = {} |
| 182 | |
| 183 | if self.name_field: |
Scott Baker | 642dbb5 | 2016-04-25 14:55:22 -0700 | [diff] [blame] | 184 | args[self.name_field] = self.obj_name |
Scott Baker | 2142bd7 | 2015-08-10 17:08:02 -0700 | [diff] [blame] | 185 | |
| 186 | # copy simple string properties from the template into the arguments |
| 187 | for prop in self.copyin_props: |
| 188 | v = self.get_property(prop) |
Scott Baker | 35a9953 | 2015-08-31 16:18:08 -0700 | [diff] [blame] | 189 | |
| 190 | v = self.try_intrinsic_function(v) |
| 191 | |
Scott Baker | 744f9a1 | 2015-09-02 16:29:16 -0700 | [diff] [blame] | 192 | if v is not None: |
Scott Baker | 2142bd7 | 2015-08-10 17:08:02 -0700 | [diff] [blame] | 193 | args[prop] = v |
| 194 | |
| 195 | return args |
| 196 | |
Scott Baker | 9fdb39f | 2015-08-04 16:44:18 -0700 | [diff] [blame] | 197 | def create(self): |
Scott Baker | 2142bd7 | 2015-08-10 17:08:02 -0700 | [diff] [blame] | 198 | xos_args = self.get_xos_args() |
| 199 | xos_obj = self.xos_model(**xos_args) |
Scott Baker | 4ce7d4f | 2016-04-22 09:26:18 -0700 | [diff] [blame] | 200 | if self.user: |
| 201 | xos_obj.caller = self.user |
Scott Baker | 2142bd7 | 2015-08-10 17:08:02 -0700 | [diff] [blame] | 202 | xos_obj.save() |
| 203 | |
Scott Baker | 2142bd7 | 2015-08-10 17:08:02 -0700 | [diff] [blame] | 204 | self.info("Created %s '%s'" % (self.xos_model.__name__,str(xos_obj))) |
Scott Baker | 9fdb39f | 2015-08-04 16:44:18 -0700 | [diff] [blame] | 205 | |
Scott Baker | 3793772 | 2015-08-14 12:41:18 -0700 | [diff] [blame] | 206 | self.postprocess(xos_obj) |
| 207 | |
Scott Baker | 9fdb39f | 2015-08-04 16:44:18 -0700 | [diff] [blame] | 208 | def update(self, obj): |
Scott Baker | 744f9a1 | 2015-09-02 16:29:16 -0700 | [diff] [blame] | 209 | xos_args = self.get_xos_args() |
Scott Baker | 41916c9 | 2015-09-03 12:15:50 -0700 | [diff] [blame] | 210 | for (k,v) in xos_args.items(): |
| 211 | setattr(obj, k, v) |
Scott Baker | eb2aa3c | 2015-09-03 14:52:22 -0700 | [diff] [blame] | 212 | self.postprocess(obj) |
Scott Baker | 744f9a1 | 2015-09-02 16:29:16 -0700 | [diff] [blame] | 213 | obj.save() |
Scott Baker | 3841b37 | 2015-08-03 14:20:31 -0700 | [diff] [blame] | 214 | |
Scott Baker | 13399c0 | 2015-08-05 16:35:09 -0700 | [diff] [blame] | 215 | def delete(self, obj): |
Scott Baker | 61c4311 | 2015-08-12 19:06:16 -0700 | [diff] [blame] | 216 | if (self.can_delete(obj)): |
| 217 | self.info("destroying object %s" % str(obj)) |
| 218 | obj.delete(purge=True) # XXX TODO: turn off purge before production |
Scott Baker | 13399c0 | 2015-08-05 16:35:09 -0700 | [diff] [blame] | 219 | |
Scott Baker | 9fce62e | 2015-08-03 15:43:54 -0700 | [diff] [blame] | 220 | def info(self, s): |
Scott Baker | e08b456 | 2015-08-11 17:23:52 -0700 | [diff] [blame] | 221 | self.engine.log(s) |
Scott Baker | 9fce62e | 2015-08-03 15:43:54 -0700 | [diff] [blame] | 222 | |