rundeck-api-java-client/jython.html
2011-08-03 22:11:23 +02:00

208 lines
12 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- Generated by Apache Maven Doxia at Aug 3, 2011 -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
<style type="text/css" media="all">
@import url("./css/maven-base.css");
@import url("./css/maven-theme.css");
@import url("./css/site.css");
</style>
<link rel="stylesheet" href="./css/print.css" type="text/css" media="print" />
<meta name="Date-Revision-yyyymmdd" content="20110803" />
<meta http-equiv="Content-Language" content="en" />
<!-- Google Analytics -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-23435653-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body class="composite">
<div id="banner">
<div id="bannerLeft">
RunDeck API - Java Client
</div>
<div class="clear">
<hr/>
</div>
</div>
<div id="breadcrumbs">
<div class="xleft">
<a href="http://rundeck.org" class="externalLink" title="RunDeck">RunDeck</a>
&gt;
<a href="./" title="RunDeck API - Java Client">RunDeck API - Java Client</a>
&gt;
</div>
<div class="xright">
<span id="publishDate">Last Published: 2011-08-03</span>
&nbsp;| <span id="projectVersion">Version: 2.1-SNAPSHOT</span>
</div>
<div class="clear">
<hr/>
</div>
</div>
<div id="leftColumn">
<div id="navcolumn">
<h5>RunDeck API - Java Client</h5>
<ul>
<li class="none">
<a href="index.html" title="Introduction">Introduction</a>
</li>
<li class="none">
<a href="status.html" title="Status">Status</a>
</li>
<li class="none">
<a href="faq.html" title="FAQ">FAQ</a>
</li>
<li class="none">
<a href="download.html" title="Download">Download</a>
</li>
<li class="none">
<a href="changes-report.html" title="Changelog">Changelog</a>
</li>
<li class="none">
<a href="apidocs/index.html" title="API Documentation">API Documentation</a>
</li>
</ul>
<h5>Scripting examples</h5>
<ul>
<li class="none">
<a href="scripting.html" title="Introduction">Introduction</a>
</li>
<li class="none">
<a href="groovy.html" title="Groovy">Groovy</a>
</li>
<li class="none">
<a href="jruby.html" title="JRuby">JRuby</a>
</li>
<li class="none">
<strong>Jython</strong>
</li>
</ul>
<h5>Older versions</h5>
<ul>
<li class="none">
<a href="1.x/index.html" title="1.x (RunDeck 1.2)">1.x (RunDeck 1.2)</a>
</li>
</ul>
<h5>Project Documentation</h5>
<ul>
<li class="collapsed">
<a href="project-info.html" title="Project Information">Project Information</a>
</li>
<li class="collapsed">
<a href="project-reports.html" title="Project Reports">Project Reports</a>
</li>
</ul>
<a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
<img class="poweredBy" alt="Built by Maven" src="./images/logos/maven-feather.png" />
</a>
</div>
</div>
<div id="bodyColumn">
<div id="contentBox">
<div class="section"><h2>Using the RunDeck API from Jython scripts<a name="Using_the_RunDeck_API_from_Jython_scripts"></a></h2></div><p>Here are some examples of what you can do with this lib and a few lines of <a class="externalLink" href="http://www.jython.org/">Python</a> (for the pythonist that don't fear running on the JVM !)</p><p>You will have to <a href="./download.html">download</a> the lib (the .jar file) to use it...</p><div class="section"><h3>Basic usage<a name="Basic_usage"></a></h3></div><p>Save the following script in a file named &quot;<tt>rundeck.py</tt>&quot;, and execute it with &quot;<tt>jython -J-cp /path/to/rundeck-api-java-client-VERSION-jar-with-dependencies.jar rundeck.py</tt>&quot;.</p><div class="source"><pre>from org.rundeck.api import RundeckClient
rundeck = RundeckClient(&quot;http://localhost:4440&quot;, &quot;admin&quot;, &quot;admin&quot;)
print(&quot;RunDeck uptime : %s&quot; % rundeck.systemInfo.uptime)
print(&quot;All RunDeck projects : %s&quot; % rundeck.projects)
print(&quot;All RunDeck nodes : %s&quot; % rundeck.nodes)
print(&quot;All RunDeck jobs : %s&quot; % rundeck.jobs)
print(&quot;All RunDeck running executions : %s&quot; % rundeck.runningExecutions)
</pre></div><div class="section"><h3>Authentication<a name="Authentication"></a></h3></div><p>Starting with RunDeck API 2, there are 2 ways to authenticate :</p><ul><li>the <b>login-based authentication</b> : with your login and password</li><li>the <b>token-based authentication</b> : with a unique token that you can generate from the RunDeck webUI</li></ul><div class="source"><pre>// using login-based authentication (admin/admin is the default login/password for a new RunDeck instance) :
rundeck = RundeckClient(&quot;http://localhost:4440&quot;, &quot;admin&quot;, &quot;admin&quot;);
// using token-based authentication :
rundeck = RundeckClient(&quot;http://localhost:4440&quot;, &quot;PDDNKo5VE29kpk4prOUDr2rsKdRkEvsD&quot;);
</pre></div><div class="section"><h3>Running a job<a name="Running_a_job"></a></h3></div><div class="source"><pre>from org.rundeck.api import RundeckClient
from org.rundeck.api import OptionsBuilder
from org.rundeck.api import NodeFiltersBuilder
rundeck = RundeckClient(&quot;http://localhost:4440&quot;, &quot;admin&quot;, &quot;admin&quot;)
// find a job from its name, group and project
job = rundeck.findJob(&quot;my-project&quot;, &quot;main-group/sub-group&quot;, &quot;job-name&quot;)
print(&quot;Found job with ID : %s&quot; % job.id)
// trigger a simple job with no options
execution = rundeck.triggerJob(job.id)
print(&quot;Execution started, you can follow it at the URL : %s&quot; % execution.url)
// or with options...
execution = rundeck.triggerJob(&quot;job-id&quot;, OptionsBuilder().addOption(&quot;option1&quot;, &quot;value one&quot;).addOption(&quot;option2&quot;, &quot;value two&quot;).toProperties())
// you can also override the nodes on which the job should execute
execution = rundeck.triggerJob(&quot;job-id&quot;, OptionsBuilder().addOption(&quot;opt&quot;, &quot;value&quot;).toProperties(), NodeFiltersBuilder().tags(&quot;prod+appserv&quot;).toProperties())
// last one : you can run a job and wait until its execution is finished :
execution = rundeck.runJob(&quot;job-id&quot;)
print(&quot;Execution finished ! Status : %s, duration : %s&quot; % (execution.status, execution.duration))
</pre></div><div class="section"><h3>Running an ad-hoc command<a name="Running_an_ad-hoc_command"></a></h3></div><div class="source"><pre>from org.rundeck.api import RundeckClient
from org.rundeck.api import NodeFiltersBuilder
rundeck = RundeckClient(&quot;http://localhost:4440&quot;, &quot;admin&quot;, &quot;admin&quot;)
// trigger the execution of the &quot;uptime&quot; command on the RunDeck server
execution = rundeck.triggerAdhocCommand(&quot;my-project&quot;, &quot;uptime&quot;)
// run the &quot;uptime&quot; command on all unix nodes
execution = rundeck.runAdhocCommand(&quot;my-project&quot;, &quot;uptime&quot;, NodeFiltersBuilder().osFamily(&quot;unix&quot;).toProperties())
</pre></div><div class="section"><h3>Running an ad-hoc script<a name="Running_an_ad-hoc_script"></a></h3></div><div class="source"><pre>from org.rundeck.api import RundeckClient
from org.rundeck.api import OptionsBuilder
from org.rundeck.api import NodeFiltersBuilder
rundeck = RundeckClient(&quot;http://localhost:4440&quot;, &quot;admin&quot;, &quot;admin&quot;)
// trigger the execution of a custom bash script on the RunDeck server
execution = rundeck.triggerAdhocScript(&quot;my-project&quot;, &quot;/tmp/my-script.sh&quot;)
// run a bash script (with options) on all unix nodes
execution = rundeck.runAdhocCommand(&quot;my-project&quot;, &quot;/tmp/my-script-with-options.sh&quot;, OptionsBuilder().addOption(&quot;option1&quot;, &quot;value one&quot;).toProperties(), NodeFiltersBuilder().osFamily(&quot;unix&quot;).toProperties())
</pre></div><div class="section"><h3>Exporting jobs<a name="Exporting_jobs"></a></h3></div><div class="source"><pre>from org.rundeck.api import RundeckClient
rundeck = RundeckClient(&quot;http://localhost:4440&quot;, &quot;admin&quot;, &quot;admin&quot;)
rundeck.exportJobsToFile(&quot;/tmp/jobs.xml&quot;, &quot;xml&quot;, &quot;my-project&quot;)
rundeck.exportJobToFile(&quot;/tmp/job.yaml&quot;, &quot;yaml&quot;, &quot;job-id&quot;)
</pre></div><div class="section"><h3>Importing jobs<a name="Importing_jobs"></a></h3></div><div class="source"><pre>from org.rundeck.api import RundeckClient
rundeck = RundeckClient(&quot;http://localhost:4440&quot;, &quot;admin&quot;, &quot;admin&quot;)
result = rundeck.importJobs(&quot;/tmp/jobs.xml&quot;, &quot;xml&quot;)
print(&quot;%s jobs successfully imported, %s jobs skipped, and %s jobs failed&quot; % (result.succeededJobs.size, result.skippedJobs.size, result.failedJobs.size))
</pre></div><div class="section"><h3>And more...<a name="And_more..."></a></h3></div><p>See the API documentation of the <a href="./apidocs/reference/org/rundeck/api/RundeckClient.html">RundeckClient</a> class for more interactions with your RunDeck instance...</p>
</div>
</div>
<div class="clear">
<hr/>
</div>
<div id="footer">
<div class="xright">
Copyright &#169; 2011.
All Rights Reserved.
</div>
<div class="clear">
<hr/>
</div>
</div>
</body>
</html>