Skip to content

Commit 7f71524

Browse files
committed
use docker run for agent update
1 parent 222d923 commit 7f71524

File tree

9 files changed

+104
-7
lines changed

9 files changed

+104
-7
lines changed

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
EDGE=1
2+
EDGE_ID=789e6e1d-9bb5-43ba-8056-49038c5ec008
3+
EDGE_KEY=aHR0cHM6Ly8xOTIuMTY4LjE2Ny4yMDc6OTQ0M3wxOTIuMTY4LjE2Ny4yMDc6ODAwMHxxTEtraFI4WGk5S1Fhbk5rSnJLZlhyRitHaEw4czE4cjVMUnpDMlRPb2F3PXwz
4+
TZ=Pacific/Auckland

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ dist
44
/.vscode/
55
.idea
66
.DS_Store
7-
go.work.sum
7+
go.work.sum
8+
.env

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ credential-helper: ## Build the credential helper (used by edge private registri
3333
download-binaries: ## Download dependant binaries
3434
@./setup.sh $(PLATFORM) $(ARCH)
3535

36+
dev-container: download-binaries ## Build and start a dev container (requires tilt. http://tilt.dev)
37+
@tilt up
38+
3639
##@ Dependencies
3740

3841
tidy: ## Tidy up the go.mod file

Tiltfile

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
local_resource(
2+
'build_agent',
3+
cmd="make agent",
4+
deps=['*.go',
5+
'agent.go',
6+
'build',
7+
'chisel',
8+
'cmd',
9+
'config',
10+
'constants',
11+
'crypto',
12+
'dist',
13+
'docker',
14+
'edge',
15+
'exec',
16+
'filesystem',
17+
'ghw',
18+
'healthcheck',
19+
'http',
20+
'internals',
21+
'kubernetes',
22+
'net',
23+
'nomad',
24+
'os',
25+
'release.sh',
26+
'serf',
27+
'static'
28+
],
29+
ignore=[
30+
'dist',
31+
'/**/*_test.go'
32+
],
33+
)
34+
35+
docker_compose('docker-compose.yaml')
36+
37+
docker_build('portainer-agent',
38+
'.',
39+
only = ['dist', 'build', 'static', 'config'],
40+
dockerfile='build/linux/alpine.Dockerfile',
41+
live_update = [
42+
sync('./dist', '/app'),
43+
sync('./static', '/app/static'),
44+
restart_container()
45+
])

docker-compose.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# To customize this, add environment options in .env
2+
# see .env.example for example
3+
4+
version: "3.9"
5+
services:
6+
agent:
7+
image: portainer-agent
8+
container_name: portainer_edge_agent_dev
9+
volumes:
10+
- /var/run/docker.sock:/var/run/docker.sock
11+
- /var/lib/docker/volumes:/var/lib/docker/volumes
12+
- /usr/share/zoneinfo:/usr/share/zoneinfo
13+
- /:/host
14+
- portainer_agent_data:/data
15+
restart: always
16+
env_file:
17+
- .env
18+
19+
volumes:
20+
portainer_agent_data:

edge/stack/stack.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,14 @@ func (manager *StackManager) performActionOnStack(queueSleepInterval time.Durati
358358
}
359359
}
360360

361+
agentUpgrade := stack.EdgeUpdateID != 0
362+
if agentUpgrade {
363+
stack.EnvVars = append(stack.EnvVars, portainer.Pair{
364+
Name: "_agentUpgrade",
365+
Value: "true",
366+
})
367+
}
368+
361369
manager.deployStack(ctx, stack, stackName, stackFileLocation)
362370
case actionDelete:
363371
stackFileLocation = fmt.Sprintf("%s/%s", SuccessStackFileFolder(stack.FileFolder), stack.FileName)

exec/docker_compose_stack.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package exec
22

33
import (
44
"context"
5+
"slices"
56

67
"github.com/portainer/agent"
78
libstack "github.com/portainer/portainer/pkg/libstack"
89
"github.com/portainer/portainer/pkg/libstack/compose"
10+
"github.com/rs/zerolog/log"
911
)
1012

1113
// DockerComposeStackService represents a service for managing stacks by using the Docker binary.
@@ -30,6 +32,20 @@ func NewDockerComposeStackService(binaryPath string) (*DockerComposeStackService
3032

3133
// Deploy executes the docker stack deploy command.
3234
func (service *DockerComposeStackService) Deploy(ctx context.Context, name string, filePaths []string, options agent.DeployOptions) error {
35+
36+
// TODO: this should probably be implemented as an edge job in future.
37+
agentUpgrade := slices.Contains(options.Env, "_agentUpgrade")
38+
if agentUpgrade {
39+
log.Debug().Msgf("Detected portainer agent upgrade")
40+
return service.deployer.Run(ctx, filePaths, "updater", libstack.RunOptions{
41+
Options: libstack.Options{
42+
ProjectName: name,
43+
WorkingDir: options.WorkingDir,
44+
Env: options.Env,
45+
},
46+
})
47+
}
48+
3349
return service.deployer.Deploy(ctx, filePaths, libstack.DeployOptions{
3450
Options: libstack.Options{
3551
ProjectName: name,

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ require (
2424
github.com/mitchellh/mapstructure v1.5.0
2525
github.com/opencontainers/image-spec v1.1.0-rc5
2626
github.com/pkg/errors v0.9.1
27-
github.com/portainer/portainer v0.6.1-0.20231227185441-27e997fe0d1e
27+
github.com/portainer/portainer v0.6.1-0.20240109020621-68dddbabbfaa
2828
github.com/rs/zerolog v1.29.0
2929
github.com/wI2L/jsondiff v0.2.0
3030
gopkg.in/alecthomas/kingpin.v2 v2.2.6
@@ -116,7 +116,7 @@ require (
116116
golang.org/x/sys v0.15.0 // indirect
117117
golang.org/x/term v0.15.0 // indirect
118118
golang.org/x/text v0.14.0 // indirect
119-
golang.org/x/time v0.1.0 // indirect
119+
golang.org/x/time v0.5.0 // indirect
120120
golang.org/x/tools v0.16.1 // indirect
121121
google.golang.org/appengine v1.6.8 // indirect
122122
google.golang.org/protobuf v1.32.0 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
277277
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
278278
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
279279
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
280-
github.com/portainer/portainer v0.6.1-0.20231227185441-27e997fe0d1e h1:XWtJUsjPDJ8F2SsYBrPT+M9bleZG3wZx652jzCEB+zo=
281-
github.com/portainer/portainer v0.6.1-0.20231227185441-27e997fe0d1e/go.mod h1:73Zf/oSGvxq8/u0AdmXGWwyiUytLz3LbUygJF3KjgmM=
280+
github.com/portainer/portainer v0.6.1-0.20240109020621-68dddbabbfaa h1:1Khny+FwWWEkY8DYDBeHDCrRuoWlMMxtoQIZ52R22xM=
281+
github.com/portainer/portainer v0.6.1-0.20240109020621-68dddbabbfaa/go.mod h1:L+FcDxUE5tztHmIZd8rCeuPwI15IOPgj7RXRSXTDiks=
282282
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
283283
github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM=
284284
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
@@ -419,8 +419,8 @@ golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
419419
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
420420
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
421421
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
422-
golang.org/x/time v0.1.0 h1:xYY+Bajn2a7VBmTM5GikTmnK8ZuX8YgnQCqZpbBNtmA=
423-
golang.org/x/time v0.1.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
422+
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
423+
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
424424
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
425425
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
426426
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=

0 commit comments

Comments
 (0)