Exemple #1
0
class TwilioSMSClient(Client):
    """Your Account Sid and Auth Token from twilio.com/console
        DANGER! This is insecure. See http://twil.io/secure
    """

    account_sid = fields.String()
    auth_token = fields.String()

    def __init__(self):
        super().__init__()
        self.__client = None

    @property
    def twilio_rest_client(self):
        if not self.__client:
            self.__client = TwilioRestClient(self.account_sid, self.auth_token)
        return self.__client

    def send_sms(self, body, sender, recievers=None):
        """send sms
            Your Account Sid and Auth Token from twilio.com/console
            DANGER! This is insecure. See http://twil.io/secure
        Arguments:
            body (string) : the sms text
            sender (text) : sender full number ex: +15558675310
        
        Keyword Arguments:
            recievers (list) : recievers phone numbers
        """
        recievers = recievers or []
        for r in recievers:
            message = self.twilio_rest_client.messages.create(body=body,
                                                              from_=sender,
                                                              to=r)
            print(message.sid)
Exemple #2
0
class TfgridSolution1(Base):
    id = fields.Integer()
    name = fields.String(default="")
    solution_type = fields.Enum(SolutionType)
    rid = fields.Integer()
    form_info = fields.Typed(dict)
    explorer = fields.String(default="")
Exemple #3
0
class Website(Base):

    port = fields.Integer(default=80)
    ssl = fields.Boolean()
    domain = fields.String()
    path = fields.String()
    locations = fields.Factory(Location)

    @property
    def path_cfg_dir(self):
        return f"{self.parent.path_cfg_dir}/servers"

    @property
    def path_cfg(self):
        return f"{self.path_cfg_dir}/{self.instance_name}.http.conf"

    @property
    def path_web(self):
        return self.parent.path_web

    def configure(self):
        """Writes configuration of the website and its locations
        """

        j.sals.fs.mkdir(self.path_cfg_dir)
        config = render_config_template("website",
                                        base_dir=j.core.dirs.BASEDIR,
                                        website=self)
        j.sals.fs.write_file(self.path_cfg, config)

        for location_name in self.locations.list_all():
            location = self.locations.get(location_name)
            location.configure()
Exemple #4
0
class PoolCreate(Base):
    json = fields.String()
    data_reservation = fields.Object(PoolCreateData)
    customer_tid = fields.Integer()
    customer_signature = fields.String()
    sponsor_tid = fields.Integer()
    sponsor_signature = fields.String()
Exemple #5
0
class PoolCreateData(Base):
    pool_id = fields.Integer()
    cus = fields.Integer()
    sus = fields.Integer()
    ipv4us = fields.Integer()
    node_ids = fields.List(fields.String())
    currencies = fields.List(fields.String())
Exemple #6
0
class S3ZDB(VDCHostBase):
    size = fields.Integer()
    port = fields.Integer()
    namespace = fields.String()
    proxy_address = fields.String()

    @classmethod
    def from_workload(cls, workload):
        result_json = j.data.serializers.json.loads(workload.info.result.data_json)
        if not result_json:
            j.logger.warning(f"Couldn't get result details for zdb workload: {workload.id}")
            return
        if "IPs" in result_json:
            ip = result_json["IPs"][0]
        else:
            ip = result_json["IP"]
        namespace = result_json["Namespace"]
        port = result_json["Port"]
        zdb = cls()
        zdb.node_id = workload.info.node_id
        zdb.pool_id = workload.info.pool_id
        zdb.wid = workload.id
        zdb.size = workload.size
        zdb.ip_address = ip
        zdb.port = port
        zdb.namespace = namespace
        return zdb
Exemple #7
0
class TrelloClient(Client):

    name = fields.String()
    api_key_ = fields.String()
    secret = fields.String()
    access_token = fields.String()
    acess_token_secret = fields.String()

    def __init__(self):
        super().__init__()
        self.__client = None

    @property
    def trello_client(self):

        if not self.token_secret:
            # print("**WILL TRY TO DO OAUTH SESSION")
            access_token = create_oauth_token(key=self.api_key,
                                              secret=self.secret)
            self.access_token_ = access_token["oauth_token"]
            self.access_token_secret = access_token["oauth_token_secret"]

        self.client = TrelloAPIClient(api_key=self.api_key_,
                                      api_secret=self.secret,
                                      token=self.token,
                                      token_secret=self.token_secret)
Exemple #8
0
class GatewayProxy(Base):
    id = fields.Integer()
    domain = fields.String(default="")
    addr = fields.String(default="")
    port = fields.Integer()
    port_tls = fields.Integer()
    info = fields.Object(ReservationInfo)
Exemple #9
0
class Explorer(Client):
    url = fields.String()
    identity_name = fields.String()

    def __init__(self, url=None, identity_name=None, **kwargs):
        super().__init__(url=url, identity_name=identity_name, **kwargs)
        if identity_name:
            self._loaded_identity = identity.export_module_as().get(
                identity_name)
        else:
            self._loaded_identity = identity.get_identity()
        self._session = requests.Session()
        self._session.hooks = dict(response=raise_for_status)

        secret = self._loaded_identity.nacl.signing_key.encode(Base64Encoder)
        auth = HTTPSignatureAuth(
            key_id=str(self._loaded_identity.tid),
            secret=secret,
            headers=["(created)", "date", "threebot-id"],
        )
        headers = {"threebot-id": str(self._loaded_identity.tid)}
        self._session.auth = auth
        self._session.headers.update(headers)

        self.nodes = Nodes(self)
        self.users = Users(self)
        self.farms = Farms(self)
        self.reservations = Reservations(self)
        self.gateway = Gateways(self)
        self.pools = Pools(self)
        self.workloads = Workloads(self)
        self.conversion = Conversion(self)
Exemple #10
0
class VMachine(VDCWorkloadBase):
    name = fields.String()
    public_ip = fields.Object(PublicIP)
    size = fields.Integer()
    resources = fields.Typed(dict)
    ip_address = fields.String(default="")

    @classmethod
    def from_workload(cls, workload):
        vmachine = cls()
        vmachine.wid = workload.id
        metadata = j.sals.reservation_chatflow.reservation_chatflow.decrypt_reservation_metadata(workload.info.metadata)
        metadata = j.data.serializers.json.loads(metadata)
        vmachine.name = metadata["form_info"]["name"]
        vmachine.pool_id = workload.info.pool_id
        vmachine.node_id = workload.info.node_id
        vmachine.size = workload.size
        vmachine.resources = VMSIZES.get(workload.size)
        vmachine.ip_address = workload.ipaddress
        if workload.public_ip:
            vmachine.public_ip.wid = workload.public_ip
            zos = get_zos()
            public_ip_workload = zos.workloads.get(workload.public_ip)
            address = str(netaddr.IPNetwork(public_ip_workload.ipaddress).ip)
            vmachine.public_ip.address = address

        return vmachine
Exemple #11
0
class VirtualMachine(Base):
    id = fields.Integer()
    name = fields.String(default="")
    hub_url = fields.String(default="")
    description = fields.String(default="")
    network_connection = fields.List(fields.Object(ContainerNetworkConnection))
    network_id = fields.String()
    farmer_tid = fields.Integer()
    size = fields.Integer()
    info = fields.Object(ReservationInfo)
    ssh_keys = fields.List(fields.String())
    public_ip = fields.Integer()
    ipaddress = fields.IPAddress()
    SIZES = VMSIZES

    def resource_units(self):

        resource_units = ResourceUnitAmount()
        size = VMSIZES.get(self.size)
        if not size:
            raise j.exceptions.Input(f"VM size {self.size} not supported")

        resource_units.cru += size["cru"]
        resource_units.mru += size["mru"]
        resource_units.sru += size["sru"]
        return resource_units
Exemple #12
0
class HardwareProof(Base):
    created = fields.DateTime()
    hardware_hash = fields.String(default="")
    disk_hash = fields.String(default="")
    hardware = fields.Typed(dict)
    disks = fields.Typed(dict)
    hypervisor = fields.List(fields.String())
Exemple #13
0
class Container(Base):
    id = fields.Integer()
    flist = fields.String(default="")
    hub_url = fields.String(default="")
    storage_url = fields.String(default="")
    environment = fields.Typed(dict)
    secret_environment = fields.Typed(dict)
    entrypoint = fields.String(default="")
    interactive = fields.Boolean(default=True)
    volumes = fields.List(fields.Object(ContainerMount))
    network_connection = fields.List(fields.Object(ContainerNetworkConnection))
    stats = fields.List(fields.Object(ContainerStats))
    farmer_tid = fields.Integer()
    logs = fields.List(fields.Object(ContainerLogs))
    capacity = fields.Object(ContainerCapacity)
    info = fields.Object(ReservationInfo)

    def resource_units(self):
        cap = self.capacity
        resource_units = ResourceUnitAmount()
        resource_units.cru = cap.cpu
        resource_units.mru = round(cap.memory / 1024 * 10000) / 10000
        storage_size = round(cap.disk_size / 1024 * 10000) / 10000
        storage_size = max(0, storage_size - 50)  # we offer the 50 first GB of storage for container root filesystem
        if cap.disk_type == DiskType.HDD:
            resource_units.hru += storage_size
        elif cap.disk_type == DiskType.SSD:
            resource_units.sru += storage_size
        return resource_units
class User(Base):
    emails = fields.List(fields.String())
    permissions = fields.List(fields.Object(Permission))
    custom_config = fields.Typed(dict)
    type = fields.Enum(UserType)
    password = fields.Secret()

    first_name = fields.String(default="")
    last_name = fields.String(default="")

    def get_full_name(self):
        name = self.first_name
        if self.last_name:
            name += " " + self.last_name
        return name

    def get_unique_name(self):
        return self.full_name.replace(" ", "") + ".user"

    full_name = fields.String(compute=get_full_name)
    unique_name = fields.String(compute=get_unique_name)

    def get_my_greeter(self):
        return Greeter(self.full_name)

    my_greeter = fields.Typed(Greeter, stored=False, compute=get_my_greeter)
    ahmed_greeter = fields.Typed(Greeter,
                                 stored=False,
                                 default=Greeter("ahmed"))
Exemple #15
0
class K8s(Base):
    id = fields.Integer()
    size = fields.Integer()
    network_id = fields.String(default="")
    ipaddress = fields.IPAddress()
    cluster_secret = fields.String(default="")
    master_ips = fields.List(fields.IPAddress())
    ssh_keys = fields.List(fields.String())
    public_ip = fields.Integer()
    stats_aggregator = fields.List(fields.Object(Statsaggregator))
    info = fields.Object(ReservationInfo)
    datastore_endpoint = fields.String(default="")
    disable_default_ingress = fields.Boolean(default=True)

    SIZES = VMSIZES

    def resource_units(self):

        resource_units = ResourceUnitAmount()
        size = VMSIZES.get(self.size)
        if not size:
            raise j.exceptions.Input(f"kubernetes size {self.size} not supported")

        resource_units.cru += size["cru"]
        resource_units.mru += size["mru"]
        resource_units.sru += size["sru"]
        return resource_units
Exemple #16
0
class SonicClient(Client):
    host = fields.String(default="127.0.0.1")
    port = fields.Integer(default=1491)
    password = fields.String(default="pass")

    def __init__(self):
        super().__init__()

        self.cached_client_search = None
        self.cached_client_ingest = None

        self.push = self.client_ingest.push
        self.pop = self.client_ingest.pop
        self.count = self.client_ingest.count
        self.flush = self.client_ingest.flush
        self.flush_collection = self.client_ingest.flush_collection
        self.flush_bucket = self.client_ingest.flush_bucket
        self.flush_object = self.client_ingest.flush_object

        self.query = self.client_search.query
        self.suggest = self.client_search.suggest

    @property
    def client_ingest(self):
        if not self.cached_client_ingest:
            self.cached_client_ingest = IngestClient(host=self.host, port=self.port, password=self.password)
        return self.cached_client_ingest

    @property
    def client_search(self):
        if not self.cached_client_search:
            self.cached_client_search = SearchClient(host=self.host, port=self.port, password=self.password)
        return self.cached_client_search
Exemple #17
0
class ChartConfig(Base):
    cert_resolver = fields.String(default="le")
    domain = fields.String(default=None)
    domain_type = fields.String()
    resources_limits = fields.Typed(dict, default={})
    backup = fields.String(default="vdc")
    ip_version = fields.String(default="IPv6")
    extra_config = fields.Typed(dict, default={})
Exemple #18
0
class GatewaySubdomain(Base):
    id = fields.Integer()
    domain = fields.String(default="")
    ips = fields.List(fields.String())
    info = fields.Object(ReservationInfo)

    def resource_units(self):
        return ResourceUnitAmount()
Exemple #19
0
class GatewayReverseProxy(Base):
    id = fields.Integer()
    domain = fields.String(default="")
    secret = fields.String(default="")
    info = fields.Object(ReservationInfo)

    def resource_units(self):
        return ResourceUnitAmount()
Exemple #20
0
class ReservationResult(Base):
    category = fields.Enum(Category)
    workload_id = fields.String(default="")
    data_json = fields.Json()
    signature = fields.Bytes()
    state = fields.Enum(State)
    message = fields.String(default="")
    epoch = fields.DateTime()
Exemple #21
0
class ContainerLogsRedis(Base):
    # deprecated, please use secret_stdout instead
    stdout = fields.String(default="")
    # deprecated, please use secret_stderr instead
    stderr = fields.String(default="")

    secret_stdout = fields.String(default="")
    secret_stderr = fields.String(default="")
Exemple #22
0
class Location(Base):
    city = fields.String(default="")
    country = fields.String(default="")
    continent = fields.String(default="")
    latitude = fields.Float()
    longitude = fields.Float()

    def __str__(self):
        return ",".join([x for x in [self.continent, self.country, self.city] if x])
Exemple #23
0
class GatewayProxy(Base):
    id = fields.Integer()
    domain = fields.String(default="")
    addr = fields.String(default="")
    port = fields.Integer()
    port_tls = fields.Integer()
    info = fields.Object(ReservationInfo)

    def resource_units(self):
        return ResourceUnitAmount()
Exemple #24
0
class K8s(Base):
    id = fields.Integer()
    size = fields.Integer()
    network_id = fields.String(default="")
    ipaddress = fields.IPAddress()
    cluster_secret = fields.String(default="")
    master_ips = fields.List(fields.IPAddress())
    ssh_keys = fields.List(fields.String())
    stats_aggregator = fields.List(fields.Object(Statsaggregator))
    info = fields.Object(ReservationInfo)
Exemple #25
0
class NetworkResource(Base):
    id = fields.Integer()
    name = fields.String(default="")
    network_iprange = fields.IPRange(default="10.10.0.0/16")
    wireguard_private_key_encrypted = fields.String(default="")
    wireguard_public_key = fields.String(default="")
    wireguard_listen_port = fields.Integer()
    iprange = fields.IPRange(default="10.10.10.0/24")
    peers = fields.List(fields.Object(WireguardPeer))
    info = fields.Object(ReservationInfo)
Exemple #26
0
class ZdbNamespace(Base):
    id = fields.Integer()
    node_id = fields.String(default="")
    size = fields.Integer()
    mode = fields.Enum(ZDBMode)
    password = fields.String(default="")
    disk_type = fields.Enum(DiskType)
    public = fields.Boolean(default=False)
    stats_aggregator = fields.List(fields.Object(Statsaggregator))
    info = fields.Object(ReservationInfo)
Exemple #27
0
class User(Base):
    user_code = fields.String(default="")
    poll_name = fields.String(default="")
    wallets_addresses = fields.List(fields.String())
    transaction_hashes = fields.List(fields.String())
    tokens = fields.Float(default=0.0)
    vote_data = fields.Typed(dict, default={})
    extra_data = fields.Typed(dict, default={})
    vote_data_weighted = fields.Typed(dict, default={})
    has_voted = fields.Boolean(default=False)
    manifesto_version = fields.String(default="2.0.0")
Exemple #28
0
class SSHKeyClient(Client):
    name = fields.String()
    public_key = fields.String()
    private_key = fields.String()  #should use secret.
    private_key_path = fields.String()  # should use secret.
    passphrase = fields.String(default="")  # should use secret.
    duration = fields.Integer()
    allow_agent = fields.Boolean()

    def __init__(self):
        super().__init__()
        if self.private_key_path and j.sals.fs.exists(self.private_key_path):
            self.load_from_file_system()

    def load_from_file_system(self):
        self.public_key = j.sals.fs.read_file(self.public_key_path)
        self.private_key = j.sals.fs.read_file(self.private_key_path)

    def generate_keys(self):
        if not self.private_key_path:
            # TODO: make sure the new sshkey name doesn't exist.
            sshkeys_dir = j.sals.fs.join_paths(j.core.config.config_root,
                                               "sshkeys")
            j.sals.fs.mkdirs(sshkeys_dir)
            self.private_key_path = j.sals.fs.join_paths(
                sshkeys_dir, j.data.idgenerator.chars(8))
        if self.passphrase and len(self.passphrase) < 5:
            raise ValueError(
                "invalid passphrase length: should be at least 5 chars.")
        cmd = 'ssh-keygen -f {} -N "{}"'.format(self.private_key_path,
                                                self.passphrase)
        rc, out, err = j.core.executors.run_local(cmd)
        if rc == 0:
            self.public_key = j.sals.fs.read_file(self.public_key_path)
            self.private_key = j.sals.fs.read_file(self.private_key_path)
        else:
            raise RuntimeError("couldn't create sshkey")

    @property
    def public_key_path(self):
        return "{}.pub".format(self.private_key_path)

    def write_to_filesystem(self):
        if not self.private_key:
            raise RuntimeError("no private key to write")

        if not self.public_key:
            raise RuntimeError("no public key to write")

        j.sals.fs.write_file(self.private_key_path, self.private_key)
        j.sals.fs.write_file(self.public_key_path, self.public_key)

    def delete_from_filesystem(self):
        pass
Exemple #29
0
class PoolPayment(Base):
    id = fields.Integer()
    farmer_id = fields.Integer()
    address = fields.String()
    expiration = fields.DateTime()
    asset = fields.String()
    amount = fields.Integer()
    paid = fields.Boolean()
    released = fields.Boolean()
    canceled = fields.Boolean()
    cause = fields.String()
Exemple #30
0
class Certbot(Base):
    DEFAULT_NAME = "certbot"
    DEFAULT_LOGS_DIR = j.sals.fs.join_paths(j.core.dirs.LOGDIR, DEFAULT_NAME)
    DEFAULT_CONFIG_DIR = j.sals.fs.join_paths(j.core.dirs.CFGDIR, DEFAULT_NAME)
    DEFAULT_WORK_DIR = j.sals.fs.join_paths(j.core.dirs.VARDIR, DEFAULT_NAME)

    # the following options match the certbot command arguments
    domain = fields.String(required=True)
    non_interactive = fields.Boolean(default=True)
    agree_tos = fields.Boolean(default=True)
    logs_dir = fields.String(default=DEFAULT_LOGS_DIR)
    config_dir = fields.String(default=DEFAULT_CONFIG_DIR)
    work_dir = fields.String(default=DEFAULT_WORK_DIR)

    email = fields.Email()
    server = fields.URL()
    eab_kid = fields.String()
    eab_hmac_key = fields.String()

    # for existing certificates
    key_path = fields.String()
    cert_path = fields.String()
    fullchain_path = fields.String()

    @property
    def run_cmd(self):
        args = [self.DEFAULT_NAME]

        for name, value in self.to_dict().items():
            if name.endswith("_"):
                continue

            if value:
                # append only if the field has a value
                name = name.replace("_", "-")
                args.append(f"--{name}")

                # append the value itself only if it's a boolean value
                # boolean options are set by adding name only
                if not isinstance(value, bool):
                    args.append(value)

        return args

    @property
    def install_cmd(self):
        # replace "certbot" with "certbot install"
        cmd = self.run_cmd
        cmd.insert(1, "install")
        return cmd

    @property
    def renew_cmd(self):
        # replace "certbot" with "certbot install"
        renew_certbot = Certbot(work_dir=self.work_dir, config_dir=self.config_dir, logs_dir=self.logs_dir, domain="")
        cmd = renew_certbot.run_cmd
        cmd.insert(1, "renew")
        return cmd