ssh_hostname

This commit is contained in:
Pavel Vorobyov
2020-05-26 16:16:36 +03:00
parent e19216f582
commit f6ee512c95
4 changed files with 93 additions and 38 deletions

View File

@@ -50,6 +50,18 @@ func New(cfg *config.XCConfig) (*Inventoree, error) {
term.Warnf("WARNING: Inventory backend will be accessed in insecure mode\n")
}
// host key field
hostKey, found := options["host_key_field"]
if !found {
hostKey = "fqdn"
}
if hostKey != "fqdn" && hostKey != "ssh_hostname" {
term.Errorf("ERROR: invalid host_key_field \"%s\", only \"fqdn\" and \"ssh_hostname\" are allowed\n", hostKey)
term.Warnf("Falling back to fqdn as host key field\n")
hostKey = "fqdn"
}
// auth configuration
authToken, found := options["auth_token"]
if !found {
@@ -61,6 +73,7 @@ func New(cfg *config.XCConfig) (*Inventoree, error) {
cacheTTL: cfg.CacheTTL,
cacheDir: cfg.CacheDir,
url: url,
hostKeyField: hostKey,
authToken: authToken,
insecure: insecure,
}, nil
@@ -274,8 +287,12 @@ func (i *Inventoree) loadRemote() error {
term.Warnf("Loading hosts...")
count = 0
fieldSet := "_id,fqdn,ssh_hostname,local_tags,group_id,datacenter_id,aliases,description"
moveToFQDN := i.hostKeyField != "fqdn"
for _, wg := range lc.WorkGroups {
path := fmt.Sprintf("/api/v2/hosts/?work_group_id=%s&_fields=_id,fqdn,local_tags,group_id,datacenter_id,aliases,description&_nopaging=true", wg.ID)
path := fmt.Sprintf("/api/v2/hosts/?work_group_id=%s&_fields=%s&_nopaging=true", wg.ID, fieldSet)
data, err = i.inventoreeGet(path)
if err != nil {
term.Errorf("\nError loading hosts of work group %s: %s", wg.Name, err)
@@ -288,6 +305,11 @@ func (i *Inventoree) loadRemote() error {
continue
}
for _, h := range hdata.Data {
if moveToFQDN && h.SSHHostname != "" {
// copying ssh_hostname to FQDN
// to keep things simple
h.FQDN = h.SSHHostname
}
lc.Hosts = append(lc.Hosts, h)
count++
}

View File

@@ -14,6 +14,7 @@ type Inventoree struct {
url string
authToken string
insecure bool
hostKeyField string
hosts []*store.Host
groups []*store.Group
workgroups []*store.WorkGroup
@@ -36,6 +37,7 @@ type workgroup struct {
type host struct {
ID string `json:"_id"`
FQDN string `json:"fqdn"`
SSHHostname string `json:"ssh_hostname"`
Description string `json:"description"`
Tags []string `json:"local_tags"`
Aliases []string `json:"aliases"`

View File

@@ -97,61 +97,103 @@ user =
mode = parallel
history_file = ~/.xc_history
cache_dir = ~/.xc_cache
cache_ttl = 336 # 24 * 7 * 2
rc_file = ~/.xcrc
raise = none
exit_confirm = true
backend_type = conductor
local_file = ~/.xc_hosts
exec_confirm = false
log_file = ~/xc.log
distribute = scp
debug = false
[executer]
ssh_threads = 50
ssh_connect_timeout = 1
ping_count = 5
ssh_command = /usr/bin/ssh
progress_bar = true
prepend_hostnames = true
remote_tmpdir = /tmp
delay = 0
[inventoree]
url = http://c.inventoree.ru
work_groups =
interpreter = bash
interpreter_sudo = sudo bash
interpreter_su = su -
Configuration is split to 3 sections: main, executer and inventoree.
[backend]
type = inventoree
url = http://inventory-stage.infra.cloud.devmail.ru
auth_token = <token>
work_groups = WorkGroup1,WorkGroup2
host_key_field = ssh_hostname
main.user is the user which will be set on xc startup. If empty, the current system user is used.
Configuration is split to 3 sections: main, executer and backend.
main.mode is the execution mode which will be set on xc startup. See "help mode" for more info on execution modes.
[main]
user is the user which will be set on xc startup. If empty, the current system user is used.
main.history_file sets the history file
mode is the execution mode which will be set on xc startup. See "help mode" for more info on execution modes.
main.cache_dir sets the cache dir for data derived from inventoree
history_file sets the history file
main.rc_file is the rcfile which will be executed on xc startup. See "help rcfiles" for more info.
cache_dir sets the cache dir for data derived from inventoree
main.raise is the raise mode which will be set on xc startup
cache_ttl sets cache ttl (in hours)
main.exit_confirm is boolean setting for disable or enable confirmation on exit
rc_file is the rcfile which will be executed on xc startup. See "help rcfiles" for more info.
main.backend_type is type of backend, conductor or localjson or localini now
raise is the raise mode which will be set on xc startup
main.local_file is path to json or ini local file, used when backend_type is localjson or localini
exit_confirm is boolean setting for disable or enable confirmation on exit
executer.ssh_threads limits the number of simultaneously running ssh commands.
distribute sets initial distribute type to either tar or scp. See "help distribute_type" to learn more.
executer.ssh_connect_timeout sets the default ssh connect timeout. You can change it at any moment using connect_timeout command.
debug sets initial debug logging on/off.
executer.ping_count is not implemented yet and does nothing
[executer]
ssh_threads limits the number of simultaneously running ssh commands.
executer.progress_bar sets progressbar on or off on xc startup
ssh_connect_timeout sets the default ssh connect timeout. You can change it at any moment using connect_timeout command.
executer.remote_tmpdir is a temporary directory used on remote servers for various xc needs
progress_bar sets progressbar on or off on xc startup
executer.delay sets a delay in seconds between hosts when executing in serial mode. See "help delay" for more info
remote_tmpdir is a temporary directory used on remote servers for various xc needs
inventoree.url sets the url of the inventoree service
delay sets a delay in seconds between hosts when executing in serial mode. See "help delay" for more info
inventoree.work_groups is a comma-separated list of work_groups which will be downloaded from inventoree.
If empty all work groups (i.e. all groups and all hosts as well) are downloaded without filtering which
may cause startup delays`,
interpreter_* sets commands executed remotely to boot the necessary interpreter according to current "raise" mode
The [backend] section sets data storage backend. Three backends are currently supported: inventoree, conductor and ini. The backend type is set by a mandatory option "type".
1. "ini" backend stores hosts and groups in a local ini-file.
There's only one option "filename" to tell xc where to find the ini-file.
Example of ini-file:
[workgroups]
workgroup1
[groups]
group1 work_group=workgroup1
group2 work_grlup=workgroup2 parent=group1
[hosts]
host1.example.com group=group1 datacenter=dc1.1
host2.example.com group=group2 datacenter=dc1.1
[datacenters]
dc1
dc1.1 parent=dc1
2. "conductor" loads hosts and groups via inventoree v1 API which is deprecated
3. "inventoree" is the most modern way to store your data. Options are following:
url - a base url to inventoree instance (inventoree >= 7.0 is required)
auth_token - your personal auth token
work_groups - a comma-separated list of workgroups to load. If the list is empty, xc will load all the workgroups which could increase loading time dramatically.
host_key_field - may be set to either "fqdn" or "ssh_hostname", this tells xc what a host is identified by.
ssh_hostname in its turn is a computed field in inventoree >= 7.2-45 which may be configured
in custom data field "ssh_hostname" like aliases are configured (using $0, $1, $2 etc as domain parts)
`,
},
"rcfiles": &helpItem{

View File

@@ -52,7 +52,6 @@ type BackendType int
// Backend types
const (
BTIni BackendType = iota
BTJSON
BTConductor
BTInventoree
)
@@ -72,7 +71,6 @@ type XCConfig struct {
SSHThreads int
SSHConnectTimeout int
SSHCommand string
PingCount int
RemoteTmpdir string
Mode string
RaiseType string
@@ -103,7 +101,6 @@ const (
defaultCacheTTL = 24
defaultThreads = 50
defaultRemoteTmpDir = "/tmp"
defaultPingCount = 5
defaultDelay = 0
defaultMode = "parallel"
defaultRaiseType = "none"
@@ -235,12 +232,6 @@ func read(filename string, secondPass bool) (*XCConfig, error) {
}
cfg.RemoteTmpdir = tmpdir
pc, err := props.GetInt("executer.ping_count")
if err != nil {
pc = defaultPingCount
}
cfg.PingCount = pc
sdi, err := props.GetString("executer.interpreter_sudo")
if err != nil {
sdi = defaultSudoInterpreter
@@ -320,8 +311,6 @@ func read(filename string, secondPass bool) (*XCConfig, error) {
switch value {
case "ini":
cfg.BackendCfg.Type = BTIni
case "json":
cfg.BackendCfg.Type = BTJSON
case "conductor":
cfg.BackendCfg.Type = BTConductor
case "inventoree":