huginn_write_file_agent 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2b434746e20b48093166aadb1257e188ec494e1f3d2dc90a69b10bb8ae6004fc
4
+ data.tar.gz: fd64ad8c653293c5a1a3e8f4b83850e695c8aa4b61db387f327b4500d903b0de
5
+ SHA512:
6
+ metadata.gz: 827b688767597431db0952926ef62ca257d2d6d84fca0e8cb07bb0147d9bad2ebcab0baa7c4c9227c4b30db913d5380663575323d94b0ecb74b3cfc992027c6a
7
+ data.tar.gz: e5b84679eea50f45525e705e3d195e35ceb55e439b236170ce631ba630f821f0b6b26f599e870a864ca8778ba5402243223b984a93bda93def53a53033e708dc
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2025 Nicolas Germain
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,176 @@
1
+ module Agents
2
+ class WriteFileAgent < Agent
3
+ include FormConfigurable
4
+ can_dry_run!
5
+ no_bulk_receive!
6
+ default_schedule 'every_1h'
7
+
8
+ description do
9
+ <<-MD
10
+ The FileWriterAgent writes files to the filesystem with specified parameters.
11
+
12
+ This agent can be used to generate files at specific locations with defined content,
13
+ permissions, and ownership.
14
+
15
+ `path` - The full path where the file should be written
16
+ `content` - The content of the file to write
17
+ `mode` - The file permissions (e.g., "0644", "0755")
18
+ `owner` - The file owner (in "user:group" format)
19
+ `expected_receive_period_in_days` - How many days can pass without receiving an event before this agent is considered inactive
20
+ `debug` - Debug mode (true/false), displays additional information in logs
21
+ MD
22
+ end
23
+
24
+ event_description <<-MD
25
+ Events look like this:
26
+
27
+ {
28
+ "status": "success",
29
+ "path": "/tmp/huginn_output.txt",
30
+ "size": 15,
31
+ "mode": "0644",
32
+ "owner": "huginn:huginn",
33
+ "timestamp": 1745522629
34
+ }
35
+
36
+ MD
37
+
38
+ def default_options
39
+ {
40
+ 'path' => '/tmp/huginn_output.txt',
41
+ 'content' => 'Default content',
42
+ 'mode' => '0644',
43
+ 'owner' => 'huginn:huginn',
44
+ 'expected_receive_period_in_days' => '10',
45
+ 'debug' => 'false'
46
+ }
47
+ end
48
+
49
+ form_configurable :path, type: :string
50
+ form_configurable :content, type: :text
51
+ form_configurable :mode, type: :string
52
+ form_configurable :owner, type: :string
53
+ form_configurable :expected_receive_period_in_days, type: :string
54
+ form_configurable :debug, type: :boolean
55
+ def validate_options
56
+ # errors.add(:base, "type has invalid value: should be 'generate'") if interpolated['type'].present? && !%w(generate).include?(interpolated['type'])
57
+
58
+ unless options['path'].present?
59
+ errors.add(:base, "path is a required field")
60
+ end
61
+
62
+ unless options['mode'].present?
63
+ errors.add(:base, "mode is a required field")
64
+ end
65
+
66
+ unless options['owner'].present?
67
+ errors.add(:base, "owner is a required field")
68
+ end
69
+
70
+ if options.has_key?('debug') && boolify(options['debug']).nil?
71
+ errors.add(:base, "if provided, debug must be true or false")
72
+ end
73
+
74
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
75
+ errors.add(:base, "Please provide 'expected_receive_period_in_days' to indicate how many days can pass before this Agent is considered to be not working")
76
+ end
77
+ end
78
+
79
+ def working?
80
+ event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
81
+ end
82
+
83
+ def receive(incoming_events)
84
+ incoming_events.each do |event|
85
+ interpolate_with(event) do
86
+ log event
87
+ write_file
88
+ end
89
+ end
90
+ end
91
+
92
+ def check
93
+ write_file
94
+ end
95
+
96
+ private
97
+
98
+
99
+ def log_curl_output(code,body)
100
+
101
+ log "request status : #{code}"
102
+
103
+ if interpolated['debug'] == 'true'
104
+ log "request status : #{code}"
105
+ log "body"
106
+ log body
107
+ end
108
+
109
+ end
110
+
111
+ # def push_model(model)
112
+ #
113
+ # log_curl_output(response.code,response.body)
114
+ #
115
+ # end
116
+
117
+ def write_file
118
+ begin
119
+ log "Attempting to write file at #{interpolated['path']}" if boolify(interpolated['debug'])
120
+
121
+ # Check if parent directory exists, create if not
122
+ dir = File.dirname(interpolated['path'])
123
+ unless File.directory?(dir)
124
+ FileUtils.mkdir_p(dir)
125
+ # log "Created parent directory: #{dir}" if boolify(interpolated['debug'])
126
+ end
127
+
128
+ # Write content to file
129
+ File.write(interpolated['path'], interpolated['content'])
130
+ log "File successfully written: #{interpolated['path']}" if boolify(interpolated['debug'])
131
+
132
+ # Convert mode to integer (from octal string)
133
+ mode_int = interpolated['mode'].to_i(8)
134
+ File.chmod(mode_int, interpolated['path'])
135
+ # log "Permissions applied: #{interpolated['mode']}" if boolify(interpolated['debug'])
136
+
137
+ # Change ownership
138
+ user, group = interpolated['owner'].split(':')
139
+ user_id = Etc.getpwnam(user).uid
140
+ group_id = Etc.getgrnam(group).gid
141
+ File.chown(user_id, group_id, interpolated['path'])
142
+ # log "Ownership changed: #{interpolated['owner']}" if boolify(interpolated['debug'])
143
+
144
+ # Create output event
145
+ create_event(payload: {
146
+ 'status' => 'success',
147
+ 'path' => interpolated['path'],
148
+ 'size' => File.size(interpolated['path']),
149
+ 'mode' => interpolated['mode'],
150
+ 'owner' => interpolated['owner'],
151
+ 'timestamp' => Time.now.to_i
152
+ })
153
+
154
+ rescue => e
155
+ error_message = "Error writing file: #{e.message}"
156
+ # log error_message, :error
157
+
158
+ create_event(payload: {
159
+ 'status' => 'error',
160
+ 'path' => interpolated['path'],
161
+ 'error' => error_message,
162
+ 'timestamp' => Time.now.to_i
163
+ })
164
+ end
165
+ end
166
+
167
+ def is_positive_integer?(string)
168
+ begin
169
+ value = Integer(string)
170
+ return value > 0
171
+ rescue
172
+ return false
173
+ end
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_write_file_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_write_file_agent/write_file_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::WriteFileAgent do
5
+ before(:each) do
6
+ @valid_options = Agents::WriteFileAgent.new.default_options
7
+ @checker = Agents::WriteFileAgent.new(:name => "WriteFileAgent", :options => @valid_options)
8
+ @checker.user = users(:bob)
9
+ @checker.save!
10
+ end
11
+
12
+ pending "add specs here"
13
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huginn_write_file_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Germain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-04-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 12.3.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 12.3.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: huginn_agent
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Write a longer description or delete this line.
56
+ email:
57
+ - ngermain@hihouhou.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE.txt
63
+ - lib/huginn_write_file_agent.rb
64
+ - lib/huginn_write_file_agent/write_file_agent.rb
65
+ - spec/write_file_agent_spec.rb
66
+ homepage: https://github.com/hihouhou/huginn_write_file_agent
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.3.3
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Write a short summary, because Rubygems requires one.
89
+ test_files:
90
+ - spec/write_file_agent_spec.rb