-
Notifications
You must be signed in to change notification settings - Fork 118
/
flatpak_helpers.rb
98 lines (83 loc) · 2.89 KB
/
flatpak_helpers.rb
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
# Copyright (c) Facebook, Inc. and its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
module CPE
module Flatpak
def manage
@manage ||= node['cpe_flatpak']['manage']
end
def chef_cache
Chef::Config[:file_cache_path]
end
def flatpak_remotes_receipt_path
"#{chef_cache}/cpe_flatpak/remotes.json"
end
def flatpak_remotes_receipt
return [] unless ::File.exist?(flatpak_remotes_receipt_path)
Chef::JSONCompat.from_json(::File.read(flatpak_remotes_receipt_path))
rescue StandardError
[]
end
def flatpak_packages_receipt_path
"#{chef_cache}/cpe_flatpak/packages.json"
end
def flatpak_packages_receipt
return [] unless ::File.exist?(flatpak_packages_receipt_path)
Chef::JSONCompat.from_json(::File.read(flatpak_packages_receipt_path))
rescue StandardError
[]
end
def as_user(cmd)
"/usr/bin/su #{CPE::Helpers.console_user} -l -c '#{cmd}'"
end
def flatpak_remote_add(remote, url)
Chef::Log.debug("Running flatpak_remote_add on #{remote}")
"/usr/bin/flatpak remote-add --if-not-exists #{remote} #{url}"
end
def flatpak_remote_remove(remote)
Chef::Log.debug("Running flatpak_remote_remove on #{remote}")
"/usr/bin/flatpak remote-delete #{remote}"
end
def flatpak_install(remote, pkg)
# To install a Flatpak package
Chef::Log.debug("Running flatpak_install #{pkg}")
"/usr/bin/flatpak install -y #{remote} #{pkg}"
end
def flatpak_run(cmd)
# To run a Flatpak package
Chef::Log.debug("Running flatpak_run #{cmd}")
as_user("/usr/bin/flatpak run #{cmd}")
end
def flatpak_remove(pkg)
# To uninstall a Flatpak package
Chef::Log.debug("Running flatpak_remove #{pkg}")
"/usr/bin/flatpak uninstall -y --force-remove #{pkg}"
end
def flatpak_command(cmd)
# For running arbitrary flatpak commands.
Chef::Log.debug("Running flatpak #{cmd}")
"/usr/bin/flatpak #{cmd}"
end
def repo_installed?(repo_name)
return false unless repo_name
repos = shell_out('/usr/bin/flatpak remotes -d').stdout.to_s
repos.split("\n").any? { |r| r.split("\t")[0].include?(repo_name) }
end
def pkg_installed?(pkg)
return false unless pkg
pkgs = shell_out('/usr/bin/flatpak list').stdout.to_s
pkgs.split("\n").any? { |p| p.include?(pkg) }
end
end
end