/
guest_inspection.rb
110 lines (95 loc) · 3.55 KB
/
guest_inspection.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
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: BUSL-1.1
module Vagrant
module Util
# Helper methods for inspecting guests to determine if specific services
# or applications are installed and in use
module GuestInspection
# Linux specific inspection helpers
module Linux
## systemd helpers
# systemd is in use
#
# @return [Boolean]
def systemd?(comm)
comm.test("ps -o comm= 1 | grep systemd", sudo: true)
end
# systemd-networkd.service is in use
#
# @param [Vagrant::Plugin::V2::Communicator] comm Guest communicator
# @return [Boolean]
def systemd_networkd?(comm)
comm.test("systemctl -q is-active systemd-networkd.service", sudo: true)
end
# Check if a unit file with the given name is defined. Name can
# be a pattern or explicit name.
#
# @param [Vagrant::Plugin::V2::Communicator] comm Guest communicator
# @param [String] name Name or pattern to search
# @return [Boolean]
def systemd_unit_file?(comm, name)
comm.test("systemctl -q list-unit-files | grep \"#{name}\"")
end
# Check if a unit is currently active within systemd
#
# @param [Vagrant::Plugin::V2::Communicator] comm Guest communicator
# @param [String] name Name or pattern to search
# @return [Boolean]
def systemd_unit?(comm, name)
comm.test("systemctl -q list-units | grep \"#{name}\"")
end
# Check if given service is controlled by systemd
#
# @param [Vagrant::Plugin::V2::Communicator] comm Guest communicator
# @param [String] service_name Name of the service to check
# @return [Boolean]
def systemd_controlled?(comm, service_name)
comm.test("systemctl -q is-active #{service_name}", sudo: true)
end
# systemd hostname set is via hostnamectl
#
# @param [Vagrant::Plugin::V2::Communicator] comm Guest communicator
# @return [Boolean]
# NOTE: This test includes actually calling `hostnamectl` to verify
# that it is in working order. This prevents attempts to use the
# hostnamectl command when it is available, but dbus is not which
# renders the command useless
def hostnamectl?(comm)
comm.test("command -v hostnamectl && hostnamectl")
end
## netplan helpers
# netplan is installed
#
# @param [Vagrant::Plugin::V2::Communicator] comm Guest communicator
# @return [Boolean]
def netplan?(comm)
comm.test("command -v netplan")
end
# is networkd isntalled
#
# @param [Vagrant::Plugin::V2::Communicator] comm Guest communicator
# @return [Boolean]
def networkd?(comm)
comm.test("command -v networkd")
end
## nmcli helpers
# nmcli is installed
#
# @param [Vagrant::Plugin::V2::Communicator] comm Guest communicator
# @return [Boolean]
def nmcli?(comm)
comm.test("command -v nmcli")
end
# NetworkManager currently controls device
#
# @param [Vagrant::Plugin::V2::Communicator] comm Guest communicator
# @param device_name [String]
# @return [Boolean]
def nm_controlled?(comm, device_name)
comm.test("nmcli -t d show #{device_name}") &&
!comm.test("nmcli -t d show #{device_name} | grep unmanaged")
end
end
end
end
end