diff options
| author | Carlos Konstanski <ckonstanski@pippiandcarlos.com> | 2017-08-30 13:09:56 -0600 |
|---|---|---|
| committer | Carlos Konstanski <ckonstanski@pippiandcarlos.com> | 2017-08-30 13:09:56 -0600 |
| commit | 649f0dd9c5ca586d8b637400dd19c0c4059447d6 (patch) | |
| tree | 5ccc992a3aa5de1bcb510911dfdb21e6a54bd197 /bootstrap | |
initial commit
Diffstat (limited to 'bootstrap')
| -rw-r--r-- | bootstrap/.gitignore | 1 | ||||
| -rw-r--r-- | bootstrap/config.yaml.example | 8 | ||||
| -rw-r--r-- | bootstrap/http_client.rb | 90 | ||||
| -rwxr-xr-x | bootstrap/pillar_config.rb | 84 | ||||
| -rwxr-xr-x | bootstrap/run_salt.sh | 4 |
5 files changed, 187 insertions, 0 deletions
diff --git a/bootstrap/.gitignore b/bootstrap/.gitignore new file mode 100644 index 0000000..5b6b072 --- /dev/null +++ b/bootstrap/.gitignore @@ -0,0 +1 @@ +config.yaml diff --git a/bootstrap/config.yaml.example b/bootstrap/config.yaml.example new file mode 100644 index 0000000..ffadfd0 --- /dev/null +++ b/bootstrap/config.yaml.example @@ -0,0 +1,8 @@ +registry: + url: ~ + username: ~ + password: ~ +jars: + url: ~ + username: ~ + password: ~ diff --git a/bootstrap/http_client.rb b/bootstrap/http_client.rb new file mode 100644 index 0000000..58eea23 --- /dev/null +++ b/bootstrap/http_client.rb @@ -0,0 +1,90 @@ +require 'json' +require 'net/http' +require 'net/https' +require 'uri' +require 'openssl' + +class HttpClient + attr_accessor :uri + attr_accessor :header + attr_accessor :cookies + attr_accessor :body + attr_accessor :response + attr_accessor :response_code + + @@req_xml = <<EOF +<?xml version="1.0"?> +<a:propfind xmlns:a="DAV:"> +<a:prop><a:resourcetype/></a:prop> +</a:propfind> +EOF + + def initialize(url: nil, username: nil, password: nil, headers: Hash.new, cookies: Hash.new, body: nil) + @uri = URI.parse(url) + @username = username + @password = password + @headers = headers + @cookies = cookies + @body = body + end + + def do_http(method: :get, do_cookies_p: false, read_timeout: 600, extra_headers: Hash.new) + resp = nil + http = Net::HTTP.new(@uri.host, @uri.port) + if uri.scheme == 'https' + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + end + http.read_timeout = read_timeout + req = case method + when :get + Net::HTTP::Get.new(uri.request_uri, @headers.merge(extra_headers)) + when :delete + Net::HTTP::Delete.new(uri.request_uri, @headers.merge(extra_headers)) + when :post + Net::HTTP::Post.new(uri.request_uri, @headers.merge(extra_headers)) + when :put + Net::HTTP::Put.new(uri.request_uri, @headers.merge(extra_headers)) + when :propfind + Net::HTTP::Propfind.new(uri.request_uri, {"Depth" => "1"}) + end + req.basic_auth(@username, @password) if @username and @password + if method == :propfind + req.body = @@req_xml + else + req.body = @body if @body + end + resp = http.request(req) + @response = resp.body + @response_code = resp.class + get_cookies(resp) if do_cookies_p + end + + # Below is a pair of example Set-Cookie headers after + # Net::HttpClient turns them into a concatenated string. Note the + # bad use of a comma to separate the cookies. It's bad because a + # comma already appears in the date. We deal with that by eating + # the date with gsub() before splitting. + # + # csrftoken=urvsX10TfzWIK1fgctZ2HjHuNQZDr3E7; expires=Wed, 16-Nov-2016 16:57:20 GMT; Max-Age=31449600; Path=/, sessionid=e0t7304jn44ssc7gw412abup4aasehgb; expires=Wed, 18-Nov-2015 17:57:20 GMT; httponly; Max-Age=3600; Path=/ + # + # rememberMe=deleteMe; Path=/Openbook; Max-Age=0; Expires=Wed, 16-Dec-2015 22:53:58 GMT + def get_cookies(resp) + if resp.response['Set-Cookie'] + rawstring = resp.response['Set-Cookie'].gsub(/expires.*?Path=/, '') + rawstring.chomp.split(",").each do |entry| + entryparts = entry.split(";") + if entryparts.length >= 1 + cookieparts = entryparts[0].split("=") + name = cookieparts[0].strip + value = cookieparts[1].strip + @cookies[name] = value + end + end + end + end + + def create_cookie(name, value) + "#{name}=#{value}; Path=/" + end +end diff --git a/bootstrap/pillar_config.rb b/bootstrap/pillar_config.rb new file mode 100755 index 0000000..60460b1 --- /dev/null +++ b/bootstrap/pillar_config.rb @@ -0,0 +1,84 @@ +#!/usr/bin/ruby + +require_relative 'http_client' +require 'digest' +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) + 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.*\.jar/ + jar_name = line.chomp.split(/[<>]/)[2] + jarparts = jar_name.split(/\//) + name, *version_parts, junk1 = jarparts[jarparts.length - 1].split(".") + ret["#{name.gsub("-", "_")}"] = { + "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("-", "_")}" + } + 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["baseimage"] = {} + yml["database"] = {} + yml["baseimage"]["image_name"] = "openbook_baseimage" + yml["baseimage"]["container_name"] = "baseimage" + yml["baseimage"]["build_dir"] = "/root/openbook_build/baseimage" + 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() + yml = process_yaml() + puts yml.merge(get_jar_versions(yml)).to_json +end + +main() +exit(0) diff --git a/bootstrap/run_salt.sh b/bootstrap/run_salt.sh new file mode 100755 index 0000000..869b52b --- /dev/null +++ b/bootstrap/run_salt.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +salt '*' state.highstate pillar="$($(dirname ${0})/pillar_config.rb)" +exit 0 |
