public String instantiateTag(Map tokens) { return s(tagFormat).formatByMap(tokens); }
@Override public String toString() { return ln(template).formatByMap(tokens); } }
@Override public boolean isModuleChanged(String playbookName) { String moduleName = StringDSL.stripEnd(playbookName, ".py"); Boolean ret = moduleChanges.get(moduleName); DebugUtils.Assert(ret != null, String.format("cannot find ansible module name[%s]", moduleName)); if (ret) { // we only need to deploy once moduleChanges.put(moduleName, false); } return ret; }
if (value != null && value instanceof String && param.resourceType() != Object.class && !StringDSL.isZStackUuid(((String) value).trim())) { errors.add(String.format("field[%s] is not a valid uuid.", f.getName()));
String srcScript = String.format("zstack-script-%s", UUID.randomUUID().toString()); scriptFile = new File(PathUtil.join(PathUtil.getFolderUnderZStackHomeFolder("temp-scripts"), srcScript)); scriptContent = s(contents).formatByMap(token); String remoteScript = ln( "/bin/bash << EOF", "cat << EOF1 > {remotePath}",
public boolean isEquals(ApplianceVmFirewallRuleVO vo) { return vo != null && startPort == vo.getStartPort() && endPort == vo.getEndPort() && protocol.equals(vo.getProtocol().toString()) &&l3NetworkUuid.equals(vo.getL3NetworkUuid()) && applianceVmUuid.equals(vo.getApplianceVmUuid()) && StringDSL.equals(allowCidr, vo.getAllowCidr()) && StringDSL.equals(sourceIp, vo.getSourceIp()) && StringDSL.equals(destIp, vo.getDestIp()); } }
private synchronized static void insertLogError(String content, ErrorCodeElaboration err, boolean matched) { DatabaseFacade dbf = getComponentLoader().getComponent(DatabaseFacade.class); String md5Sum = StringDSL.getMd5Sum(content); ElaborationVO mvo = Q.New(ElaborationVO.class).eq(ElaborationVO_.md5sum, md5Sum).find(); if (mvo != null) { mvo.setDistance(err.getDistance()); mvo.setMatched(matched); mvo.setRepeats(mvo.getRepeats() + 1); dbf.updateAndRefresh(mvo); } else { mvo = new ElaborationVO(); mvo.setDistance(err.getDistance()); mvo.setRepeats(1L); mvo.setMatched(matched); mvo.setMd5sum(md5Sum); mvo.setErrorInfo(content); dbf.persistAndRefresh(mvo); } }
@Transactional private void fixImageCacheUuid() { String sql = "select c, pri.type from ImageCacheVO c, PrimaryStorageVO pri where c.primaryStorageUuid = pri.uuid" + " and c.imageUuid is null"; TypedQuery<Tuple> q = dbf.getEntityManager().createQuery(sql, Tuple.class); List<Tuple> ts = q.getResultList(); for (Tuple t : ts) { ImageCacheVO c = t.get(0, ImageCacheVO.class); String psType = t.get(1, String.class); String imgUuid; if ("Ceph".equals(psType)) { imgUuid = c.getInstallUrl().split("@")[1]; } else if ("NFS".equals(psType) || "SharedMountPoint".equals(psType)) { imgUuid = new File(c.getInstallUrl()).getName().split("\\.")[0]; } else if ("LocalStorage".equals(psType)) { String[] pair = c.getInstallUrl().split(";"); imgUuid = new File(pair[0]).getName().split("\\.")[0]; } else { throw new CloudRuntimeException(String.format("unknown primary storage type[%s] for the ImageCacheVO[id:%s]", psType, c.getId())); } if (!StringDSL.isZStackUuid(imgUuid)) { throw new CloudRuntimeException(String.format("the image UUID[%s] parsed from the URL[%s] of the ImageCacheVO[id:%s] " + "on the primary storage[type:%s] looks no correct", imgUuid, c.getInstallUrl(), c.getId(), psType)); } c.setImageUuid(imgUuid); dbf.getEntityManager().merge(c); } }
public Ssh checkTool(String...toolNames) { String tool = StringUtils.join(Arrays.asList(toolNames), " "); String cmdstr = s("EXIT (){ echo \"$1\"; exit 1;}; cmds=\"{0}\"; for cmd in $cmds; do which $cmd >/dev/null 2>&1 || EXIT \"Not find command: $cmd\"; done").format(tool); return command(cmdstr); }
@Override public APIMessage intercept(APIMessage msg) throws ApiMessageInterceptionException { if (msg instanceof APICreateMessage) { APICreateMessage cmsg = (APICreateMessage) msg; if (cmsg.getResourceUuid() != null) { if (!StringDSL.isZStackUuid(cmsg.getResourceUuid())) { throw new ApiMessageInterceptionException(argerr("resourceUuid[%s] is not a valid uuid. A valid uuid is a UUID(v4 recommended) with '-' stripped. " + "see http://en.wikipedia.org/wiki/Universally_unique_identifier for format of UUID, the regular expression ZStack uses" + " to validate a UUID is '[0-9a-f]{8}[0-9a-f]{4}[1-5][0-9a-f]{3}[89ab][0-9a-f]{3}[0-9a-f]{12}'", cmsg.getResourceUuid())); } } } return msg; }
public static String i18n(String str, Map<String, String> args) { Map<String, String> nargs = new HashMap<>(); args.forEach((k, v) -> nargs.put(k, toI18nString(v))); return ln(toI18nString(str)).formatByMap(nargs); }
} catch (NumberFormatException e) { if (time.endsWith("s")) { time = StringDSL.stripEnd(time, "s"); return TimeUnit.SECONDS.toMillis(Long.valueOf(time)); } else if (time.endsWith("S")) { time = StringDSL.stripEnd(time, "S"); return TimeUnit.SECONDS.toMillis(Long.valueOf(time)); } else if (time.endsWith("m")) { time = StringDSL.stripEnd(time, "m"); return TimeUnit.MINUTES.toMillis(Long.valueOf(time)); } else if (time.endsWith("M")) { time = StringDSL.stripEnd(time, "M"); return TimeUnit.MINUTES.toMillis(Long.valueOf(time)); } else if (time.endsWith("h")) { time = StringDSL.stripEnd(time, "h"); return TimeUnit.HOURS.toMillis(Long.valueOf(time)); } else if (time.endsWith("H")) { time = StringDSL.stripEnd(time, "H"); return TimeUnit.HOURS.toMillis(Long.valueOf(time)); } else if (time.endsWith("d")) { time = StringDSL.stripEnd(time, "d"); return TimeUnit.DAYS.toMillis(Long.valueOf(time)); } else if (time.endsWith("D")) { time = StringDSL.stripEnd(time, "D"); return TimeUnit.DAYS.toMillis(Long.valueOf(time)); } else if (time.endsWith("w")) { time = StringDSL.stripEnd(time, "w"); return TimeUnit.DAYS.toMillis(Long.valueOf(time) * 7); } else if (time.endsWith("W")) {
public SshResult runScriptWithToken(String scriptName, Map token) { String scriptPath = PathUtil.findFileOnClassPath(scriptName, true).getAbsolutePath(); if (token == null) { token = new HashMap(); } String contents = null; try { contents = FileUtils.readFileToString(new File(scriptPath)); } catch (IOException e) { throw new RuntimeException(e); } String script = s(contents).formatByMap(token); return runScript(script); }
@Override public void validateSystemTag(String resourceUuid, Class resourceType, String systemTag) { String uuid = PrimaryStorageSystemTags.PRIMARY_STORAGE_ALLOCATOR_UUID_TAG.getTokenByTag( systemTag, PrimaryStorageSystemTags.PRIMARY_STORAGE_ALLOCATOR_UUID_TAG_TOKEN); if (!StringDSL.isZStackUuid(uuid)) { throw new ApiMessageInterceptionException(argerr("%s is invalid. %s is not a valid zstack uuid", systemTag, uuid)); } if (!dbf.isExist(uuid, PrimaryStorageVO.class)) { throw new ApiMessageInterceptionException(argerr("no primary storage[uuid:%s] found", resourceUuid)); } } });
ScriptRunner(String script) { String remoteScript = ln( "/bin/bash << EOF", "cat << EOF1 > {remotePath}", "{scriptContent}", "EOF1", "/bin/bash {remotePath} 1>{stdout} 2>{stderr}", "ret=$?", "test -f {stdout} && cat {stdout}", "test -f {stderr} && cat {stderr} 1>&2", "rm -f {remotePath}", "rm -f {stdout}", "rm -f {stderr}", "exit $ret", "EOF" ).formatByMap(map(e("remotePath", String.format("/tmp/%s", UUID.randomUUID().toString())), e("scriptContent", script), e("stdout", String.format("/tmp/%s", UUID.randomUUID().toString())), e("stderr", String.format("/tmp/%s", UUID.randomUUID().toString())) )); scriptCommand = createCommand(remoteScript); }
var.put("foreignVO", foreignVOClass.getSimpleName()); if (where.isEmpty()) { return s("{entity}.{primaryKey} in (select {subEntity}.{foreignKey} from {foreignVO} {subEntity})").formatByMap(var); } else { var.put("condition", StringUtils.join(where, " and ").trim()); return s("{entity}.{primaryKey} in (select {subEntity}.{foreignKey} from {foreignVO} {subEntity} where {condition})").formatByMap(var); return s("{entity}.{foreignKey} in (select {expandedEntity}.{expandedKey} from {expandedVO} {expandedEntity})").formatByMap(var); } else { var.put("condition", StringUtils.join(where, " and ").trim()); return s("{entity}.{foreignKey} in (select {expandedEntity}.{expandedKey} from {expandedVO} {expandedEntity} where {condition})").formatByMap(var);
if (!StringDSL.isZStackUuid(vo.getUuid())) { logger.warn(String.format("found a weird management node, it's UUID not a ZStack uuid, delete it. %s", JSONObjectUtil.toJsonString(ManagementNodeInventory.valueOf(vo))));