Class: Morpheus::Cli::Workflows

Inherits:
Object
  • Object
show all
Includes:
CliCommand
Defined in:
lib/morpheus/cli/workflows.rb

Instance Method Summary collapse

Methods included from CliCommand

#build_common_options, included

Constructor Details

#initializeWorkflows

Returns a new instance of Workflows.



11
12
13
# File 'lib/morpheus/cli/workflows.rb', line 11

def initialize() 
	@appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance	
end

Instance Method Details

#add(args) ⇒ Object



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

def add(args)
	workflow_name = args[0]
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus workflows add [name] "
		opts.on("--tasks x,y,z", Array, "List of tasks to run in order") do |list|
			options[:task_names]= list
		end
		build_common_options(opts, options, [:json, :remote])
	end
	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	optparse.parse(args)
	connect(options)
	begin
		tasks = []
		options[:task_names].each do |task_name|
			tasks << find_task_by_name_or_code_or_id(task_name)['id']
		end

		payload = {taskSet: {name: workflow_name, tasks: tasks}}
		json_response = @task_sets_interface.create(payload)
		if options[:json]
				print JSON.pretty_generate(json_response)
		else
			print "\n", cyan, "Workflow #{json_response['taskSet']['name']} created successfully", reset, "\n\n"
		end
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#connect(opts) ⇒ Object



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

def connect(opts)
	if opts[:remote]
		@appliance_url = opts[:remote]
		@appliance_name = opts[:remote]
		@access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials(opts)
	else
		@access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials(opts)
	end
	@api_client = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url)		
	@tasks_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).tasks
	@task_sets_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).task_sets
	if @access_token.empty?
		print_red_alert "Invalid Credentials. Unable to acquire access token. Please verify your credentials and try again."
		exit 1
	end
end

#handle(args) ⇒ Object



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

def handle(args) 
	if args.empty?
		puts "\nUsage: morpheus workflows [list,add,remove]\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 'update'
			update(args[1..-1])
		else
			puts "\nUsage: morpheus workflows [list,add,remove]\n\n"
			exit 127
	end
end

#list(args) ⇒ Object



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
80
81
82
83
84
85
86
87
88
# File 'lib/morpheus/cli/workflows.rb', line 54

def list(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus workflows list [-s] [-o] [-m]"
		build_common_options(opts, options, [:list, :json, :remote])
	end
	optparse.parse(args)
	connect(options)
	begin
		params = {}
		[:phrase, :offset, :max, :sort, :direction].each do |k|
			params[k] = options[k] unless options[k].nil?
		end
		json_response = @task_sets_interface.get(params)
		if options[:json]
				print JSON.pretty_generate(json_response)
		else
			task_sets = json_response['taskSets']
			print "\n" ,cyan, bold, "Morpheus Workflows\n","==================", reset, "\n\n"
			if task_sets.empty?
				puts yellow,"No workflows currently configured.",reset
			else
				print cyan
				tasks_table_data = task_sets.collect do |task_set|
					{name: task_set['name'], id: task_set['id']}
				end
				tp tasks_table_data, :id, :name
			end
			print reset,"\n\n"
		end
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#remove(args) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/morpheus/cli/workflows.rb', line 128

def remove(args)
	workflow_name = args[0]
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus workflows remove [name]"
		build_common_options(opts, options, [:auto_confirm, :json, :remote])
	end
	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	optparse.parse(args)
	connect(options)
	begin
		workflow = find_workflow_by_name_or_code_or_id(workflow_name)
		exit 1 if workflow.nil?
		unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the workflow #{workflow['name']}?")
			exit 1
		end
		json_response = @tasks_interface.destroy(task['id'])
		if options[:json]
				print JSON.pretty_generate(json_response)
		else
			print "\n", cyan, "Workflow #{workflow['name']} removed", reset, "\n\n"
		end
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#update(args) ⇒ Object



125
126
# File 'lib/morpheus/cli/workflows.rb', line 125

def update(args) 
end