Beispiel #1
0
  protected void exceptionIfImageSizeGreaterThanAvailableCapacity(String url) {
    if (CoreGlobalProperty.UNIT_TEST_ON) {
      return;
    }

    url = url.trim();
    if (!url.startsWith("http") && !url.startsWith("https")) {
      return;
    }

    String len;
    try {
      HttpHeaders header = restf.getRESTTemplate().headForHeaders(url);
      len = header.getFirst("Content-Length");
    } catch (Exception e) {
      logger.warn(String.format("cannot get image.  The image url : %s.", url));
      return;
    }
    if (len == null) {
      return;
    }

    long size = Long.valueOf(len);
    if (size > self.getAvailableCapacity()) {
      throw new OperationFailureException(
          errf.stringToOperationError(
              String.format(
                  "the backup storage[uuid:%s, name:%s] has not enough capacity to download the image[%s]."
                      + "Required size:%s, available size:%s",
                  self.getUuid(), self.getName(), url, size, self.getAvailableCapacity())));
    }
  }
 public void reply(HttpEntity<String> entity, Object rsp) {
   String taskUuid = entity.getHeaders().getFirst(RESTConstant.TASK_UUID);
   String callbackUrl = entity.getHeaders().getFirst(RESTConstant.CALLBACK_URL);
   String rspBody = JSONObjectUtil.toJsonString(rsp);
   HttpHeaders headers = new HttpHeaders();
   headers.setContentType(MediaType.APPLICATION_JSON);
   headers.setContentLength(rspBody.length());
   headers.set(RESTConstant.TASK_UUID, taskUuid);
   HttpEntity<String> rreq = new HttpEntity<String>(rspBody, headers);
   restf.getRESTTemplate().exchange(callbackUrl, HttpMethod.POST, rreq, String.class);
 }
Beispiel #3
0
  @Test
  public void test() throws ApiSenderException {
    VmInstanceInventory vm = deployer.vms.get("TestVm");
    ImageInventory iso = deployer.images.get("TestIso");
    PrimaryStorageInventory ps = deployer.primaryStorages.get("ceph-pri");

    restf.installBeforeAsyncJsonPostInterceptor(
        new BeforeAsyncJsonPostInterceptor() {
          @Override
          public void beforeAsyncJsonPost(String url, Object body, TimeUnit unit, long timeout) {
            if (body instanceof AttachIsoCmd) {
              AttachIsoCmd cmd = (AttachIsoCmd) body;
              isoto = (KvmCephIsoTO) cmd.iso;
            }
          }

          @Override
          public void beforeAsyncJsonPost(String url, String body, TimeUnit unit, long timeout) {}
        });

    api.attachIso(vm.getUuid(), iso.getUuid(), null);
    Assert.assertNotNull(isoto);
    Assert.assertFalse(isoto.getMonInfo().isEmpty());

    CephPrimaryStorageVO ceph = dbf.findByUuid(ps.getUuid(), CephPrimaryStorageVO.class);
    Assert.assertEquals(ceph.getMons().size(), isoto.getMonInfo().size());

    for (final CephPrimaryStorageMonVO mon : ceph.getMons()) {
      MonInfo info =
          CollectionUtils.find(
              isoto.getMonInfo(),
              new Function<MonInfo, MonInfo>() {
                @Override
                public MonInfo call(MonInfo arg) {
                  return arg.getHostname().equals(mon.getHostname()) ? arg : null;
                }
              });

      Assert.assertNotNull(info);
    }
  }
Beispiel #4
0
  private void refreshFirewall(
      final ApplianceVmRefreshFirewallMsg msg, final NoErrorCompletion completion) {
    class RuleCombiner {
      long total;
      Map<String, List<ApplianceVmFirewallRuleVO>> rules =
          new HashMap<String, List<ApplianceVmFirewallRuleVO>>();
      List<ApplianceVmFirewallRuleTO> result = new ArrayList<ApplianceVmFirewallRuleTO>();
      Map<String, String> l3NicMacMap = new HashMap<String, String>();

      {
        for (VmNicVO nic : self.getVmNics()) {
          l3NicMacMap.put(nic.getL3NetworkUuid(), nic.getMac());
        }

        SimpleQuery<ApplianceVmFirewallRuleVO> q = dbf.createQuery(ApplianceVmFirewallRuleVO.class);
        q.add(ApplianceVmFirewallRuleVO_.applianceVmUuid, Op.EQ, self.getUuid());
        total = q.count();
      }

      List<ApplianceVmFirewallRuleTO> merge() {
        if (total == 0) {
          return new ArrayList<ApplianceVmFirewallRuleTO>();
        }

        prepare();
        normalize();
        return result;
      }

      private void normalize() {
        for (List<ApplianceVmFirewallRuleVO> vos : rules.values()) {
          if (!vos.isEmpty()) {
            normalize(vos);
          }
        }
      }

      private void normalize(List<ApplianceVmFirewallRuleVO> vos) {
        String l3Uuid = null;
        String sip = null;
        String dip = null;
        String allowedCidr = null;
        ApplianceVmFirewallProtocol protocol = null;

        RangeSet rset = new RangeSet();
        for (ApplianceVmFirewallRuleVO vo : vos) {
          if (l3Uuid == null) {
            l3Uuid = vo.getL3NetworkUuid();
          }
          if (sip == null) {
            sip = vo.getSourceIp();
          }
          if (dip == null) {
            dip = vo.getDestIp();
          }
          if (allowedCidr == null) {
            allowedCidr = vo.getAllowCidr();
          }
          if (protocol == null) {
            protocol = vo.getProtocol();
          }
          rset.closed(vo.getStartPort(), vo.getEndPort());
        }

        List<Range> rs = rset.merge();
        for (Range r : rs) {
          ApplianceVmFirewallRuleTO to = new ApplianceVmFirewallRuleTO();
          to.setDestIp(dip);
          to.setNicMac(l3NicMacMap.get(l3Uuid));
          to.setProtocol(protocol.toString());
          to.setAllowCidr(allowedCidr);
          to.setSourceIp(sip);
          to.setStartPort((int) r.getStart());
          to.setEndPort((int) r.getEnd());
          result.add(to);
        }
      }

      private void prepare() {
        int offset = 0;
        int step = 1000;
        while (offset < total) {
          SimpleQuery<ApplianceVmFirewallRuleVO> q =
              dbf.createQuery(ApplianceVmFirewallRuleVO.class);
          q.add(ApplianceVmFirewallRuleVO_.applianceVmUuid, Op.EQ, self.getUuid());
          q.setLimit(step);
          q.setStart(offset);
          List<ApplianceVmFirewallRuleVO> vos = q.list();
          for (ApplianceVmFirewallRuleVO vo : vos) {
            String key =
                String.format(
                    "%s-%s-%s-%s-%s",
                    vo.getL3NetworkUuid(),
                    vo.getProtocol(),
                    vo.getSourceIp(),
                    vo.getDestIp(),
                    vo.getAllowCidr());
            List<ApplianceVmFirewallRuleVO> lst = rules.get(key);
            if (lst == null) {
              lst = new ArrayList<ApplianceVmFirewallRuleVO>();
              rules.put(key, lst);
            }
            lst.add(vo);
          }
          offset += step;
        }
      }
    }

    final ApplianceVmRefreshFirewallReply reply = new ApplianceVmRefreshFirewallReply();
    refreshVO();
    ErrorCode allowed = validateOperationByState(msg, self.getState(), SysErrors.OPERATION_ERROR);
    if (allowed != null) {
      reply.setError(allowed);
      bus.reply(msg, reply);
      completion.done();
      return;
    }

    RefreshFirewallCmd cmd = new RefreshFirewallCmd();
    List<ApplianceVmFirewallRuleTO> tos = new RuleCombiner().merge();
    cmd.setRules(tos);

    resf.asyncJsonPost(
        buildUrl(ApplianceVmConstant.REFRESH_FIREWALL_PATH),
        cmd,
        new JsonAsyncRESTCallback<RefreshFirewallRsp>(msg, completion) {
          @Override
          public void fail(ErrorCode err) {
            reply.setError(err);
            bus.reply(msg, reply);
            completion.done();
          }

          @Override
          public void success(RefreshFirewallRsp ret) {
            if (!ret.isSuccess()) {
              logger.warn(
                  String.format(
                      "failed to refresh firewall rules on appliance vm[uuid:%s, name:%s], %s",
                      self.getUuid(), self.getName(), ret.getError()));
              reply.setError(errf.stringToOperationError(ret.getError()));
            }

            bus.reply(msg, reply);
            completion.done();
          }

          @Override
          public Class<RefreshFirewallRsp> getReturnClass() {
            return RefreshFirewallRsp.class;
          }
        });
  }