Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix AF object handling with podman container engine #1759

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/exec"

"github.com/astronomer/astro-cli/airflow/runtimes"
"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/stdcopy"
"github.com/pkg/errors"
Expand All @@ -17,8 +18,12 @@ const (
)

// AirflowCommand is the main method of interaction with Airflow
func AirflowCommand(id, airflowCommand string) string {
cmd := exec.Command("docker", "exec", "-it", id, "bash", "-c", airflowCommand)
func AirflowCommand(id, airflowCommand string) (string, error) {
containerRuntime, err := runtimes.GetContainerRuntimeBinary()
if err != nil {
return "", err
}
cmd := exec.Command(containerRuntime, "exec", "-it", id, "bash", "-c", airflowCommand)
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr

Expand All @@ -28,7 +33,7 @@ func AirflowCommand(id, airflowCommand string) string {
}

stringOut := string(out)
return stringOut
return stringOut, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noticed we're changing the function signature here and it got me wondering why this function didn't already return an error. Should we also return the error here on L32? - return errors.Wrapf(err, "error encountered executing airflow command") or something?

}

// ExecPipe does pipe stream into stdout/stdin and stderr
Expand Down
20 changes: 19 additions & 1 deletion docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import (
"bufio"
"bytes"
"fmt"
"os"
"strings"
"testing"

"github.com/astronomer/astro-cli/config"
testUtil "github.com/astronomer/astro-cli/pkg/testing"

"github.com/docker/docker/api/types"
"github.com/stretchr/testify/suite"
)
Expand All @@ -29,8 +33,22 @@ func (s *Suite) TestExecPipe() {
}

func (s *Suite) TestAirflowCommand() {
testUtil.InitTestConfig(testUtil.LocalPlatform)
s.Run("success", func() {
out := AirflowCommand("test-id", "-f docker_image_test.go")
err := config.CFG.DockerCommand.SetHomeString("docker")
s.NoError(err)
out, err := AirflowCommand("test-id", "-f docker_image_test.go")
s.NoError(err)
s.Empty(out)
})

s.Run("error", func() {
err := config.CFG.DockerCommand.SetHomeString("")
s.NoError(err)
err = os.Setenv("PATH", "") // set PATH to empty string to force error on container runtime check
s.NoError(err)
out, err := AirflowCommand("test-id", "-f docker_image_test.go")
s.Error(err)
s.Empty(out)
})
}
105 changes: 81 additions & 24 deletions settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,19 @@ func ConfigSettings(id, settingsFile string, envConns map[string]astrocore.Envir
return err
}
if pools {
AddPools(id, version)
if err := AddPools(id, version); err != nil {
return fmt.Errorf("error adding pools: %w", err)
}
}
if variables {
AddVariables(id, version)
if err := AddVariables(id, version); err != nil {
return fmt.Errorf("error adding variables: %w", err)
}
}
if connections {
AddConnections(id, version, envConns)
if err := AddConnections(id, version, envConns); err != nil {
return fmt.Errorf("error adding connections: %w", err)
}
}
return nil
}
Expand Down Expand Up @@ -105,7 +111,7 @@ func InitSettings(settingsFile string) error {
}

// AddVariables is a function to add Variables from settings.yaml
func AddVariables(id string, version uint64) {
func AddVariables(id string, version uint64) error {
variables := settings.Airflow.Variables
for _, variable := range variables {
if !objectValidator(0, variable.VariableName) {
Expand All @@ -123,15 +129,19 @@ func AddVariables(id string, version uint64) {
airflowCommand := fmt.Sprintf(baseCmd, variable.VariableName)

airflowCommand += fmt.Sprintf("'%s'", variable.VariableValue)
out := execAirflowCommand(id, airflowCommand)
out, err := execAirflowCommand(id, airflowCommand)
if err != nil {
return fmt.Errorf("Error adding variable %s: %w", variable.VariableName, err)
}
logrus.Debugf("Adding variable logs:\n" + out)
fmt.Printf("Added Variable: %s\n", variable.VariableName)
}
}
return nil
}

// AddConnections is a function to add Connections from settings.yaml
func AddConnections(id string, version uint64, envConns map[string]astrocore.EnvironmentObjectConnection) {
func AddConnections(id string, version uint64, envConns map[string]astrocore.EnvironmentObjectConnection) error {
connections := settings.Airflow.Connections
connections = AppendEnvironmentConnections(connections, envConns)

Expand Down Expand Up @@ -169,7 +179,10 @@ func AddConnections(id string, version uint64, envConns map[string]astrocore.Env
connPortArg = "--conn_port"
}
airflowCommand := baseListCmd
out := execAirflowCommand(id, airflowCommand)
out, err := execAirflowCommand(id, airflowCommand)
if err != nil {
return fmt.Errorf("error listing connections: %w", err)
}

for i := range connections {
var j int
Expand All @@ -185,7 +198,10 @@ func AddConnections(id string, version uint64, envConns map[string]astrocore.Env
if strings.Contains(out, quotedConnID) || strings.Contains(out, conn.ConnID) {
fmt.Printf("Updating Connection %q...\n", conn.ConnID)
airflowCommand = fmt.Sprintf("%s %s %q", baseRmCmd, connIDArg, conn.ConnID)
execAirflowCommand(id, airflowCommand)
_, err = execAirflowCommand(id, airflowCommand)
if err != nil {
return fmt.Errorf("error removing connection %s: %w", conn.ConnID, err)
}
}

if !objectValidator(1, conn.ConnType, conn.ConnURI) {
Expand Down Expand Up @@ -225,10 +241,14 @@ func AddConnections(id string, version uint64, envConns map[string]astrocore.Env
airflowCommand += fmt.Sprintf("%s '%s' ", connURIArg, conn.ConnURI)
}

out := execAirflowCommand(id, airflowCommand)
out, err := execAirflowCommand(id, airflowCommand)
if err != nil {
return fmt.Errorf("error adding connection %s: %w", conn.ConnID, err)
}
logrus.Debugf("Adding Connection logs:\n\n" + out)
fmt.Printf("Added Connection: %s\n", conn.ConnID)
}
return nil
}

func AppendEnvironmentConnections(connections Connections, envConnections map[string]astrocore.EnvironmentObjectConnection) Connections {
Expand Down Expand Up @@ -271,7 +291,7 @@ func AppendEnvironmentConnections(connections Connections, envConnections map[st
}

// AddPools is a function to add Pools from settings.yaml
func AddPools(id string, version uint64) {
func AddPools(id string, version uint64) error {
pools := settings.Airflow.Pools
baseCmd := "airflow "

Expand All @@ -294,14 +314,18 @@ func AddPools(id string, version uint64) {
airflowCommand += "''"
}
fmt.Println(airflowCommand)
out := execAirflowCommand(id, airflowCommand)
out, err := execAirflowCommand(id, airflowCommand)
if err != nil {
return fmt.Errorf("error adding pool %s: %w", pool.PoolName, err)
}
logrus.Debugf("Adding pool logs:\n" + out)
fmt.Printf("Added Pool: %s\n", pool.PoolName)
} else {
fmt.Printf("Skipping %s: Pool Slot must be set.\n", pool.PoolName)
}
}
}
return nil
}

func objectValidator(bound int, args ...string) bool {
Expand Down Expand Up @@ -347,12 +371,18 @@ func EnvExport(id, envFile string, version uint64, connections, variables bool)

func EnvExportVariables(id, envFile string) error {
// setup airflow command to export variables
out := execAirflowCommand(id, airflowVarExport)
out, err := execAirflowCommand(id, airflowVarExport)
if err != nil {
return fmt.Errorf("error exporting variables: %w", err)
}
logrus.Debugf("Env Export Variables logs:\n\n" + out)

if strings.Contains(out, "successfully") {
// get variables from file created by airflow command
out = execAirflowCommand(id, catVarFile)
out, err = execAirflowCommand(id, catVarFile)
if err != nil {
return fmt.Errorf("error reading variables file: %w", err)
}

m := map[string]string{}
err := json.Unmarshal([]byte(out), &m)
Expand All @@ -375,20 +405,29 @@ func EnvExportVariables(id, envFile string) error {
}
}
fmt.Println("Aiflow variables successfully export to the file " + envFile + "\n")
_ = execAirflowCommand(id, rmVarFile)
_, err = execAirflowCommand(id, rmVarFile)
if err != nil {
return fmt.Errorf("error removing variables file: %w", err)
}
return nil
}
return errors.New("variable export unsuccessful")
}

func EnvExportConnections(id, envFile string) error {
// Airflow command to export connections to env uris
out := execAirflowCommand(id, airflowConnExport)
out, err := execAirflowCommand(id, airflowConnExport)
if err != nil {
return fmt.Errorf("error exporting connections: %w", err)
}
logrus.Debugf("Env Export Connections logs:\n" + out)

if strings.Contains(out, "successfully") {
// get connections from file craeted by airflow command
out = execAirflowCommand(id, catConnFile)
out, err = execAirflowCommand(id, catConnFile)
if err != nil {
return fmt.Errorf("error reading connections file: %w", err)
}

vars := strings.Split(out, "\n")
// add connections to the env file
Expand All @@ -411,7 +450,10 @@ func EnvExportConnections(id, envFile string) error {
}
fmt.Println("Aiflow connections successfully export to the file " + envFile + "\n")
rmCmd := "rm tmp.connection"
_ = execAirflowCommand(id, rmCmd)
_, err = execAirflowCommand(id, rmCmd)
if err != nil {
return fmt.Errorf("error removing connections file: %w", err)
}
return nil
}
return errors.New("connection export unsuccessful")
Expand Down Expand Up @@ -460,7 +502,10 @@ func Export(id, settingsFile string, version uint64, connections, variables, poo

func ExportConnections(id string) error {
// Setup airflow command to export connections
out := execAirflowCommand(id, airflowConnectionList)
out, err := execAirflowCommand(id, airflowConnectionList)
if err != nil {
return fmt.Errorf("error listing connections: %w", err)
}
logrus.Debugf("Export Connections logs:\n" + out)
// remove all color from output of the airflow command
plainOut := re.ReplaceAllString(out, "")
Expand All @@ -469,7 +514,7 @@ func ExportConnections(id string) error {

var connections AirflowConnections

err := yaml.Unmarshal([]byte(yamlCons), &connections)
err = yaml.Unmarshal([]byte(yamlCons), &connections)
if err != nil {
return err
}
Expand Down Expand Up @@ -517,12 +562,18 @@ func ExportConnections(id string) error {

func ExportVariables(id string) error {
// setup files
out := execAirflowCommand(id, airflowVarExport)
out, err := execAirflowCommand(id, airflowVarExport)
if err != nil {
return fmt.Errorf("error exporting variables: %w", err)
}
logrus.Debugf("Export Variables logs:\n" + out)

if strings.Contains(out, "successfully") {
// get variables created by the airflow command
out = execAirflowCommand(id, catVarFile)
out, err = execAirflowCommand(id, catVarFile)
if err != nil {
return fmt.Errorf("error reading variables file: %w", err)
}

m := map[string]string{}
err := json.Unmarshal([]byte(out), &m)
Expand Down Expand Up @@ -550,7 +601,10 @@ func ExportVariables(id string) error {
if err != nil {
return err
}
_ = execAirflowCommand(id, rmVarFile)
_, err = execAirflowCommand(id, rmVarFile)
if err != nil {
return fmt.Errorf("error removing variables file: %w", err)
}
fmt.Printf("successfully exported variables\n\n")
return nil
}
Expand All @@ -560,7 +614,10 @@ func ExportVariables(id string) error {
func ExportPools(id string) error {
// Setup airflow command to export pools
airflowCommand := ariflowPoolsList
out := execAirflowCommand(id, airflowCommand)
out, err := execAirflowCommand(id, airflowCommand)
if err != nil {
return fmt.Errorf("error listing pools: %w", err)
}
logrus.Debugf("Export Pools logs:\n" + out)

// remove all color from output of the airflow command
Expand All @@ -570,7 +627,7 @@ func ExportPools(id string) error {
// remove warnings and extra text from the the output
yamlpools := "- description:" + strings.SplitN(plainOut, "- description:", 2)[1] //nolint:gomnd

err := yaml.Unmarshal([]byte(yamlpools), &pools)
err = yaml.Unmarshal([]byte(yamlpools), &pools)
if err != nil {
return err
}
Expand Down
Loading