Пример #1
0
    def put(self, token):
        try:
            data = self.parser.parse_args()
            validateObj = ServiceModel.validateData(data, request)
            if type(validateObj) is dict:
                return {"success": False, "errors": validateObj}, 400

            service_id = decodeID(token)
            service = ServiceModel.find_by_id(service_id)
            if not service or service.token != token:
                return {"success": False, 'message': 'Record Not Found'}, 404

            service.service_name = data['service_name']
            service.logo = data['logo']
            service.brand_id = data['brand_id']
            service.category_id = data['category_id']
            service.modified_on = datetime.now()
            service.modified_by = 1
            name = data['service_name']
            if name.strip():
                service.save_to_db()
            else:
                return {
                    "success": False,
                    "message": "String Should not be empty"
                }

            return {
                "success": True,
                "message": "Record updated successfully."
            }, 200
        except Exception as e:
            return {"success": False, "message": str(e)}
Пример #2
0
 def get(cls, token):
     try:
         service_id = decodeID(token)
         service = ServiceModel.find_by_id(service_id)
         if not service or service.token != token:
             return {"success": False, 'message': 'Record Not Found'}, 404
         return service.json(), 200
     except Exception as e:
         return {"success": False, "message": str(e)}
Пример #3
0
    def delete(cls, token):
        try:
            service_id = decodeID(token)
            service = ServiceModel.find_by_id(service_id)
            if not service or service.token != token:
                return {"success": False, 'message': 'Record Not Found'}, 404

            service.deleted_by = 1
            service.deleted_on = datetime.now()
            service.save_to_db()
            #service.delete_from_db()
            return {"success": True, 'message': 'Record deleted.'}, 200
        except Exception as e:
            return {"success": False, "message": str(e)}
Пример #4
0
  def delete(self, _id):
    """DELETE method to delete service by ID."""    

    try:
      service = ServiceModel.find_by_id(_id)
    except:
      return response(500, None, f"An error occured while trying to delete service {service.name}.", None), 500

    if service:
      try:
        service.delete_from_db()
      except:
        return response(500, None, f"An error occured while trying delete service {data['name']}.", None), 500
      return response(200, f"Service {service.name} has been deleted.", None, None), 200

    return response(404, None, f"Service with id {_id} does not exist.", None), 404
Пример #5
0
  def get(self, _id):
    """GET method to retrieve a service by ID."""    

    try:
      service = ServiceModel.find_by_id(_id)
    except:
      return {
        'error': f"An error occured while trying to retrieve service."
      }, 500

    if service:
      return response(201, f"Service {service.name} has been retrieved.", None, service.json()), 201

    return {
      'error': f"Service with ID {_id} does not exist."
    }, 400
Пример #6
0
  def post(self, _id):
    """POST method to save the project files to the file system."""

    if not StackModel.find_by_id(_id):
      return response(404, None, f"Stack with ID {_id} does not exist.", None), 404

    stack = StackModel.find_by_id(_id)

    if not stack.proxy_service:
      return response(400, None, f"Stack {stack.name} has no proxy_service defined.", None), 400

    if not stack.proxy_port:
      return response(400, None, f"Stack {stack.name} has no proxy_port defined.", None), 400

    # Creating necessary folders
    stacks_folder = StackModel.get_stacks_folder()
    caddyconf_folder = stacks_folder + "/conf.d"
    project_folder = stack.get_project_folder()

    compose_location = project_folder + "/docker-compose.yml"
    caddyconf_location = caddyconf_folder + f"/{stack.name}.conf"

    if not path.exists(project_folder):
      makedirs(project_folder)

    if not path.exists(caddyconf_folder):
      makedirs(caddyconf_folder)

    data_compose = get_compose_data(services=stack.services, stack_name=stack.name)
    data_caddyconf = {
      'subdomain': stack.subdomain,
      'email': stack.email,
      'proxy_service': ServiceModel.find_by_id(stack.proxy_service).name,
      'proxy_port': stack.proxy_port
    }

    if data_compose:
      compose_string = get_compose(data_compose)
      caddyconf_string = get_caddyconf(data_caddyconf)

      if create_file(compose_location, compose_string) and create_file(caddyconf_location, caddyconf_string):
        return response(200, f"Stack {stack.name} has been applied.", None, None), 200
      else:
        return response(500, None, f"An error has occured while trying to save compose file.", None), 500

    else:
      return response(500, None, f"An error has occured while trying to assemble data for compose file", None), 500
Пример #7
0
  def post(self):
    """POST method to create a new stack."""

    data = self.parser.parse_args()

    if StackModel.find_by_name(data['name']):
      return response(400, None, f"Stack with name {data['name']} already exists.", None), 400

    # Argument check
    args = self.check_args(data)
    if args['code'] is not 200:
      return response(args['code'], None, args['error'], None), args['code']

    stack = StackModel(name = data['name'],
                       description = data['description'],
                       subdomain = data['subdomain'] if data['subdomain'] else 'localhost',
                       email = data['email'],
                       proxy_port = data['proxy_port'])

    if data['services'] and data['services'] != [None]:
      for x in data['services']:
          service = ServiceModel.find_by_id(x)
          if service:
            stack.services.append(service)
          else:
            return response(400, None, f"Service with ID {x} cannot be found.", None), 400

    # Check if proxy_serve is part of stack.services
    if data['proxy_service']:
      try:
        if data['proxy_service'] not in [x.id for x in stack.services]:
          return response(400, None, f"Service with ID {data['proxy_service']} is not part of Stack {stack.name}'s services.", None), 400
        else:
          stack.proxy_service = data['proxy_service']
      except:
        print_exc()
        return response(500, None, f"An internal error occured while checking the proxy_service.", None), 500

    try:
      stack.save_to_db()
    except:
      return response(500, None, f"An error occured while trying to update stack {data['name']}.", None), 500

    return response(201, f"Stack {data['name']} has been updated.", None, stack.json()), 201
Пример #8
0
  def put(self):
    """PUT method to create or update a stack."""

    data = self.parser.parse_args()

    # Argument check
    args = self.check_args(data)
    if args['code'] is not 200:
      return response(args['code'], None, args['error'], None), args['code']

    stack = StackModel.find_by_name(data['name'])



    if stack:
      stack.name = data['name']
      stack.description = data['description']
      stack.subdomain = data['subdomain'] if data['subdomain'] else 'localhost',
      stack.email = data['email']
      stack.proxy_port = data['proxy_port']
      stack.proxy_service = data['proxy_service']
      stack.last_changed = datetime.now()

      # Update m:n-Table for stacks:services
      if data['services'] and data['services'] != [None]:
        # Get sets of services which need to be updated and deleted
        old = {x.id for x in stack.services}
        new = set(data['services'])
        to_update = new - old
        to_delete = old - new
        
        # Remove old services
        for x in to_delete:
          stack.services.remove(ServiceModel.find_by_id(x))

        # Add new services
        for x in to_update:
          service = ServiceModel.find_by_id(x)
          if service:
            stack.services.append(service)
          else:
            return response(400, None, f"Service with ID {x} cannot be found.", None), 400
      else:
        for x in [y.id for y in stack.services]:
          stack.services.remove(ServiceModel.find_by_id(x))

    else:
      stack = StackModel(name = data['name'],
                         description = data['description'],
                         subdomain = data['subdomain'] if data['subdomain'] else 'localhost',
                         email = data['email'],
                         proxy_port = data['proxy_port'])

      if data['services'] and data['services'] != [None]:
        for x in data['services']:
          service = ServiceModel.find_by_id(x)
          if service:
            stack.services.append(service)
          else:
            return response(400, None, f"Service with ID {x} cannot be found.", None), 400
    
    # Check if proxy_service is part of stack.services
    if data['proxy_service']:
      try:
        if data['proxy_service'] not in [x.id for x in stack.services]:
          return response(400, None, f"Service with ID {data['proxy_service']} is not part of Stack {stack.name}'s services.", None), 400
        else:
          pass
      except:
        print_exc()
        return response(500, None, f"An internal error occured while checking the proxy_service.", None), 500

    try:
      stack.save_to_db()
    except:
      print_exc()
      return response(500, None, f"An error occured while trying to update stack {data['name']}.", None), 500

    return response(201, f"Stack {data['name']} has been updated.", None, stack.json()), 201