Search in sources :

Example 1 with JobType

use of com.tremolosecurity.config.xml.JobType in project OpenUnison by TremoloSecurity.

the class LoadJobsFromK8s method createJob.

private void createJob(JSONObject item, String name) throws ProvisioningException {
    HttpCon nonwatchHttp = null;
    JobType job = new JobType();
    job.setName(name);
    JSONObject spec = (JSONObject) item.get("spec");
    StringBuffer b = new StringBuffer();
    b.setLength(0);
    OpenUnisonConfigLoader.integrateIncludes(b, (String) spec.get("className"));
    job.setClassName(b.toString());
    b.setLength(0);
    OpenUnisonConfigLoader.integrateIncludes(b, (String) spec.get("group"));
    job.setGroup(b.toString());
    JSONArray params = (JSONArray) spec.get("params");
    for (Object o : params) {
        JSONObject param = (JSONObject) o;
        ParamWithValueType pt = new ParamWithValueType();
        b.setLength(0);
        OpenUnisonConfigLoader.integrateIncludes(b, (String) param.get("name"));
        pt.setName(b.toString());
        b.setLength(0);
        OpenUnisonConfigLoader.integrateIncludes(b, (String) param.get("value"));
        pt.setValue(b.toString());
        job.getParam().add(pt);
    }
    JSONArray secretParams = (JSONArray) spec.get("secretParams");
    if (secretParams != null) {
        try {
            nonwatchHttp = this.k8sWatch.getK8s().createClient();
            String token = this.k8sWatch.getK8s().getAuthToken();
            for (Object o : secretParams) {
                JSONObject secretParam = (JSONObject) o;
                String paramName = (String) secretParam.get("name");
                String secretName = (String) secretParam.get("secretName");
                String secretKey = (String) secretParam.get("secretKey");
                String secretValue = this.k8sWatch.getSecretValue(secretName, secretKey, token, nonwatchHttp);
                ParamWithValueType pt = new ParamWithValueType();
                pt.setName(paramName);
                pt.setValue(secretValue);
                job.getParam().add(pt);
            }
        } catch (Exception e) {
            throw new ProvisioningException("Could not load secrets for '" + name + "'");
        } finally {
            if (nonwatchHttp != null) {
                try {
                    nonwatchHttp.getHttp().close();
                } catch (IOException e) {
                }
                nonwatchHttp.getBcm().close();
            }
        }
    }
    job.setCronSchedule(new CronScheduleType());
    JSONObject cron = (JSONObject) spec.get("cronSchedule");
    b.setLength(0);
    OpenUnisonConfigLoader.integrateIncludes(b, (String) cron.get("seconds"));
    job.getCronSchedule().setSeconds(b.toString());
    b.setLength(0);
    OpenUnisonConfigLoader.integrateIncludes(b, (String) cron.get("minutes"));
    job.getCronSchedule().setMinutes(b.toString());
    b.setLength(0);
    OpenUnisonConfigLoader.integrateIncludes(b, (String) cron.get("hours"));
    job.getCronSchedule().setHours(b.toString());
    b.setLength(0);
    OpenUnisonConfigLoader.integrateIncludes(b, (String) cron.get("dayOfMonth"));
    job.getCronSchedule().setDayOfMonth(b.toString());
    b.setLength(0);
    OpenUnisonConfigLoader.integrateIncludes(b, (String) cron.get("month"));
    job.getCronSchedule().setMonth(b.toString());
    b.setLength(0);
    OpenUnisonConfigLoader.integrateIncludes(b, (String) cron.get("dayOfWeek"));
    job.getCronSchedule().setDayOfWeek(b.toString());
    b.setLength(0);
    OpenUnisonConfigLoader.integrateIncludes(b, (String) cron.get("year"));
    job.getCronSchedule().setYear(b.toString());
    try {
        this.cfgMgr.getProvisioningEngine().addNewJob(jobKeys, job);
    } catch (ClassNotFoundException | SchedulerException | ProvisioningException e) {
        throw new ProvisioningException("Could not add job '" + name + "'", e);
    }
}
Also used : SchedulerException(org.quartz.SchedulerException) JSONArray(org.json.simple.JSONArray) IOException(java.io.IOException) SchedulerException(org.quartz.SchedulerException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) JobType(com.tremolosecurity.config.xml.JobType) JSONObject(org.json.simple.JSONObject) CronScheduleType(com.tremolosecurity.config.xml.CronScheduleType) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) JSONObject(org.json.simple.JSONObject) ParamWithValueType(com.tremolosecurity.config.xml.ParamWithValueType)

Example 2 with JobType

use of com.tremolosecurity.config.xml.JobType in project OpenUnison by TremoloSecurity.

the class SendMessageThread method initScheduler.

@Override
public void initScheduler() throws ProvisioningException {
    if (this.cfgMgr.getCfg().getProvisioning() == null || this.cfgMgr.getCfg().getProvisioning().getScheduler() == null) {
        logger.warn("Scheduler not defined");
        return;
    }
    SchedulingType sct = this.cfgMgr.getCfg().getProvisioning().getScheduler();
    Properties scheduleProps = new Properties();
    scheduleProps.setProperty("org.quartz.scheduler.instanceName", sct.getInstanceLabel());
    /*String instanceLabel = null;
		try {
			Enumeration<NetworkInterface> enumer = NetworkInterface.getNetworkInterfaces();
			while (enumer.hasMoreElements()) {
				NetworkInterface ni = enumer.nextElement();
				Enumeration<InetAddress> enumeri = ni.getInetAddresses();
				while (enumeri.hasMoreElements()) {
					InetAddress addr = enumeri.nextElement();
					if (addr.getHostAddress().startsWith(sct.getInstanceIPMask())) {
						instanceLabel = addr.getHostAddress();
					}
				}
			}
		} catch (SocketException e) {
			throw new ProvisioningException("Could not read network addresses",e);
		}
		
		if (instanceLabel == null) {
			logger.warn("No IP starts with '" + sct.getInstanceIPMask() + "'");
			instanceLabel = "AUTO";
		}*/
    scheduleProps.setProperty("org.quartz.scheduler.instanceId", UUID.randomUUID().toString());
    scheduleProps.setProperty("org.quartz.threadPool.threadCount", Integer.toString(sct.getThreadCount()));
    if (sct.isUseDB()) {
        scheduleProps.setProperty("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
        scheduleProps.setProperty("org.quartz.jobStore.driverDelegateClass", sct.getScheduleDB().getDelegateClassName());
        scheduleProps.setProperty("org.quartz.jobStore.dataSource", "scheduleDB");
        scheduleProps.setProperty("org.quartz.dataSource.scheduleDB.driver", sct.getScheduleDB().getDriver());
        scheduleProps.setProperty("org.quartz.dataSource.scheduleDB.URL", sct.getScheduleDB().getUrl());
        scheduleProps.setProperty("org.quartz.dataSource.scheduleDB.user", sct.getScheduleDB().getUser());
        scheduleProps.setProperty("org.quartz.dataSource.scheduleDB.password", sct.getScheduleDB().getPassword());
        scheduleProps.setProperty("org.quartz.dataSource.scheduleDB.maxConnections", Integer.toString(sct.getScheduleDB().getMaxConnections()));
        scheduleProps.setProperty("org.quartz.dataSource.scheduleDB.validationQuery", sct.getScheduleDB().getValidationQuery());
        scheduleProps.setProperty("org.quartz.jobStore.useProperties", "true");
        scheduleProps.setProperty("org.quartz.jobStore.isClustered", "true");
    } else {
        scheduleProps.setProperty("org.quartz.jobStore.class", "org.quartz.simpl.RAMJobStore");
    }
    try {
        /*String classpath = System.getProperty("java.class.path");
			String[] classpathEntries = classpath.split(File.pathSeparator);
			for (String cp : classpathEntries) {
				System.out.println(cp);
			}*/
        PrintStream out = new PrintStream(new FileOutputStream(System.getProperty(OpenUnisonConstants.UNISON_CONFIG_QUARTZDIR) + "/quartz.properties"));
        scheduleProps.store(out, "Unison internal scheduler properties");
        out.flush();
        out.close();
    } catch (IOException e) {
        throw new ProvisioningException("Could not write to quartz.properties", e);
    }
    try {
        this.scheduler = StdSchedulerFactory.getDefaultScheduler();
        this.scheduler.start();
        this.cfgMgr.addThread(new StopScheduler(this.scheduler));
        HashSet<String> jobKeys = new HashSet<String>();
        for (JobType jobType : sct.getJob()) {
            addNewJob(jobKeys, jobType);
        }
        DynamicPortalUrlsType dynamicJobs = cfgMgr.getCfg().getProvisioning().getScheduler().getDynamicJobs();
        if (dynamicJobs != null && dynamicJobs.isEnabled()) {
            String className = dynamicJobs.getClassName();
            HashMap<String, Attribute> cfgAttrs = new HashMap<String, Attribute>();
            for (ParamType pt : dynamicJobs.getParams()) {
                Attribute attr = cfgAttrs.get(pt.getName());
                if (attr == null) {
                    attr = new Attribute(pt.getName());
                    cfgAttrs.put(pt.getName(), attr);
                }
                attr.getValues().add(pt.getValue());
            }
            DynamicJobs dynJobs = null;
            try {
                dynJobs = (DynamicJobs) Class.forName(className).newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                throw new ProvisioningException("Could not create dynmaic job", e);
            }
            dynJobs.loadDynamicJobs(cfgMgr, this, cfgAttrs, jobKeys);
        }
        for (String groupName : scheduler.getJobGroupNames()) {
            this.deleteRemovedJobs(jobKeys, groupName);
        }
    } catch (SchedulerException e) {
        throw new ProvisioningException("Could not initialize scheduler", e);
    } catch (ClassNotFoundException e) {
        throw new ProvisioningException("Could not initialize scheduler", e);
    }
}
Also used : PrintStream(java.io.PrintStream) SchedulerException(org.quartz.SchedulerException) Attribute(com.tremolosecurity.saml.Attribute) LDAPAttribute(com.novell.ldap.LDAPAttribute) HashMap(java.util.HashMap) SchedulingType(com.tremolosecurity.config.xml.SchedulingType) IOException(java.io.IOException) Properties(java.util.Properties) ParamType(com.tremolosecurity.config.xml.ParamType) DynamicJobs(com.tremolosecurity.provisioning.jobs.DynamicJobs) JobType(com.tremolosecurity.config.xml.JobType) FileOutputStream(java.io.FileOutputStream) DynamicPortalUrlsType(com.tremolosecurity.config.xml.DynamicPortalUrlsType) StopScheduler(com.tremolosecurity.provisioning.scheduler.StopScheduler) HashSet(java.util.HashSet)

Aggregations

JobType (com.tremolosecurity.config.xml.JobType)2 IOException (java.io.IOException)2 SchedulerException (org.quartz.SchedulerException)2 LDAPAttribute (com.novell.ldap.LDAPAttribute)1 CronScheduleType (com.tremolosecurity.config.xml.CronScheduleType)1 DynamicPortalUrlsType (com.tremolosecurity.config.xml.DynamicPortalUrlsType)1 ParamType (com.tremolosecurity.config.xml.ParamType)1 ParamWithValueType (com.tremolosecurity.config.xml.ParamWithValueType)1 SchedulingType (com.tremolosecurity.config.xml.SchedulingType)1 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)1 DynamicJobs (com.tremolosecurity.provisioning.jobs.DynamicJobs)1 StopScheduler (com.tremolosecurity.provisioning.scheduler.StopScheduler)1 HttpCon (com.tremolosecurity.provisioning.util.HttpCon)1 Attribute (com.tremolosecurity.saml.Attribute)1 FileOutputStream (java.io.FileOutputStream)1 PrintStream (java.io.PrintStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 JSONArray (org.json.simple.JSONArray)1