Skip to content

Commit

Permalink
Drop tsuru_unit_agent unused code (#2686)
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Apr 9, 2024
1 parent 32141a6 commit 1ff3c39
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 126 deletions.
2 changes: 1 addition & 1 deletion api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,7 @@ func isInternalEnv(envKey string) bool {
}

func internalEnvs() []string {
return []string{"TSURU_APPNAME", "TSURU_APP_TOKEN", "TSURU_SERVICE", "TSURU_APPDIR"}
return []string{"TSURU_APPNAME", "TSURU_SERVICES", "TSURU_APPDIR"}
}

var envVarUnixLikeRegexp = regexp.MustCompile(`^[_a-zA-Z][_a-zA-Z0-9]*$`)
Expand Down
14 changes: 3 additions & 11 deletions api/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3265,9 +3265,6 @@ func (s *S) TestGetEnvAllEnvs(c *check.C) {
c.Assert(err, check.IsNil)
c.Assert(len(result), check.Equals, len(expected))
for _, r := range result {
if r.Name == "TSURU_APP_TOKEN" {
continue
}
for _, e := range expected {
if e.Name == r.Name {
c.Check(e.Public, check.Equals, r.Public)
Expand Down Expand Up @@ -3378,8 +3375,7 @@ func (s *S) TestSetEnvTsuruInternalEnvorimentVariableInApp(c *check.C) {
Envs: []apiTypes.Env{
{Name: "TSURU_APPNAME", Value: "everything-i-want", Alias: ""},
{Name: "TSURU_APPDIR", Value: "everything-i-want", Alias: ""},
{Name: "TSURU_APPN_TOKEN", Value: "everything-i-want", Alias: ""},
{Name: "TSURU_SERVICE", Value: "everything-i-want", Alias: ""},
{Name: "TSURU_SERVICES", Value: "everything-i-want", Alias: ""},
},
NoRestart: false,
Private: false,
Expand Down Expand Up @@ -4079,17 +4075,13 @@ func (s *S) TestIsEnvVarValid(c *check.C) {
}{
{},
{
envs: []apiTypes.Env{{Name: "TSURU_SERVICE"}},
expectedError: "cannot change an internal environment variable (TSURU_SERVICE): write-protected environment variable",
envs: []apiTypes.Env{{Name: "TSURU_SERVICES"}},
expectedError: "cannot change an internal environment variable (TSURU_SERVICES): write-protected environment variable",
},
{
envs: []apiTypes.Env{{Name: "TSURU_APPNAME"}},
expectedError: "cannot change an internal environment variable (TSURU_APPNAME): write-protected environment variable",
},
{
envs: []apiTypes.Env{{Name: "TSURU_APP_TOKEN"}},
expectedError: "cannot change an internal environment variable (TSURU_APP_TOKEN): write-protected environment variable",
},
{
envs: []apiTypes.Env{{Name: "TSURU_APPDIR"}},
expectedError: "cannot change an internal environment variable (TSURU_APPDIR): write-protected environment variable",
Expand Down
2 changes: 1 addition & 1 deletion app/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ var exportEnvironmentsAction = action.Action{
app := ctx.Params[0].(*App)
app, err := GetByName(ctx.Context, app.Name)
if err == nil {
vars := []string{"TSURU_APPNAME", "TSURU_APPDIR", "TSURU_APP_TOKEN"}
vars := []string{"TSURU_APPNAME", "TSURU_APPDIR"}
app.UnsetEnvs(bind.UnsetEnvArgs{
VariableNames: vars,
ShouldRestart: true,
Expand Down
2 changes: 1 addition & 1 deletion app/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (s *S) TestExportEnvironmentsForward(c *check.C) {

func (s *S) TestExportEnvironmentsBackward(c *check.C) {
envNames := []string{
"TSURU_APP_TOKEN",
"TSURU_APPNAME",
}
app := App{
Name: "moon",
Expand Down
3 changes: 0 additions & 3 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1785,7 +1785,6 @@ func (s *S) TestAddInstanceFirst(c *check.C) {
delete(allEnvs, tsuruEnvs.TsuruServicesEnvVar)
delete(allEnvs, "TSURU_APPDIR")
delete(allEnvs, "TSURU_APPNAME")
delete(allEnvs, "TSURU_APP_TOKEN")
c.Assert(allEnvs, check.DeepEquals, map[string]bindTypes.EnvVar{
"DATABASE_HOST": {
Name: "DATABASE_HOST",
Expand Down Expand Up @@ -3606,8 +3605,6 @@ func (s *S) TestListUsesCachedRouterAddrs(c *check.C) {
apps, err := List(context.TODO(), nil)
c.Assert(err, check.IsNil)
c.Assert(apps, check.HasLen, 2)
delete(apps[0].Env, "TSURU_APP_TOKEN")
delete(apps[1].Env, "TSURU_APP_TOKEN")
sort.Slice(apps, func(i, j int) bool {
return apps[i].Name < apps[j].Name
})
Expand Down
55 changes: 2 additions & 53 deletions provision/dockercommon/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
package dockercommon

import (
"errors"
"fmt"
"strings"

"github.com/tsuru/config"
"github.com/tsuru/tsuru/provision"
appTypes "github.com/tsuru/tsuru/types/app"
provTypes "github.com/tsuru/tsuru/types/provision"
Expand All @@ -33,53 +33,6 @@ func ContainerCmdsDataFromVersion(version appTypes.AppVersion) (ContainerCmdsDat
return cmdData, nil
}

// ArchiveBuildCmds build a image using the archive method.
func ArchiveBuildCmds(app provision.App, archiveURL string) []string {
return buildCmds(app, "build", "archive", archiveURL)
}

// DeployCmds deploys an unit builded by tsuru.
func DeployCmds(app provision.App) []string {
uaCmds := unitAgentCmds(app)
uaCmds = append(uaCmds, "deploy-only")
finalCmd := strings.Join(uaCmds, " ")
return []string{"/bin/sh", "-lc", finalCmd}
}

func buildCmds(app provision.App, agentCmd string, params ...string) []string {
deployCmd, err := config.GetString("docker:deploy-cmd")
if err != nil {
deployCmd = "/var/lib/tsuru/deploy"
}
cmds := append([]string{deployCmd}, params...)
uaCmds := unitAgentCmds(app)
uaCmds = append(uaCmds, `"`+strings.Join(cmds, " ")+`"`, agentCmd)
finalCmd := strings.Join(uaCmds, " ")
return []string{"/bin/sh", "-lc", finalCmd}
}

func unitAgentCmds(app provision.App) []string {
host, _ := config.GetString("host")
token := app.Envs()["TSURU_APP_TOKEN"].Value
return []string{"tsuru_unit_agent", host, token, app.GetName()}
}

// runWithAgentCmds returns the list of commands that should be passed when the
// provisioner will run a unit using tsuru_unit_agent to start.
//
// This will only be called for legacy containers that have not been re-
// deployed since the introduction of independent units per 'process' in
// 0.12.0.
func runWithAgentCmds(app provision.App) ([]string, error) {
runCmd, err := config.GetString("docker:run-cmd:bin")
if err != nil {
runCmd = "/var/lib/tsuru/start"
}
host, _ := config.GetString("host")
token := app.Envs()["TSURU_APP_TOKEN"].Value
return []string{"tsuru_unit_agent", host, token, app.GetName(), runCmd}, nil
}

func ProcessCmdForVersion(processName string, cmdData ContainerCmdsData) ([]string, string, error) {
if processName == "" {
if len(cmdData.processes) == 0 {
Expand All @@ -105,11 +58,7 @@ func LeanContainerCmds(processName string, cmdData ContainerCmdsData, app provis
return nil, "", err
}
if len(processCmd) == 0 {
// Legacy support, no processes are yet registered for this app's
// containers.
var cmds []string
cmds, err = runWithAgentCmds(app)
return cmds, "", err
return nil, "", errors.New("Legacy support of app's container are deprecated")
}
var extraCmds []string
if cmdData.yamlData.Hooks != nil {
Expand Down
56 changes: 0 additions & 56 deletions provision/dockercommon/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package dockercommon_test

import (
"context"
"fmt"
"testing"

"github.com/tsuru/config"
Expand All @@ -17,7 +16,6 @@ import (
"github.com/tsuru/tsuru/provision/provisiontest"
"github.com/tsuru/tsuru/servicemanager"
appTypes "github.com/tsuru/tsuru/types/app"
bindTypes "github.com/tsuru/tsuru/types/bind"
check "gopkg.in/check.v1"
)

Expand Down Expand Up @@ -50,38 +48,6 @@ func (s *S) SetUpTest(c *check.C) {
c.Assert(err, check.IsNil)
}

func (s *S) TestArchiveBuildCmds(c *check.C) {
app := provisiontest.NewFakeApp("app-name", "python", 1)
config.Set("host", "tsuru_host")
defer config.Unset("host")
tokenEnv := bindTypes.EnvVar{
Name: "TSURU_APP_TOKEN",
Value: "app_token",
Public: true,
}
app.SetEnv(tokenEnv)
archiveURL := "https://s3.amazonaws.com/wat/archive.tar.gz"
expectedPart1 := fmt.Sprintf("/var/lib/tsuru/deploy archive %s", archiveURL)
expectedAgent := fmt.Sprintf(`tsuru_unit_agent tsuru_host app_token app-name "%s" build`, expectedPart1)
cmds := dockercommon.ArchiveBuildCmds(app, archiveURL)
c.Assert(cmds, check.DeepEquals, []string{"/bin/sh", "-lc", expectedAgent})
}

func (s *S) TestDeployCmds(c *check.C) {
app := provisiontest.NewFakeApp("app-name", "python", 1)
config.Set("host", "tsuru_host")
defer config.Unset("host")
tokenEnv := bindTypes.EnvVar{
Name: "TSURU_APP_TOKEN",
Value: "app_token",
Public: true,
}
app.SetEnv(tokenEnv)
expectedAgent := "tsuru_unit_agent tsuru_host app_token app-name deploy-only"
cmds := dockercommon.DeployCmds(app)
c.Assert(cmds, check.DeepEquals, []string{"/bin/sh", "-lc", expectedAgent})
}

func newVersion(c *check.C, app appTypes.App, customData map[string]interface{}) appTypes.AppVersion {
version, err := servicemanager.AppVersion.NewAppVersion(context.TODO(), appTypes.NewVersionArgs{
App: app,
Expand Down Expand Up @@ -135,28 +101,6 @@ func (s *S) TestRunLeanContainersCmdHooks(c *check.C) {
c.Assert(cmds, check.DeepEquals, expected)
}

func (s *S) TestRunLeanContainersCmdNoProcesses(c *check.C) {
customData := map[string]interface{}{}
fakeApp := provisiontest.NewFakeApp("sample", "python", 0)
version := newVersion(c, fakeApp, customData)
app := provisiontest.NewFakeApp("app-name", "python", 1)
config.Set("host", "tsuru_host")
defer config.Unset("host")
tokenEnv := bindTypes.EnvVar{
Name: "TSURU_APP_TOKEN",
Value: "app_token",
Public: true,
}
app.SetEnv(tokenEnv)
cmdData, err := dockercommon.ContainerCmdsDataFromVersion(version)
c.Assert(err, check.IsNil)
cmds, process, err := dockercommon.LeanContainerCmds("", cmdData, app)
c.Assert(err, check.IsNil)
c.Assert(process, check.Equals, "")
expected := []string{"tsuru_unit_agent", "tsuru_host", "app_token", "app-name", "/var/lib/tsuru/start"}
c.Assert(cmds, check.DeepEquals, expected)
}

func (s *S) TestRunLeanContainersImplicitProcess(c *check.C) {
customData := map[string]interface{}{
"processes": map[string]interface{}{
Expand Down

0 comments on commit 1ff3c39

Please sign in to comment.