Skip to content

Commit

Permalink
added lib/installation/scc_client.rb
Browse files Browse the repository at this point in the history
for connecting to the SCC server
  • Loading branch information
Ladislav Slezak committed Dec 17, 2013
1 parent 19cd17d commit d7b52be
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
4 changes: 3 additions & 1 deletion package/yast2-installation.spec
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Source1: YaST2-Second-Stage.service
Source2: YaST2-Firstboot.service

BuildRequires: docbook-xsl-stylesheets libxslt update-desktop-files yast2-core-devel
BuildRequires: yast2-devtools >= 3.1.10
BuildRequires: yast2-devtools >= 3.1.14
BuildRequires: rubygem-rspec

# xmllint
Expand Down Expand Up @@ -197,6 +197,8 @@ install -m 644 %{SOURCE2} %{buildroot}%{_unitdir}
%dir /usr/share/autoinstall/modules
%dir %{yast_yncludedir}/installation
%{yast_yncludedir}/installation/*
%dir %{yast_libdir}/installation
%{yast_libdir}/installation/*.rb

# agents
%{yast_scrconfdir}/etc_passwd.scr
Expand Down
3 changes: 3 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ ynclude_DATA = \
include/installation/misc.rb \
include/installation/inst_inc_all.rb

ylibdir = "${yast2dir}/lib/installation"
ylib_DATA = lib/installation/scc_client.rb

scrconf_DATA = \
scrconf/cfg_boot.scr \
scrconf/cfg_windowmanager.scr \
Expand Down
76 changes: 76 additions & 0 deletions src/lib/installation/scc_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# encoding: utf-8

require "yast"

require "json"

# HTTP libs
require "uri"
require "net/http"

module Yast

class SccClient

attr_accessor :url, :email, :reg_code

# FIXME: internal testing SCC instance, change to the public production server later
DEFAULT_SCC_URL = "http://10.122.166.25:3000"

MAX_REDIRECTS = 10

def initialize

end

# initial registration via API
# TODO: proxy support? (http://apidock.com/ruby/Net/HTTP)
def announce(location = nil, redirects = MAX_REDIRECTS)
raise RuntimeError, "Reached maximum number of HTTP redirects, aborting" if redirects == 0

scc_url = URI(location || DEFAULT_SCC_URL + "/connect/announce")

http = Net::HTTP.new(scc_url.host, scc_url.port)

if scc_url.scheme.downcase == "https"
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
end

request = Net::HTTP::Post.new(scc_url.request_uri)
# see https://github.com/SUSE/happy-customer/wiki/Connect-API#wiki-sys_create
request["Authorization"] = "Token token=\"#{token}\""
request["Content-Type"] = "application/json"
request["Accept"] = "application/json"

# FIXME
request.body = {
"email" => email,
"hostname" => "Foo",
"hwinfo": {
"sockets": 2,
"graphics": "nvidia"
}
}.to_json

response = http.request(request)

case response
when Net::HTTPSuccess then
if response["content-type"] == "application/json"
result = JSON.parse(response.body)
else
raise RuntimeError, "Unexpected content-type: #{response['content-type']}"
end
when Net::HTTPRedirection then
location = response['location']
Builtins.y2milestone("Redirected to #{location}")
announce(location, redirects - 1)
else
# TODO error handling
response.value
end

end
end
end

0 comments on commit d7b52be

Please sign in to comment.