Class: Nuri::Master

Inherits:
Object
  • Object
show all
Includes:
Choreographer, Helper, Orchestrator
Defined in:
lib/nuri/master.rb

Defined Under Namespace

Modules: Model Classes: GoalGenerator, VMProcedureModifier

Constant Summary collapse

SfpUnknown =
Sfp::Unknown.new
SfpUndefined =
Sfp::Undefined.new
AgentSchema =
'$.Node'
CloudSchema =
'$.Cloud'
VMSchema =
'$.VM'
InstallModule =
File.dirname(__FILE__) + '/../../bin/nuri-install-module'

Constants included from Helper

Helper::DefaultHTTPOpenTimeout, Helper::DefaultHTTPReadTimeout

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Choreographer

#deploy_bsig, #get_bsig, #postprocess, #purge_bsig, #push_cache_model

Methods included from Helper

#delete_data, #get_data, #post_data, #put_data

Methods included from Orchestrator

#assign_action_with_id, #execute_plan, #next_thread_id, #thread_execute_action

Constructor Details

#initialize(p = {}) ⇒ Master

Returns a new instance of Master.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/nuri/master.rb', line 19

def initialize(p={})
	@mutex_vm_updater = Mutex.new
	@cloudfinder = Sfp::Helper::CloudFinder.new
	@local_agent = nil

	# set modules directory
	if p[:modules_dir] and File.directory?(p[:modules_dir])
		@modules_dir = File.expand_path(p[:modules_dir])
	elsif ENV['NURI_HOME'].is_a?(String) and ENV['NURI_HOME'].strip.length > 0
		@modules_dir = File.join(ENV['NURI_HOME'], 'modules')
	elsif File.directory?(File.expand_path(File.dirname(__FILE__) + '/../../modules'))
		@modules_dir = File.expand_path(File.dirname(__FILE__) + '/../../modules')
	elsif File.directory?(File.expand_path('./modules'))
		@modules_dir = File.expand_path('./modules')
	else
		@modules_dir = '/var/nuri/modules'
	end
	fail "Invalid modules directory #{@modules_dir}!" if !File.directory?(@modules_dir)

	@sas_post_processor = SASPostProcessor

	set_model(p)
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



17
18
19
# File 'lib/nuri/master.rb', line 17

def model
  @model
end

Instance Method Details

#get_plan(p = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/nuri/master.rb', line 62

def get_plan(p={})
	# set parameters value to be given to the planner
	p[:sfp] = create_plan_task(p)
	p[:sas_post_processor] = SASPostProcessor

	print "Planning "

	plan = nil
	planning_time = Benchmark.measure do
		planner = Sfp::Planner.new
		plan = planner.solve(p)
	end

	print (p[:color] ? "[Finish] ".green : "[Finish] ")
	puts format_benchmark(planning_time)

	plan
end

#get_state(p = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/nuri/master.rb', line 81

def get_state(p={})
	state = {}
	vms = get_vms
	agents = get_agents

	# push agents list to each agent
	push_agents_list

	mutex = Mutex.new

	# get state of non-VM nodes
	(agents.keys - vms.keys).each do |name|
		Thread.new {
			node_name = name
			node_state = get_node_state(node_name, !!p[:push_modules])
			mutex.synchronize { state[node_name] = node_state }
			state[node_name] = node_state
		}
	end
	total = agents.keys.length - vms.keys.length

	# wait until all threads have finish
	wait? { (state.length >= total) }

	# assign VMs' address
	exist_vms, not_exist_vms = update_vms_address(state)

	# get state of existing VM nodes
	exist_vms.each_key { |name|
		Thread.new {
			node_name = name
			node_state = get_node_state(node_name, !!p[:push_modules])
			mutex.synchronize { state[node_name] = node_state }
			state[node_name] = node_state
		}
	}

	# get state of non-existing VM nodes
	not_exist_vms.each { |name,model|
		state[name] = get_not_exist_vm_state(model)
	}

	# wait until all threads have finish
	wait? { (state.length >= agents.length) }

	# update <vm>.in_cloud value
	update_cloud_vm_relations(state, vms)

	agents.merge!(exist_vms)
	push_agents_list(agents, {:reset => true})

	state
end

#set_model(p = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/nuri/master.rb', line 43

def set_model(p={})
	if p[:model_file]
		home_dir = File.expand_path File.dirname(p[:model_file])
		@parser = Sfp::Parser.new({:home_dir => home_dir})
		@parser.parse File.read(p[:model_file])
		p[:model] = @parser.root
	end

	@model = (p.is_a?(Hash) and p[:model].is_a?(Hash) ? p[:model] : {})
	push_agents_list if @model.length > 0

	# find a list of cloud proxy
	@model.accept(@cloudfinder.reset)

	# create a set of not-exist VMs' state
	@map = generate_not_exist_vm_state(false)
	SASPostProcessor.set_map(@map)
end