def get(self, id):
        """
        download ssh private key
        """
        try:
            try:
                ssh_key = self.session.query(SSHKey).get(id)
                private_file = os.path.join(ssh_key.cache_dir, 'id_rsa')
                username = ssh_key.username
            except Exception as e:
                raise WorkFlowError("query database error, %s" % e.message)

            self.set_header('Content-Type', 'application/force-download')
            self.set_header(
                'Content-Disposition',
                'attachment; filename=%s-%s' % (username, 'id_rsa'))
            with open(private_file, "rb") as f:
                try:
                    while True:
                        _buffer = f.read(4096)
                        if _buffer:
                            self.write(_buffer)
                        else:
                            f.close()
                            self.finish()
                            return
                except Exception as e:
                    raise HTTPError(500,
                                    reason='read file failed: %s' % e.message)
        except Exception as e:
            logger.error(e.message)
            self.my_response(400, e.message)
 def _validate_json(self):
     try:
         data = json.loads(self.request.body)
     except ValueError as e:
         logger.error("dump input data error: %s" % e.message)
         raise WorkFlowError("the pass data must be json data structure!")
     self.validate_data.data = data
 def _validate_required(self):
     required_field = ['resource', 'username']
     for required in required_field:
         if required not in self.validate_data.data:
             raise WorkFlowError("%s must be required!" % required)
     self.validate_data.resource = self.validate_data.data['resource']
     self.validate_data.username = self.validate_data.data['username']
     self.validate_data.key_pass = self.validate_data.data.get(
         "key_pass", None)
 def _validate_required(self):
     required_field = ['resource', 'command']
     for field in self.validate_data.data:
         if field not in required_field:
             raise WorkFlowError("%s must be required!" % field)
    def _validate(self, message):
        """
        validate the input data are validated.

        :param message: the required message. the message date structure is :
        playbook: [{"hosts": <hostname>, "role": <rolename>}, {"hosts": <hostname>, "role": <rolename>}]
        extra_args: {"variable1": <a>, "variable2": <b>}
        inventory: [{"ecs_code": testCode, "ssh_port": 22, "ssh_user": "******", "ssh_pass": "******"}]

        :return: None, but set the validated_data attribute.
        """
        # 检查是否是正确的json格式的数据
        try:
            data = json.loads(json.loads(message))
            self.validated_data.status = True
            logger.debug("websocket input data: %s" % data)
        except ValueError as e:
            logger.warning("decode json failed! :%s" % e.message)
            self.validated_data.status = False
            raise WorkFlowError("the message you require must be json string!")

        # 检查playbook传人的数据是否合法
        try:
            self.validated_data.playbook = data['playbook']
        except Exception as e:
            message = "the field playbook are required!"
            logger.warning(message + e.message)
            raise WorkFlowError(message)

        if not isinstance(self.validated_data.playbook, list):
            raise WorkFlowError("the field playbook must be a array object.")

        if not self.validated_data.playbook:
            raise WorkFlowError("the field playbook has no data")

        playbook_keys = self.validated_data.playbook[0].keys()
        playbook_required_key = ["hosts", "roles"]
        for key in playbook_required_key:
            if key not in playbook_keys:
                raise WorkFlowError("%s are required!" % key)

        # 检查inventory传人的数据是否合法
        try:
            self.validated_data.inventory = data['inventory']
        except Exception as e:
            raise WorkFlowError("the field inventory are required!")

        if not isinstance(self.validated_data.inventory, list):
            raise WorkFlowError("the inventory must be an array!")

        if not self.validated_data.inventory:
            raise WorkFlowError("the inventory has no ecs!")

        if not isinstance(self.validated_data.inventory[0], dict):
            raise WorkFlowError("the inventory ecs element must be an object!")

        inventory_keys = self.validated_data.inventory[0].keys()
        inventory_required_key = ["ecs_code", "username", "password", "port"]
        for key in inventory_required_key:
            if key not in inventory_keys:
                raise WorkFlowError("%s are required!" % key)

        # extra_args是非必须参数
        self.validated_data.extra_args = data.get("variables", None)
 def _validate_required(self):
     required_field = ['resource']
     for required in required_field:
         if required not in self.validate_data.data:
             raise WorkFlowError("%s must be required!" % required)
     self.validate_data.resource = self.validate_data.data['resource']