Skip to content

Commit 79cdebe

Browse files
committed
Fixed docker facts to check for active swarm clusters before running docker swarm sub-commands.
1 parent f94b5b8 commit 79cdebe

File tree

4 files changed

+194
-7
lines changed

4 files changed

+194
-7
lines changed

Diff for: lib/facter/docker.rb

+27-4
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,20 @@ def interfaces
8080
Facter.add(:docker_worker_join_token) do
8181
setcode do
8282
if Facter::Core::Execution.which('docker')
83-
val = Facter::Core::Execution.execute(
84-
"#{docker_command} swarm join-token worker -q", timeout: 90
83+
# only run `docker swarm` commands if this node is in active in a cluster
84+
docker_json_str = Facter::Core::Execution.execute(
85+
"#{docker_command} info --format '{{json .}}'", timeout: 90
8586
)
87+
begin
88+
docker = JSON.parse(docker_json_str)
89+
if docker.fetch('Swarm', {})['LocalNodeState'] == 'active'
90+
val = Facter::Core::Execution.execute(
91+
"#{docker_command} swarm join-token worker -q", timeout: 90
92+
)
93+
end
94+
rescue JSON::ParserError
95+
nil
96+
end
8697
end
8798
val
8899
end
@@ -91,14 +102,26 @@ def interfaces
91102
Facter.add(:docker_manager_join_token) do
92103
setcode do
93104
if Facter::Core::Execution.which('docker')
94-
val = Facter::Core::Execution.execute(
95-
"#{docker_command} swarm join-token manager -q", timeout: 90
105+
# only run `docker swarm` commands if this node is in active in a cluster
106+
docker_json_str = Facter::Core::Execution.execute(
107+
"#{docker_command} info --format '{{json .}}'", timeout: 90
96108
)
109+
begin
110+
docker = JSON.parse(docker_json_str)
111+
if docker.fetch('Swarm', {})['LocalNodeState'] == 'active'
112+
val = Facter::Core::Execution.execute(
113+
"#{docker_command} swarm join-token manager -q", timeout: 90
114+
)
115+
end
116+
rescue JSON::ParserError
117+
nil
118+
end
97119
end
98120
val
99121
end
100122
end
101123

124+
102125
Facter.add(:docker) do
103126
setcode do
104127
docker_version = Facter.value(:docker_client_version)

Diff for: spec/fixtures/facts/docker_info

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
},
132132
"ControlAvailable": false,
133133
"Error": "",
134-
"LocalNodeState": "inactive",
134+
"LocalNodeState": "active",
135135
"Managers": 0,
136136
"NodeAddr": "",
137137
"NodeID": "",

Diff for: spec/fixtures/facts/docker_info_swarm_inactive

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
{
2+
"Architecture": "x86_64",
3+
"BridgeNfIp6tables": true,
4+
"BridgeNfIptables": true,
5+
"CPUSet": true,
6+
"CPUShares": true,
7+
"CgroupDriver": "cgroupfs",
8+
"ClusterAdvertise": "",
9+
"ClusterStore": "",
10+
"ContainerdCommit": {
11+
"Expected": "4ab9917febca54791c5f071a9d1f404867857fcc",
12+
"ID": "4ab9917febca54791c5f071a9d1f404867857fcc"
13+
},
14+
"Containers": 46,
15+
"ContainersPaused": 0,
16+
"ContainersRunning": 5,
17+
"ContainersStopped": 41,
18+
"CpuCfsPeriod": true,
19+
"CpuCfsQuota": true,
20+
"Debug": false,
21+
"DefaultRuntime": "runc",
22+
"DockerRootDir": "/var/lib/docker",
23+
"Driver": "aufs",
24+
"DriverStatus": [
25+
[
26+
"Root Dir",
27+
"/var/lib/docker/aufs"
28+
],
29+
[
30+
"Backing Filesystem",
31+
"extfs"
32+
],
33+
[
34+
"Dirs",
35+
"408"
36+
],
37+
[
38+
"Dirperm1 Supported",
39+
"false"
40+
]
41+
],
42+
"ExperimentalBuild": false,
43+
"HttpProxy": "",
44+
"HttpsProxy": "",
45+
"ID": "VYL2:ZOEC:PG3V:3UFK:EXBT:FR3X:6IY4:ELX4:EQ5B:35C7:6OPZ:EQC6",
46+
"IPv4Forwarding": true,
47+
"Images": 50,
48+
"IndexServerAddress": "https://index.docker.io/v1/",
49+
"InitBinary": "docker-init",
50+
"InitCommit": {
51+
"Expected": "949e6fa",
52+
"ID": "949e6fa"
53+
},
54+
"Isolation": "",
55+
"KernelMemory": true,
56+
"KernelVersion": "3.13.0-115-generic",
57+
"Labels": null,
58+
"LiveRestoreEnabled": false,
59+
"LoggingDriver": "json-file",
60+
"MemTotal": 1977839616,
61+
"MemoryLimit": true,
62+
"NCPU": 2,
63+
"NEventsListener": 0,
64+
"NFd": 52,
65+
"NGoroutines": 50,
66+
"Name": "docker00",
67+
"NoProxy": "",
68+
"OSType": "linux",
69+
"OomKillDisable": true,
70+
"OperatingSystem": "Ubuntu 14.04.5 LTS",
71+
"Plugins": {
72+
"Authorization": null,
73+
"Network": [
74+
"bridge",
75+
"host",
76+
"macvlan",
77+
"null",
78+
"overlay"
79+
],
80+
"Volume": [
81+
"local"
82+
]
83+
},
84+
"RegistryConfig": {
85+
"IndexConfigs": {
86+
"docker.io": {
87+
"Mirrors": null,
88+
"Name": "docker.io",
89+
"Official": true,
90+
"Secure": true
91+
}
92+
},
93+
"InsecureRegistryCIDRs": [
94+
"127.0.0.0/8"
95+
],
96+
"Mirrors": []
97+
},
98+
"RuncCommit": {
99+
"Expected": "54296cf40ad8143b62dbcaa1d90e520a2136ddfe",
100+
"ID": "54296cf40ad8143b62dbcaa1d90e520a2136ddfe"
101+
},
102+
"Runtimes": {
103+
"runc": {
104+
"path": "docker-runc"
105+
}
106+
},
107+
"SecurityOptions": [
108+
"name=apparmor"
109+
],
110+
"ServerVersion": "17.03.1-ce",
111+
"SwapLimit": false,
112+
"Swarm": {
113+
"Cluster": {
114+
"CreatedAt": "0001-01-01T00:00:00Z",
115+
"ID": "",
116+
"Spec": {
117+
"CAConfig": {},
118+
"Dispatcher": {},
119+
"EncryptionConfig": {
120+
"AutoLockManagers": false
121+
},
122+
"Orchestration": {},
123+
"Raft": {
124+
"ElectionTick": 0,
125+
"HeartbeatTick": 0
126+
},
127+
"TaskDefaults": {}
128+
},
129+
"UpdatedAt": "0001-01-01T00:00:00Z",
130+
"Version": {}
131+
},
132+
"ControlAvailable": false,
133+
"Error": "",
134+
"LocalNodeState": "inactive",
135+
"Managers": 0,
136+
"NodeAddr": "",
137+
"NodeID": "",
138+
"Nodes": 0,
139+
"RemoteManagers": null
140+
},
141+
"SystemStatus": null,
142+
"SystemTime": "2017-04-11T01:12:52.292117616-04:00"
143+
}

Diff for: spec/unit/lib/facter/docker_spec.rb

+23-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
allow(Facter::Core::Execution).to receive(:execute).and_call_original
1111

1212
if Facter.value(:kernel) == 'windows'
13-
docker_command = 'powershell -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -c docker'
13+
@docker_command = 'powershell -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -c docker'
1414
allow(Facter::Core::Execution).to receive(:which).with('dhcpcd').and_return('C:\Windows\dhcpd.exe')
1515
allow(Facter::Core::Execution).to receive(:which).with('route').and_return('C:\Windows\System32\ROUTE.EXE')
1616
allow(Facter::Core::Execution).to receive(:which).with('docker').and_return('C:\Program Files\Docker\docker.exe')
1717
else
18-
docker_command = 'docker'
18+
@docker_command = 'docker'
1919
allow(Facter::Core::Execution).to receive(:which).with('route').and_return('/usr/bin/route')
2020
allow(Facter::Core::Execution).to receive(:which).with('dhcpcd').and_return('/usr/bin/dhcpd')
2121
allow(Facter::Core::Execution).to receive(:which).with('docker').and_return('/usr/bin/docker')
@@ -129,4 +129,25 @@
129129
)
130130
end
131131
end
132+
133+
describe 'docker swarm worker join-token with inactive swarm cluster' do
134+
before :each do
135+
docker_info = File.read(fixtures('facts', 'docker_info_swarm_inactive'))
136+
allow(Facter::Core::Execution).to receive(:execute).with("#{@docker_command} info --format '{{json .}}'", timeout: 90).and_return(docker_info)
137+
end
138+
it do
139+
expect(Facter.fact(:docker_worker_join_token).value).to be_nil
140+
end
141+
end
142+
143+
describe 'docker swarm manager join-token with inactive swarm cluster' do
144+
before :each do
145+
docker_info = File.read(fixtures('facts', 'docker_info_swarm_inactive'))
146+
allow(Facter::Core::Execution).to receive(:execute).with("#{@docker_command} info --format '{{json .}}'", timeout: 90).and_return(docker_info)
147+
end
148+
149+
it do
150+
expect(Facter.fact(:docker_manager_join_token).value).to be_nil
151+
end
152+
end
132153
end

0 commit comments

Comments
 (0)