Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
540 views
in Technique[技术] by (71.8m points)

jar - Run cmd commands with open source puppet

Can someone please help me with the open source puppet?
I want to provide a jar file to a windows client and execute the .jar file with the command line.
The .jar file is actually an update for an application which is running as a service.

I am poorly familiar with the puppet language but would guess something like this to execute the jar file:

exec { 'jar_execution':
  command => 'cmd.exe /c java -jar foo.jar',
} 

Should this be part of the manifest which could look like this?

service { 'fooservice':
  name      => foo_service,
  ensure    => running,
  enable    => true,
}

file { 'foo.jar':
  path => 'C:/foo/temp/foo.jar',
  ensure => file,
  source => "puppet:///modules/foo/foo.jar",
} 

exec { 'jar_execution':
  command => 'cmd.exe /c java -jar C:/foor/temp/foo.jar',
} 

And how does the agent actually run this command?

question from:https://stackoverflow.com/questions/65947437/run-cmd-commands-with-open-source-puppet

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are few architectural considerations:

  • Use archive resource (from archive module) instead of: file with your .jar in your module's .git history.
  • Have all artifacts (e.g. binary files) served by another service like Apache Archiva, Nexus or Artifactory or even Puppet Server itself. If the .jar is not developed by your company, you may want to use the authoritative source with maybe some internal caching.
  • Name your resources in a way that are global to your whole infrastructure, otherwise you may have events that notify themselves and produce undesired outcome.
  • Order of resources in puppet doesn't matter. You want to order the resources the way it makes sense to you and use the before, after, require, notify to ensure dependency.

I would recommend having binaries files outside of a module, as binaries are not supposed to be versioned. You would probably have another service that can serve files, or even your puppet infrastructure can provide those packages, in the similar way it provides the puppet-agent itself, regardless if you use Puppet OSS or Puppet Enterprise.

archive { 'C:/foo/temp/foo.jar':
  source  => https://location/to/your/.jar,
}

exec { 'C:/foo/temp/foo.jar':  # notice in resource name recommended to use / not 
  command     => 'cmd.exe /c java -jar C:/foor/temp/foo.jar',
  refreshonly => true # this one executes exec only if the file was re-downloaded (maybe it's signature has changed), or the file was removed from disk and was re-downloaded.
  onlyif      => 'test -f C:fooempfoo.jar' # or some command that guards you to not run this .jar every time you run puppet
  subscribe   => Archive['C:/foo/temp/foo.jar'],
  notify      => Service['foo_service'] # most probably you need to restart the service after you ran the .jar, otherwise you wouldn't have added in your question.
}

service { 'foo_service':
  ensure    => running,
  enable    => true,
}

I notice that in your example you don't need to remove your .jar after being executed. In case you need that, another exec command can remove the file downloaded in the same


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...