Class: Morpheus::Cli::Remote

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CliCommand

#build_common_options, included

Constructor Details

#initialize ⇒ Remote

Returns a new instance of Remote.



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

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

Class Method Details

.active_appliance ⇒ Object

Provides the current active appliance url information



172
173
174
175
176
177
# File 'lib/morpheus/cli/remote.rb', line 172

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_path ⇒ Object



196
197
198
199
200
201
202
203
# File 'lib/morpheus/cli/remote.rb', line 196

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_file ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/morpheus/cli/remote.rb', line 181

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

.save_appliances(appliance_map) ⇒ Object



205
206
207
# File 'lib/morpheus/cli/remote.rb', line 205

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



72
73
74
75
76
77
78
79
80
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 72

def add(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = "Usage: morpheus remote add [name] [host] [--default]"
    build_common_options(opts, options, [])
    opts.on( '-d', '--default', "Make this the default remote appliance" ) do
      options[:default] = true
    end
  end
  optparse.parse(args)
  if args.count < 2
    puts "\n#{optparse.banner}\n\n"
    return
  end

  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 options[:default] == true
      set_active_appliance name
    end
  end
  ::Morpheus::Cli::Remote.save_appliances(@appliances)
  list([])
end

#handle(args) ⇒ Object



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

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/morpheus/cli/remote.rb', line 35

def list(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = "Usage: morpheus remote list"
    build_common_options(opts, options, [])
  end
  optparse.parse(args)

  print "\n" ,cyan, bold, "Morpheus Appliances\n","==================", reset, "\n\n"
  # print red, bold, "red bold", reset, "\n"
  if @appliances == nil || @appliances.empty?
    puts yellow,"No remote appliances configured.",reset
  else
    rows = @appliances.collect do |app_name, v|
      {
        active: (v[:active] ? "=>" : ""), 
        name: app_name, 
        host: v[:host]
      }
    end
    print cyan
    tp rows, {:active => {:display_name => ""}}, {:name => {:width => 16}}, {:host => {:width => 40}}
    print reset
  
    # @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
end

#remove(args) ⇒ Object



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/morpheus/cli/remote.rb', line 103

def remove(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = "Usage: morpheus remote remove [name]"
    build_common_options(opts, options, [])
    opts.on( '-d', '--default', "Make this the default remote appliance" ) do
      options[:default] = true
    end
  end
  optparse.parse(args)
  if args.empty?
    puts "\n#{optparse.banner}\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



161
162
163
164
165
166
167
168
169
# File 'lib/morpheus/cli/remote.rb', line 161

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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/morpheus/cli/remote.rb', line 135

def use(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = "Usage: morpheus remote use [name]"
    build_common_options(opts, options, [])
    opts.on( '-d', '--default', "Make this the default remote appliance. This does the same thing as remote use." ) do
      options[:default] = true
    end
  end
  optparse.parse(args)
  if args.empty?
    puts "\n#{optparse.banner}\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
    set_active_appliance name
  end
  ::Morpheus::Cli::Remote.save_appliances(@appliances)
  list([])
  @@appliance = nil
end