Class: Mozzn::Commands::App

Inherits:
Thor
  • Object
show all
Defined in:
lib/mozzn/commands/app.rb

Instance Method Summary collapse

Instance Method Details

#consoleObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/mozzn/commands/app.rb', line 195

def console 
token = Mozzn::Config.new.read['token']
if token.nil?
  raise Thor::Error,"You need to login in order to continue."
end
mozzn = Mozzn::Api.new(token)
params = {
  name: appname
}
search_path = "applications/search"
begin
  response = mozzn.get(search_path, params)
  if response.has_key?('info')
    raise Thor::Error, "#{response['info']}"
  else
    id = response['app_id']
    instances_path = "applications/#{id}/instances"
    response = mozzn.get(instances_path,nil)
    ip_address = response['instances'].first['data']['ip_address']
    if options[:command].present?
      system( "ssh app@#{ip_address} #{options[:command]}" )
    else
      system( "ssh app@#{ip_address}")
    end
    status = $?.exitstatus
  end
rescue JSON::ParserError => e
  raise Thor::Error,"You do not have an application with the name #{params[:name]}. Please check the application name."
end
rescue Mozzn::Disconnected
  say 'Unable to connect to Mozzn. Check your internet connection!', :red
rescue Mozzn::UnexpectedOutput
  say 'UnexpectedOutput', :red
end

#create(name = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mozzn/commands/app.rb', line 9

def create name = nil
  token = Mozzn::Config.new.read['token']
  if token.nil?
    raise Thor::Error,"You need to login in order to continue."
  end
  mozzn = Mozzn::Api.new(token)
  if name == nil
    raise Thor::Error, "You must enter application name."
  end
  path = 'applications'
  params = {
    application: {
      name: name
    }
  }
  response = mozzn.post(path, params)
  if response.has_key?('message')
    raise Thor::Error, "#{response['message']}."
  else
    say response['info'], :green
    git = Git.init
    unless File.exist?('.git')
        git.add(all: true)
      begin
        git.commit('First commit')
      rescue Git::GitExecuteError => e
        # Do nothing
      end
    end
    begin
      git.add_remote("mozzn", "[email protected]:#{name}.git")
    rescue Git::GitExecuteError => e
      say 'Git remote already configured, skipping.'
    end
  end
rescue Mozzn::Disconnected
  say 'Unable to connect to Mozzn check the internet connection!', :red
rescue Mozzn::UnexpectedOutput
  say 'UnexpectedOutput', :red
end

#destroyObject



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
80
81
82
83
84
# File 'lib/mozzn/commands/app.rb', line 52

def destroy 
  token = Mozzn::Config.new.read['token']
  if token.nil?
    raise Thor::Error,"You need to login in order to continue."
  end
  mozzn = Mozzn::Api.new(token)
  if options['appname'].present?
    name = options['appname']
  else
    name = appname
  end
  params = {
    name: name
  }
  search_path = "applications/search"
  begin
  response = mozzn.get(search_path, params)
  if response.has_key?('info')
    raise Thor::Error, "#{response['info']}."
  else
    id = response['app_id']
    resources_path = "applications/#{id}/remove"
    response = mozzn.get(resources_path,nil)
    say response['info'], :green 
  end  
  rescue JSON::ParserError => e
    raise Thor::Error,"You do not have an application with the name #{params[:name]}. Please check the application name."
  end
  rescue Mozzn::Disconnected
  say 'Unable to connect to Mozzn. Check your internet connection!', :red
  rescue Mozzn::UnexpectedOutput
  say 'UnexpectedOutput', :red
end

#listObject



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
# File 'lib/mozzn/commands/app.rb', line 87

def list
  token = Mozzn::Config.new.read['token']
  if token.nil?
    raise Thor::Error,"You need to login in order to continue."
  end
  mozzn = Mozzn::Api.new(token)
  path = "applications"
  response = mozzn.get(path, nil)
  if response.has_key? ('message')
    say response['message'], :yellow
    return
  end
  table = Terminal::Table.new(headings: ['Name', 'Status']) do |t|
    response['applications'].each do |app|
      key = app['name']
      value = app['status']
      t.add_row [key, value]  
    end
  end
  say "Your applications are:"
  say table
  rescue Mozzn::Disconnected
  say 'Unable to connect to Mozzn. Check your internet connection!', :red
  rescue Mozzn::UnexpectedOutput
  say 'UnexpectedOutput', :red  
end

#resourcesObject



116
117
118
119
120
121
122
123
124
125
126
127
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/mozzn/commands/app.rb', line 116

def resources 
  token = Mozzn::Config.new.read['token']
  if token.nil?
    raise Thor::Error,"You need to login in order to continue."
  end
  mozzn = Mozzn::Api.new(token)
  if options['appname'].present?
    name = options['appname']
  else
    name = appname
  end
  params = {
    name: name
  }
  search_path = "applications/search"
  begin
  response = mozzn.get(search_path, params)
  if response.has_key?('info')
    raise Thor::Error, "#{response['info']}"
  else
    id = response['app_id']
    resources_path = "applications/#{id}/resources"
    response = mozzn.get(resources_path,nil)
    if response.has_key?('info')
      raise Thor::Error, "#{response['info']}"
    else
      table1 = Terminal::Table.new(headings: ['Process', 'Command']) do |t|
        response['resources'].each do |resource|
          key = (resource.has_key?('command') ? resource['name'] : nil)
          value = (resource.has_key?('command') ? resource['command'] : nil)
          if key.present?
            t.add_row [key, value]
          end
        end
      end
      table2 = Terminal::Table.new(headings: ['Database', 'Role']) do |t|
        response['resources'].each do |resource|
          key = (resource.has_key?('role') ? resource['name'] : nil)
          value = (resource.has_key?('role') ? resource['role'] : nil)
          if key.present?
            t.add_row [key, value]
          end
        end
      end
    end
    instances_path = "applications/#{id}/instances"
    response = mozzn.get(instances_path,nil)
    if response.has_key?('info')
      raise Thor::Error, "#{response['info']}"
    else
      table3 = Terminal::Table.new(headings: ['Name', 'IP']) do |t|
        response['instances'].each do |instant|
          key = instant['data']['name']
          value = instant['data']['ip_address']
          if key.present?
            t.add_row [key, value]
          end
        end
      end
    end
    say "Processes:"
    say "#{table1}"
    say "Databases:"
    say "#{table2}"
    say "Instances:"
    say "#{table3}"  
  end

  rescue JSON::ParserError => e
    raise Thor::Error,"You do not have an application with the name #{params[:appname]}. Please check the application name."
  end
rescue Mozzn::Disconnected
  say 'Unable to connect to Mozzn. Check your internet connection!', :red
rescue Mozzn::UnexpectedOutput
  say 'UnexpectedOutput', :red
end