citasks 0.1.5 → 0.1.6

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/citasks/ci_lib.rb +145 -49
  3. data/lib/citasks.rb +16 -6
  4. metadata +15 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f3d41bf164110e3e84deac18d24e4d0f55a31081
4
- data.tar.gz: 59e7113f5f58269dc6208b5e427861c5195c5bfa
3
+ metadata.gz: 3f0bea593eadc9785d910210959c70c25b237074
4
+ data.tar.gz: 94b8c0f329f8ca21e8db669d88e9a6dfc2f79d4a
5
5
  SHA512:
6
- metadata.gz: 91261b3b0c0c5acf3a633470a214a3f679590881279afe74ab84452a02f785cef050ae72cee8c55ac5adb4916e931661668b99e726bb334117e72c521dca86b5
7
- data.tar.gz: a0486b4b83ea0000c9019ee565301d64bbe44b93f6a9d9c9a3bb22207ef92313324da1bdbd938a7cf6fd7817aeb10dae1d1369508bcd11b4a51da79996b14ba7
6
+ metadata.gz: bc2efd8a6201e60d1793d51d9bd40f0791200fc6e13354747c2587e6205a07e5edf4d0bc3b08b6fd5291a0e743c06d21040e10276c6854d64a4ad59a842d535f
7
+ data.tar.gz: 3bdb3bcb088dde6b6065f3eaa55c183174a3b1dbeec0c0200d05828d4c5c6b77e36811b44a597e79e3a3222897187a60b360c2ecb294e9b0208ef81a07cb31af
@@ -1,6 +1,8 @@
1
1
  require 'gitlab'
2
2
  require 'securerandom'
3
3
  require "erb"
4
+ require "rest-client"
5
+ require "pp"
4
6
 
5
7
  def _write fullpath, content
6
8
  File.open fullpath, "w" do |fh|
@@ -12,7 +14,7 @@ def token_shared_persistently
12
14
  #create a shared token across tasks
13
15
  secret_token_file = ".token"
14
16
  if File.exists? secret_token_file
15
- token = File.read secret_token_file
17
+ token = File.read(secret_token_file).chomp
16
18
  else
17
19
  token = SecureRandom.uuid
18
20
  File.open secret_token_file, "w" do |fh|
@@ -23,15 +25,16 @@ def token_shared_persistently
23
25
  end
24
26
 
25
27
  module JenkinsTools
26
- WORKFLOW_PLUGIN = ENV["WORKFLOW_PLUGIN"] || "workflow-job@2.14.1"
27
- GITLAB_PLUGIN = ENV["GITLAB_PLUGIN"] || "gitlab-plugin@1.4.7"
28
- WORkFLOW_CPS_PLUGIN = ENV["WORkFLOW_CPS_PLUGIN"] || "workflow-cps@2.39"
29
- GIT_PLUGIN = ENV["GIT_PLUGIN"] || "git@3.4.0"
30
-
28
+ WORKFLOW_PLUGIN = ENV["WORKFLOW_PLUGIN"] || "workflow-job"
29
+ GITLAB_PLUGIN = ENV["GITLAB_PLUGIN"] || "gitlab-plugin"
30
+ WORkFLOW_CPS_PLUGIN = ENV["WORkFLOW_CPS_PLUGIN"] || "workflow-cps"
31
+ GIT_PLUGIN = ENV["GIT_PLUGIN"] || "git"
32
+ # JENKIN_PROJECT_ENDPOINT_AUTHENTICATION = ENV["JENKIN_PROJECT_ENDPOINT_AUTHENTICATION"].to_s.upcase == "TRUE"
33
+
31
34
  # git_repo_url = http://virtuous-porcupine-gitlab-ce/wenzm/icp-static-web.git, gitlab-wenzm-password
32
35
  def self.gen_job_xml job_name, xml_file_name, git_repo_url, repo_credential_id_in_jenkins, secret_token=nil
33
- enable_secret_token = ENV["JENKIN_PROJECT_ENDPOINT_AUTHENTICATION"].upcase == "TRUE"
34
-
36
+ enable_secret_token = true
37
+
35
38
  if enable_secret_token
36
39
  secret_token = token_shared_persistently if secret_token.nil?
37
40
  end
@@ -73,7 +76,7 @@ module JenkinsTools
73
76
  </org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
74
77
  </properties>
75
78
  <definition class="org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition" plugin="#{WORkFLOW_CPS_PLUGIN}">
76
- <scm class="hudson.plugins.git.GitSCM" plugin="#[GIT_PLUGIN}">
79
+ <scm class="hudson.plugins.git.GitSCM" plugin="#{GIT_PLUGIN}">
77
80
  <configVersion>2</configVersion>
78
81
  <userRemoteConfigs>
79
82
  <hudson.plugins.git.UserRemoteConfig>
@@ -131,47 +134,126 @@ module JenkinsTools
131
134
  ]
132
135
  ){
133
136
  node('my-pod') {
134
- stage('clone git repo'){
137
+ stage('check out') {
135
138
  checkout scm
139
+ }
136
140
 
141
+ stage('compile'){
137
142
  container('compiler'){
138
- stage('Compile and Build'){
139
- sh("echo compile")
140
- }
143
+ sh "echo compile"
144
+ }
145
+ }
146
+
147
+ stage('Docker Build'){
148
+ container('citools'){
149
+ // sleep 3600
150
+ sh "echo build docker image"
151
+ // sh "rake -f build.rb docker:01_build_image docker:02_push_to_ICp_registry"
141
152
  }
153
+ }
142
154
 
155
+ stage('Deploy to ICP'){
143
156
  container('citools'){
144
- stage('Docker Build'){
145
- // sleep 3600
146
- sh "echo build docker image"
147
- // sh "rake -f build.rb docker:01_build_image docker:02_push_to_ICp_registry"
148
- }
149
-
150
- stage('Deploy into k8s'){
151
- sh "echo rollout to k8s"
152
- // sh "rake -f build.rb k8s:01_deploy_to_k8s"
153
- }
157
+ // sleep 3600
158
+ echo "deploy to icp..."
159
+ // sh "rake -f build.rb k8s:01_deploy_to_k8s"
154
160
  }
155
161
  }
162
+
163
+ //stage('Deployment'){
164
+ // parallel 'deploy to icp': {
165
+ // container('citools'){
166
+ // echo "deploy to icp..."
167
+ // // sh "rake -f build.rb k8s:01_deploy_to_k8s"
168
+
169
+ // }
170
+ // },
171
+
172
+ // 'deploy to others': {
173
+ // container('citools'){
174
+ // echo "deploy to others..."
175
+ // }
176
+ // }
177
+ //}
156
178
  }
157
- }
179
+ }
158
180
  EOF
159
181
  end
160
182
 
161
183
  def self.post_new_job job_name, xml_file, base_url, user, token
162
- system %Q(curl -s -XPOST "#{base_url}/createItem?name=#{job_name}" --data-binary "@#{xml_file}" -H "Content-Type:text/xml" --user "#{user}:#{token}")
184
+ # system %Q(curl -s -XPOST "#{base_url}/createItem?name=#{job_name}" --data-binary "@#{xml_file}" -H "Content-Type:text/xml" --user "#{user}:#{token}")
185
+ req = RestClient::Request.new(
186
+ method: :post,
187
+ user: user,
188
+ password: token,
189
+ url: "#{base_url}/createItem?name=#{job_name}",
190
+ timeout: 30,
191
+ headers: {
192
+ "Content-Type" => "text/xml",
193
+ },
194
+ payload: File.read(xml_file)
195
+ )
196
+
197
+ begin
198
+ res = req.execute
199
+ rescue RestClient::ExceptionWithResponse => err
200
+ case err.http_code
201
+ when 301, 302, 307
202
+ err.response.follow_redirection
203
+ else
204
+ raise
205
+ end
206
+ end
207
+
163
208
  end
164
209
 
165
210
  def self.download_job job_name, xml_file, base_url, user, token
166
- system %Q(curl -s "#{base_url}/job/#{job_name}/config.xml" -o #{xml_file} --user "#{user}:#{token}")
211
+ # system %Q(curl -s "#{base_url}/job/#{job_name}/config.xml" -o #{xml_file} --user "#{user}:#{token}")
212
+ req = RestClient::Request.new(
213
+ method: :get,
214
+ user: user,
215
+ password: token,
216
+ url: "#{base_url}/job/#{job_name}/config.xml",
217
+ timeout: 30
218
+ )
219
+
220
+ res = req.execute
221
+
222
+ File.open xml_file, "w" do |fh|
223
+ fh.puts res.body
224
+ end
167
225
  end
168
226
 
169
227
  def self.delete! job_name, base_url, user, token
170
- system %Q(curl -XPOST "#{base_url}/job/#{job_name}/doDelete" --user "#{user}:#{token}")
228
+ # system %Q(curl -XPOST "#{base_url}/job/#{job_name}/doDelete" --user "#{user}:#{token}")
229
+ req = RestClient::Request.new(
230
+ method: :post,
231
+ user: user,
232
+ password: token,
233
+ url: "#{base_url}/job/#{job_name}/doDelete",
234
+ timeout: 30
235
+ )
236
+
237
+ begin
238
+ res = req.execute
239
+ rescue RestClient::ExceptionWithResponse => err
240
+ case err.http_code
241
+ when 301, 302, 307
242
+ err.response.follow_redirection
243
+ else
244
+ raise
245
+ end
246
+ end
171
247
  end
172
248
 
173
249
  def self.trigger_build job_name,build_token, base_url
174
- system %Q(curl "#{base_url}/job/#{job_name}/build?token=#{build_token}")
250
+ # system %Q(curl "#{base_url}/job/#{job_name}/build?token=#{build_token}")
251
+ req = RestClient::Request.new(
252
+ method: :get,
253
+ url: "#{base_url}/job/#{job_name}/build?token=#{build_token}",
254
+ timeout: 30
255
+ )
256
+ res = req.execute
175
257
  end
176
258
 
177
259
  end
@@ -195,7 +277,7 @@ module GitlabTools
195
277
  end
196
278
 
197
279
  secret_token = token_shared_persistently if secret_token.nil?
198
-
280
+
199
281
  Gitlab.add_project_hook project.id, hooked_url, :push_events => 1,:enable_ssl_verification=>0, :token=> secret_token
200
282
 
201
283
  end
@@ -207,7 +289,7 @@ module GitlabTools
207
289
  p.name== repo_name
208
290
  end
209
291
  if project.nil?
210
- puts "repo #{repo_name} doesn't exists"
292
+ puts "repo #{repo_name} doesn't exists"
211
293
  return
212
294
  end
213
295
 
@@ -219,15 +301,17 @@ module Builder
219
301
  def self.create_env app_name
220
302
  _write ".env.build", <<~EOF
221
303
  IMAGE_NAME=#{app_name}
222
-
223
- PRIVATE_DOCKER_REGISTRY_NAME=mycluster.icp
304
+
305
+ PRIVATE_DOCKER_REGISTRY_NAME=#{ENV["ICP_REGISTRY_HOSTNAME"]}
224
306
  PRIVATE_DOCKER_REGISTRY_PORT=8500
225
307
  PRIVATE_DOCKER_REGISTRY_IP=#{ENV["ICP_MASTER_IP"]}
226
308
  PRIVATE_DOCKER_REGISTRY_NAMESPACE=#{ENV["PRIVATE_DOCKER_REGISTRY_NAMESPACE"]}
227
-
309
+
228
310
  PRIVATE_DOCKER_REGISTRY_USER=admin
229
311
  PRIVATE_DOCKER_REGISTRY_USER_PASSWORD=admin
230
-
312
+
313
+ PRIVATE_DOCKER_REGISTRY_PULL_SECRET=#{ENV["IMAGE_PULL_SECRET"]}
314
+
231
315
  K8S_NAMESPACE=#{ENV["K8S_NAMESPACE"]}
232
316
  EOF
233
317
  end
@@ -240,7 +324,7 @@ module Builder
240
324
 
241
325
  require_relative "docker.rb"
242
326
  require_relative "k8s.rb"
243
-
327
+
244
328
  @task_index=0
245
329
  def next_task_index
246
330
  @task_index += 1
@@ -291,14 +375,14 @@ module Builder
291
375
  etc_hosts_entry = sprintf("%s %s", ENV["PRIVATE_DOCKER_REGISTRY_IP"], ENV["PRIVATE_DOCKER_REGISTRY_NAME"])
292
376
  Shell.run %Q(echo "\#{etc_hosts_entry}" >> /etc/hosts)
293
377
  end
294
-
378
+
295
379
  def self.push_to_registry image_name, tag
296
380
  private_registry = sprintf("%s:%s", ENV["PRIVATE_DOCKER_REGISTRY_NAME"], ENV["PRIVATE_DOCKER_REGISTRY_PORT"])
297
381
  namespace = ENV["PRIVATE_DOCKER_REGISTRY_NAMESPACE"]
298
382
 
299
383
  cmds = ShellCommandConstructor.construct_command %Q{
300
384
  docker login -u \#{ENV["PRIVATE_DOCKER_REGISTRY_USER"]} -p \#{ENV["PRIVATE_DOCKER_REGISTRY_USER_PASSWORD"]} \#{private_registry}
301
-
385
+
302
386
  docker tag \#{image_name}:\#{tag} \#{private_registry}/\#{namespace}/\#{image_name}:\#{tag}
303
387
  docker push \#{private_registry}/\#{namespace}/\#{image_name}:\#{tag}
304
388
  }
@@ -306,31 +390,41 @@ module Builder
306
390
  end
307
391
  end
308
392
  EOF
309
-
393
+
310
394
  _write lib_dir + '/k8s.rb', <<~EOF
311
395
  require "erb"
312
396
  require_relative "shell"
313
397
 
314
398
  module KubeTools
399
+ def self.create_namespace namespace
400
+ Shell.run %Q(kubectl create namespace \#{namespace} || echo ignore exists error)
401
+ end
402
+
403
+ def self.create_pull_secret namespace, user, pass, secret_name
404
+ Shell.run %Q(kubectl delete secret \#{secret_name} -n \#{namespace} || echo ignore non exists error)
405
+ Shell.run %Q(kubectl create secret docker-registry \#{secret_name} -n \#{namespace} --docker-server=\#{ENV["PRIVATE_DOCKER_REGISTRY_NAME"]}:\#{ENV["PRIVATE_DOCKER_REGISTRY_PORT"]} --docker-username=\#{ENV["PRIVATE_DOCKER_REGISTRY_USER"]} --docker-password=\#{ENV["PRIVATE_DOCKER_REGISTRY_USER_PASSWORD"]} --docker-email=\#{ENV["PRIVATE_DOCKER_REGISTRY_USER"]}@\#{ENV["PRIVATE_DOCKER_REGISTRY_NAME"]})
406
+ end
407
+
408
+
315
409
  def self.create_new_yaml yaml_template_file, yaml_file, data = {}
316
410
  erb = ERB.new(File.read(yaml_template_file))
317
411
  b = binding
318
-
412
+
319
413
  data.each_pair do |key, value|
320
414
  b.local_variable_set(key, value)
321
415
  end
322
-
416
+
323
417
  File.open yaml_file, "w" do |fh|
324
418
  fh.puts erb.result(b)
325
419
  end
326
420
  end
327
421
 
328
- def self.deploy_to_k8s namespace, deployment, yaml_file, image_name, new_image_name
422
+ def self.deploy_to_k8s yaml_file
329
423
  Shell.run %Q(kubectl apply -f \#{yaml_file})
330
424
  end
331
425
  end
332
426
  EOF
333
-
427
+
334
428
  end
335
429
 
336
430
  def self.create_rakefile
@@ -353,6 +447,7 @@ module Builder
353
447
  desc "push to ICp registry"
354
448
  task "\#{next_task_index}_push_to_ICp_registry" do
355
449
  DockerTools.add_etc_hosts
450
+ KubeTools.create_namespace ENV["K8S_NAMESPACE"]
356
451
  DockerTools.push_to_registry image_name, tag
357
452
  end
358
453
  end
@@ -362,11 +457,14 @@ module Builder
362
457
 
363
458
  desc "deploy into k8s"
364
459
  task "\#{next_task_index}_deploy_to_k8s" do
460
+ KubeTools.create_pull_secret ENV["K8S_NAMESPACE"], ENV["PRIVATE_DOCKER_REGISTRY_USER"], ENV["PRIVATE_DOCKER_REGISTRY_USER_PASSWORD"], ENV["PRIVATE_DOCKER_REGISTRY_PULL_SECRET"]
461
+
365
462
  yaml_template_file = "\#{image_name}.k8.template.yaml"
366
463
  yaml_file = "\#{image_name}.yaml"
367
464
 
368
465
  private_registry = sprintf("%s:%s", ENV["PRIVATE_DOCKER_REGISTRY_NAME"], ENV["PRIVATE_DOCKER_REGISTRY_PORT"])
369
466
  namespace = ENV["PRIVATE_DOCKER_REGISTRY_NAMESPACE"]
467
+
370
468
  full_new_image_name = "\#{private_registry}/\#{namespace}/\#{image_name}:\#{tag}"
371
469
  data = {
372
470
  new_image: full_new_image_name
@@ -374,11 +472,9 @@ module Builder
374
472
 
375
473
  KubeTools.create_new_yaml yaml_template_file, yaml_file, data
376
474
 
377
- deployment = image_name
378
- KubeTools.deploy_to_k8s ENV["K8S_NAMESPACE"], deployment, yaml_file, image_name, full_new_image_name
379
-
475
+ KubeTools.deploy_to_k8s yaml_file
380
476
  end
381
- end
477
+ end
382
478
  OUTEOF
383
479
  end
384
480
 
@@ -388,13 +484,13 @@ module Builder
388
484
  FROM bitnami/minideb
389
485
  ADD exe /
390
486
  ENV LISTENING_PORT 80
391
-
487
+
392
488
  CMD ["/exe"]
393
489
  EOF
394
490
  end
395
491
  end
396
492
 
397
- def self.create_k8_file namespace, app_name
493
+ def self.create_k8_file namespace, app_name, pull_secret=ENV["IMAGE_PULL_SECRET"]
398
494
  _write "#{app_name}.k8.template.yaml", <<~EOF
399
495
  apiVersion: extensions/v1beta1
400
496
  kind: Deployment
@@ -415,7 +511,7 @@ module Builder
415
511
  - name: #{app_name}
416
512
  image: <%= new_image %>
417
513
  imagePullSecrets:
418
- - name: admin.registrykey
514
+ - name: #{pull_secret}
419
515
  ---
420
516
  apiVersion: v1
421
517
  kind: Service
data/lib/citasks.rb CHANGED
@@ -22,16 +22,16 @@ namespace "init" do
22
22
  JENKINS_USER = wenzm
23
23
  JENKINS_USER_API_TOKEN = f432de6a2fbeaaf58757f76194dcd825
24
24
 
25
- JENKIN_PROJECT_ENDPOINT_AUTHENTICATION = false
26
-
27
25
  JOB_NAME=#{project}
28
26
  REPO_NAME=#{project}
29
27
 
30
28
  COMPILER_DOCKER_IMAGE=maven:3.5-jdk-8
31
29
 
32
30
  #for private docker registry
31
+ ICP_REGISTRY_HOSTNAME=dev.icp
33
32
  ICP_MASTER_IP=192.168.10.100
34
-
33
+ IMAGE_PULL_SECRET=admin.regkey
34
+
35
35
  K8S_NAMESPACE=default
36
36
  EOF
37
37
 
@@ -42,7 +42,7 @@ namespace "init" do
42
42
  content =<<~EOF
43
43
  .token
44
44
  EOF
45
-
45
+
46
46
  fh.puts content
47
47
  end
48
48
  end
@@ -60,6 +60,10 @@ namespace "Jenkins" do
60
60
  JenkinsTools.gen_jenkins_file
61
61
  end
62
62
 
63
+ task "download_job_as_xml" do
64
+ JenkinsTools.download_job job_name, job_name + ".server.xml", ENV["JENKINS_URL"], ENV["JENKINS_USER"], ENV["JENKINS_USER_API_TOKEN"]
65
+ end
66
+
63
67
  desc "create a new project #{job_name}"
64
68
  task "#{next_task_index}_create_new_project" do
65
69
  xml_file = job_name + ".xml"
@@ -72,7 +76,13 @@ namespace "Jenkins" do
72
76
  Builder.create_lib_files
73
77
  Builder.create_rakefile
74
78
  Builder.create_k8_file ENV["K8S_NAMESPACE"] || "default",job_name
75
- Builder.create_dockerfile
79
+ Builder.create_dockerfile
80
+ end
81
+
82
+ desc "create a new project with existing job xml. No other scaffolding files"
83
+ task "#{next_task_index}_create_new_project_with_job_xml" do
84
+ xml_file = job_name + ".xml"
85
+ JenkinsTools.post_new_job job_name, xml_file, ENV["JENKINS_URL"], ENV["JENKINS_USER"], ENV["JENKINS_USER_API_TOKEN"]
76
86
  end
77
87
 
78
88
  desc "delete #{job_name}"
@@ -123,7 +133,7 @@ namespace "git" do
123
133
  msg = args.msg || "update"
124
134
  sh %Q(git add . && git commit -m "#{msg}")
125
135
  end
126
-
136
+
127
137
  desc "set remote origin to #{git_repo_url}"
128
138
  task "#{next_task_index}_set_remote_orgin" do
129
139
  sh %Q(git remote add origin #{git_repo_url})
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: citasks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zhimin Wen
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
27
41
  description: ci/cd tools for gitlab + jenkins. Libray and rake tasks
28
42
  email: zhimin.wen@gmail.com
29
43
  executables: []