foreman_discovery 1.0.0.rc1 → 1.0.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,108 @@
1
+ #!/bin/bash
2
+
3
+ ## Remaster TCL for the Discovery Image
4
+ # (from http://wiki.tinycorelinux.net/wiki:remastering)
5
+ #
6
+ # This script will build the boot image. You will need squashfs-tools and
7
+ # advancecomp. Run it as root, or with passwordless sudo, or the password prompts
8
+ # may get lost when the rake task is run.
9
+
10
+ # Setup
11
+ GEMS="facter json_pure rack rack-protection tilt sinatra"
12
+ TGZS="libssl-0.9.8 ruby"
13
+
14
+ TOPDIR=`mktemp -d`
15
+ cd $TOPDIR
16
+
17
+ # Download/Unpack TCL ISO
18
+ if [ -f /tmp/tcl.iso ]; then
19
+ echo "Using cached TCL iso"
20
+ cp /tmp/tcl.iso ./tcl.iso
21
+ else
22
+ wget http://distro.ibiblio.org/tinycorelinux/4.x/x86/release/Core-current.iso -O tcl.iso
23
+ fi
24
+ mkdir loop && mount -oloop tcl.iso loop/
25
+ cp loop/boot/core.gz loop/boot/vmlinuz .
26
+ umount loop && rmdir loop
27
+
28
+ # Modify basic image:
29
+ mkdir extract && cd extract
30
+ zcat $TOPDIR/core.gz | sudo cpio -i -H newc -d
31
+ mkdir opt/gems && cd opt/gems
32
+ for n in $GEMS ; do gem fetch $n ; done
33
+
34
+ # Build the init script
35
+ echo "" >> ../bootlocal.sh
36
+ echo "sleep 20" >> ../bootlocal.sh # network can be slow to come up, and we're not in a rush
37
+ echo "/opt/foreman_startup.rb" >> ../bootlocal.sh
38
+ echo "/opt/discovery_init.sh" >> ../bootlocal.sh
39
+
40
+ # Get the downloader
41
+ wget https://raw.github.com/theforeman/foreman_discovery/master/extra/foreman_startup.rb -O ../foreman_startup.rb
42
+ chmod 755 ../foreman_startup.rb
43
+
44
+ # Build the fallback script
45
+ echo "#!/bin/sh" >> ../discovery_init.sh
46
+ for n in $GEMS ; do echo "gem install -l --no-ri --no-rdoc /opt/gems/`ls *$n-[0-9]*.gem`" >> ../discovery_init.sh ; done
47
+ echo "" >> ../bootlocal.sh
48
+ echo "/usr/share/foreman-proxy/bin/smart-proxy" >> ../discovery_init.sh
49
+ echo "/usr/share/foreman-proxy/bin/discover_host" >> ../discovery_init.sh
50
+ chmod 755 ../discovery_init.sh
51
+
52
+ # Repack
53
+ cd $TOPDIR/extract
54
+ find | cpio -o -H newc | gzip -2 > $TOPDIR/tinycore.gz
55
+ cd $TOPDIR && advdef -z4 tinycore.gz
56
+ rm -rf $TOPDIR/extract
57
+ IMAGES="tinycore.gz "
58
+
59
+ # Convert TGZs to GZs
60
+ for url in $TGZS ; do
61
+ wget http://repo.tinycorelinux.net/4.x/x86/tcz/$url.tcz
62
+ unsquashfs $url.tcz && cd squashfs-root
63
+ find | cpio -o -H newc | gzip -2 > ../$url.gz
64
+ cd .. && advdef -z4 $url.gz
65
+ rm -rf squashfs-root
66
+ IMAGES="$IMAGES $url.gz "
67
+ done
68
+
69
+ # Build Rubygems:
70
+ wget http://production.cf.rubygems.org/rubygems/rubygems-1.8.24.tgz
71
+ mkdir tmp-install-rubygems && cd tmp-install-rubygems
72
+ mkdir rubygems
73
+ tar xvzf ../rubygems-1.8.24.tgz && cd rubygems-1.8.24
74
+ ruby setup.rb --destdir=../rubygems --prefix=/usr/local
75
+ cd ../rubygems
76
+ sed -i 's?#!.*?#!/usr/local/bin/ruby?' ./usr/local/bin/gem
77
+ mkdir ./usr/local/lib/ruby/1.8 -p
78
+ mv ./usr/local/lib/* ./usr/local/lib/ruby/1.8
79
+ chown -R 0:0 .
80
+ find | cpio -o -H newc | gzip -2 > $TOPDIR/rubygems.gz
81
+ cd $TOPDIR && advdef -z4 rubygems.gz
82
+ rm -rf tmp-install-rubygems rubygems-1.8.24.tgz
83
+ IMAGES="$IMAGES rubygems.gz "
84
+
85
+ # Proxy.gz
86
+ mkdir -p ./proxy/usr/share
87
+ mkdir -p ./proxy/var/run/foreman-proxy
88
+ mkdir -p ./proxy/var/log/foreman-proxy
89
+ git clone https://github.com/theforeman/smart-proxy.git ./proxy/usr/share/foreman-proxy
90
+ wget https://raw.github.com/theforeman/foreman_discovery/master/extra/discover_host -O ./proxy/usr/share/foreman-proxy/bin/discover_host
91
+ chmod 755 ./proxy/usr/share/foreman-proxy/bin/discover_host
92
+ cp ./proxy/usr/share/foreman-proxy/config/settings.yml{.example,}
93
+ sed -i 's/.*:bmc:.*/:bmc: true/' ./proxy/usr/share/foreman-proxy/config/settings.yml
94
+ sed -i 's/.*:bmc_default_provider:.*/:bmc_default_provider: shell/' ./proxy/usr/share/foreman-proxy/config/settings.yml
95
+ #Shell interface calls shutdown which doesn't exist in TCL
96
+ mkdir -p ./proxy/sbin
97
+ echo -e "#\x21/bin/sh\nexec /sbin/reboot" > ./proxy/sbin/shutdown
98
+ chmod 755 ./proxy/sbin/shutdown
99
+ cd proxy
100
+ find | cpio -o -H newc | gzip -2 > ../proxy.gz
101
+ cd $TOPDIR && advdef -z4 proxy.gz
102
+ rm -rf proxy
103
+ IMAGES="$IMAGES proxy.gz"
104
+
105
+ # Use Gz chaining:
106
+ cat $IMAGES > initrd.gz
107
+ chmod 755 $TOPDIR
108
+ echo "#TMPDIR# $TOPDIR"
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'fileutils'
4
+ require 'net/http'
5
+ require 'net/https'
6
+ require 'uri'
7
+ require 'yaml'
8
+
9
+ # Would like to not need this...
10
+ require 'rubygems'
11
+ require 'json'
12
+
13
+ def discover_server
14
+ server = (discover_by_pxe or discover_by_dns)
15
+ unless server =~ /^http/
16
+ server = "http://#{server}"
17
+ end
18
+ server
19
+ end
20
+
21
+ def discover_by_pxe
22
+ begin
23
+ contents = File.open("/proc/cmdline", 'r') { |f| f.read }
24
+ server_ip = contents.split.map { |x| $1 if x.match(/foreman.ip=(.*)/)}.compact
25
+ if server_ip.size == 1
26
+ return server_ip.join
27
+ else
28
+ return false
29
+ end
30
+ rescue
31
+ return false
32
+ end
33
+ end
34
+
35
+ def discover_by_dns
36
+ begin
37
+ contents = File.open("/proc/cmdline", 'r') { |f| f.read }
38
+ server_name = contents.split.map { |x| $1 if x.match(/foreman.server=(.*)/)}.compact
39
+ server_name = server_name.size == 1 ? server_name.join : 'foreman'
40
+
41
+ require 'socket'
42
+ return TCPSocket.gethostbyname(server_name)[3..-1].first || false
43
+ rescue
44
+ return false
45
+ end
46
+ end
47
+
48
+ # Main
49
+
50
+ $stdout.reopen("/tmp/proxy-discovery.log", "w")
51
+ $stderr.reopen("/tmp/proxy-discovery.err", "w")
52
+
53
+ # Get facts from the Proxy
54
+ puts "retrieving facts"
55
+ begin
56
+ uri = URI.parse("http://localhost:8443/facts")
57
+ http = Net::HTTP.new(uri.host, uri.port)
58
+ response = http.request(Net::HTTP::Get.new(uri.request_uri))
59
+ # Ugly
60
+ facts = YAML.dump(JSON.parse(response.body))
61
+ rescue Exception => e
62
+ raise "Could not get facts from Proxy: #{e}"
63
+ end
64
+
65
+ puts "Importing facts"
66
+ begin
67
+ uri = URI.parse(discover_server)
68
+ http = Net::HTTP.new(uri.host, uri.port)
69
+ if uri.scheme == 'https' then
70
+ http.use_ssl = true
71
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
72
+ end
73
+ req = Net::HTTP::Post.new("/fact_values/create?type=Host::Discovered")
74
+ req.set_form_data({'facts' => facts})
75
+ response = http.request(req)
76
+ puts response.body
77
+ rescue Exception => e
78
+ raise "Could not send facts to Foreman: #{e}"
79
+ end
@@ -0,0 +1,11 @@
1
+ #!/bin/sh
2
+
3
+ echo "Starting custom discovery script"
4
+
5
+ GEMS="facter json_pure rack rack-protection tilt sinatra"
6
+ for n in $GEMS ; do gem install --no-ri --no-rdoc -l `ls /opt/gems/*$n-[0-9]*.gem` ; done
7
+
8
+ /usr/share/foreman-proxy/bin/smart-proxy
9
+ /usr/share/foreman-proxy/bin/discover_host
10
+
11
+ echo "Done with custom discovery script"
@@ -0,0 +1,33 @@
1
+ disk_types = ["xvd","vd","sd","hd"]
2
+ total_size = 0
3
+ total_disks = 0
4
+
5
+ disk_types.each do |type|
6
+ Dir.open("/sys/block").entries.grep(/^#{type}/).each do |d|
7
+ size = %x{/bin/cat /sys/block/#{d}/size}.chomp
8
+ total_size += size.to_i
9
+ total_disks += 1
10
+ Facter.add("disk_size_#{d}".to_sym) do
11
+ setcode do
12
+ size
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ Facter.add(:disk_total_size) do
19
+ setcode do
20
+ total_size
21
+ end
22
+ end
23
+
24
+ Facter.add(:disk_total_number) do
25
+ setcode do
26
+ total_disks
27
+ end
28
+ end
29
+
30
+ #! /usr/bin/env ruby
31
+ %x{fdisk -l 2>/dev/null}.split("\n").each do |l|
32
+ puts $1 if l.match(/^Disk \/dev\/(?:(?:(?:sd|hd|x?vd)[a-z])|mmcblk[0-9]+).*?(\d+) bytes/)
33
+ end
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'fileutils'
4
+ require 'net/http'
5
+ require 'net/https'
6
+ require 'uri'
7
+
8
+ def discover_server
9
+ server = (discover_by_pxe or discover_by_dns)
10
+ unless server =~ /^http/
11
+ server = "http://#{server}"
12
+ end
13
+ server
14
+ end
15
+
16
+ def discover_by_pxe
17
+ begin
18
+ contents = File.open("/proc/cmdline", 'r') { |f| f.read }
19
+ server_ip = contents.split.map { |x| $1 if x.match(/foreman.ip=(.*)/)}.compact
20
+ if server_ip.size == 1
21
+ return server_ip.join
22
+ else
23
+ return false
24
+ end
25
+ rescue
26
+ return false
27
+ end
28
+ end
29
+
30
+ def discover_by_dns
31
+ begin
32
+ contents = File.open("/proc/cmdline", 'r') { |f| f.read }
33
+ server_name = contents.split.map { |x| $1 if x.match(/foreman.server=(.*)/)}.compact
34
+ server_name = server_name.size == 1 ? server_name.join : 'razor'
35
+
36
+ require 'socket'
37
+ return TCPSocket.gethostbyname(server_name)[3..-1].first || false
38
+ rescue
39
+ return false
40
+ end
41
+ end
42
+
43
+ begin
44
+ uri = URI.parse(discover_server+'/discovery_init.sh')
45
+ http = Net::HTTP.new(uri.host, uri.port)
46
+ if uri.scheme == 'https' then
47
+ http.use_ssl = true
48
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
49
+ end
50
+ response = http.request(Net::HTTP::Get.new(uri.request_uri))
51
+ if response.code == "200"
52
+ open("/opt/discovery_init.sh", "wb") do |file|
53
+ file.write(response.body)
54
+ end
55
+ `chmod 755 /opt/discovery_init.sh`
56
+ end
57
+ rescue Exception => e
58
+ puts e.message
59
+ puts e.backtrace
60
+ end
@@ -1,3 +1,3 @@
1
1
  module ForemanDiscovery
2
- VERSION = "1.0.0.rc1"
2
+ VERSION = "1.0.0.rc2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_discovery
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.rc1
4
+ version: 1.0.0.rc2
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -96,6 +96,11 @@ files:
96
96
  - app/views/discovers/show.html.erb
97
97
  - app/views/discovers/select_multiple_organization.html.erb
98
98
  - app/controllers/discovers_controller.rb
99
+ - extra/build_iso.sh
100
+ - extra/discovery_init.sh.example
101
+ - extra/discover_host
102
+ - extra/foreman_startup.rb
103
+ - extra/disk_facts.rb
99
104
  - config/initializers/discovery_setup.rb
100
105
  - config/environment.rb
101
106
  - config/routes.rb