Archi Forum

Archi Plug-ins => jArchi => Topic started by: vegardv on September 20, 2019, 14:42:59 PM

Title: Running jarchi in batch mode
Post by: vegardv on September 20, 2019, 14:42:59 PM
I have written some scripts which creates/updates some archimate models from our CMDB. I would like to run these scripts periodically in batch mode. Preferably without any GUI so that in can be run like a cron job.
Is this possible?

Title: Re: Running jarchi in batch mode
Post by: Phil Beauvoir on September 20, 2019, 14:44:10 PM
Yes, by using the ACLI. See https://github.com/archimatetool/archi/wiki/Archi-Command-Line-Interface
Title: Re: Running jarchi in batch mode
Post by: vegardv on September 23, 2019, 08:45:12 AM
Fantastic!
Next question: Can I supply arguments to the jscript on the command line?
Title: Re: Running jarchi in batch mode
Post by: Phil Beauvoir on September 23, 2019, 08:45:49 AM
> Can I supply arguments to the jscript on the command line?

No. It just runs the script.
Title: Re: Running jarchi in batch mode
Post by: Jean-Baptiste Sarrodie on September 23, 2019, 18:29:02 PM
Hi,

Quote from: Phil Beauvoir on September 23, 2019, 08:45:49 AM
> Can I supply arguments to the jscript on the command line?

No. It just runs the script.

A workaround is to have your jArchi script load another script which only sets up some parameters. This script can easily be created by another batch before.

Regards,

JB
Title: Re: Running jarchi in batch mode
Post by: Phil Beauvoir on September 23, 2019, 18:33:35 PM
Quote from: Jean-Baptiste Sarrodie on September 23, 2019, 18:29:02 PM
Hi,

Quote from: Phil Beauvoir on September 23, 2019, 08:45:49 AM
> Can I supply arguments to the jscript on the command line?

No. It just runs the script.

A workaround is to have your jArchi script load another script which only sets up some parameters. This script can easily be created by another batch before.

Regards,

JB

There you see. I knew there were some smart people around here.  ;)
Title: Re: Running jarchi in batch mode
Post by: Jean-Baptiste Sarrodie on September 23, 2019, 19:33:24 PM
Hi,

Quote from: Phil Beauvoir on September 23, 2019, 18:33:35 PM
There you see. I knew there were some smart people around here.  ;)

8)

Even smarter (but not tested): as one can use Java classes inside jArchi, it should be possible (from within jArchi) to access the command line used when starting Archi, so in theory one could read CLI parameters and act accordingly.

Regards,

JB
Title: Re: Running jarchi in batch mode
Post by: vegardv on February 13, 2020, 14:18:08 PM
Quote from: Phil Beauvoir on September 20, 2019, 14:44:10 PM
Yes, by using the ACLI. See https://github.com/archimatetool/archi/wiki/Archi-Command-Line-Interface

So, I am still playing around with jArchi scripting, attempting
to automate some model generation in batch mode. We also use the
collaboration plugin for versioning. Via the CLI I can clone and
load a model like this:

xvfb-run archi -application com.archimatetool.commandline.app -consoleLog -nosplash \
  --modelrepository.cloneModel my-git-repo-url \
  --modelrepository.identityFile my-ssh-id-file \
  --modelrepository.passFile secret-passphrase-file \
  --modelrepository.loadModel "My model"

 
I have created some jArchi scripts that queries some REST-like
APIs provided by our CMDB-system, and automatically updates the
loaded model.

Combining a script 'tmp.ajs' that will update the model, I can write a
command that will clone the model from a repo, load it, update it via
a jArchi script, and finally save the resulting updated
model. Example:

  xvfb-run archi -application com.archimatetool.commandline.app -consoleLog -nosplash \
  --modelrepository.cloneModel my-git-repo-url \
  --modelrepository.identityFile my-ssh-id-file \
  --modelrepository.passFile secret-passphrase-file \
  --modelrepository.loadModel "My model" \
  --script tmp.ajs \
  --saveModel "updated-model.archimate"

This works fine. I end up with a file which contains the updated
model, but this model is not "connected to" the repo.

When using the collaboration plugin, it seems like the model is
temporarily saved into '.git/temp.archimate'. I wonder if I can simply
save the model directly into '.git/temp.archimate' using a command like
above. Then I can use the GUI to load this file and manually perform
the Commit and Publish operations.

I guess what I would like to see is some CLI options like:
--modelrepository.commit
--modelrepository.push

This would allow me to keep the model automatically updated in a batch
script, but still allowing manual updating via the same repo. Is this
feasible?

I realize that commiting and pushing may result in conflicts which is
not easily handled via the CLI.

Title: Re: Running jarchi in batch mode
Post by: Jean-Baptiste Sarrodie on February 13, 2020, 14:38:23 PM
Hi,

Quote from: vegardv on February 13, 2020, 14:18:08 PM
When using the collaboration plugin, it seems like the model is
temporarily saved into '.git/temp.archimate'. I wonder if I can simply
save the model directly into '.git/temp.archimate' using a command like
above. Then I can use the GUI to load this file and manually perform
the Commit and Publish operations.

Short answer: this will work ;-)
Long answer: when commiting through the GUI, Archi opens the temp.archimate file (if it wasn't already opened), delete everything in the (git) working tree, export the content as a set of XML files, then does the commit itself. So if you change this temp.archimate file (whatever way you change it) it will works.

Quote from: vegardv on February 13, 2020, 14:18:08 PM
I guess what I would like to see is some CLI options like:
--modelrepository.commit
--modelrepository.push

That would be good, can you please open a GitHub issue for that on the plugin repo: https://github.com/archimatetool/archi-modelrepository-plugin/issues

Regards,

JB
Title: Re: Running jarchi in batch mode
Post by: vegardv on February 14, 2020, 06:44:23 AM
Quote from: Jean-Baptiste Sarrodie on February 13, 2020, 14:38:23 PM

Quote from: vegardv on February 13, 2020, 14:18:08 PM
I guess what I would like to see is some CLI options like:
--modelrepository.commit
--modelrepository.push

That would be good, can you please open a GitHub issue for that on the plugin repo: https://github.com/archimatetool/archi-modelrepository-plugin/issues


Issue created: https://github.com/archimatetool/archi-modelrepository-plugin/issues/121
Title: Re: Running jarchi in batch mode
Post by: vegardv on February 19, 2020, 09:55:48 AM
Quote from: vegardv on February 14, 2020, 06:44:23 AM


Issue created: https://github.com/archimatetool/archi-modelrepository-plugin/issues/121

I appreciate that this change requires extensive rewriting. I have commented on the issue, suggesting that maybe it is easier to implement a CLI-option to save the model as a set of XML files, ready to be Git added, commited, and pushed. The git operations can be performed outside of Archi.
Title: Re: Running jarchi in batch mode
Post by: Jean-Baptiste Sarrodie on February 19, 2020, 20:33:45 PM
Hi,


Quote from: vegardv on February 19, 2020, 09:55:48 AM
The git operations can be performed outside of Archi.

No, this is not possible: this would most certainly lead to a model which is screwed and won't be usable anymore. This has been explained several time on the forum and GitHub.

Regards,

JB
Title: Re: Running jarchi in batch mode
Post by: AlexL on February 27, 2020, 09:40:39 AM
Following the topic, is there any convienent way to pass arguments to jArchi script when calling it in a batch or GUI mode ?
Would be good enough to have a way like using argparase for js. For example https://www.npmjs.com/package/argparse
Title: Re: Running jarchi in batch mode
Post by: Phil Beauvoir on February 27, 2020, 10:55:15 AM
What arguments are we talking about? Do we mean the ones passed via the ACLI as in the following -myArg "HelloWord"?

Archi -application com.archimatetool.commandline.app -consoleLog -nosplash --script.runScript "MyScript.ajs" -myArg "HelloWord"

If so, these can be accessed from jArchi with the following:

var args = Java.type("org.eclipse.core.runtime.Platform").getApplicationArgs()

This returns an object with the args.
Title: Re: Running jarchi in batch mode
Post by: AlexL on February 27, 2020, 11:05:05 AM
Thanks, that's good enough. But I mean a call more like this:
Archi -application com.archimatetool.commandline.app -consoleLog -nosplash --script.runScript "MyScript.ajs --flag-a -A ValOfA --an-argument argVal positional_argument"

Will examplegiven  work for it?

And one more thing to mention. If script is called from CLI by another script (shell for example) what will be it's working directory ? Same as parent batch?
Title: Re: Running jarchi in batch mode
Post by: Phil Beauvoir on February 27, 2020, 11:50:47 AM
This returns the CLI arguments:


var args = Java.type("org.eclipse.core.runtime.Platform").getApplicationArgs();

for(i = 0; i < args.length; i++) {
    console.log(args[i]);
}
Title: Re: Running jarchi in batch mode
Post by: AlexL on February 27, 2020, 12:38:07 PM
Thanks!
Title: Re: Running jarchi in batch mode
Post by: Jean-Baptiste Sarrodie on February 27, 2020, 13:00:43 PM
Hi,

Quote from: Phil Beauvoir on February 27, 2020, 11:50:47 AM
This returns the CLI arguments:


var args = Java.type("org.eclipse.core.runtime.Platform").getApplicationArgs();

for(i = 0; i < args.length; i++) {
    console.log(args[i]);
}


@Phil: but this will no more work with next version because of how this issue (https://github.com/archimatetool/archi/issues/573) has been solved, no?

If so, then maybe we should add an explicit option like "--script.arg" which would be kept so that your sample code can be used.

JB
Title: Re: Running jarchi in batch mode
Post by: Phil Beauvoir on February 27, 2020, 13:04:18 PM
@JB No, it won't affect it. Non-ACLI arguments are filtered out when running the Command Line Providers. The raw string array from Eclipse's Platform.getApplicationArgs() will return all user args regardless.
Title: Re: Running jarchi in batch mode
Post by: Jean-Baptiste Sarrodie on February 27, 2020, 13:21:40 PM
Hi,

Quote from: Phil Beauvoir on February 27, 2020, 13:04:18 PM
@JB No, it won't affect it. Non-ACLI arguments are filtered out when running the Command Line Providers. The raw string array from Eclipse's Platform.getApplicationArgs() will return all user args regardless.

Yes, you're right. So basically we could even provide a kind of API for this with a full JS implementation through init.js

JB
Title: Re: Running jarchi in batch mode
Post by: Phil Beauvoir on February 27, 2020, 13:23:12 PM
Quote from: Jean-Baptiste Sarrodie on February 27, 2020, 13:21:40 PM
Hi,

Quote from: Phil Beauvoir on February 27, 2020, 13:04:18 PM
@JB No, it won't affect it. Non-ACLI arguments are filtered out when running the Command Line Providers. The raw string array from Eclipse's Platform.getApplicationArgs() will return all user args regardless.

Yes, you're right. So basically we could even provide a kind of API for this with a full JS implementation through init.js

JB

You mean like the one I've already written in init.js?


function getArgs() {
return Java.type("org.eclipse.core.runtime.Platform").getApplicationArgs();
}


8)
Title: Re: Running jarchi in batch mode
Post by: Jean-Baptiste Sarrodie on February 27, 2020, 13:26:32 PM
Yes, this one ;-)
Title: Re: Running jarchi in batch mode
Post by: AlexL on February 27, 2020, 14:10:05 PM
Quote from: Phil Beauvoir on February 27, 2020, 13:23:12 PM
Quote from: Jean-Baptiste Sarrodie on February 27, 2020, 13:21:40 PM
Hi,

Quote from: Phil Beauvoir on February 27, 2020, 13:04:18 PM
@JB No, it won't affect it. Non-ACLI arguments are filtered out when running the Command Line Providers. The raw string array from Eclipse's Platform.getApplicationArgs() will return all user args regardless.

Yes, you're right. So basically we could even provide a kind of API for this with a full JS implementation through init.js

JB

You mean like the one I've already written in init.js?


function getArgs() {
return Java.type("org.eclipse.core.runtime.Platform").getApplicationArgs();
}


8)

So when it's planned to be released? )
Title: Re: Running jarchi in batch mode
Post by: Phil Beauvoir on February 27, 2020, 14:13:41 PM
Quote from: AlexL on February 27, 2020, 14:10:05 PM

So when it's planned to be released? )

The source code is available now - https://github.com/archimatetool/archi-scripting-plugin

You can also edit your copy of init.js to add the lines, although it amounts to the same code that I posted above.
Title: Re: Running jarchi in batch mode
Post by: vegardv on March 12, 2020, 09:09:37 AM
Quote from: Phil Beauvoir on February 27, 2020, 10:55:15 AM
What arguments are we talking about? Do we mean the ones passed via the ACLI as in the following -myArg "HelloWord"?

Archi -application com.archimatetool.commandline.app -consoleLog -nosplash --script.runScript "MyScript.ajs" -myArg "HelloWord"

If so, these can be accessed from jArchi with the following:

var args = Java.type("org.eclipse.core.runtime.Platform").getApplicationArgs()

This returns an object with the args.

This is great!

Using archi 4.6.0 I get an error when running the example exactly as given:
org.apache.commons.cli.UnrecognizedOptionException: Unrecognized option: -myArg

but creating a script like this:
var args = Java.type("org.eclipse.core.runtime.Platform").getApplicationArgs()
for (var i=0; i<args.length; i++){
    console.log ("script was called with arg: "+args[i]);
}

.. and invoking it like this:
archi -application com.archimatetool.commandline.app -consoleLog -nosplash --script.runScript args.js -- -myArg foo

results in:
script was called with arg: --script.runScript
script was called with arg: args.js
script was called with arg: --
script was called with arg: -myarg
script was called with arg: foo


So it works in principle, but is this the appropriate way to supply CLI arguments?
Title: Re: Running jarchi in batch mode
Post by: Phil Beauvoir on March 12, 2020, 09:33:53 AM
> Using archi 4.6.0 I get an error when running the example exactly as given:


Fixed in Archi 4.7 Early Access version.

jArchi 0.7.2 has the new JS function getArgs() which is a wrapper around the Java call.

var args = getArgs();

for(i = 0; i < args.length; i++) {
    console.log(args);
}