citasks 0.1.1 → 0.1.2

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 (5) hide show
  1. checksums.yaml +4 -4
  2. data/lib/citasks/ci_lib.rb +202 -15
  3. data/lib/citasks.rb +46 -7
  4. data/readme.md +10 -7
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 78164b53af04e75190a4116f08a5b148880fd564
4
- data.tar.gz: 70ac11f150c4a8a73c6924d7e86e3ec935c07432
3
+ metadata.gz: 61342eea10fb6965562862aa798de4148ba392fe
4
+ data.tar.gz: 8a38bf71b2c16b40d9ed4e133b3d8aad2ea668a5
5
5
  SHA512:
6
- metadata.gz: 5937ccb9144cb89fb5899db4482ae15aefc3940e9b1a7ccaa5a3fd4f71bfe91f6d519b56497f074fca1e141bc0c640e6f193fbf5866a2d43b82b0ef53526d511
7
- data.tar.gz: e070c096c11bfc0457caa172d543f67dd033a66eb33329f06029b0559b2a72a218fdd5526ec5d496c2d4a77e87742a18202e94f1cc1046801b850f145e9dc62e
6
+ metadata.gz: 57153b1f2ef44b3b34a23b8856a4feffc59b49ca05c4bb8e36a9334c4c74bfc0d11ca735c2e593df39316bcfafbc4070d11b3427dc741849f7446346e160fc84
7
+ data.tar.gz: 605d55609a5728ae6b2399a5585fe0b9394f12b646a4a9c10ed6cca9aaaf379ae93ab2718dc5caae326d1bddfc4c0925cc3071adeef2f64925b4652f22373e4c
@@ -1,4 +1,5 @@
1
1
  require 'gitlab'
2
+ require 'securerandom'
2
3
 
3
4
  module JenkinsTools
4
5
  WORKFLOW_PLUGIN = ENV["WORKFLOW_PLUGIN"] || "workflow-job@2.14.1"
@@ -80,14 +81,14 @@ module JenkinsTools
80
81
  //A Jenkinsfile for start
81
82
  podTemplate(label: 'my-pod',
82
83
  containers:[
83
- // containerTemplate(name: 'compiler', image:'compiler/image',ttyEnabled: true, command: 'cat', envVars:[
84
- // containerEnvVar(key: 'BUILD_NUMBER', value: env.BUILD_NUMBER),
85
- // containerEnvVar(key: 'BUILD_ID', value: env.BUILD_ID),
86
- // containerEnvVar(key: 'BUILD_URL', value: env.BUILD_URL),
87
- // containerEnvVar(key: 'BUILD_TAG', value: env.BUILD_TAG),
88
- // containerEnvVar(key: 'JOB_NAME', value: env.JOB_NAME)
89
- // ],
90
- // ),
84
+ containerTemplate(name: 'compiler', image:'#{ENV["COMPILER_DOCKER_IMAGE"]}',ttyEnabled: true, command: 'cat', envVars:[
85
+ containerEnvVar(key: 'BUILD_NUMBER', value: env.BUILD_NUMBER),
86
+ containerEnvVar(key: 'BUILD_ID', value: env.BUILD_ID),
87
+ containerEnvVar(key: 'BUILD_URL', value: env.BUILD_URL),
88
+ containerEnvVar(key: 'BUILD_TAG', value: env.BUILD_TAG),
89
+ containerEnvVar(key: 'JOB_NAME', value: env.JOB_NAME)
90
+ ],
91
+ ),
91
92
  containerTemplate(name: 'citools', image:'zhiminwen/citools',ttyEnabled: true, command: 'cat', envVars:[
92
93
  // these env is only available in container template? podEnvVar deosn't work?!
93
94
  containerEnvVar(key: 'BUILD_NUMBER', value: env.BUILD_NUMBER),
@@ -107,20 +108,22 @@ module JenkinsTools
107
108
  stage('clone git repo'){
108
109
  checkout scm
109
110
 
110
- // container('compiler'){
111
- // stage('Compile and Build'){
112
- // sh("echo compile")
113
- // }
114
- // }
111
+ container('compiler'){
112
+ stage('Compile and Build'){
113
+ sh("echo compile")
114
+ }
115
+ }
115
116
 
116
117
  container('citools'){
117
118
  stage('Docker Build'){
118
119
  // sleep 3600
119
- sh "echo build docker image"
120
+ sh "echo build docker image"
121
+ // sh "rake -f build.rb docker:01_build_image docker:02_push_to_ICp_registry"
120
122
  }
121
123
 
122
124
  stage('Deploy into k8s'){
123
125
  sh "echo rollout to k8s"
126
+ // sh "rake -f build.rb k8s:01_deploy_to_k8s"
124
127
  }
125
128
  }
126
129
  }
@@ -176,6 +179,190 @@ module GitlabTools
176
179
 
177
180
  def self.delete! repo_name, gitlab_url, token
178
181
  _setup_gitlab gitlab_url, token
179
- Gitlab.delete_project repo_name
182
+
183
+ project = Gitlab.projects.find do |p|
184
+ p.name== repo_name
185
+ end
186
+ if project.nil?
187
+ puts "repo #{repo_name} doesn't exists"
188
+ return
189
+ end
190
+
191
+ Gitlab.delete_project project.id
192
+ end
193
+ end
194
+
195
+ module Builder
196
+ def self.create_env app_name
197
+ envs = <<~EOF
198
+ IMAGE_NAME=#{app_name}
199
+
200
+ PRIVATE_DOCKER_REGISTRY_NAME=master.cfc
201
+ PRIVATE_DOCKER_REGISTRY_PORT=8500
202
+ PRIVATE_DOCKER_REGISTRY_IP=#{ENV["ICP_MASTER_IP"]}
203
+
204
+ PRIVATE_DOCKER_REGISTRY_USER=admin
205
+ PRIVATE_DOCKER_REGISTRY_USER_PASSWORD=admin
206
+
207
+ EOF
208
+
209
+ File.open ".env.build", "w" do |fh|
210
+ fh.puts envs
211
+ end
212
+ end
213
+
214
+ def self.create_rakefile
215
+ content = <<~OUTEOF
216
+ require 'sshkit_addon'
217
+ require 'dotenv'
218
+ require "yaml"
219
+
220
+ Dotenv.load ".env.build"
221
+
222
+ @task_index=0
223
+ def next_task_index
224
+ @task_index += 1
225
+ sprintf("%02d", @task_index)
226
+ end
227
+
228
+ image_name = ENV["IMAGE_NAME"]
229
+ tag=ENV["BUILD_NUMBER"]||"B1"
230
+
231
+ namespace "docker" do
232
+ @task_index=0
233
+ desc "build docker image"
234
+ task "\#{next_task_index}_build_image" do
235
+ sh %Q(env)
236
+ sh %Q(docker build -t \#{image_name}:\#{tag} .)
237
+ end
238
+
239
+ desc "push to ICp registry"
240
+ task "\#{next_task_index}_push_to_ICp_registry" do
241
+ etc_hosts_entry = sprintf("%s %s", ENV["PRIVATE_DOCKER_REGISTRY_IP"], ENV["PRIVATE_DOCKER_REGISTRY_NAME"])
242
+ private_registry = sprintf("%s:%s", ENV["PRIVATE_DOCKER_REGISTRY_NAME"], ENV["PRIVATE_DOCKER_REGISTRY_PORT"])
243
+
244
+ cmds = ShellCommandConstructor.construct_command %Q{
245
+ echo "\#{etc_hosts_entry}" >> /etc/hosts
246
+ docker login -u \#{ENV["PRIVATE_DOCKER_REGISTRY_USER"]} -p \#{ENV["PRIVATE_DOCKER_REGISTRY_USER"]} \#{private_registry}
247
+
248
+ docker tag \#{image_name}:\#{tag} \#{private_registry}/default/\#{image_name}:\#{tag}
249
+ docker push \#{private_registry}/default/\#{image_name}:\#{tag}
250
+ }
251
+ sh cmds
252
+ end
253
+ end
254
+
255
+ namespace "k8s" do
256
+ @task_index=0
257
+ desc "deploy into k8s"
258
+ task "\#{next_task_index}_deploy_to_k8s" do
259
+ file = "\#{image_name}.k8.template.yaml"
260
+ docs = YAML.load_stream File.read(file)
261
+ private_registry = sprintf("%s:%s", ENV["PRIVATE_DOCKER_REGISTRY_NAME"], ENV["PRIVATE_DOCKER_REGISTRY_PORT"])
262
+ new_image_name = "\#{private_registry}/default/\#{image_name}:\#{tag}"
263
+
264
+ File.open yaml_file = "\#{image_name}.yaml", "w" do |fh|
265
+ docs.each do |doc|
266
+ if doc["kind"] == "Deployment"
267
+ doc["spec"]["template"]["spec"]["containers"][0]["image"] = new_image_name
268
+ end
269
+ fh.puts doc.to_yaml
270
+ end
271
+ end
272
+
273
+ deployment = image_name
274
+ sh %Q(kubectl get deployment | grep \#{deployment} ) do |ok, res|
275
+ if ok #already exists
276
+ sh %Q(kubectl apply -f \#{yaml_file})
277
+ sh %Q(kubectl set image deployment/\#{deployment} \#{image_name}=\#{new_image_name})
278
+
279
+ sh %Q(kubectl rollout status deployment/\#{deployment})
280
+ else
281
+ puts "no deployment yet. create it"
282
+ sh %Q(kubectl create -f \#{yaml_file} --record)
283
+ end
284
+ end
285
+
286
+ end
287
+ end
288
+ OUTEOF
289
+
290
+ File.open "build.rb", "w" do |fh|
291
+ fh.puts content
292
+ end
293
+ end
294
+
295
+ def self.create_dockerfile
296
+ File.open "Dockerfile", "w" do |fh|
297
+ fh.puts <<~EOF
298
+ FROM bitnami/minideb
299
+ ADD exe /
300
+ ENV LISTENING_PORT 80
301
+
302
+ CMD ["/exe"]
303
+ EOF
304
+ end
305
+ end
306
+
307
+ def self.create_k8_file app_name
308
+ content = <<~EOF
309
+ apiVersion: extensions/v1beta1
310
+ kind: Deployment
311
+ metadata:
312
+ name: #{app_name}
313
+ labels:
314
+ app: #{app_name}
315
+ type: jenkins-build
316
+ spec:
317
+ replicas: 2
318
+ template:
319
+ metadata:
320
+ labels:
321
+ app: #{app_name}
322
+ spec:
323
+ containers:
324
+ - name: #{app_name}
325
+ #this will be replaced dynamically in the deployment
326
+ image: #{app_name}:latest
327
+ imagePullSecrets:
328
+ - name: admin.registrykey
329
+ ---
330
+ apiVersion: v1
331
+ kind: Service
332
+ metadata:
333
+ name: #{app_name}
334
+ labels:
335
+ app: #{app_name}
336
+ spec:
337
+ type: NodePort
338
+ ports:
339
+ - port: 80
340
+ targetPort: 80
341
+ protocol: TCP
342
+ name: http
343
+ selector:
344
+ app: #{app_name}
345
+ ---
346
+ apiVersion: extensions/v1beta1
347
+ kind: Ingress
348
+ metadata:
349
+ name: #{app_name}-ingress
350
+ labels:
351
+ app: #{app_name}-ingress
352
+ spec:
353
+ rules:
354
+ - host: k8s.myvm.io
355
+ http:
356
+ paths:
357
+ - path: /
358
+ backend:
359
+ serviceName: #{app_name}
360
+ servicePort: http
361
+ EOF
362
+
363
+ File.open "#{app_name}.k8.template.yaml", "w" do |fh|
364
+ fh.puts content
365
+ end
366
+
180
367
  end
181
368
  end
data/lib/citasks.rb CHANGED
@@ -9,17 +9,25 @@ namespace "Jenkins" do
9
9
  sprintf("%s/%s/%s.git",ENV["GITLAB_IN_CLUSTER_BASE_URL"], ENV["GITLAB_USER"], ENV["REPO_NAME"])
10
10
  end
11
11
 
12
- desc "create a new project"
12
+ task :gen_Jenkinsfile do
13
+ JenkinsTools.gen_jenkins_file
14
+ end
15
+
16
+ desc "create a new project #{job_name}"
13
17
  task "#{next_task_index}_create_new_project" do
14
18
  xml_file = job_name + ".xml"
15
19
  JenkinsTools.gen_job_xml job_name, xml_file, git_repo_url_in_cluster, ENV["JENKINS_GIT_USER_CREDENTIAL_ID"]
16
-
17
20
  JenkinsTools.post_new_job job_name, xml_file, ENV["JENKINS_URL"], ENV["JENKINS_USER"], ENV["JENKINS_USER_API_TOKEN"]
18
21
 
19
22
  JenkinsTools.gen_jenkins_file
23
+
24
+ Builder.create_env job_name
25
+ Builder.create_rakefile
26
+ Builder.create_k8_file job_name
27
+ Builder.create_dockerfile
20
28
  end
21
29
 
22
- desc "delete"
30
+ desc "delete #{job_name}"
23
31
  task "#{next_task_index}_delete" do
24
32
  JenkinsTools.delete! job_name, ENV["JENKINS_URL"], ENV["JENKINS_USER"], ENV["JENKINS_USER_API_TOKEN"]
25
33
  end
@@ -34,7 +42,7 @@ namespace "Gitlab" do
34
42
  sprintf("%s/%s/%s.git",ENV["GITLAB_BASE_URL"], ENV["GITLAB_USER"], ENV["REPO_NAME"])
35
43
  end
36
44
 
37
- desc "create a new gitlab repo"
45
+ desc "create a new gitlab repo of #{repo_name}"
38
46
  task "#{next_task_index}_create_new_repo" do
39
47
  GitlabTools.new_repo repo_name, ENV["GITLAB_BASE_URL"], ENV["GITLAB_API_TOKEN"]
40
48
  end
@@ -47,8 +55,39 @@ namespace "Gitlab" do
47
55
  GitlabTools.setup_hook repo_name, ENV["GITLAB_BASE_URL"], ENV["GITLAB_API_TOKEN"],hooked_jenkins_url
48
56
  end
49
57
 
50
- desc "delete"
58
+ desc "delete #{repo_name}"
51
59
  task "#{next_task_index}_delete" do
52
- GitlabTools.delete repo_name, ENV["GITLAB_BASE_URL"], ENV["GITLAB_API_TOKEN"]
60
+ GitlabTools.delete! repo_name, ENV["GITLAB_BASE_URL"], ENV["GITLAB_API_TOKEN"]
61
+ end
62
+ end
63
+
64
+ namespace "git" do
65
+ @task_index = 0
66
+
67
+ repo_name=ENV["REPO_NAME"]
68
+
69
+ def git_repo_url
70
+ sprintf("%s/%s/%s.git",ENV["GITLAB_BASE_URL"], ENV["GITLAB_USER"], ENV["REPO_NAME"])
71
+ end
72
+
73
+ desc "add and commit"
74
+ task "#{next_task_index}_commit", [:msg] do |t, args|
75
+ msg = args.msg || "update"
76
+ sh %Q(git add . && git commit -m "#{msg}")
77
+ end
78
+
79
+ desc "set remote origin to #{git_repo_url}"
80
+ task "#{next_task_index}_set_remote_orgin" do
81
+ sh %Q(git remote add origin #{git_repo_url})
53
82
  end
54
- end
83
+
84
+ desc "reset remote url #{git_repo_url}"
85
+ task "#{next_task_index}_set_remote_url" do
86
+ sh %Q(git remote set-url origin #{git_repo_url})
87
+ end
88
+
89
+ desc "push"
90
+ task "#{next_task_index}_push" do
91
+ sh %Q(git push -u origin master)
92
+ end
93
+ end
data/readme.md CHANGED
@@ -15,11 +15,11 @@ require 'citasks'
15
15
  ## Usage
16
16
  There is a rake task library that you can require to start
17
17
 
18
- In your rake task file
18
+ In your rake task file. Note the order is after the loading of the env
19
19
  ```
20
- require 'citasks'
21
20
  require 'dotenv'
22
21
  Dotenv.load
22
+ require 'citasks'
23
23
  ```
24
24
 
25
25
  In the .env file define the following
@@ -32,7 +32,6 @@ GITLAB_BASE_URL=
32
32
  GITLAB_IN_CLUSTER_BASE_URL=
33
33
  GITLAB_API_TOKEN=
34
34
 
35
-
36
35
  JOB_NAME=
37
36
  JENKINS_URL=
38
37
  JENKINS_IN_CLUSTER_URL=
@@ -50,9 +49,13 @@ rake -T
50
49
  The tasks is shown as below,
51
50
 
52
51
  ```
53
- rake Gitlab:01_create_new_repo # create a new gitlab repo
52
+ rake Gitlab:01_create_new_repo # create a new gitlab repo of icp-demo-app
54
53
  rake Gitlab:02_webhook # setup webhook
55
- rake Gitlab:03_delete # delete
56
- rake Jenkins:01_create_new_project # create a new project
57
- rake Jenkins:02_delete # delete
54
+ rake Gitlab:03_delete # delete icp-demo-app
55
+ rake Jenkins:01_create_new_project # create a new project icp-demo-app
56
+ rake Jenkins:02_delete # delete icp-demo-app
57
+ rake git:01_commit[msg] # add and commit
58
+ rake git:02_set_remote_orgin # set remote origin to http://localhost...
59
+ rake git:03_set_remote_url # reset remote url http://localhost:312...
60
+ rake git:04_push # push
58
61
  ```
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.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zhimin Wen