-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathconn.rb
More file actions
122 lines (113 loc) · 3.7 KB
/
conn.rb
File metadata and controls
122 lines (113 loc) · 3.7 KB
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
# frozen_string_literal: true
require 'bolt/util'
require 'bolt/inventory'
module BoltSpec
module Conn
def conn_info(transport)
default_host = 'localhost'
default_user = 'bolt'
default_password = 'bolt'
default_second_user = 'test'
default_second_pw = 'test'
default_key = File.expand_path(File.join(__dir__, '..', '..', 'fixtures/keys/id_rsa'))
default_port = 0
additional_config = {}
tu = transport.upcase
case transport
when 'ssh'
default_port = 20022
when 'winrm'
default_port = 25985
additional_config = { ssl: false,
'connect-timeout': 120 }
when 'docker', 'podman'
default_user = ''
default_password = ''
default_host = 'ubuntu_node'
when 'lxd'
default_host = 'testlxd'
when 'jail'
default_user = 'root'
default_host = 'bolt'
else
raise Error, "The transport must be either 'ssh', 'winrm', 'docker', 'podman', 'lxd' or 'jail'."
end
additional_config.merge(
protocol: transport,
host: ENV["BOLT_#{tu}_HOST"] || default_host,
user: ENV["BOLT_#{tu}_USER"] || default_user,
password: ENV["BOLT_#{tu}_PASSWORD"] || default_password,
port: (ENV["BOLT_#{tu}_PORT"] || default_port).to_i,
key: ENV["BOLT_#{tu}_KEY"] || default_key,
second_user: ENV["BOLT_#{tu}_SECOND_USER"] || default_second_user,
second_pw: ENV["BOLT_#{tu}_SECOND_PW"] || default_second_pw,
system_user: `whoami`.strip
)
end
def conn_uri(transport, include_password: false, override_port: nil)
conn = conn_info(transport)
passwd = include_password ? ":#{conn[:password]}" : ''
port = ":#{override_port || conn[:port]}"
"#{conn[:protocol]}://#{conn[:user]}#{passwd}@#{conn[:host]}#{port}"
end
def conn_target(transport, include_password: false, options: nil)
inventory = Bolt::Inventory.empty
target = inventory.get_target(conn_uri(transport, include_password: include_password))
inventory.set_config(target, transport, options) if options
target
end
def conn_inventory
groups = %w[ssh winrm].map do |transport|
{ "name" => transport,
"targets" => [conn_uri(transport)],
"config" => {
transport => Bolt::Util.walk_keys(conn_info(transport), &:to_s)
} }
end
{ "groups" => groups }
end
def docker_inventory(root: false)
usernamepassword = root ? 'root' : 'bolt'
{
'groups' => [
{
'name' => 'ssh',
'targets' => [
{
'name' => 'ubuntu_node',
'alias' => 'agentless',
'config' => { 'ssh' => { 'port' => 20022 } }
}
],
'groups' => [
{
'name' => 'nix_agents',
'targets' => [
{
'name' => 'puppet_8_node',
'config' => { 'ssh' => { 'port' => 20024 } }
},
{
'name' => 'puppet_7_node',
'config' => { 'ssh' => { 'port' => 20025 } }
}
]
}
],
'config' => {
'ssh' => {
'host' => 'localhost',
'host-key-check' => false,
'user' => usernamepassword,
'password' => usernamepassword
}
}
}
]
}
end
def root_password
'root'
end
end
end