Class: PI::Cli::Command::Projects

Inherits:
Base show all
Includes:
PI::Cli::ChooseHelper, InteractHelper
Defined in:
lib/cli/commands/projects.rb

Constant Summary

Constants included from InteractHelper

InteractHelper::ESCAPES, InteractHelper::EVENTS

Constants included from PI::Cli::ChooseHelper

PI::Cli::ChooseHelper::DEFAULTS, PI::Cli::ChooseHelper::NO_SET, PI::Cli::ChooseHelper::YES_SET

Instance Method Summary collapse

Methods included from InteractHelper

#asks, #read_char, #read_event, #read_line

Methods included from PI::Cli::ChooseHelper

#check_envname_valid?, #check_envvalue_valid?, #check_name_valid?, #check_status, #choose_app, #choose_app_help, #choose_app_help_target_not_all, #choose_dnsid, #choose_project, #choose_serviceid, #choose_target, #get_graceful, #select_apps

Methods inherited from Base

#auth_token, #client, #initialize, #target_url

Constructor Details

This class inherits a constructor from PI::Cli::Command::Base

Instance Method Details

#create_project(project = nil) ⇒ Object



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
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
134
135
136
137
138
139
140
141
142
# File 'lib/cli/commands/projects.rb', line 61

def create_project(project=nil) 
  client.     
  loop{
    unless project
      project = ask "Project Name"
      err "Project Name required." if project.nil? || project.empty?
    end
    if not check_name_valid?(project)
      display "Invalid project name '#{project}'." 
      project =nil
      next
    else if project_exists?(project)
      display "Project '#{project}' already exists." 
      project =nil 
      next
    else         
      break
      end
    end
  }       

  runtimes = client.runtimes
  err "No Runtimes" if runtimes.nil? || runtimes.empty?
  runtime = ask "Select Runtime", :choices => runtimes, :indexed => true
  display "Selected Runtime: ",false
  display "#{runtime}"

  frameworks = client.frameworks(runtime)
  err "No Frameworks" if frameworks.nil? || frameworks.empty?
  framework = ask "Select Framework", :choices => frameworks, :indexed => true
  display "Selected Framework: ",false
  display "#{framework}"
  
  description = ask "Please enter in the description", :default => nil unless description
   
  github_info = client.github_info
  
  if github_info.nil?      
    manifest = {
    :name => "#{project}",
    :runtime     => "#{runtime}",
    :framework   => "#{framework}",
    :description => "#{description}"
  }
  else 
    repositoryTypes =["cloudpi","github"]
    repositoryType = ask "Select Repository Type", :choices => repositoryTypes, :indexed => true
    display "Selected Repository Type: ",false
    display "#{repositoryType}"

    if repositoryType == "github"
      isNew = "yes"
      publishTypes =["public","private"]
      publishType = ask "Select Publish Type", :choices => publishTypes, :indexed => true          
      display "Selected publish Type: ",false
      display "#{publishType}"
    end
    manifest = {
      :name => "#{project}",
      :runtime     => "#{runtime}",
      :framework   => "#{framework}",
      :description => "#{description}",
      :repositoryType => "#{repositoryType}",
      :isNew       => "#{isNew}",
      :publishType => "#{publishType}"
    }
  end
 
  display "Creating project \"#{project}\": ",false
  
  t = Thread.new do     
  loop do
    display '.', false 
    sleep (1)
    break unless t.alive?
  end
  end
        
  client.create_project(project, manifest)
  Thread.kill(t)
  display "OK".green
end

#delete_binary(projectid = nil) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/cli/commands/projects.rb', line 246

def delete_binary(projectid=nil)
  client.
  unless projectid
    useproject = choose_project
    projectid = useproject[:id]
  end 
  binary = client.project_binary(projectid)
  err "No binary!" if binary.nil? || binary.empty?     
  binaryname = ask"Select Binary", :choices => binary.collect { |b| b[:versionName] }, :indexed => true
  display "Selected Binary: ",false
  display "#{binaryname}"  
  binaryid = nil
  binary.each do |b|
    binaryid = b[:id].to_s if binaryname == b[:versionName]
  end   
  client.delete_binary(binaryid)
  display 'OK'.green
end

#delete_project(projectid = nil) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/cli/commands/projects.rb', line 144

def delete_project(projectid=nil)
  client.
  unless projectid
    useproject = choose_project
    projectid = useproject[:id]
  end
  display "Deleting project: ",false
  
  t = Thread.new do     
  loop do
    display '.', false 
    sleep (1)
    break unless t.alive?
  end
  end
  
  client.delete_project(projectid)
  Thread.kill(t)
  display 'OK'.green
end

#project_commits(projectid = nil) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/cli/commands/projects.rb', line 300

def project_commits(projectid=nil)
  client.
  unless projectid
    useproject = choose_project
    projectid = useproject[:id]
  end
  tags = client.project_tags(projectid)
  return display JSON.pretty_generate(tags || []) if @options[:json]
  return display "No Commits" if tags.nil? || tags.empty?
  tag = ask "Select Tag", :choices => tags, :indexed => true
  display "Selected Tag: ",false
  display "#{tag}"

  commits = client.project_commits_bytag(projectid,tag)
  commits.sort! {|a, b| a[:date] <=> b[:date] }
  return display JSON.pretty_generate(commits || []) if @options[:json]
  return display "No Commits" if commits.nil? || commits.empty?
  commits_table = table do |t|
    t.headings = 'CommitId', 'Date', 'Comment'  
    commits.each do |commit|
      t << [commit[:commit], commit[:date], commit[:comment]]
    end
  end
  display commits_table
end

#project_events(projectid = nil) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/cli/commands/projects.rb', line 265

def project_events(projectid=nil)
  client.
  unless projectid
    useproject = choose_project
    projectid = useproject[:id]
  end           
  events = client.project_events(projectid)
  return display JSON.pretty_generate(events || []) if @options[:json]
  return display "No Events." if events.empty?
  events_table = table do |t|
    t.headings = 'Target', 'AppName','EventTime', 'EventInfo','EventDetail'
    events.each do |event|
      t << [event[:targetName], event[:applicationName], event[:createdAt], event[:eventType], event[:eventDetail]]
    end
  end
  display events_table
end

#project_tags(projectid = nil) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/cli/commands/projects.rb', line 283

def project_tags(projectid=nil)
  client.
  unless projectid
    useproject = choose_project
    projectid = useproject[:id]
  end
  tags ||= client.project_tags(projectid)
  return display JSON.pretty_generate(tags || []) if @options[:json]
  return display "No Tags." if tags.size == 0
  tags_table = table do |t|
    t.headings = ['Tag']
    tags.each { |f| t << [f]}
  end
  display "\n"
  display tags_table
end

#projectsObject



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
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cli/commands/projects.rb', line 12

def projects
  client.
  user = client.
  proj_sum = client.project_sum
  total_proj = Integer(proj_sum[1])       
  return display "No Projects" if total_proj == 0 
  pageNum = -1
  numPerPage = -1    
  if total_proj >= 50
    puts "Total projects: #{total_proj}."  
    numPerPage = nil     
    loop{
    numPerPage = ask "Number per page"
    if not numPerPage =~ /^[1-9]\d*$/
      display "Invalid Number! Please input number from 1 to #{total_proj}" 
      numPerPage =nil
      next
    else
      break
    end
   } 
   numPerPage = numPerPage.to_i
   maxpage = (total_proj.to_f/numPerPage).ceil
   loop{
    pageNum = ask "Page number"
    if not pageNum =~ /^[1-9]\d*$/
      display "Invalid Number! Please input number from 1 to #{maxpage}" 
      pageNum =nil
      next
    else
      break
    end
   } 
   pageNum = pageNum.to_i       
  end 
  projects = client.projects(pageNum, numPerPage)
  return display "No Projects" if projects.nil? || projects.empty?
  return display JSON.pretty_generate(projects) if @options[:json]
  projects.sort! {|a, b| a[:name] <=> b[:name] }
  display "\n"        
  projects_table = table do |t|
    t.headings = 'ID', 'Name', 'Runtime', 'Framework', 'Git Type','GitUrl', 'Description'
    projects.each do |project|
      t << [project[:id], project[:name], project[:runtime], project[:framework], project[:repositoryType], project[:gitUrl], project[:description]]
    end
  end
  display projects_table
end

#upload(projectid = nil) ⇒ Object



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
192
193
194
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/cli/commands/projects.rb', line 165

def upload(projectid=nil)
  client. 
  path = @options[:path]
  if path        
    err "Not war file or path does not exist!" if not ((path =~ /\.(war)$/) && (File.exists? path))
  end
  version = @options[:ver]
  description = @options[:desc]
  projects = client.projects
  err "No Projects" if projects.nil? || projects.empty?
  java_projects = projects.delete_if{ |p| p[:runtime] =~ /^(ruby)/i}
  java_projects.sort! {|a, b| a[:name] <=> b[:name] }
  if projectid
    flag = nil
    java_projects.each do |j|  
      flag = true if projectid == j[:id].to_s
    end
    err "The project is not found or not a java project! " unless flag 
  else    
    project = ask "Select Project", :choices => java_projects.collect { |j| j[:name] }, :indexed => true
    display "Selected Project: ",false
    display "#{project}"
    project = client.project_search(project)
    projectid = project[:id]
  end
        
  if version
    tmp = client.project_binary_existed(projectid, version)       
    err "Version '#{version}' already exists." if tmp.include?("true")
  end
  
  unless path        
    loop{
    path = ask "Please enter the path of upload war file"
    if not ((path =~ /\.(war)$/) && (File.exists? path))
      display "Not war file or path does not exist!" 
      path =nil
      next
    else
      break
    end
    } 
  end
  
  unless version
    loop{
      version = ask "Please enter the version"
      tmp = client.project_binary_existed(projectid, version)
      if tmp.include?("true")
        display "Version '#{version}' already exists."
        version =nil
      next
      else
      break
      end
    }
  end  
         
  description = ask "Please enter in the description", :default => nil unless description
  upload_file = File.new(path, 'rb')
  manifest = {
    :projectId => "#{projectid}",
    :fileWAR     => upload_file,
    :versionName => "#{version}",
    :description => "#{description}"
  }     
  display "Uploading \"#{File.basename(path)}\": ",false
  
  t = Thread.new do     
  loop do
    display '.', false 
    sleep (1)
    break unless t.alive?
  end
  end
  
  client.upload(manifest)
  Thread.kill(t)
  display 'OK'.green
end