Class: Morpheus::Cli::Remote

Inherits:
Object
  • Object
show all
Includes:
Term::ANSIColor
Defined in:
lib/morpheus/cli/remote.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRemote

Returns a new instance of Remote.



10
11
12
# File 'lib/morpheus/cli/remote.rb', line 10

def initialize() 
	@appliances = ::Morpheus::Cli::Remote.load_appliance_file
end

Class Method Details

.active_applianceObject

Provides the current active appliance url information



130
131
132
133
134
135
# File 'lib/morpheus/cli/remote.rb', line 130

def self.active_appliance
	if !defined?(@@appliance) || @@appliance.nil?
		@@appliance = load_appliance_file.select { |k,v| v[:active] == true}
	end
	return @@appliance.keys[0], @@appliance[@@appliance.keys[0]][:host]
end

.appliances_file_pathObject



153
154
155
156
157
158
159
160
# File 'lib/morpheus/cli/remote.rb', line 153

def self.appliances_file_path
	home_dir = Dir.home
	morpheus_dir = File.join(home_dir,".morpheus")
	if !Dir.exist?(morpheus_dir)
		Dir.mkdir(morpheus_dir)
	end
	return File.join(morpheus_dir,"appliances")
end

.load_appliance_fileObject



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/morpheus/cli/remote.rb', line 139

def self.load_appliance_file
	remote_file = appliances_file_path
	if File.exist? remote_file
		return YAML.load_file(remote_file)
	else
		return {
			morpheus: {
				host: 'https://api.gomorpheus.com',
				active: true
			}
		}
	end
end

.save_appliances(appliance_map) ⇒ Object



162
163
164
# File 'lib/morpheus/cli/remote.rb', line 162

def self.save_appliances(appliance_map)
	File.open(appliances_file_path, 'w') {|f| f.write appliance_map.to_yaml } #Store
end

Instance Method Details

#add(args) ⇒ Object



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
# File 'lib/morpheus/cli/remote.rb', line 51

def add(args)
	if args.count < 2
		puts "\nUsage: morpheus remote add [name] [host] [--default]\n\n"
		return
	end
	params = {}
	optparse = OptionParser.new do|opts|
		params[:default] = false
		opts.on( '-d', '--default', "Default has been set" ) do
			params[:default] = true
		end
	end
	optparse.parse(args)

	name = args[0].to_sym
	if @appliances[name] != nil
		print red, "Remote appliance already configured for #{args[0]}", reset, "\n"
	else
		@appliances[name] = {
			host: args[1],
			active: false
		}
		if params[:default] == true
			set_active_appliance name
		end
	end
	::Morpheus::Cli::Remote.save_appliances(@appliances)
	list([])
end

#handle(args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/morpheus/cli/remote.rb', line 14

def handle(args) 
	if args.empty?
		puts "\nUsage: morpheus remote [list,add,remove,use] [name] [host]\n\n"
     return
	end

	case args[0]
		when 'list'
			list(args[1..-1])
		when 'add'
			add(args[1..-1])
		when 'remove'
			remove(args[1..-1])
		when 'use'
			use(args[1..-1])
		else
			puts "\nUsage: morpheus remote [list,add,remove,use] [name] [host]\n\n"
	end
end

#list(args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/morpheus/cli/remote.rb', line 34

def list(args)
	print "\n" ,cyan, bold, "Morpheus Appliances\n","==================", reset, "\n\n"
	# print red, bold, "red bold", reset, "\n"
	if @appliances == nil || @appliances.empty?
	else
	end
	@appliances.each do |app_name, v| 
		print cyan
		if v[:active] == true
			print bold, "=> #{app_name}\t#{v[:host]}",reset,"\n"
		else
			print "=  #{app_name}\t#{v[:host]}",reset,"\n"
		end
	end
	print "\n\n# => - current\n\n"
end

#remove(args) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/morpheus/cli/remote.rb', line 81

def remove(args)
	if args.empty?
		puts "\nUsage: morpheus remote remove [name]\n\n"
		return
	end
	name = args[0].to_sym
	if @appliances[name] == nil
		print red, "Remote appliance not configured for #{args[0]}", reset, "\n"
	else
		active = false
		if @appliances[name][:active]
			active = true
		end
		@appliances.delete(name)
		if active && !@appliances.empty?
			@appliances[@appliances.keys.first][:active] = true
		end
	end
	::Morpheus::Cli::Remote.save_appliances(@appliances)
	list([])
end

#set_active_appliance(name) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/morpheus/cli/remote.rb', line 119

def set_active_appliance(name)
	@appliances.each do |k,v|
		if k == name
			v[:active] = true
		else
			v[:active] = false
		end
	end
end

#use(args) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/morpheus/cli/remote.rb', line 103

def use(args)
	if args.empty?
		puts "Usage: morpheus remote use [name]"
		return
	end
	name = args[0].to_sym
	if @appliances[name] == nil
		print red, "Remote appliance not configured for #{args[0]}", reset, "\n"
	else
		set_active_appliance name
	end
	::Morpheus::Cli::Remote.save_appliances(@appliances)
	list([])
	@@appliance = nil
end