Beispiel #1
0
  def post(self):
    """POST method to create a new service."""

    data = self.parser.parse_args()

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

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

    volumes = ServiceModel.join_volume_string(data)
    env = ServiceModel.join_env_string(data)
    exposed_ports = ServiceModel.join_port_string(data['exposed_ports'])
    mapped_ports = ServiceModel.join_port_string(data['mapped_ports'])

    service = ServiceModel(name = data['name'],
                           blueprint_id = data['blueprint'],
                           description = data['description'],
                           exposed_ports = exposed_ports,
                           mapped_ports = mapped_ports,
                           volumes = volumes,
                           restart = data['restart'],
                           env = env)

    # Add stack to service table
    if data['stacks'] and data['stacks'] != [None]:
      for x in data['stacks']:
        stack = StackModel.find_by_id(x)
        service.stacks.append(stack)

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

    return response(201, f"Service {data['name']} has been updated.", None, service.json()), 201
Beispiel #2
0
  def put(self):
    """PUT method to create or update a service."""    

    data = self.parser.parse_args()

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

    service = ServiceModel.find_by_name(data['name'])

    volumes = ServiceModel.join_volume_string(data)
    env = ServiceModel.join_env_string(data)
    exposed_ports = ServiceModel.join_port_string(data['exposed_ports'])
    mapped_ports = ServiceModel.join_port_string(data['mapped_ports'])
    
    if service:
      service.name = data['name']
      service.description = data['description']
      service.exposed_ports = exposed_ports
      service.mapped_ports = mapped_ports
      service.volumes = volumes
      service.env = env
      service.restart = data['restart']
      service.blueprint_id = data['blueprint']

      if data['stacks'] and data['stacks'] != [None]:
        # Get sets of stacks which need to be updated or deleted
        old = {x.id for x in service.stacks}
        new = set(data['stacks'])
        to_update = new - old
        to_delete = old - new

        # Remove old stacks
        for x in to_delete:
          service.stacks.remove(StackModel.find_by_id(x))

        # Add new services
        for x in to_update:
          service.stacks.append(StackModel.find_by_id(x))
      else:
        for x in [y.id for y in service.stacks]:
          service.stacks.remove(StackModel.find_by_id(x))

    else:
      service = ServiceModel(name = data['name'],
                             description = data['description'],
                             exposed_ports = exposed_ports,
                             mapped_ports = mapped_ports,
                             volumes = volumes,
                             env = env,
                             restart = data['restart'],
                             blueprint_id = data['blueprint'])

      if data['stacks'] and data['stacks'] != [None]:
        for x in data['stacks']:
          stack = StackModel.find_by_id(x)
          service.stacks.append(stack)

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

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