class Backend(OODict): """A cloud vm provider backend""" enabled = BoolField() machine_count = IntField() apiurl = StrField() apikey = HtmlSafeStrField() apisecret = StrField() title = HtmlSafeStrField() tenant_name = HtmlSafeStrField() auth_version = HtmlSafeStrField() region = HtmlSafeStrField() poll_interval = IntField(10000) provider = HtmlSafeStrField() ## datacenter = StrField() compute_endpoint = StrField() docker_port = IntField(4243) machines = make_field(Machines)() starred = ListField() unstarred = ListField() def __repr__(self): print_fields = ['title', 'provider', 'region'] return super(Backend, self).__repr__(print_fields) def get_id(self): from mist.io.helpers import b58_encode if self.provider == 'docker': concat = '%s%s%s' % (self.provider, self.title, self.apiurl) elif self.provider == 'bare_metal': name = self.machines.values()[0].name concat = '%s%s%s' % (self.provider, '', name) else: concat = '%s%s%s' % (self.provider, self.region, self.apikey) return b58_encode(int(sha1(concat).hexdigest(), 16))
class Keypair(OODict): """An ssh keypair.""" public = StrField() private = StrField() default = BoolField() machines = ListField() def generate(self): """Generates a new RSA keypair and assignes to self.""" from Crypto import Random Random.atfork() key = RSA.generate(2048) self.private = key.exportKey() self.public = key.exportKey('OpenSSH') def isvalid(self): """Checks if self is a valid RSA keypair.""" from Crypto import Random Random.atfork() message = 'Message 1234567890' if 'ssh-rsa' in self.public: public_key_container = RSA.importKey(self.public) private_key_container = RSA.importKey(self.private) encr_message = public_key_container.encrypt(message, 0) decr_message = private_key_container.decrypt(encr_message) if message == decr_message: return True return False def construct_public_from_private(self): """Constructs pub key from self.private and assignes to self.public. Only works for RSA. """ from Crypto import Random Random.atfork() if 'RSA' in self.private: try: key = RSA.importKey(self.private) public = key.publickey().exportKey('OpenSSH') self.public = public return True except: pass return False def __repr__(self): return super(Keypair, self).__repr__(['default', 'machines'])
class User(DalUser): """The basic model class is User. It contains all the methods necessary to find and save users in memcache and in mongo. It transforms the user dict into an object with consistent attributes instead of inconsistent dict with missing keys. """ email = StrField() mist_api_token = StrField() clouds = make_field(Clouds)() keypairs = make_field(Keypairs)() def __repr__(self): return super(User, self).__repr__(['email'])
class Machine(OODict): """A saved machine in the machines list of some backend. For the time being, only bare metal machines are saved, for API backends we get the machine list from the provider. """ ## hasMonitoring = BoolField() uuid = StrField() ## monitor_server = make_field(MonitorServer)() dns_name = HtmlSafeStrField() public_ips = ListField() ## collectd_password = StrField() name = HtmlSafeStrField() ssh_port = IntField(22) os_type = StrField('unix')
class Cloud(OODict): """A cloud vm provider cloud""" enabled = BoolField() machine_count = IntField() apiurl = StrField() apikey = HtmlSafeStrField() apisecret = StrField() title = HtmlSafeStrField() tenant_name = HtmlSafeStrField() auth_version = HtmlSafeStrField() region = HtmlSafeStrField() poll_interval = IntField(10000) provider = HtmlSafeStrField() ## datacenter = StrField() compute_endpoint = StrField() key_file = StrField() cert_file = StrField() ca_cert_file = StrField() ssh_port = IntField(22) docker_port = IntField(4243) machines = make_field(Machines)() starred = ListField() unstarred = ListField() images_location = StrField() def __repr__(self): print_fields = ['title', 'provider', 'region'] return super(Cloud, self).__repr__(print_fields) def get_id(self): from mist.io.helpers import b58_encode if self.provider == 'docker': concat = '%s%s%s' % (self.provider, self.title, self.apiurl) elif self.provider in ['bare_metal', 'coreos']: name = self.machines.values()[0].name concat = '%s%s%s' % (self.provider, '', name) elif self.provider == 'openstack' or 'hpcloud' in self.provider: concat = "%s%s%s%s%s" % (self.provider, self.region, self.apikey, self.apiurl, self.tenant_name) elif self.provider == 'libvirt': concat = "%s%s" % (self.provider, self.apiurl) elif self.provider in ['vcloud', 'indonesian_vcloud', 'vsphere']: concat = "%s%s%s%s" % (self.provider, self.apikey, self.apisecret, self.apiurl) else: concat = '%s%s%s' % (self.provider, self.region, self.apikey) return b58_encode(int(sha1(concat).hexdigest(), 16))