nitos_testbed_rc 1.0.0.pre.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
data/README.md ADDED
@@ -0,0 +1,16 @@
1
+ Nitos Testbed resource controllers
2
+ =================
3
+
4
+ Contains:
5
+
6
+ - Frisbee resource controller which conrols frisbee and imagezip in order to
7
+ save and load images to nodes.
8
+
9
+ - CM resource controller which controls chassis managers on nodes.
10
+
11
+ - User resource controller which administers users.
12
+
13
+ - om6 script which orchistrates the above.
14
+
15
+ These tools are under development. Unpredictable behaviour is to be expected untill
16
+ a stable version is provided.
data/bin/cm_proxy ADDED
@@ -0,0 +1,146 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'omf_rc'
4
+ require 'omf_common'
5
+ require 'yaml'
6
+
7
+ $stdout.sync = true
8
+
9
+ @config = YAML.load_file('/etc/nitos_testbed_rc/cm_proxy_conf.yaml')
10
+ # @config = YAML.load_file(File.join(File.dirname(File.expand_path(__FILE__)), '../etc/cm_proxy_conf.yaml'))
11
+ @auth = @config[:auth]
12
+ @xmpp = @config[:xmpp]
13
+
14
+ require 'nitos_testbed_rc/cm_factory'
15
+
16
+ cm_entity_cert = File.expand_path(@auth[:entity_cert])
17
+ cm_entity_key = File.expand_path(@auth[:entity_key])
18
+ cm_entity = OmfCommon::Auth::Certificate.create_from_pem(File.read(cm_entity_cert))#, File.read(cm_entity_key))
19
+
20
+ trusted_roots = File.expand_path(@auth[:root_cert_dir])
21
+
22
+ opts = {
23
+ communication: {
24
+ url: "xmpp://#{@xmpp[:username]}:#{@xmpp[:password]}@#{@xmpp[:server]}",
25
+ auth: {
26
+ authenticate: true,
27
+ pdp: {
28
+ constructor: 'CmPDP'
29
+ }
30
+ }
31
+ }
32
+ }
33
+
34
+ class CmPDP
35
+ def initialize(opts = {})
36
+ debug "AUTH INIT>>> #{opts}"
37
+ @config = YAML.load_file('/etc/nitos_testbed_rc/cm_proxy_conf.yaml')
38
+ # @config = YAML.load_file(File.join(File.dirname(File.expand_path(__FILE__)), '../etc/cm_proxy_conf.yaml'))
39
+ end
40
+
41
+ def authorize(msg, &block)
42
+ debug "AUTH message received: #{msg.operation}"
43
+ if msg.operation.to_sym == :configure
44
+ wait = true
45
+ result = nil
46
+ OmfCommon.comm.subscribe(@config[:testbedTopic]) do |am_con|
47
+ acc = _get_account_name(msg)
48
+
49
+ if acc.nil?
50
+ error "AUTH error: acc nill"
51
+ msg.properties.state.error_msg = "Account name not found"
52
+ result = msg
53
+ wait = false
54
+ next
55
+ end
56
+
57
+ node_name = msg.properties.state.node
58
+ am_con.request([:nodes]) do |n_msg|
59
+ nodes = n_msg.read_property("nodes")[:resources]
60
+ node = nil
61
+ nodes.each do |n|
62
+ if n[:resource][:name].to_s == node_name.to_s
63
+ node = n
64
+ break
65
+ end
66
+ end
67
+
68
+ lease = nil
69
+ if node.nil?
70
+ error "AUTH error: Node nill"
71
+ msg.properties.state.error_msg = "Wrong node name."
72
+ result = msg
73
+ wait = false
74
+ next
75
+ else
76
+ am_con.request([:leases]) do |l_msg|
77
+ leases = l_msg.read_property("leases")[:resources]
78
+ leases.each do |l|
79
+ if Time.parse(l[:resource][:valid_from]) <= Time.now && Time.parse(l[:resource][:valid_until]) >= Time.now
80
+ l[:resource][:components].each do |c|
81
+ if c[:component][:name] == node_name.to_s && l[:resource][:account][:name] == acc
82
+ lease = l
83
+ break #found the correct lease
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+ if lease.nil? #if lease is nil it means no matching lease is found
90
+ error "AUTH error: Lease nill"
91
+ msg.properties.state.error_msg = "Node is not leased by your account."
92
+ result = msg
93
+ wait = false
94
+ next
95
+ else
96
+ debug "AUTH PASSED"
97
+ msg.properties.state.node = node
98
+ result = msg
99
+ wait = false
100
+ next
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+
107
+ #waiting for the whole process to be completed
108
+ while wait
109
+ sleep 1
110
+ end
111
+
112
+ return result if result
113
+ else
114
+ debug "AUTH PASSED"
115
+ return msg
116
+ end
117
+ # msg
118
+ end
119
+
120
+ private
121
+ def _get_account_name(msg)
122
+ #subject is ~ /C=US/ST=CA/O=ACME/OU=Roadrunner/CN=37a96f60-c53d-50d9-bbbf-3c552b89bdc5/emailAddress=root@nitlab.inf.uth.gr
123
+ subj = msg.issuer.subject.to_s
124
+ subj.gsub!(/.*CN=/, '')
125
+ subj.gsub!(/.*emailAddress=/, '')
126
+ subj.gsub!(/@.*/, '')
127
+ debug "AUTH user: #{subj}"
128
+ return subj
129
+ end
130
+ end
131
+
132
+ OmfCommon.init(@config[:operationMode], opts) do |el|#communication: { url: "xmpp://#{@xmpp[:proxy_user]}:#{@xmpp[:password]}@#{@xmpp[:server]}", auth: {} }) do
133
+ OmfCommon.comm.on_connected do |comm|
134
+ OmfCommon::Auth::CertificateStore.instance.register_default_certs(trusted_roots)
135
+ cm_entity.resource_id = OmfCommon.comm.local_topic.address
136
+ OmfCommon::Auth::CertificateStore.instance.register(cm_entity)
137
+
138
+ info "CM Factory >> Connected to XMPP server"
139
+ cmFact = OmfRc::ResourceFactory.create(:cm_factory, { uid: 'cm_factory', certificate: cm_entity })
140
+
141
+ comm.on_interrupted {
142
+ cmFact.disconnect
143
+ }
144
+ end
145
+ end
146
+
data/bin/frisbee_proxy ADDED
@@ -0,0 +1,155 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'omf_rc'
4
+ require 'omf_common'
5
+ require 'yaml'
6
+
7
+ $stdout.sync = true
8
+ @config = YAML.load_file('/etc/nitos_testbed_rc/frisbee_proxy_conf.yaml')
9
+ # @config = YAML.load_file(File.join(File.dirname(File.expand_path(__FILE__)), '.../etc/frisbee_proxy_conf.yaml'))
10
+ @auth = @config[:auth]
11
+ @xmpp = @config[:xmpp]
12
+
13
+ require 'nitos_testbed_rc/frisbee'
14
+ require 'nitos_testbed_rc/frisbeed'
15
+ require 'nitos_testbed_rc/imagezip_server'
16
+ require 'nitos_testbed_rc/imagezip_client'
17
+ require 'nitos_testbed_rc/frisbee_factory'
18
+
19
+ frisbee_entity_cert = File.expand_path(@auth[:entity_cert])
20
+ frisbee_entity_key = File.expand_path(@auth[:entity_key])
21
+ frisbee_entity = OmfCommon::Auth::Certificate.create_from_pem(File.read(frisbee_entity_cert))#, File.read(frisbee_entity_key))
22
+
23
+ trusted_roots = File.expand_path(@auth[:root_cert_dir])
24
+
25
+ opts = {
26
+ communication: {
27
+ url: "xmpp://#{@xmpp[:username]}:#{@xmpp[:password]}@#{@xmpp[:server]}",
28
+ auth: {
29
+ authenticate: true,
30
+ pdp: {
31
+ constructor: 'FrisbeePDP'
32
+ }
33
+ }
34
+ }
35
+ }
36
+
37
+ class FrisbeePDP
38
+ def initialize(opts = {})
39
+ debug "AUTH INIT>>> #{opts}"
40
+ @config = YAML.load_file('/etc/nitos_testbed_rc/frisbee_proxy_conf.yaml')
41
+ # @config = YAML.load_file(File.join(File.dirname(File.expand_path(__FILE__)), '.../etc/frisbee_proxy_conf.yaml'))
42
+ end
43
+
44
+ def authorize(msg, &block)
45
+ debug "AUTH message received: #{msg.operation}"
46
+ if msg.operation.to_sym == :create
47
+ if msg.rtype.to_sym == :frisbee || msg.rtype.to_sym == :imagezip_client
48
+ wait = true
49
+ result = nil
50
+ OmfCommon.comm.subscribe(@config[:testbedTopic]) do |am_con|
51
+ acc = _get_account_name(msg)
52
+
53
+ if acc.nil?
54
+ error "AUTH error: acc nill"
55
+ msg.propertie.error_msg = "Account name not found"
56
+ result = msg
57
+ wait = false
58
+ next
59
+ end
60
+
61
+ node_name = msg.properties.node_topic
62
+ am_con.request([:nodes]) do |n_msg|
63
+ nodes = n_msg.read_property("nodes")[:resources]
64
+ node = nil
65
+ nodes.each do |n|
66
+ if n[:resource][:name].to_s == node_name.to_s
67
+ node = n
68
+ break
69
+ end
70
+ end
71
+
72
+ lease = nil
73
+ if node.nil?
74
+ error "AUTH error: Node nill"
75
+ msg.properties.error_msg = "Wrong node name."
76
+ result = msg
77
+ wait = false
78
+ next
79
+ else
80
+ am_con.request([:leases]) do |l_msg|
81
+ leases = l_msg.read_property("leases")[:resources]
82
+ leases.each do |l|
83
+ if Time.parse(l[:resource][:valid_from]) <= Time.now && Time.parse(l[:resource][:valid_until]) >= Time.now
84
+ l[:resource][:components].each do |c|
85
+ if c[:component][:name] == node_name.to_s && l[:resource][:account][:name] == acc
86
+ lease = l
87
+ break #found the correct lease
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ if lease.nil? #if lease is nil it means no matching lease is found
94
+ error "AUTH error: Lease nill"
95
+ msg.properties.error_msg = "Node is not leased by your account."
96
+ result = msg
97
+ wait = false
98
+ next
99
+ else
100
+ debug "AUTH PASSED"
101
+ msg.properties.node = node
102
+ result = msg
103
+ wait = false
104
+ next
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+ #waiting for the whole process to be completed
112
+ while wait
113
+ sleep 1
114
+ end
115
+
116
+ return result if result
117
+ else
118
+ debug "AUTH PASSED"
119
+ return msg
120
+ end
121
+
122
+ else
123
+ debug "AUTH PASSED"
124
+ return msg
125
+ end
126
+ # msg
127
+ end
128
+
129
+ private
130
+ def _get_account_name(msg)
131
+ #subject is ~ /C=US/ST=CA/O=ACME/OU=Roadrunner/CN=37a96f60-c53d-50d9-bbbf-3c552b89bdc5/emailAddress=root@nitlab.inf.uth.gr
132
+ subj = msg.issuer.subject.to_s
133
+ subj.gsub!(/.*CN=/, '')
134
+ subj.gsub!(/.*emailAddress=/, '')
135
+ subj.gsub!(/@.*/, '')
136
+ debug "AUTH user: #{subj}"
137
+ return subj
138
+ end
139
+ end
140
+
141
+ OmfCommon.init(@config[:operationMode], opts) do |el|#communication: { url: "xmpp://#{@xmpp[:proxy_user]}:#{@xmpp[:password]}@#{@xmpp[:server]}", auth: {} }) do
142
+ OmfCommon.comm.on_connected do |comm|
143
+ OmfCommon::Auth::CertificateStore.instance.register_default_certs(trusted_roots)
144
+ frisbee_entity.resource_id = OmfCommon.comm.local_topic.address
145
+ OmfCommon::Auth::CertificateStore.instance.register(frisbee_entity)
146
+ info "Frisbee Factory >> Connected to XMPP server"
147
+
148
+ frisbeeFact = OmfRc::ResourceFactory.create(:frisbee_factory, { uid: 'frisbee_factory', certificate: frisbee_entity })
149
+
150
+ comm.on_interrupted {
151
+ frisbeeFact.disconnect
152
+ }
153
+ end
154
+ end
155
+
data/bin/install_ntrc ADDED
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+ require 'fileutils'
3
+
4
+ spec = Gem::Specification.find_by_name("nitos_testbed_rc")
5
+ gem_root = spec.gem_dir
6
+ config_path = "/etc/nitos_testbed_rc"
7
+
8
+ puts "Copying configuration files from '#{gem_root}'."
9
+
10
+ unless File.directory?(config_path)
11
+ puts "Generating directory '#{config_path}'."
12
+ FileUtils.mkdir_p(config_path)
13
+ end
14
+
15
+ conf_files = []
16
+ conf_files << "cm_proxy_conf.yaml"
17
+ conf_files << "frisbee_proxy_conf.yaml"
18
+ conf_files << "user_proxy_conf.yaml"
19
+ conf_files << "omf_script_conf.yaml"
20
+
21
+ conf_files.each do |file|
22
+ puts "Corying configuration file '#{gem_root}/etc/#{file}' to '#{config_path}'."
23
+ FileUtils.cp "#{gem_root}/etc/#{file}", "#{config_path}/#{file}"
24
+ FileUtils.chmod 0644, "#{config_path}/#{file}"
25
+ end
26
+ puts "done."