foreman_discovery 1.1.1 → 1.2.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +13 -5
  2. data/README.md +19 -71
  3. data/Rakefile +15 -16
  4. data/app/controllers/discovers_controller.rb +10 -11
  5. data/app/helpers/discovers_helper.rb +9 -9
  6. data/app/models/host/discovered.rb +6 -5
  7. data/app/models/host/managed_extensions.rb +2 -2
  8. data/app/models/setting/discovered.rb +8 -2
  9. data/app/overrides/add_dashboard_table.rb +9 -4
  10. data/app/views/discovers/_discovers_list.html.erb +14 -14
  11. data/app/views/discovers/_selected_hosts.html.erb +8 -4
  12. data/app/views/discovers/multiple_destroy.html.erb +2 -1
  13. data/app/views/discovers/select_multiple_location.html.erb +2 -2
  14. data/app/views/discovers/select_multiple_organization.html.erb +2 -2
  15. data/app/views/discovers/show.html.erb +4 -4
  16. data/config/initializers/discovery_setup.rb +0 -17
  17. data/extra/ovirt-node-plugin-foreman/COPYING +340 -0
  18. data/extra/ovirt-node-plugin-foreman/Makefile.am +60 -0
  19. data/extra/ovirt-node-plugin-foreman/README.md +217 -0
  20. data/extra/ovirt-node-plugin-foreman/autogen.sh +66 -0
  21. data/extra/ovirt-node-plugin-foreman/conf/Makefile.am +29 -0
  22. data/extra/ovirt-node-plugin-foreman/conf/foreman-plugin.firewall.in +4 -0
  23. data/extra/ovirt-node-plugin-foreman/conf/foreman-plugin.logs +2 -0
  24. data/extra/ovirt-node-plugin-foreman/configure.ac +62 -0
  25. data/extra/ovirt-node-plugin-foreman/ovirt-node-plugin-foreman.spec.in +145 -0
  26. data/extra/ovirt-node-plugin-foreman/recipe/Makefile.am +33 -0
  27. data/extra/ovirt-node-plugin-foreman/recipe/foreman-plugin-iso.ks +5 -0
  28. data/extra/ovirt-node-plugin-foreman/recipe/foreman-plugin-minimizer.ks +160 -0
  29. data/extra/ovirt-node-plugin-foreman/src/Makefile.am +32 -0
  30. data/extra/ovirt-node-plugin-foreman/src/discover-host.rb.in +186 -0
  31. data/extra/ovirt-node-plugin-foreman/src/discover-host.service +14 -0
  32. data/extra/ovirt-node-plugin-foreman/src/discovery-version-fact.rb.in +25 -0
  33. data/extra/ovirt-node-plugin-foreman/src/find-missing-libs +35 -0
  34. data/lib/{tasks.rake → discovery.rake} +22 -0
  35. data/lib/foreman_discovery/engine.rb +39 -7
  36. data/lib/foreman_discovery/version.rb +1 -1
  37. data/test/functional/discovers_controller_test.rb +3 -16
  38. data/test/test_helper.rb +2 -2
  39. data/test/test_plugin_helper.rb +6 -0
  40. data/test/unit/facts.json +92 -0
  41. data/test/unit/host_discovered_test.rb +20 -2
  42. metadata +55 -37
  43. data/lib/discovery_home_helper_patch.rb +0 -18
  44. data/test/unit/facts.yml +0 -91
@@ -0,0 +1,29 @@
1
+ # vim: ts=8:sw=8:noet
2
+ #
3
+ # Copyright (C) 2013 Red Hat, Inc.
4
+ #
5
+ # This program is free software; you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation; version 2 of the License.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17
+ # MA 02110-1301, USA. A copy of the GNU General Public License is
18
+ # also available at http://www.gnu.org/copyleft/gpl.html.
19
+ # Copyright (C) 2010, Red Hat, Inc.
20
+ #
21
+
22
+ EXTRA_DIST= \
23
+ foreman-plugin.*
24
+
25
+ pluginsdir = $(sysconfdir)/ovirt-plugins.d
26
+
27
+ dist_plugins_DATA = \
28
+ foreman-plugin.logs \
29
+ foreman-plugin.firewall
@@ -0,0 +1,4 @@
1
+ # Note that we uninstall ovirt-node so this is not effective, but we
2
+ # keep this for completeness of the foreman plugin.
3
+ 8443,tcp
4
+ @EXTRA_FIREWALL_RULE@
@@ -0,0 +1,2 @@
1
+ # no relevant log files for foreman
2
+ # (we are logging everything in /tmp in development mode)
@@ -0,0 +1,62 @@
1
+ # vim: ts=8:sw=8:noet
2
+ #
3
+ # Copyright (C) 2013 Red Hat, Inc.
4
+ #
5
+ # This program is free software; you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation; version 2 of the License.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17
+ # MA 02110-1301, USA. A copy of the GNU General Public License is
18
+ # also available at http://www.gnu.org/copyleft/gpl.html.
19
+
20
+ AC_INIT([ovirt-node-plugin-foreman], [0.3.0], [foreman-dev@googlegroups.com])
21
+ AM_INIT_AUTOMAKE([-Wall -Werror foreign -Wno-portability tar-pax])
22
+ AC_SUBST([ENGINENAME], ['Foreman Discovery'])
23
+ PKG_PROG_PKG_CONFIG
24
+
25
+ AC_MSG_CHECKING([whether to enable plugin debug support])
26
+ AC_ARG_ENABLE(debug,
27
+ AS_HELP_STRING([--enable-debug],
28
+ [turn on debug support (starts ssh daemon and sets root password)]))
29
+ if test x$enable_debug = xyes; then
30
+ AC_MSG_RESULT([yes])
31
+ AC_MSG_NOTICE([WARNING: ssh daemon enabled and root password set to 'development'])
32
+ PLUGIN_DEBUG=1
33
+ EXTRA_FIREWALL_RULE="22,tcp"
34
+ else
35
+ AC_MSG_RESULT([no])
36
+ PLUGIN_DEBUG=0
37
+ EXTRA_FIREWALL_RULE="#22,tcp"
38
+ fi
39
+ AC_SUBST(PLUGIN_DEBUG)
40
+ AC_SUBST(EXTRA_FIREWALL_RULE)
41
+
42
+ AC_MSG_CHECKING([for systemd unit directory])
43
+ AC_ARG_WITH([systemdsystemunitdir],
44
+ AS_HELP_STRING([--with-systemdsystemunitdir=DIR],
45
+ [Directory for systemd service files]),
46
+ [], [with_systemdsystemunitdir=$($PKG_CONFIG --variable=systemdsystemunitdir systemd)])
47
+ if test "x$with_systemdsystemunitdir" != xno; then
48
+ AC_SUBST([systemdsystemunitdir], [$with_systemdsystemunitdir])
49
+ fi
50
+ AM_CONDITIONAL(HAVE_SYSTEMD, [test -n "$with_systemdsystemunitdir" -a "x$with_systemdsystemunitdir" != xno ])
51
+ AC_MSG_RESULT([done])
52
+
53
+ AC_CONFIG_FILES([Makefile
54
+ src/Makefile
55
+ conf/Makefile
56
+ recipe/Makefile
57
+ src/discover-host.rb
58
+ conf/foreman-plugin.firewall
59
+ src/discovery-version-fact.rb
60
+ ovirt-node-plugin-foreman.spec])
61
+
62
+ AC_OUTPUT
@@ -0,0 +1,145 @@
1
+ # vim: ts=4:sw=4:et
2
+ #
3
+ # Copyright (C) 2013 Red Hat, Inc.
4
+ #
5
+ # This program is free software; you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation; version 2 of the License.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17
+ # MA 02110-1301, USA. A copy of the GNU General Public License is
18
+ # also available at http://www.gnu.org/copyleft/gpl.html.
19
+
20
+ Summary: A plugin for Foreman Discovery nodes
21
+ Name: ovirt-node-plugin-foreman
22
+ Version: @VERSION@
23
+ Release: 1%{?BUILD_NUMBER}%{?extra_release}%{?dist}
24
+ Source0: %{name}-%{version}.tar.gz
25
+ License: GPLv2+
26
+ Group: Applications/System
27
+
28
+ BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-buildroot
29
+ URL: http://www.ovirt.org/
30
+ Requires: ovirt-node >= 3.0.0
31
+ Requires: facter
32
+ Requires: foreman-proxy
33
+ Requires: sudo
34
+ %if "@PLUGIN_DEBUG@" == "1"
35
+ Requires: openssh-server
36
+ Requires: vim-minimal
37
+ Requires: file telnet lsof
38
+ %endif
39
+
40
+ BuildArch: noarch
41
+
42
+ BuildRequires: systemd
43
+
44
+ %define app_root %{_datadir}/%{name}
45
+ %define recipe_root %{_datadir}/ovirt-node-recipe
46
+
47
+ %description
48
+ Provides Foreman Proxy and Discovery component for automatic registration
49
+ of nodes in Foreman and provisioning.
50
+
51
+ %package recipe
52
+ Summary: Kickstarts for building Node isos including %{name}
53
+ Group: Applications/System
54
+ Requires: ovirt-node-recipe >= 2.6.0
55
+
56
+ %description recipe
57
+ Provides kickstart files for generating an oVirt Node ISO image containing
58
+ %{name}.
59
+
60
+ %prep
61
+ %setup -q
62
+
63
+
64
+ %build
65
+ %if "@PLUGIN_DEBUG@" == "1"
66
+ %configure --enable-debug
67
+ %else
68
+ %configure
69
+ %endif
70
+
71
+ %install
72
+ %{__rm} -rf %{buildroot}
73
+ make install DESTDIR=%{buildroot}
74
+
75
+ %post
76
+ # Modify login issue
77
+ sed -i 's/oVirt Node Hypervisor/Foreman Discovery/' /etc/issue
78
+
79
+ # Reserve tty1 only for logs
80
+ rm -f /etc/systemd/system/getty.target.wants/getty@tty1.service
81
+
82
+ # In development mode set password and enable ssh daemon (with root access)
83
+ %if "@PLUGIN_DEBUG@" == "1"
84
+ echo "root:development" | chpasswd
85
+ sed -i 's/^.*PasswordAuthentication.*$/PasswordAuthentication yes/' /etc/ssh/sshd_config
86
+ sed -i 's/^.*PermitRootLogin.*$/PermitRootLogin yes/' /etc/ssh/sshd_config
87
+ %endif
88
+
89
+ # Configure foreman-proxy
90
+ %if "@PLUGIN_DEBUG@" == "1"
91
+ LOGLEVEL="DEBUG"
92
+ %else
93
+ LOGLEVEL="ERROR"
94
+ %endif
95
+ sed -i 's|.*:log_file:.*|:log_file: /dev/stdout|' /etc/foreman-proxy/settings.yml
96
+ sed -i "s/.*:log_level:.*/:log_level: $LOGLEVEL/" /etc/foreman-proxy/settings.yml
97
+ sed -i 's/.*:bmc:.*/:bmc: true/' /etc/foreman-proxy/settings.yml
98
+ sed -i 's/.*:bmc_default_provider:.*/:bmc_default_provider: shell/' /etc/foreman-proxy/settings.yml
99
+
100
+ # Connect smart-proxy standard output to tty1
101
+ sed '/^ExecStart/aStandardOutput=tty' -i /usr/lib/systemd/system/foreman-proxy.service
102
+ sed '/^ExecStart/aTTYPath=/dev/tty1' -i /usr/lib/systemd/system/foreman-proxy.service
103
+
104
+ # Enable Foreman Proxy service
105
+ systemctl enable foreman-proxy.service
106
+
107
+ # Enable Discover Host service (must be executed as the last - idle - service)
108
+ systemctl enable discover-host.service
109
+
110
+ # Force NetworkManager to wait for IP address
111
+ systemctl enable NetworkManager-wait-online.service
112
+
113
+ # Add foreman-proxy user to sudo and disable interactive tty for reboot
114
+ sed -i -e 's/^Defaults.*requiretty/Defaults !requiretty/g' /etc/sudoers
115
+ echo "foreman-proxy ALL=NOPASSWD: /sbin/shutdown" >> /etc/sudoers
116
+
117
+ # Limit maximum use of systemd journal (it is kept in memory for stateless)
118
+ sed -i -e 's/^.*SystemMaxUse.*$/SystemMaxUse=20M/g' /etc/systemd/journald.conf
119
+
120
+ # When using ntpdate during start, do not pause when time servers not available
121
+ [ -f /etc/sysconfig/ntpdate ] && sed -i -e 's/^.*RETRIES.*$/RETRIES=0/g' /etc/sysconfig/ntpdate
122
+
123
+ # Set extra directory for facter
124
+ sed -i '/\[Service\]/a Environment="FACTERLIB=/usr/share/ovirt-node-plugin-foreman"' /usr/lib/systemd/system/foreman-proxy.service
125
+
126
+ %preun
127
+
128
+ %files recipe
129
+ %{recipe_root}
130
+
131
+ %files
132
+ %{_bindir}/discover-host.rb
133
+ %{_bindir}/find-missing-libs
134
+ %{_datadir}/%{name}/discovery-version-fact.rb
135
+ %{_unitdir}/discover-host.service
136
+ %{_sysconfdir}/ovirt-plugins.d
137
+
138
+ %changelog
139
+ * Thu Dec 19 2013 Lukas Zapletal <lzap+rpm@redhat.com> 0.2.0-1
140
+ - Logging on tty1
141
+ - Changes to /etc/issue
142
+ - Faster start
143
+
144
+ * Wed Oct 30 2013 Lukas Zapletal <lzap+rpm@redhat.com> 0.1.0-1
145
+ - Initial version
@@ -0,0 +1,33 @@
1
+ # vim: ts=8:sw=8:noet
2
+ #
3
+ # Copyright (C) 2013 Red Hat, Inc.
4
+ #
5
+ # This program is free software; you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation; version 2 of the License.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17
+ # MA 02110-1301, USA. A copy of the GNU General Public License is
18
+ # also available at http://www.gnu.org/copyleft/gpl.html.
19
+
20
+ EXTRA_DIST = *.ks
21
+
22
+ OVIRT_NODE_RECIPEdir = $(datadir)/ovirt-node-recipe
23
+ pluginsdir = $(sysconfdir)/ovirt-plugins.d
24
+
25
+ OVIRT_NODE_RECIPE_DATA = \
26
+ foreman-plugin-minimizer.ks \
27
+ foreman-plugin-iso.ks
28
+
29
+ dist_plugins_DATA = \
30
+ foreman-plugin-minimizer.ks
31
+
32
+ install-data-hook:
33
+ mv $(DESTDIR)/$(pluginsdir)/foreman-plugin-minimizer.ks $(DESTDIR)/$(pluginsdir)/foreman-plugin.minimize
@@ -0,0 +1,5 @@
1
+ %include ovirt-node-image.ks
2
+ %packages --excludedocs --nobase
3
+ ovirt-node-plugin-foreman
4
+ %end
5
+ %include foreman-plugin-minimizer.ks
@@ -0,0 +1,160 @@
1
+ # Minimize script optimized for Foreman Discovery plugin (Fedora 19)
2
+ #
3
+ # Use this to find interesting packages in the image:
4
+ #
5
+ # rpm -qa --queryformat '%{SIZE} %{NAME}\n' | sort -n -r | head -n100
6
+ #
7
+ # Use this tool (part of this plugin) to check for missing libs:
8
+ #
9
+ # find-missing-libs
10
+ #
11
+
12
+ # ovirt-node and deps
13
+ droprpm ovirt-node
14
+ droprpm collectd
15
+ droprpm collectd-*
16
+
17
+ # virtualization
18
+ droprpm qemu
19
+ droprpm qemu-*
20
+ droprpm ipxe-roms-qemu
21
+ droprpm xen-licenses
22
+ droprpm vhostmd
23
+ droprpm libvirt
24
+ droprpm libvirt-*
25
+
26
+ # python
27
+ droprpm python
28
+ droprpm python-*
29
+ droprpm *-python
30
+ droprpm pyliblzma
31
+ droprpm pyxattr
32
+ droprpm pygpgme
33
+ droprpm pygobject*
34
+
35
+ # selinux (need to provide selinux=0 kernel argument)
36
+ droprpm selinux-policy-targeted
37
+ droprpm policycoreutils
38
+
39
+ # x window
40
+ droprpm cairo
41
+ droprpm cairo-gobject
42
+ droprpm colord-libs
43
+ droprpm gdk-pixbuf2
44
+ droprpm gsettings-desktop-schemas
45
+ droprpm gtk3
46
+ droprpm hicolor-icon-theme
47
+ droprpm libsndfile
48
+ droprpm libvorbis
49
+ droprpm libX*
50
+ droprpm libxcb
51
+ droprpm mesa-*
52
+ droprpm pango
53
+ droprpm pulseaudio-libs
54
+ droprpm SDL
55
+ droprpm vte3
56
+
57
+ # various
58
+ droprpm acpid
59
+ droprpm aic94xx-firmware
60
+ droprpm alsa-lib
61
+ droprpm anyterm
62
+ droprpm avahi
63
+ droprpm bc
64
+ droprpm bfa-firmware
65
+ droprpm bridge-utils
66
+ droprpm btrfs-progs
67
+ droprpm ceph-libs
68
+ droprpm cracklib-dicts
69
+ droprpm device-mapper-multipath
70
+ droprpm dmraid
71
+ droprpm dosfstools
72
+ droprpm dracut-fips
73
+ droprpm dracut-network
74
+ droprpm e2fsprogs
75
+ droprpm efibootmgr
76
+ droprpm fcoe-utils
77
+ droprpm gdb
78
+ droprpm glusterfs
79
+ droprpm glusterfs-*
80
+ droprpm hwdata
81
+ droprpm irqbalance
82
+ droprpm iscsi-initiator-utils
83
+ droprpm jfsutils
84
+ droprpm kbd-misc
85
+ droprpm kexec-tools
86
+ droprpm kpartx
87
+ droprpm less
88
+ droprpm libguestfs
89
+ droprpm libguestfs-*
90
+ droprpm libicu
91
+ droprpm libmlx4
92
+ droprpm mcelog
93
+ droprpm net-snmp-libs
94
+ droprpm ntfs-3g
95
+ droprpm ntfsprogs
96
+ droprpm numactl
97
+ droprpm openssh-clients
98
+ droprpm parted
99
+ droprpm patch
100
+ droprpm pciutils
101
+ droprpm plymouth
102
+ droprpm plymouth-*
103
+ droprpm poppler-data
104
+ droprpm psmisc
105
+ droprpm PyPAM
106
+ droprpm reiserfs-utils
107
+ droprpm rsync
108
+ droprpm rsyslog
109
+ droprpm setools-console
110
+ droprpm sos
111
+ droprpm spice-server
112
+ droprpm squashfs-tools
113
+ droprpm strace
114
+ droprpm sysfsutils
115
+ droprpm sysstat
116
+ droprpm systemtap-runtime
117
+ droprpm tcpdump
118
+ droprpm tuned
119
+ droprpm tzdata
120
+ droprpm urw-fonts
121
+ droprpm usbutils
122
+ droprpm vconfig
123
+ droprpm xen-libs
124
+ droprpm xfsprogs
125
+ droprpm zfs-fuse
126
+
127
+ # various files/directories
128
+ drop /usr/share/gems/cache
129
+ drop /usr/share/qemu
130
+ drop /usr/share/mime
131
+ drop /usr/share/groff
132
+ drop /usr/share/locale
133
+
134
+ # The following packages are REQUIRED
135
+ #
136
+ # ruby
137
+ # facter
138
+ # sudo
139
+ # ipmitool
140
+
141
+ # The following packages are required to boot
142
+ #
143
+ # ncurses-libs
144
+ # NetworkManager-glib
145
+ # grubby
146
+ # libselinux
147
+ # lua
148
+ # sqlite
149
+ # openldap
150
+ # mozjs17
151
+
152
+ # The following packages SHOULD not be dropped
153
+ # (used in development mode)
154
+ #
155
+ # file
156
+ # vim-minimal
157
+ # openssh-server
158
+ # lsof
159
+
160
+ # EOF
@@ -0,0 +1,32 @@
1
+ # vim: ts=8:sw=8:noet
2
+ #
3
+ # Copyright (C) 2013 Red Hat, Inc.
4
+ #
5
+ # This program is free software; you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation; version 2 of the License.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17
+ # MA 02110-1301, USA. A copy of the GNU General Public License is
18
+ # also available at http://www.gnu.org/copyleft/gpl.html.
19
+
20
+ dist_bin_SCRIPTS = \
21
+ discover-host.rb \
22
+ find-missing-libs
23
+
24
+ if HAVE_SYSTEMD
25
+ dist_systemdsystemunit_DATA = discover-host.service
26
+ endif
27
+
28
+ dist_pkgdata_DATA = discovery-version-fact.rb
29
+
30
+ CLEANFILES = \
31
+ discover-host.rb \
32
+ discovery-version-fact.rb
@@ -0,0 +1,186 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # vim: ts=2:sw=2:et
4
+ #
5
+ # Copyright (C) 2012-2013 Red Hat, Inc.
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; version 2 of the License.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with this program; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
+ # MA 02110-1301, USA. A copy of the GNU General Public License is
20
+ # also available at http://www.gnu.org/copyleft/gpl.html.
21
+
22
+ PACKAGE_NAME = '@PACKAGE_NAME@'
23
+ PACKAGE_VERSION = '@PACKAGE_VERSION@'
24
+ ENGINE_NAME = '@ENGINENAME@'
25
+ DEBUG = ('@PLUGIN_DEBUG@'.to_i == 1) rescue false
26
+ PROXY_CACHE = '/tmp/proxy_cache'
27
+
28
+ require 'fileutils'
29
+ require 'net/http'
30
+ require 'net/https'
31
+ require 'uri'
32
+
33
+ # For comparison
34
+ require 'rubygems'
35
+ require 'facter'
36
+ require 'yaml'
37
+
38
+ BANNER = <<'EOS'
39
+
40
+
41
+
42
+ _____
43
+ | ___|__ _ __ ___ _ __ ___ __ _ _ __
44
+ | |_ / _ \| '__/ _ \ '_ ` _ \ / _` | '_ \
45
+ | _| (_) | | | __/ | | | | | (_| | | | |
46
+ |_|__\___/|_| \___|_| |_| |_|\__,_|_| |_|
47
+ | _ \(_)___ ___ _____ _____ _ __ _ _
48
+ | | | | / __|/ __/ _ \ \ / / _ \ '__| | | |
49
+ | |_| | \__ \ (_| (_) \ V / __/ | | |_| |
50
+ |____/|_|___/\___\___/ \_/ \___|_| \__, |
51
+ |___/
52
+
53
+
54
+ EOS
55
+
56
+ $start_time = Time.now
57
+ def time_prefix
58
+ Time.at(Time.now - $start_time).strftime("[%_4s]")
59
+ rescue
60
+ "[ ? ]"
61
+ end
62
+
63
+ def println msg
64
+ puts "#{time_prefix} #{msg}\n"
65
+ end
66
+
67
+ def debug msg
68
+ if DEBUG
69
+ println msg
70
+ end
71
+ end
72
+
73
+ def error msg
74
+ $stderr.puts "#{time_prefix} #{msg}"
75
+ end
76
+
77
+ def cmdline
78
+ return File.open("/proc/cmdline", 'r') { |f| f.read }
79
+ end
80
+
81
+ def discover_server
82
+ debug "Parsing kernel line: #{cmdline}"
83
+ server = (discover_by_pxe or discover_by_dns)
84
+ unless server =~ /^http/
85
+ server = "http://#{server}"
86
+ end
87
+ server
88
+ end
89
+
90
+ def discover_by_pxe
91
+ begin
92
+ server_ip = cmdline.split.map { |x| $1 if x.match(/foreman.ip=(.*)/)}.compact
93
+ if server_ip.size == 1
94
+ result = server_ip.join
95
+ println "Discovered by PXE: #{result}"
96
+ return result
97
+ else
98
+ return false
99
+ end
100
+ rescue
101
+ return false
102
+ end
103
+ end
104
+
105
+ def discover_by_dns
106
+ begin
107
+ server_name = cmdline.split.map { |x| $1 if x.match(/foreman.server=(.*)/)}.compact
108
+ server_name = server_name.size == 1 ? server_name.join : 'foreman'
109
+
110
+ require 'socket'
111
+ result = TCPSocket.gethostbyname(server_name)[3..-1].first || false
112
+ println "Discovered by DNS: #{result}"
113
+ return result
114
+ rescue
115
+ return false
116
+ end
117
+ end
118
+
119
+ def upload
120
+ ip = Facter.value('ipaddress')
121
+ server = discover_server
122
+ println "Triggering import of facts from Foreman (#{server}, ip=#{ip})"
123
+ data = ip.nil? ? {} : {'ip' => ip}
124
+ begin
125
+ uri = URI.parse(server)
126
+ http = Net::HTTP.new(uri.host, uri.port)
127
+ if uri.scheme == 'https' then
128
+ http.use_ssl = true
129
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
130
+ end
131
+ req = Net::HTTP::Post.new("/discovers")
132
+ req.set_form_data(data)
133
+ response = http.request(req)
134
+ debug "Response from Foreman #{response.code}: #{response.body}"
135
+ if response.code == "200"
136
+ return true
137
+ else
138
+ return false
139
+ end
140
+ rescue Exception => e
141
+ error "Could not send facts to Foreman: #{e}"
142
+ return false
143
+ end
144
+ end
145
+
146
+ def write_cache(data)
147
+ File.open(PROXY_CACHE, 'w') {|f| f.write(data) }
148
+ end
149
+
150
+ def read_cache
151
+ File.read(PROXY_CACHE)
152
+ rescue => e
153
+ "empty cache"
154
+ end
155
+
156
+ # Main
157
+
158
+ $stdout.reopen("/dev/tty1", "w")
159
+ $stderr.reopen("/dev/tty1", "w")
160
+
161
+ # Script was (re)started - delete old data
162
+ File.unlink(PROXY_CACHE) if File.exists?(PROXY_CACHE)
163
+
164
+ puts "\e[H\e[2J"
165
+ println BANNER
166
+ TAG = DEBUG ? 'D' : 'P'
167
+ println "This is Foreman Discovery #{PACKAGE_VERSION} (#{TAG}), tty1 is reserved for logs. "
168
+ if DEBUG
169
+ println "The image was build with debug support, you can switch over to tty2+ "
170
+ println "and login as root/development."
171
+ end
172
+ println "Some interesting facts about this system:"
173
+ facts = Facter.to_hash.select {|k,v| k =~ /address|hardware|manufacturer|productname|memorytotal/}
174
+ facts.keys.sort.each {|k| println " #{k}: #{facts[k]}"}
175
+ println "\nLogs from discovery services now follows:"
176
+
177
+ # loop, but only upload on changes
178
+ while true do
179
+ sleep DEBUG ? 30 : 60
180
+ uninteresting_facts=/kernel|operatingsystem|osfamily|ruby|path|time|swap|free|filesystem|version|selinux/i
181
+ facts = Facter.to_hash.reject! {|k,v| k =~ uninteresting_facts }
182
+ unless YAML.load(read_cache) == facts
183
+ debug "Fact cache invalid, reloading to foreman"
184
+ write_cache(YAML.dump(facts)) if upload
185
+ end
186
+ end
@@ -0,0 +1,14 @@
1
+ [Unit]
2
+ Description=Discover this host to Foreman
3
+ Wants=basic.target
4
+ After=basic.target network.target nss-lookup.target
5
+
6
+ [Service]
7
+ Type=idle
8
+ ExecStartPre=/usr/sbin/iptables -I INPUT 1 -m state --state NEW -m tcp -p tcp --dport 8443 -j ACCEPT
9
+ ExecStart=/usr/bin/discover-host.rb
10
+ KillMode=process
11
+ Restart=always
12
+
13
+ [Install]
14
+ WantedBy=multi-user.target