Skip to content

Commit

Permalink
use docker run for agent update
Browse files Browse the repository at this point in the history
  • Loading branch information
hookenz committed Jan 10, 2024
1 parent 222d923 commit 7f71524
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
EDGE=1
EDGE_ID=789e6e1d-9bb5-43ba-8056-49038c5ec008
EDGE_KEY=aHR0cHM6Ly8xOTIuMTY4LjE2Ny4yMDc6OTQ0M3wxOTIuMTY4LjE2Ny4yMDc6ODAwMHxxTEtraFI4WGk5S1Fhbk5rSnJLZlhyRitHaEw4czE4cjVMUnpDMlRPb2F3PXwz
TZ=Pacific/Auckland
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ dist
/.vscode/
.idea
.DS_Store
go.work.sum
go.work.sum
.env
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ credential-helper: ## Build the credential helper (used by edge private registri
download-binaries: ## Download dependant binaries
@./setup.sh $(PLATFORM) $(ARCH)

dev-container: download-binaries ## Build and start a dev container (requires tilt. http://tilt.dev)
@tilt up

##@ Dependencies

tidy: ## Tidy up the go.mod file
Expand Down
45 changes: 45 additions & 0 deletions Tiltfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local_resource(
'build_agent',
cmd="make agent",
deps=['*.go',
'agent.go',
'build',
'chisel',
'cmd',
'config',
'constants',
'crypto',
'dist',
'docker',
'edge',
'exec',
'filesystem',
'ghw',
'healthcheck',
'http',
'internals',
'kubernetes',
'net',
'nomad',
'os',
'release.sh',
'serf',
'static'
],
ignore=[
'dist',
'/**/*_test.go'
],
)

docker_compose('docker-compose.yaml')

docker_build('portainer-agent',
'.',
only = ['dist', 'build', 'static', 'config'],
dockerfile='build/linux/alpine.Dockerfile',
live_update = [
sync('./dist', '/app'),
sync('./static', '/app/static'),
restart_container()
])
20 changes: 20 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# To customize this, add environment options in .env
# see .env.example for example

version: "3.9"
services:
agent:
image: portainer-agent
container_name: portainer_edge_agent_dev
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /var/lib/docker/volumes:/var/lib/docker/volumes
- /usr/share/zoneinfo:/usr/share/zoneinfo
- /:/host
- portainer_agent_data:/data
restart: always
env_file:
- .env

volumes:
portainer_agent_data:
8 changes: 8 additions & 0 deletions edge/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,14 @@ func (manager *StackManager) performActionOnStack(queueSleepInterval time.Durati
}
}

agentUpgrade := stack.EdgeUpdateID != 0
if agentUpgrade {
stack.EnvVars = append(stack.EnvVars, portainer.Pair{
Name: "_agentUpgrade",
Value: "true",
})
}

manager.deployStack(ctx, stack, stackName, stackFileLocation)
case actionDelete:
stackFileLocation = fmt.Sprintf("%s/%s", SuccessStackFileFolder(stack.FileFolder), stack.FileName)
Expand Down
16 changes: 16 additions & 0 deletions exec/docker_compose_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package exec

import (
"context"
"slices"

"github.com/portainer/agent"
libstack "github.com/portainer/portainer/pkg/libstack"
"github.com/portainer/portainer/pkg/libstack/compose"
"github.com/rs/zerolog/log"
)

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

// Deploy executes the docker stack deploy command.
func (service *DockerComposeStackService) Deploy(ctx context.Context, name string, filePaths []string, options agent.DeployOptions) error {

// TODO: this should probably be implemented as an edge job in future.
agentUpgrade := slices.Contains(options.Env, "_agentUpgrade")
if agentUpgrade {
log.Debug().Msgf("Detected portainer agent upgrade")
return service.deployer.Run(ctx, filePaths, "updater", libstack.RunOptions{
Options: libstack.Options{
ProjectName: name,
WorkingDir: options.WorkingDir,
Env: options.Env,
},
})
}

return service.deployer.Deploy(ctx, filePaths, libstack.DeployOptions{
Options: libstack.Options{
ProjectName: name,
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0
github.com/opencontainers/image-spec v1.1.0-rc5
github.com/pkg/errors v0.9.1
github.com/portainer/portainer v0.6.1-0.20231227185441-27e997fe0d1e
github.com/portainer/portainer v0.6.1-0.20240109020621-68dddbabbfaa
github.com/rs/zerolog v1.29.0
github.com/wI2L/jsondiff v0.2.0
gopkg.in/alecthomas/kingpin.v2 v2.2.6
Expand Down Expand Up @@ -116,7 +116,7 @@ require (
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.1.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.16.1 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.32.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/portainer/portainer v0.6.1-0.20231227185441-27e997fe0d1e h1:XWtJUsjPDJ8F2SsYBrPT+M9bleZG3wZx652jzCEB+zo=
github.com/portainer/portainer v0.6.1-0.20231227185441-27e997fe0d1e/go.mod h1:73Zf/oSGvxq8/u0AdmXGWwyiUytLz3LbUygJF3KjgmM=
github.com/portainer/portainer v0.6.1-0.20240109020621-68dddbabbfaa h1:1Khny+FwWWEkY8DYDBeHDCrRuoWlMMxtoQIZ52R22xM=
github.com/portainer/portainer v0.6.1-0.20240109020621-68dddbabbfaa/go.mod h1:L+FcDxUE5tztHmIZd8rCeuPwI15IOPgj7RXRSXTDiks=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
Expand Down Expand Up @@ -419,8 +419,8 @@ golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.1.0 h1:xYY+Bajn2a7VBmTM5GikTmnK8ZuX8YgnQCqZpbBNtmA=
golang.org/x/time v0.1.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
Expand Down

0 comments on commit 7f71524

Please sign in to comment.