summaryrefslogtreecommitdiff
path: root/bootstrap/pillar_config.rb
blob: 56bdb3dd11286a64e7240d7f65e54f463c9b4c17 (plain)
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
#!/usr/bin/ruby

require_relative 'http_client'
require 'digest'
require 'getoptlong'
require "json"
require 'socket'
require "yaml"

def sha()
    seed = ""
    File.open("/dev/urandom", "r") do |f|
        seed = "#{f.sysread(1024)}"
    end
    Digest::SHA256.hexdigest(seed)
end

def get_ip_address()
    ip_arr = Socket.ip_address_list.map { |addr|
        addr.ip_address
    }.reject { |addr|
        addr == "172.17.0.1" or addr.start_with?("127") or addr.include?("::")
    }
    if ip_arr.length == nil
        "127.0.0.1"
    else
        ip_arr[0]
    end
end

def get_jar_versions(yml, branch)
    ret = Hash.new
    begin
        http_client = HttpClient.new(url: yml["jars"]["url"],
                                     username: yml["jars"]["username"],
                                     password: yml["jars"]["password"])
        http_client.do_http(method: :propfind)
        http_client.response.each_line do |line|
            if line =~ /D:href.*#{branch}.*\.jar/
                jar_name = line.chomp.split(/[<>]/)[2]
                jarparts = jar_name.split(/\//)
                name, *version_parts, junk1 = jarparts[jarparts.length - 1].split(".")
                ret["#{name.gsub("-", "_").gsub("_#{branch}", "")}"] = {
                    "jar_name" => jarparts[jarparts.length - 1],
                    "jar_version" => version_parts.join("."),
                    "image_name" => "openbook_#{name.gsub("-", "_")}",
                    "container_name" => "#{name.gsub("-", "_")}",
                    "build_dir" => "/root/openbook_build/#{name.gsub("-", "_").gsub("_#{branch}", "")}"
                }
            end
        end
    rescue Exception => e
        STDERR.puts "Error while contacting webdav endpoint: #{e.message}"
        exit(1)
    end
    ret
end

def process_yaml()
    env_yaml_path = "#{File.dirname($0)}/config.yaml"
    if ! File.file?(env_yaml_path)
        STDERR.puts "Unable to find config.yaml. Exiting."
        exit(1)
    end
    yml = YAML.load_file(env_yaml_path)
    yml["database"] = {}
    yml["database"]["image_name"] = "openbook_database"
    yml["database"]["container_name"] = "database"
    yml["database"]["build_dir"] = "/root/openbook_build/database"
    yml["ansible"] = Hash.new
    yml["ansible"]["build_dir"] = "/root/openbook_build/ansible"
    yml
end

def main()
    branch = ""
    opts = GetoptLong.new(["--branch", "-b", GetoptLong::REQUIRED_ARGUMENT])

    opts.each do |opt, arg|
        case opt
        when "--branch"
            branch = arg
		end
	end

    yml = process_yaml()
    puts yml.merge(get_jar_versions(yml, branch)).to_json
end

main()
exit(0)