Search in sources :

Example 1 with FedMsgSubscriberProviderData

use of com.redhat.jenkins.plugins.ci.provider.data.FedMsgSubscriberProviderData in project jms-messaging-plugin by jenkinsci.

the class FedMsgMessagingPluginIntegrationTest method testTriggeringUsingFedMsgLogger.

@Test
public void testTriggeringUsingFedMsgLogger() throws Exception {
    FreeStyleProject jobA = j.createFreeStyleProject();
    jobA.getBuildersList().add(new Shell("echo CI_MESSAGE = $CI_MESSAGE"));
    attachTrigger(new CIBuildTrigger(true, Collections.singletonList(new FedMsgSubscriberProviderData("test", new MessagingProviderOverrides("org.fedoraproject.dev.logger.log"), Collections.singletonList(new MsgCheck("compose", ".+compose_id.+message.+")), "CI_MESSAGE", 60))), jobA);
    // Allow for connection
    Thread.sleep(5000);
    File privateKey = File.createTempFile("ssh", "key");
    FileUtils.copyURLToFile(FedmsgRelayContainer.class.getResource("FedmsgRelayContainer/unsafe"), privateKey);
    Files.setPosixFilePermissions(privateKey.toPath(), singleton(OWNER_READ));
    File ssh = File.createTempFile("jenkins", "ssh");
    FileUtils.writeStringToFile(ssh, "#!/bin/sh\n" + "exec ssh -o StrictHostKeyChecking=no -i " + privateKey.getAbsolutePath() + " fedmsg2@" + fedmsgRelay.getSshIPAndPort() + " fedmsg-logger " + " \"$@\"");
    Files.setPosixFilePermissions(ssh.toPath(), new HashSet<>(Arrays.asList(OWNER_READ, OWNER_EXECUTE)));
    System.out.println(FileUtils.readFileToString(ssh));
    ProcessBuilder gitLog1Pb = new ProcessBuilder(ssh.getAbsolutePath(), "--message='{\"compose\": " + "{\"compose_id\": \"This is a message.\"}}'", "--json-input");
    String output = stringFrom(logProcessBuilderIssues(gitLog1Pb, "ssh"));
    System.out.println(output);
    Thread.sleep(2000);
    waitUntilScheduledBuildCompletes();
    j.assertBuildStatusSuccess(jobA.getLastBuild());
    j.assertLogContains("This is a message", jobA.getLastBuild());
}
Also used : Shell(hudson.tasks.Shell) FedMsgSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.FedMsgSubscriberProviderData) CIBuildTrigger(com.redhat.jenkins.plugins.ci.CIBuildTrigger) FedmsgRelayContainer(com.redhat.jenkins.plugins.ci.integration.fixtures.FedmsgRelayContainer) FreeStyleProject(hudson.model.FreeStyleProject) MessagingProviderOverrides(com.redhat.jenkins.plugins.ci.messaging.MessagingProviderOverrides) MsgCheck(com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck) File(java.io.File) Test(org.junit.Test)

Example 2 with FedMsgSubscriberProviderData

use of com.redhat.jenkins.plugins.ci.provider.data.FedMsgSubscriberProviderData in project jms-messaging-plugin by jenkinsci.

the class FedMsgMessagingWorker method receive.

@Override
public void receive(String jobname, ProviderData pdata) {
    FedMsgSubscriberProviderData pd = (FedMsgSubscriberProviderData) pdata;
    int timeoutInMs = (pd.getTimeout() != null ? pd.getTimeout() : FedMsgSubscriberProviderData.DEFAULT_TIMEOUT_IN_MINUTES) * 60 * 1000;
    if (interrupt) {
        log.info("we have been interrupted at start of receive");
        return;
    }
    while (!subscribe(jobname)) {
        if (!Thread.currentThread().isInterrupted()) {
            try {
                int WAIT_SECONDS = 2;
                Thread.sleep(WAIT_SECONDS * 1000);
            } catch (InterruptedException e) {
                // We were interrupted while waiting to retry. We will
                // jump ship on the next iteration.
                // NB: The interrupt flag was cleared when
                // InterruptedException was thrown. We have to
                // re-install it to make sure we eventually leave this
                // thread.
                Thread.currentThread().interrupt();
            }
        }
    }
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
    long lastSeenMessage = new Date().getTime();
    try {
        while ((new Date().getTime() - lastSeenMessage) < timeoutInMs) {
            if (poller.poll(1000) > 0) {
                pollerClosed = false;
                if (poller.pollin(0)) {
                    ZMsg z = ZMsg.recvMsg(poller.getSocket(0));
                    // Reset timer
                    lastSeenMessage = new Date().getTime();
                    // 
                    String json = z.getLast().toString();
                    FedmsgMessage data = mapper.readValue(json, FedmsgMessage.class);
                    if (provider.verify(data.getBodyJson(), pd.getChecks(), jobname)) {
                        Map<String, String> params = new HashMap<>();
                        params.put("CI_MESSAGE", data.getBodyJson());
                        trigger(jobname, provider.formatMessage(data), params);
                    }
                }
            } else {
                if (interrupt) {
                    log.info("We have been interrupted...");
                    pollerClosed = true;
                    break;
                }
            }
        }
        if (!interrupt) {
            log.info("No message received for the past " + timeoutInMs + " ms, re-subscribing for job '" + jobname + "'.");
            unsubscribe(jobname);
        }
    } catch (Exception e) {
        if (!Thread.currentThread().isInterrupted()) {
            // Something other than an interrupt causes this.
            // Unsubscribe, but stay in our loop and try to reconnect..
            log.log(Level.WARNING, "JMS exception raised, going to re-subscribe for job '" + jobname + "'.", e);
            // Try again next time.
            unsubscribe(jobname);
        }
    }
}
Also used : FedMsgSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.FedMsgSubscriberProviderData) FedmsgMessage(com.redhat.jenkins.plugins.ci.messaging.data.FedmsgMessage) HashMap(java.util.HashMap) ZMsg(org.zeromq.ZMsg) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Date(java.util.Date) IOException(java.io.IOException)

Example 3 with FedMsgSubscriberProviderData

use of com.redhat.jenkins.plugins.ci.provider.data.FedMsgSubscriberProviderData in project jms-messaging-plugin by jenkinsci.

the class MessageProviderMigrator method updateCIMessageSubscriberBuilder.

private static boolean updateCIMessageSubscriberBuilder(AbstractProject<?, ?> p, CIMessageSubscriberBuilder builder) {
    if (builder.getProviderData() == null) {
        if (builder.getProviderName() == null) {
            builder.setProviderName(GlobalCIConfiguration.get().getConfigs().get(0).getName());
        }
        JMSMessagingProvider prov = GlobalCIConfiguration.get().getProvider(builder.getProviderName());
        if (prov instanceof ActiveMqMessagingProvider) {
            ActiveMQSubscriberProviderData apd = new ActiveMQSubscriberProviderData(builder.getProviderName());
            apd.setOverrides(builder.getOverrides());
            apd.setSelector(builder.getSelector());
            apd.setChecks(builder.getChecks());
            apd.setVariable(builder.getVariable());
            apd.setTimeout(builder.getTimeout());
            builder.setProviderData(apd);
        } else if (prov instanceof FedMsgMessagingProvider) {
            FedMsgSubscriberProviderData fpd = new FedMsgSubscriberProviderData(builder.getProviderName());
            fpd.setOverrides(builder.getOverrides());
            fpd.setVariable(builder.getVariable());
            fpd.setTimeout(builder.getTimeout());
            builder.setProviderData(fpd);
        } else {
            RabbitMQSubscriberProviderData rpd = new RabbitMQSubscriberProviderData(builder.getProviderName());
            rpd.setOverrides(builder.getOverrides());
            rpd.setVariable(builder.getVariable());
            rpd.setTimeout(builder.getTimeout());
            builder.setProviderData(rpd);
        }
        try {
            p.save();
            return true;
        } catch (IOException e) {
            log.log(Level.WARNING, "Failed to save project", e);
        }
    }
    return false;
}
Also used : FedMsgSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.FedMsgSubscriberProviderData) ActiveMQSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.ActiveMQSubscriberProviderData) RabbitMQSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.RabbitMQSubscriberProviderData) IOException(java.io.IOException)

Example 4 with FedMsgSubscriberProviderData

use of com.redhat.jenkins.plugins.ci.provider.data.FedMsgSubscriberProviderData in project jms-messaging-plugin by jenkinsci.

the class FedMsgMessagingWorker method waitForMessage.

@Override
public String waitForMessage(Run<?, ?> build, TaskListener listener, ProviderData pdata) {
    FedMsgSubscriberProviderData pd = (FedMsgSubscriberProviderData) pdata;
    log.info("Waiting for message.");
    listener.getLogger().println("Waiting for message.");
    for (MsgCheck msgCheck : pd.getChecks()) {
        log.info(" with check: " + msgCheck.toString());
        listener.getLogger().println(" with check: " + msgCheck);
    }
    Integer timeout = (pd.getTimeout() != null ? pd.getTimeout() : FedMsgSubscriberProviderData.DEFAULT_TIMEOUT_IN_MINUTES);
    log.info(" with timeout: " + timeout);
    listener.getLogger().println(" with timeout: " + timeout);
    ZMQ.Context lcontext = ZMQ.context(1);
    ZMQ.Poller lpoller = lcontext.poller(1);
    ZMQ.Socket lsocket = lcontext.socket(ZMQ.SUB);
    String ltopic = getTopic(provider);
    try {
        ltopic = PluginUtils.getSubstitutedValue(getTopic(provider), build.getEnvironment(listener));
    } catch (IOException | InterruptedException e) {
        log.warning(e.getMessage());
    }
    lsocket.subscribe(ltopic.getBytes(StandardCharsets.UTF_8));
    lsocket.setLinger(0);
    lsocket.connect(provider.getHubAddr());
    lpoller.register(lsocket, ZMQ.Poller.POLLIN);
    ObjectMapper mapper = new ObjectMapper();
    long startTime = new Date().getTime();
    int timeoutInMs = timeout * 60 * 1000;
    try {
        while ((new Date().getTime() - startTime) < timeoutInMs) {
            if (lpoller.poll(1000) > 0) {
                if (lpoller.pollin(0)) {
                    ZMsg z = ZMsg.recvMsg(lpoller.getSocket(0));
                    listener.getLogger().println("Received a message");
                    String json = z.getLast().toString();
                    FedmsgMessage data = mapper.readValue(json, FedmsgMessage.class);
                    String body = data.getBodyJson();
                    if (!provider.verify(body, pd.getChecks(), jobname)) {
                        continue;
                    }
                    if (StringUtils.isNotEmpty(pd.getVariable())) {
                        EnvVars vars = new EnvVars();
                        vars.put(pd.getVariable(), body);
                        build.addAction(new CIEnvironmentContributingAction(vars));
                    }
                    return body;
                }
            }
        }
        log.severe("Timed out waiting for message!");
        listener.getLogger().println("Timed out waiting for message!");
    } catch (Exception e) {
        log.log(Level.SEVERE, "Unhandled exception waiting for message.", e);
    } finally {
        try {
            ZMQ.Socket s = lpoller.getSocket(0);
            lpoller.unregister(s);
            s.disconnect(provider.getHubAddr());
            lsocket.unsubscribe(ltopic.getBytes(StandardCharsets.UTF_8));
            lsocket.close();
            lcontext.term();
        } catch (Exception e) {
            listener.getLogger().println("exception in finally");
        }
    }
    return null;
}
Also used : FedMsgSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.FedMsgSubscriberProviderData) IOException(java.io.IOException) MsgCheck(com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck) ZMsg(org.zeromq.ZMsg) Date(java.util.Date) IOException(java.io.IOException) EnvVars(hudson.EnvVars) FedmsgMessage(com.redhat.jenkins.plugins.ci.messaging.data.FedmsgMessage) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ZMQ(org.zeromq.ZMQ) CIEnvironmentContributingAction(com.redhat.jenkins.plugins.ci.CIEnvironmentContributingAction)

Example 5 with FedMsgSubscriberProviderData

use of com.redhat.jenkins.plugins.ci.provider.data.FedMsgSubscriberProviderData in project jms-messaging-plugin by jenkinsci.

the class CIBuildTrigger method readResolve.

@Override
protected Object readResolve() {
    if (providers == null) {
        log.info("Migrating CIBuildTrigger for job '" + getJobName() + "'.");
        providers = new ArrayList<>();
        if (providerData == null) {
            if (providerName == null) {
                log.info("Provider is null for trigger for job '" + getJobName() + "'.");
                JMSMessagingProvider provider = GlobalCIConfiguration.get().getConfigs().get(0);
                if (provider != null) {
                    providerName = provider.getName();
                    providerUpdated = true;
                    saveJob();
                }
            }
            JMSMessagingProvider provider = GlobalCIConfiguration.get().getProvider(providerName);
            if (provider != null) {
                if (provider instanceof ActiveMqMessagingProvider) {
                    log.info("Creating '" + providerName + "' trigger provider data for job '" + getJobName() + "'.");
                    ActiveMQSubscriberProviderData a = new ActiveMQSubscriberProviderData(providerName);
                    a.setSelector(selector);
                    a.setOverrides(overrides);
                    a.setChecks(checks);
                    providers.add(a);
                    providerUpdated = true;
                    saveJob();
                } else if (provider instanceof FedMsgMessagingProvider) {
                    log.info("Creating '" + providerName + "' trigger provider data for job '" + getJobName() + "'.");
                    FedMsgSubscriberProviderData f = new FedMsgSubscriberProviderData(providerName);
                    f.setOverrides(overrides);
                    f.setChecks(checks);
                    providers.add(f);
                    providerUpdated = true;
                    saveJob();
                }
            } else {
                log.warning("Unable to find provider '" + providerName + "', so unable to upgrade job.");
            }
        } else {
            providers.add(providerData);
            providerUpdated = true;
            saveJob();
        }
    }
    return this;
}
Also used : FedMsgSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.FedMsgSubscriberProviderData) ActiveMqMessagingProvider(com.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingProvider) ActiveMQSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.ActiveMQSubscriberProviderData) JMSMessagingProvider(com.redhat.jenkins.plugins.ci.messaging.JMSMessagingProvider) FedMsgMessagingProvider(com.redhat.jenkins.plugins.ci.messaging.FedMsgMessagingProvider)

Aggregations

FedMsgSubscriberProviderData (com.redhat.jenkins.plugins.ci.provider.data.FedMsgSubscriberProviderData)5 IOException (java.io.IOException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 MsgCheck (com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck)2 FedmsgMessage (com.redhat.jenkins.plugins.ci.messaging.data.FedmsgMessage)2 ActiveMQSubscriberProviderData (com.redhat.jenkins.plugins.ci.provider.data.ActiveMQSubscriberProviderData)2 Date (java.util.Date)2 ZMsg (org.zeromq.ZMsg)2 CIBuildTrigger (com.redhat.jenkins.plugins.ci.CIBuildTrigger)1 CIEnvironmentContributingAction (com.redhat.jenkins.plugins.ci.CIEnvironmentContributingAction)1 FedmsgRelayContainer (com.redhat.jenkins.plugins.ci.integration.fixtures.FedmsgRelayContainer)1 ActiveMqMessagingProvider (com.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingProvider)1 FedMsgMessagingProvider (com.redhat.jenkins.plugins.ci.messaging.FedMsgMessagingProvider)1 JMSMessagingProvider (com.redhat.jenkins.plugins.ci.messaging.JMSMessagingProvider)1 MessagingProviderOverrides (com.redhat.jenkins.plugins.ci.messaging.MessagingProviderOverrides)1 RabbitMQSubscriberProviderData (com.redhat.jenkins.plugins.ci.provider.data.RabbitMQSubscriberProviderData)1 EnvVars (hudson.EnvVars)1 FreeStyleProject (hudson.model.FreeStyleProject)1 Shell (hudson.tasks.Shell)1 File (java.io.File)1