feat: print, build, save

BREAKING_CHANGE: function parameters chaged
This commit is contained in:
Giles Bradshaw 2020-10-23 01:03:07 +01:00
parent 0aaec64873
commit fd13fe0e03
7 changed files with 302 additions and 74 deletions

61
build.libsonnet Normal file
View File

@ -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),
],
}

View File

@ -3,12 +3,11 @@ local images = import 'lib/images.libsonnet';
local environment = import 'lib/node_modules/@sigyl/jsonnet-drone-environment/environment.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 compose = import 'lib/node_modules/@sigyl/jsonnet-compose/compose.libsonnet';
local util = import 'lib/util.libsonnet'; local util = import 'lib/util.libsonnet';
local login = import 'lib/login.libsonnet';
function(config) function(config)
function( function(
name,
root,
buildSecrets, buildSecrets,
publicSecrets, publicSecrets,
secretSecrets, secretSecrets,
@ -22,68 +21,10 @@ function(config)
disable: false, disable: false,
depth: 0, 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:[ 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( images(config).scp(
'%(root)s%(name)s' % { root: root, name: name } '/%(root)s/%(name)s' % config
), ),
images(config).wait(15),
build(config)(buildSecrets),
compose( compose(
std.map( std.map(
function(secret) environment.envSet(secret), function(secret) environment.envSet(secret),
@ -107,8 +48,10 @@ function(config)
) + ) +
[ [
'set -e', 'set -e',
'cd %(root)s%(name)s' % { root: root, name: name }, 'export NAME=%s' % config.name,
'sh .drone/login.sh', 'export ROOT=%s' % config.root,
'cd /%(root)s/%(name)s' % config,
login,
'sh .drone/pull.sh', 'sh .drone/pull.sh',
'sh .drone/deploy.sh', 'sh .drone/deploy.sh',
] ]
@ -116,4 +59,13 @@ function(config)
}, },
), ),
], ],
} } {
trigger +: {
event +: [
'promote',
],
target +: [
'deploy',
],
},
}

View File

@ -1,5 +1,6 @@
local images = import 'images.libsonnet'; local images = import 'images.libsonnet';
local environment = import 'node_modules/@sigyl/jsonnet-drone-environment/environment.libsonnet'; local environment = import 'node_modules/@sigyl/jsonnet-drone-environment/environment.libsonnet';
local login = import 'login.libsonnet';
function(config) function(config)
function(secrets) function(secrets)
@ -14,9 +15,10 @@ function(config)
], ],
commands: [ commands: [
'set -e', 'set -e',
'sh .drone/login.sh', 'export NAME=%s' % config.name,
'export ROOT=%s' % config.root,
login,
'sh .drone/build.sh', 'sh .drone/build.sh',
'sh .drone/push.sh', 'sh .drone/push.sh',
'sh .drone/logout.sh',
], ],
} }

15
lib/login.libsonnet Normal file
View File

@ -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
|||

53
print.libsonnet Normal file
View File

@ -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',
],
},
}

View File

@ -3,6 +3,7 @@ local images = import 'lib/images.libsonnet';
local environment = import 'lib/node_modules/@sigyl/jsonnet-drone-environment/environment.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 compose = import 'lib/node_modules/@sigyl/jsonnet-compose/compose.libsonnet';
local util = import 'lib/util.libsonnet'; local util = import 'lib/util.libsonnet';
local login = import 'lib/login.libsonnet';
function(config) function(config)
{ {
@ -10,8 +11,7 @@ function(config)
type: 'docker', type: 'docker',
name: 'registry', name: 'registry',
clone: { clone: {
disable: false, disable: true,
depth: 0,
}, },
services: [ services: [
images(config).docker { images(config).docker {
@ -50,9 +50,10 @@ function(config)
}, },
}, },
], ],
steps:[ steps:
images(config).docker { std.map(
name +: 'pull and save docker images:', function(def) images(config).docker {
name: def.load,
environment +: environment environment +: environment
.environmentSecrets(config.secrets), .environmentSecrets(config.secrets),
volumes: [ volumes: [
@ -63,12 +64,37 @@ function(config)
], ],
commands: [ commands: [
'set -e', 'set -e',
'export REGISTRY=$${REGISTRY_DOMAIN}:$${REGISTRY_PORT}/', login,
'%(script)s $${REGISTRY} "$${REGISTRY_PASSWORD}"' % config, |||
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: [ image_pull_secrets: [
'dockerconfigjson' 'dockerconfigjson'
] ]
} {
trigger +: {
event +: [
'promote',
],
target +: [
'registry',
],
},
} }

119
save.libsonnet Normal file
View File

@ -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',
],
},
}