File: 07-implementing-a-task.md

package info (click to toggle)
ruby-puppet-resource-api 1.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,232 kB
  • sloc: ruby: 9,573; sh: 4; makefile: 2
file content (181 lines) | stat: -rw-r--r-- 4,504 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Implementing a Task

> TODO: this is NOT fine, yet

* add bolt gem
```
Gemfile:
  optional:
    ':development':
      - gem: 'puppet-resource_api'
      - gem: 'faraday'
      # add this
      - gem: 'bolt'
```


```
david@davids:~/tmp/hue_workshop$ pdk update --force
pdk (INFO): Updating david-hue_workshop using the default template, from 1.10.0 to 1.10.0

----------Files to be modified----------
Gemfile

----------------------------------------

You can find a report of differences in update_report.txt.


------------Update completed------------

1 files modified.

david@davids:~/tmp/hue_workshop$ pdk bundle install
pdk (INFO): Using Ruby 2.5.3
pdk (INFO): Using Puppet 6.4.2
[...]
Bundle complete! 11 Gemfile dependencies, 122 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

david@davids:~/tmp/hue_workshop$
```

* add ruby_task_helper module
<!--
```# .fixtures.yml
---
fixtures:
  forge_modules:
     ruby_task_helper: "puppetlabs/ruby_task_helper"
```

```
david@davids:~/tmp/hue_workshop$ pdk bundle exec rake spec_prep
pdk (INFO): Using Ruby 2.5.3
pdk (INFO): Using Puppet 6.4.2
Notice: Preparing to install into /home/david/tmp/hue_workshop/spec/fixtures/modules ...
Notice: Downloading from https://forgeapi.puppet.com ...
Notice: Installing -- do not interrupt ...
/home/david/tmp/hue_workshop/spec/fixtures/modules
└── puppetlabs-ruby_task_helper (v0.3.0)
I, [2019-06-04T13:12:04.615368 #32070]  INFO -- : Creating symlink from spec/fixtures/modules/hue_workshop to /home/david/tmp/hue_workshop
david@davids:~/tmp/hue_workshop$
``` -->

Using the development version:

```
fixtures:
  # forge_modules:
  #    ruby_task_helper: "puppetlabs/ruby_task_helper"
  repositories:
    ruby_task_helper:
      repo: "git://github.com/da-ar/puppetlabs-ruby_task_helper"
      ref: "38745f8e7c2521c50bbf1b8e03318006cdac7a02"
```

```
david@davids:~/tmp/hue_workshop$ pdk bundle exec rake spec_prep
pdk (INFO): Using Ruby 2.5.3
pdk (INFO): Using Puppet 6.4.2
HEAD is now at 38745f8 (FM-7955) Update to use Transport helper code
Cloning into 'spec/fixtures/modules/ruby_task_helper'...
I, [2019-06-04T13:43:58.577944 #9390]  INFO -- : Creating symlink from spec/fixtures/modules/hue_workshop to /home/david/tmp/hue_workshop
david@davids:~/tmp/hue_workshop$
```

* `pdk new task` based on https://github.com/puppetlabs/puppetlabs-panos/blob/main/tasks/apikey.rb

```
david@davids:~/tmp/hue_workshop$ pdk new task alarm
pdk (INFO): Creating '/home/david/tmp/hue_workshop/tasks/alarm.sh' from template.
pdk (INFO): Creating '/home/david/tmp/hue_workshop/tasks/alarm.json' from template.
david@davids:~/tmp/hue_workshop$ mv /home/david/tmp/hue_workshop/tasks/alarm.sh /home/david/tmp/hue_workshop/tasks/alarm.rb
david@davids:~/tmp/hue_workshop$
```

* `tasks/alarm.json`
```json
{
  "puppet_task_version": 1,
  "supports_noop": false,
  "remote": true,
  "description": "A short description of this task",
  "parameters": {
    "name": {
      "type": "String",
      "description": "The lamp to alarm"
    }
  },
  "files": [
    "ruby_task_helper/files/task_helper.rb",
    "hue_workshop/lib/puppet/transport/hue.rb",
    "hue_workshop/lib/puppet/transport/schema/hue.rb"
  ]
}
```

* `tasks/alarm.rb`

```ruby
#!/opt/puppetlabs/puppet/bin/ruby

require 'puppet'
require_relative "../../ruby_task_helper/files/task_helper.rb"

class AlarmTask < TaskHelper
  def task(params = {}, remote = nil)
    name = params[:name]
    5.times do |i|
      remote.transport.hue_put("lights/#{name}/state",
        name: name,
        on: false,
      )
      sleep 1.0
      remote.transport.hue_put("lights/#{name}/state",
        name: name,
        on: true,
        hue: 10000*i,
        sat: 255
      )
      sleep 1.0
    end
    {}
  end
end

if __FILE__ == $0
  AlarmTask.run
end
```

* execute `pdk bundle exec bolt ...`

```yaml
# inventory.yaml
---
nodes:
  - name: "192.168.43.195"
    alias: hub1
    config:
      transport: remote
      remote:
        remote-transport: hue
        key: "onmdTvd198bMrC6QYyVE9iasfYSeyAbAj3XyQzfL"
```

```
david@davids:~/tmp/hue_workshop$ pdk bundle exec bolt task run hue_workshop::alarm --modulepath spec/fixtures/modules/ --target hub1 --inventoryfile inventory.yaml
pdk (INFO): Using Ruby 2.5.3
pdk (INFO): Using Puppet 6.4.2
Started on 192.168.43.195...
Finished on 192.168.43.195:
  {
  }
Successful on 1 node: 192.168.43.195
Ran on 1 node in 11.32 seconds

david@davids:~/tmp/hue_workshop$
```

* profit!