summaryrefslogtreecommitdiff
path: root/code/environments/production/modules/apt/tasks/init.rb
blob: d256468b64a739fdaa6446f49b1d5069bc46891d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/opt/puppetlabs/puppet/bin/ruby
require 'json'
require 'open3'
require 'puppet'

def apt_get(action)
  cmd = ['apt-get', action]
  cmd << ' -y' if action == 'upgrade'
  stdout, stderr, status = Open3.capture3(*cmd)
  raise Puppet::Error, stderr if status != 0
  { status: stdout.strip }
end

params = JSON.parse(STDIN.read)
action = params['action']

begin
  result = apt_get(action)
  puts result.to_json
  exit 0
rescue Puppet::Error => e
  puts({ status: 'failure', error: e.message }.to_json)
  exit 1
end