diff --git a/build.libsonnet b/build.libsonnet new file mode 100644 index 0000000..f3f2b04 --- /dev/null +++ b/build.libsonnet @@ -0,0 +1,61 @@ +local build = import 'lib/build.libsonnet'; +local images = import 'lib/images.libsonnet'; +local environment = import 'lib/node_modules/@sigyl/jsonnet-drone-environment/environment.libsonnet'; +local compose = import 'lib/node_modules/@sigyl/jsonnet-compose/compose.libsonnet'; +local util = import 'lib/util.libsonnet'; +local login = import 'lib/login.libsonnet'; + + +function(config) + function( + buildSecrets, + ) + { + kind: 'pipeline', + type: 'docker', + name: 'build', + clone: { + disable: false, + depth: 0, + }, + services: [ + images(config).docker { + privileged: true, + volumes: [ + { + name: 'dockersock', + path: '/var/run', + }, + { + name: 'ca', + path: '/etc/docker/certs.d', + }, + { + name: 'daemonjson', + path: '/etc/docker/daemon.json', + }, + ], + }, + ], + volumes: [ + { + name: 'dockersock', + temp: {}, + }, + { + name: 'ca', + host: { + path: '/etc/docker/certs.d', + }, + }, + { + name: 'daemonjson', + host: { + path: '/etc/docker/daemon.json', + }, + }, + ], + steps:[ + build(config)(buildSecrets), + ], + } diff --git a/deploy.libsonnet b/deploy.libsonnet index a37a68d..0b44d21 100644 --- a/deploy.libsonnet +++ b/deploy.libsonnet @@ -3,12 +3,11 @@ local images = import 'lib/images.libsonnet'; local environment = import 'lib/node_modules/@sigyl/jsonnet-drone-environment/environment.libsonnet'; local compose = import 'lib/node_modules/@sigyl/jsonnet-compose/compose.libsonnet'; local util = import 'lib/util.libsonnet'; +local login = import 'lib/login.libsonnet'; function(config) function( - name, - root, buildSecrets, publicSecrets, secretSecrets, @@ -22,68 +21,10 @@ function(config) disable: false, depth: 0, }, - services: [ - images(config).docker { - privileged: true, - volumes: [ - { - name: 'dockersock', - path: '/var/run', - }, - { - name: 'ca', - path: '/etc/docker/certs.d', - }, - { - name: 'daemonjson', - path: '/etc/docker/daemon.json', - }, - ], - }, - ], - volumes: [ - { - name: 'dockersock', - temp: {}, - }, - { - name: 'ca', - host: { - path: '/etc/docker/certs.d', - }, - }, - { - name: 'daemonjson', - host: { - path: '/etc/docker/daemon.json', - }, - }, - ], steps:[ - compose( - std.map( - function(secret) util.printEnv('env-%s' % name, secret), - buildSecrets + - publicSecrets + - secretSecrets - ) - ) - ( - images(config).ssh { - settings +: { - script +: [ - 'rm -f env-%s' % name, - ], - }, - }, - ) { - name: 'print env', - }, images(config).scp( - '%(root)s%(name)s' % { root: root, name: name } + '/%(root)s/%(name)s' % config ), - images(config).wait(15), - build(config)(buildSecrets), compose( std.map( function(secret) environment.envSet(secret), @@ -107,8 +48,10 @@ function(config) ) + [ 'set -e', - 'cd %(root)s%(name)s' % { root: root, name: name }, - 'sh .drone/login.sh', + 'export NAME=%s' % config.name, + 'export ROOT=%s' % config.root, + 'cd /%(root)s/%(name)s' % config, + login, 'sh .drone/pull.sh', 'sh .drone/deploy.sh', ] @@ -116,4 +59,13 @@ function(config) }, ), ], - } + } { + trigger +: { + event +: [ + 'promote', + ], + target +: [ + 'deploy', + ], + }, + } diff --git a/lib/build.libsonnet b/lib/build.libsonnet index 23b62a3..e396aa3 100644 --- a/lib/build.libsonnet +++ b/lib/build.libsonnet @@ -1,5 +1,6 @@ local images = import 'images.libsonnet'; local environment = import 'node_modules/@sigyl/jsonnet-drone-environment/environment.libsonnet'; +local login = import 'login.libsonnet'; function(config) function(secrets) @@ -14,9 +15,10 @@ function(config) ], commands: [ 'set -e', - 'sh .drone/login.sh', + 'export NAME=%s' % config.name, + 'export ROOT=%s' % config.root, + login, 'sh .drone/build.sh', 'sh .drone/push.sh', - 'sh .drone/logout.sh', ], } \ No newline at end of file diff --git a/lib/login.libsonnet b/lib/login.libsonnet new file mode 100644 index 0000000..8a7a8ce --- /dev/null +++ b/lib/login.libsonnet @@ -0,0 +1,15 @@ +||| + n=0 + while : + do + docker login $${REGISTRY_DOMAIN}:$${REGISTRY_PORT}/ --username client --password "$${REGISTRY_PASSWORD}" \\ + && break # substitute your command here + n=$((n+1)) + if [ $n -ge 10 ]; then + echo "login failed" + exit 1 + fi + echo "retrying login..$n" + sleep 5 + done +||| diff --git a/print.libsonnet b/print.libsonnet new file mode 100644 index 0000000..7fc5cb4 --- /dev/null +++ b/print.libsonnet @@ -0,0 +1,53 @@ +local build = import 'lib/build.libsonnet'; +local images = import 'lib/images.libsonnet'; +local environment = import 'lib/node_modules/@sigyl/jsonnet-drone-environment/environment.libsonnet'; +local compose = import 'lib/node_modules/@sigyl/jsonnet-compose/compose.libsonnet'; +local util = import 'lib/util.libsonnet'; +local login = import 'lib/login.libsonnet'; + + +function(config) + function( + buildSecrets, + publicSecrets, + secretSecrets, + ) + { + kind: 'pipeline', + type: 'docker', + name: 'print', + clone: { + disable: false, + depth: 0, + }, + steps:[ + compose( + std.map( + function(secret) util.printEnv('env-%s' % config.name, secret), + buildSecrets + + publicSecrets + + secretSecrets + ) + ) + ( + images(config).ssh { + settings +: { + script +: [ + 'rm -f env-%s' % config.name, + ], + }, + }, + ) { + name: 'print env', + }, + ], + } { + trigger +: { + event +: [ + 'promote', + ], + target +: [ + 'print', + ], + }, + } diff --git a/registry.libsonnet b/registry.libsonnet index b96198d..dde3217 100644 --- a/registry.libsonnet +++ b/registry.libsonnet @@ -3,6 +3,7 @@ local images = import 'lib/images.libsonnet'; local environment = import 'lib/node_modules/@sigyl/jsonnet-drone-environment/environment.libsonnet'; local compose = import 'lib/node_modules/@sigyl/jsonnet-compose/compose.libsonnet'; local util = import 'lib/util.libsonnet'; +local login = import 'lib/login.libsonnet'; function(config) { @@ -10,8 +11,7 @@ function(config) type: 'docker', name: 'registry', clone: { - disable: false, - depth: 0, + disable: true, }, services: [ images(config).docker { @@ -50,9 +50,10 @@ function(config) }, }, ], - steps:[ - images(config).docker { - name +: 'pull and save docker images:', + steps: + std.map( + function(def) images(config).docker { + name: def.load, environment +: environment .environmentSecrets(config.secrets), volumes: [ @@ -63,12 +64,37 @@ function(config) ], commands: [ 'set -e', - 'export REGISTRY=$${REGISTRY_DOMAIN}:$${REGISTRY_PORT}/', - '%(script)s $${REGISTRY} "$${REGISTRY_PASSWORD}"' % config, + login, + ||| + n=0 + while : + do + docker pull %(load)s \\ + && docker tag %(load)s $${REGISTRY_DOMAIN}:$${REGISTRY_PORT}/%(root)s/%(name)s/%(save)s \\ + && docker push $${REGISTRY_DOMAIN}:$${REGISTRY_PORT}/%(root)s/%(name)s/%(save)s && break + n=$((n+1)) + if [ $n -ge 10 ]; then + echo "initialise failed" + exit 1 + fi + echo "retrying..$n" + sleep 5 + done + ||| % (config + def), ], }, - ], + config.images, + ), image_pull_secrets: [ 'dockerconfigjson' ] + } { + trigger +: { + event +: [ + 'promote', + ], + target +: [ + 'registry', + ], + }, } diff --git a/save.libsonnet b/save.libsonnet new file mode 100644 index 0000000..bd274d9 --- /dev/null +++ b/save.libsonnet @@ -0,0 +1,119 @@ +local build = import 'lib/build.libsonnet'; +local images = import 'lib/images.libsonnet'; +local environment = import 'lib/node_modules/@sigyl/jsonnet-drone-environment/environment.libsonnet'; +local compose = import 'lib/node_modules/@sigyl/jsonnet-compose/compose.libsonnet'; +local util = import 'lib/util.libsonnet'; +local login = import 'lib/login.libsonnet'; + + +function(config) + function( + defs, + builtDefs, + ) + { + kind: 'pipeline', + type: 'docker', + name: 'save', + clone: { + disable: true, + }, + steps: [ + images(config).ssh { + settings +: { + script +: [ + 'mkdir -p /%(root)s/.images/%(name)s/built' % config, + 'rm -f /%(root)s/.images/%(name)s/*.*' % config, + 'rm -f /%(root)s/.images/%(name)s/built/*.*' % config, + ], + }, + } + { + name: 'mkdir', + }, + ] + + std.map( + function(def) + images(config).ssh { + settings +: { + envs +: [ + 'registry_domain', + 'registry_port', + 'registry_password', + ], + script +: [ + login, + 'docker pull $${REGISTRY_DOMAIN}:$${REGISTRY_PORT}/%(root)s/%(name)s/%(save)s' % (config + def), + 'docker save $${REGISTRY_DOMAIN}:$${REGISTRY_PORT}/%(root)s/%(name)s/%(save)s -o /%(root)s/.images/%(name)s/%(out)s.tar' % ( + config + def + + { + out: std.strReplace(def.save, '/', '_'), + } + ), + 'echo "docker load %(out)s.tar" >> /%(root)s/.images/%(name)s/load.sh' % ( + config + { + out: std.strReplace(def.save, '/', '_'), + } + ), + 'echo "docker tag $${REGISTRY_DOMAIN}:$${REGISTRY_PORT}/%(root)s/%(name)s/%(pull)s /%(root)s/DOLLAR1/%(pull)s" >> /%(root)s/.images/%(name)s/load.sh' % ( + config + + { + pull: def.save, + } + ), + ], + }, + } + + { + name: def.save, + }, + defs + ) + + std.map( + function(def) + images(config).ssh { + settings +: { + envs +: [ + 'registry_domain', + 'registry_port', + 'registry_password', + ], + script +: [ + login, + 'docker pull $${REGISTRY_DOMAIN}:$${REGISTRY_PORT}/%(root)s/%(name)s/%(save)s' % (config + { save: def }), + 'docker save $${REGISTRY_DOMAIN}:$${REGISTRY_PORT}/%(root)s/%(name)s/%(save)s -o /%(root)s/.images/%(name)s/built/%(out)s.tar' % ( + config + + { + save: def, + out: std.strReplace(def, '/', '_'), + } + ), + 'echo "docker load %(out)s.tar" >> /%(root)s/.images/%(name)s/built/load.sh' % ( + config + { + out: std.strReplace(def, '/', '_'), + } + ), + 'echo "docker tag $${REGISTRY_DOMAIN}:$${REGISTRY_PORT}/%(root)s/%(name)s/%(pull)s /%(root)s/DOLLAR1/%(pull)s" >> /%(root)s/.images/%(name)s/built/load.sh' % ( + config + + { + pull: def, + } + ), + ], + }, + } + + { + name: def, + }, + builtDefs + ), + } { + trigger +: { + event +: [ + 'promote', + ], + target +: [ + 'sve', + ], + }, + }