pi 0.1.24 → 0.1.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,9 +5,10 @@ module PI::Cli::Command
5
5
 
6
6
  def app_dns(appid_or_appname=nil)
7
7
  client.check_login_status
8
- app = choose_app_help_target_not_all(appid_or_appname)
8
+ app, appid = choose_app_help_target_not_all(appid_or_appname)
9
+ appid = (appid == nil ? app[:id] : appid)
9
10
 
10
- dns = client.app_dns(app[:id])
11
+ dns = client.app_dns(appid)
11
12
  return display JSON.pretty_generate(dns) if @options[:json]
12
13
  return display "No DNS!" if dns.nil? || dns.empty?
13
14
  dns_table = table do |t|
@@ -21,19 +22,25 @@ module PI::Cli::Command
21
22
 
22
23
  def map_dns(appid_or_appname=nil)
23
24
  client.check_login_status
24
- app = choose_app_help_target_not_all(appid_or_appname)
25
- dns = client.usable_dns(app[:id])
25
+ dnsname = @options[:dns]
26
+ app, appid = choose_app_help_target_not_all(appid_or_appname)
27
+ appid = (appid == nil ? app[:id] : appid)
28
+ dns = client.usable_dns(appid)
26
29
  err "No usable dns!" if dns.nil? || dns.empty?
27
- choices = Array.new
28
- dns.each do |s|
29
- choices << s[:name]
30
+ if dnsname
31
+ tmp = dnsname.split(",")
32
+ manifest = Array.new
33
+ tmp.each do |t|
34
+ manifest.push(t)
35
+ end
36
+ else
37
+ manifest = asks "Select dns, indexed list?", :choices => dns.collect { |d| d[:name] }, :indexed => true
38
+ display "Selected dns: ",false
39
+ manifest.each do |m|
40
+ display "#{m} ",false
41
+ end
42
+ display "\n"
30
43
  end
31
- manifest = asks "Select dns, indexed list?", :choices => choices, :indexed => true
32
- display "Selected dns: ",false
33
- manifest.each do |m|
34
- display "#{m} ",false
35
- end
36
- display "\n"
37
44
  display "Mapping dns: ",false
38
45
 
39
46
  t = Thread.new do
@@ -44,27 +51,161 @@ module PI::Cli::Command
44
51
  end
45
52
  end
46
53
 
47
- client.map_dns(app[:id],manifest)
54
+ client.map_dns(appid,manifest)
48
55
  Thread.kill(t)
49
56
  display "OK".green
50
57
  end
51
58
 
52
59
  def unmap_dns(appid_or_appname=nil)
53
60
  client.check_login_status
54
- app = choose_app_help_target_not_all(appid_or_appname)
55
- dns = client.app_dns(app[:id])
61
+ dnsname = @options[:dns]
62
+ app, appid = choose_app_help_target_not_all(appid_or_appname)
63
+ appid = (appid == nil ? app[:id] : appid)
64
+ dns = client.app_dns(appid)
56
65
  err "No mapped dns!" if dns.nil? || dns.empty?
57
- urls = asks "Select DNS", :choices => dns, :indexed => true
58
- display "Selected DNS: ",false
59
- urls.each do |m|
60
- display "#{m} ",false
61
- end
62
- display "\n"
66
+ if dnsname
67
+ urls = dnsname.split(",")
68
+ else
69
+ urls = asks "Select DNS", :choices => dns, :indexed => true
70
+ display "Selected DNS: ",false
71
+ urls.each do |m|
72
+ display "#{m} ",false
73
+ end
74
+ display "\n"
75
+ end
76
+
63
77
  urls.each do |url|
64
- do_unmap_dns(app[:id], url)
78
+ do_unmap_dns(appid, url)
65
79
  end
66
80
  end
67
81
 
82
+ def dns
83
+ dns = client.dns
84
+ return display "No Services" if dns.nil? || dns.empty?
85
+ return display JSON.pretty_generate(dns) if @options[:json]
86
+ dns.sort! {|a, b| a[:name] <=> b[:name] }
87
+ dns_table = table do |t|
88
+ t.headings = 'ID', 'DNS Name', 'Project Name', 'Description'
89
+ dns.each do |s|
90
+ t << [s[:id],s[:name], s[:projectName], s[:bak]]
91
+ end
92
+ end
93
+ display dns_table
94
+ end
95
+
96
+ def create_dns(dnsname=nil)
97
+ projectname = @options[:project]
98
+ description = @options[:desc]
99
+ zoneid = @options[:zoneid]
100
+ dns_zone = client.dns_zone_list
101
+ err"No dns zone" if dns_zone.nil? || dns_zone.empty?
102
+ if zoneid
103
+ zoneid_flag = false
104
+ dns_zone.each do |d|
105
+ zoneid_flag = true if d[:id].to_s == zoneid
106
+ end
107
+ err"The dns zone id is not exist!" if zoneid_flag != true
108
+ end
109
+ projects = client.projects
110
+ projectname = ask"Project name", :choices => projects.collect { |p| p[:name] }, :indexed => true unless projectname
111
+ projectid = nil
112
+ projects.each do |p|
113
+ projectid = p[:id] if p[:name] == projectname
114
+ end
115
+ err "Invild project" if projectid.nil?
116
+ unless zoneid
117
+ zonename = ask"Select dns zone", :choices => dns_zone.collect { |d| d[:name] }, :indexed => true
118
+ dns_zone.each do |d|
119
+ zoneid = d[:id] if d[:name] == zonename
120
+ end
121
+ err"Invalid zone" if zoneid.nil?
122
+ end
123
+
124
+ loop{
125
+ dnsname = ask"DNS name" unless dnsname
126
+ tmp = client.verify_dns(dnsname, zoneid)
127
+ if not check_name_valid?(dnsname)
128
+ display "Invalid dns name '#{dnsname}'."
129
+ dnsname =nil
130
+ next
131
+ else if tmp[1] == "false"
132
+ display "DNS '#{dnsname}' already exists."
133
+ dnsname =nil
134
+ next
135
+ else
136
+ break
137
+ end
138
+ end
139
+ }
140
+ # dnsname = dnsname.concat(".#{zone}")
141
+ description = ask"Description", :default => nil unless description
142
+ manifest = {
143
+ :name => dnsname,
144
+ :zoneId => zoneid,
145
+ :projectId => projectid,
146
+ :bak => description
147
+ }
148
+ client.create_dns(manifest)
149
+ display"OK".green
150
+ end
151
+
152
+ def delete_dns(dnsid=nil)
153
+ unless dnsid
154
+ dnsid = choose_dnsid
155
+ end
156
+ client.delete_dns(dnsid)
157
+ display"OK".green
158
+ end
159
+
160
+ def register_dns(dnsid=nil)
161
+ unless dnsid
162
+ dnsid = choose_dnsid
163
+ end
164
+ targets = client.usable_target(dnsid)
165
+ target = ask"Select target", :choices => targets.collect { |t| t[:targetName] }, :indexed => true
166
+ continents = client.dns_continents
167
+ tmp = continents.values
168
+ tmp.sort! {|a, b| a <=> b }
169
+ continent = ask"Select continent", :choices => tmp.collect { |t| t }, :indexed => true
170
+ display "Select continent: #{continent}"
171
+ continent = continents.index("#{continent}")
172
+ countries = client.dns_country(continent)
173
+ tmp = countries.values
174
+ tmp.sort! {|a, b| a <=> b }
175
+ country = ask"Select country", :choices => tmp.collect { |t| t }, :indexed => true
176
+ display "Select country: #{country}"
177
+ country = countries.index("#{country}")
178
+ manifest = {
179
+ :continentName => continent,
180
+ :countryCode => country
181
+ }
182
+ client.register_dns(dnsid, target, manifest)
183
+ display "OK".green
184
+ end
185
+
186
+ def deregister_dns(dnsid=nil)
187
+ unless dnsid
188
+ dnsid = choose_dnsid
189
+ end
190
+
191
+ targets = client.usable_target(dnsid)
192
+ target = ask"Select target", :choices => targets.collect { |t| t[:targetName] }, :indexed => true
193
+ choices = Array.new
194
+ record = Array.new
195
+ targets.each do |t|
196
+ record = t[:records] if t[:targetName] == target
197
+ end
198
+ err"No record" if record.nil? || record.empty?
199
+ view = ask"Select record", :choices => record.collect { |t| t[:view] }, :indexed => true
200
+ recordid = nil
201
+ record.each do |r|
202
+ recordid = r[:id] if r[:view] == view
203
+ end
204
+
205
+ client.deregister_dns(recordid)
206
+ display "OK".green
207
+ end
208
+
68
209
  private
69
210
 
70
211
  def do_unmap_dns(appid, url)
@@ -6,10 +6,19 @@ module PI::Cli::Command
6
6
  end
7
7
 
8
8
  def set_target(target_url)
9
- target_url = "http://#{target_url}" unless /^https?/ =~ target_url
9
+ PI::Cli::Config.store_target(target_url)
10
+ if target_url =~ /(https?:\/\/)?staging.samsungcloud.org/
11
+ if /^https?/ =~ target_url
12
+ target_url = target_url.split("//", 2)
13
+ target_url = target_url[1]
14
+ end
15
+ target_url = "http://api.#{target_url}"
16
+ else
17
+ target_url = "http://#{target_url}" unless /^https?/ =~ target_url
18
+ end
10
19
  target_url = target_url.gsub(/\/+$/, '')
11
20
  client = PI::Client.new(target_url)
12
- PI::Cli::Config.store_target(target_url)
21
+ # PI::Cli::Config.store_target(target_url)
13
22
  end
14
23
 
15
24
  end
@@ -181,11 +181,7 @@ module PI::Cli::Command
181
181
  end
182
182
  err "The project is not found or not a java project! Project name :#{project}" unless flag
183
183
  else
184
- project_choices = Array.new
185
- java_projects.each do |p|
186
- project_choices << p[:name]
187
- end
188
- project = ask "Select Project", :choices => project_choices, :indexed => true
184
+ project = ask "Select Project", :choices => java_projects.collect { |j| j[:name] }, :indexed => true
189
185
  display "Selected Project: ",false
190
186
  display "#{project}"
191
187
  end
@@ -254,12 +250,8 @@ module PI::Cli::Command
254
250
  project = useproject[:name]
255
251
  end
256
252
  binary = client.project_binary(project)
257
- err "No binary!" if binary.nil? || binary.empty?
258
- choices = Array.new
259
- binary.each do |b|
260
- choices << b[:versionName]
261
- end
262
- binaryname = ask"Select Binary", :choices => choices, :indexed => true
253
+ err "No binary!" if binary.nil? || binary.empty?
254
+ binaryname = ask"Select Binary", :choices => binary.collect { |b| b[:versionName] }, :indexed => true
263
255
  display "Selected Binary: ",false
264
256
  display "#{binaryname}"
265
257
  binaryid = nil
@@ -5,8 +5,9 @@ module PI::Cli::Command
5
5
 
6
6
  def app_service(appid_or_appname=nil)
7
7
  client.check_login_status
8
- app = choose_app_help_target_not_all(appid_or_appname)
9
- services = client.app_service(app[:id])
8
+ app, appid = choose_app_help_target_not_all(appid_or_appname)
9
+ appid = (appid == nil ? app[:id] : appid)
10
+ services = client.app_service(appid)
10
11
  return display JSON.pretty_generate(services) if @options[:json]
11
12
  return display "No Services" if services.nil? || services.empty?
12
13
  services.sort! {|a, b| a[:name] <=> b[:name] }
@@ -21,21 +22,27 @@ module PI::Cli::Command
21
22
 
22
23
  def bind_service(appid_or_appname=nil)
23
24
  client.check_login_status
24
- app = choose_app_help_target_not_all(appid_or_appname)
25
- services = client.usable_services(app[:id])
25
+ service = @options[:service]
26
+ app, appid = choose_app_help_target_not_all(appid_or_appname)
27
+ appid = (appid == nil ? app[:id] : appid)
28
+ services = client.usable_services(appid)
26
29
  err "No usable services!" if services.nil? || services.empty?
27
30
 
28
- choices = Array.new
29
- services.each do |s|
30
- choices << s[:name]
31
- end
32
-
33
- manifest = asks "Select services, indexed list?", :choices => choices, :indexed => true
34
- display "Selected services: ",false
35
- manifest.each do |m|
36
- display "#{m} ",false
37
- end
38
- display "\n"
31
+ if service
32
+ tmp = service.split(",")
33
+ manifest = Array.new
34
+ tmp.each do |s|
35
+ manifest.push(s)
36
+ end
37
+ else
38
+ manifest = asks "Select services, indexed list?", :choices => services.collect { |s| s[:name] }, :indexed => true
39
+ display "Selected services: ",false
40
+ manifest.each do |m|
41
+ display "#{m} ",false
42
+ end
43
+ display "\n"
44
+ end
45
+
39
46
  display "Binding service: ",false
40
47
 
41
48
  t = Thread.new do
@@ -46,7 +53,7 @@ module PI::Cli::Command
46
53
  end
47
54
  end
48
55
 
49
- client.bind_service(app[:id],manifest)
56
+ client.bind_service(appid,manifest)
50
57
  result = check_status(app[:targetName], app[:name], "bindservice")
51
58
  Thread.kill(t)
52
59
  if result[:code] == 200
@@ -62,24 +69,153 @@ module PI::Cli::Command
62
69
 
63
70
  def unbind_service(appid_or_appname=nil)
64
71
  client.check_login_status
65
- app = choose_app_help_target_not_all(appid_or_appname)
66
- services = client.app_service(app[:id])
72
+ service = @options[:service]
73
+ app, appid = choose_app_help_target_not_all(appid_or_appname)
74
+ appid = (appid == nil ? app[:id] : appid)
75
+ services = client.app_service(appid)
67
76
  err "No binded services!" if services.nil? || services.empty?
68
- service_choices = Array.new
69
- services.each do |s|
70
- service_choices << s[:name]
71
- end
72
- services = asks "Select Service", :choices => service_choices, :indexed => true
73
- display "Selected DNS: ",false
74
- services.each do |m|
75
- display "#{m} ",false
76
- end
77
- display "\n"
77
+ if service
78
+ services = service.split(",")
79
+ else
80
+ services = asks "Select Service", :choices => services.collect { |s| s[:name] }, :indexed => true
81
+ display "Selected DNS: ",false
82
+ services.each do |m|
83
+ display "#{m} ",false
84
+ end
85
+ display "\n"
86
+ end
87
+
78
88
  services.each do |service|
79
89
  do_unbind_service(app, service)
80
90
  end
81
91
  end
82
92
 
93
+ def services
94
+ services = client.services
95
+ return display "No Services" if services.nil? || services.empty?
96
+ return display JSON.pretty_generate(services) if @options[:json]
97
+ services.sort! {|a, b| a[:name] <=> b[:name] }
98
+ services_table = table do |t|
99
+ t.headings = 'ID', 'Name', 'Farm Name', 'Service Type', 'Version', 'Owner'
100
+ services.each do |s|
101
+ t << [s[:id],s[:name], s[:farmName], s[:type] ,s[:version], s[:ownerName]]
102
+ end
103
+ end
104
+ display services_table
105
+ end
106
+
107
+ def service(serviceid=nil)
108
+ if serviceid
109
+ services = client.services
110
+ err "No services" if services.nil? || services.empty?
111
+ serviceid_flag = false
112
+ services.each do |s|
113
+ serviceid_flag = true if s[:id].to_s == serviceid
114
+ end
115
+ err"The service is not exist!" if serviceid_flag != true
116
+ else
117
+ serviceid = choose_serviceid
118
+ end
119
+ service = client.service(serviceid)
120
+ return display "No Services" if service.nil? || service.empty?
121
+ return display JSON.pretty_generate(service) if @options[:json]
122
+ display service[:new_data]
123
+ end
124
+
125
+ def import_service(importurl=nil)
126
+ importurl = ask"Input the url to import" unless importurl
127
+ client.import_service(importurl)
128
+ display"OK".green
129
+ end
130
+
131
+ def export_service(serviceid=nil)
132
+ unless serviceid
133
+ serviceid = choose_serviceid
134
+ end
135
+ serviceurl = client.export_service(serviceid)
136
+ display "#{serviceurl[1]}"
137
+ end
138
+
139
+ def check_service(serviceid=nil)
140
+ unless serviceid
141
+ serviceid = choose_serviceid
142
+ end
143
+ check = client.check_service(serviceid)
144
+ if check[1] == "true"
145
+ display "Service is available."
146
+ else
147
+ display "Service is unavailable."
148
+ end
149
+ end
150
+
151
+ def delete_service(serviceid=nil)
152
+ unless serviceid
153
+ serviceid = choose_serviceid
154
+ end
155
+ client.delete_service(serviceid)
156
+ display"OK".green
157
+ end
158
+
159
+ def register_service(serviceid=nil)
160
+ target = @options[:target]
161
+ servicetype = @options[:type]
162
+ if servicetype
163
+ err "Invalid type. Please use --type 1 or --type 2" unless servicetype =~ /[1|2]/
164
+ end
165
+ if serviceid
166
+ services = client.services
167
+ err "No services" if services.nil? || services.empty?
168
+ serviceid_flag = false
169
+ services.each do |s|
170
+ serviceid_flag = true if s[:id].to_s == serviceid
171
+ end
172
+ err"The service is not exist!" if serviceid_flag != true
173
+ else
174
+ serviceid = choose_serviceid
175
+ end
176
+ targets = client.usable_target(serviceid)
177
+ err "No targets" if targets.nil? || targets.empty?
178
+ if target
179
+ target_flag = false
180
+ targets.each do |t|
181
+ target_flag = true if t[:name] == target
182
+ end
183
+ err"Invalid target name" if target_flag != true
184
+ else
185
+ target = ask"Select target", :choices => targets.collect { |t| t[:name] }, :indexed => true
186
+ end
187
+ unless servicetype
188
+ choices = ["App Service", "Session Service"]
189
+ servicetype = ask"Select service type", :choices => choices, :indexed => true
190
+ if servicetype == "App Service"
191
+ servicetype = 1
192
+ else
193
+ servicetype = 2
194
+ end
195
+ end
196
+ client.register_service(serviceid, target, servicetype)
197
+ display "OK".green
198
+ end
199
+
200
+ def deregister_service(serviceid=nil)
201
+ servicetargetname = @options[:target]
202
+ unless serviceid
203
+ serviceid = choose_serviceid
204
+ end
205
+ service_target = client.service_target(serviceid)
206
+ err"No target available to deregister" if service_target.nil? || service_target.empty?
207
+ unless servicetargetname
208
+ servicetargetname = ask"Select target", :choices => service_target.collect { |t| t[:targetName] }, :indexed => true
209
+ end
210
+ servicetargetid = nil
211
+ service_target.each do |s|
212
+ servicetargetid = s[:id] if servicetargetname == s[:targetName]
213
+ end
214
+ err " The service is not registered to this target!" if servicetargetid == nil
215
+ client.deregister_service(servicetargetid)
216
+ display "OK".green
217
+ end
218
+
83
219
  private
84
220
 
85
221
  def do_unbind_service(app, service)