Skip to content

Commit

Permalink
refactor(stack-cntlr): install project deps using Automation API
Browse files Browse the repository at this point in the history
  • Loading branch information
metral committed Sep 3, 2020
1 parent ee48652 commit 4ba8f90
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
5 changes: 3 additions & 2 deletions pkg/apis/pulumi/v1alpha1/stack_types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package v1alpha1

import (
"github.com/pulumi/pulumi/sdk/v2/go/common/workspace"
"context"

"github.com/pulumi/pulumi/sdk/v2/go/x/auto"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -172,7 +173,7 @@ type StackController interface {
// Project setup:

// InstallProjectDependencies installs the package manager dependencies for the project's language.
InstallProjectDependencies(runtime workspace.ProjectRuntimeInfo) error
InstallProjectDependencies(ctx context.Context, workspace auto.Workspace) error
// SetEnvs populates the environment of the stack run with values
// from an array of Kubernetes ConfigMaps in a Namespace.
SetEnvs(configMapNames []string, namespace string) error
Expand Down
41 changes: 19 additions & 22 deletions pkg/controller/stack/stack_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
libpredicate "github.com/operator-framework/operator-lib/predicate"
"github.com/pkg/errors"
pulumiv1alpha1 "github.com/pulumi/pulumi-kubernetes-operator/pkg/apis/pulumi/v1alpha1"
"github.com/pulumi/pulumi/sdk/v2/go/common/workspace"
"github.com/pulumi/pulumi/sdk/v2/go/x/auto"
"github.com/pulumi/pulumi/sdk/v2/go/x/auto/optrefresh"
git "gopkg.in/src-d/go-git.v4"
Expand Down Expand Up @@ -440,18 +439,18 @@ func (sess *reconcileStackSession) SetSecretEnvs(secrets []string, namespace str
}

// runCmd runs the given command with stdout and stderr hooked up to the logger.
func (sess *reconcileStackSession) runCmd(title string, cmd *exec.Cmd) (string, string, error) {
func (sess *reconcileStackSession) runCmd(title string, cmd *exec.Cmd, workspace auto.Workspace) (string, string, error) {
// If not overridden, set the command to run in the working directory.
if cmd.Dir == "" {
cmd.Dir = sess.workdir
cmd.Dir = workspace.WorkDir()
}

// Init environment variables.
if len(cmd.Env) == 0 {
cmd.Env = os.Environ()
}
// If there are extra environment variables, set them.
if envvars := sess.autoStack.Workspace().GetEnvVars(); envvars != nil {
if envvars := workspace.GetEnvVars(); envvars != nil {
for k, v := range envvars {
e := []string{k, v}
cmd.Env = append(cmd.Env, strings.Join(e, "="))
Expand Down Expand Up @@ -503,6 +502,7 @@ func (sess *reconcileStackSession) SetupPulumiWorkdir() error {
ProjectPath: sess.stack.RepoDir,
CommitHash: sess.stack.Commit,
Branch: sess.stack.Branch,
Setup: sess.InstallProjectDependencies,
}

var err error
Expand Down Expand Up @@ -533,30 +533,27 @@ func (sess *reconcileStackSession) SetupPulumiWorkdir() error {
sess.autoStack = autoStack
sess.autoStack.Workspace().SetEnvVar("PULUMI_ACCESS_TOKEN", sess.accessToken)

// TODO(autoapi): needed to get project runtime for installing deps
// https://github.com/pulumi/pulumi/issues/5266
sess.workdir = sess.autoStack.Workspace().WorkDir()

// Update the stack config and secret config values.
err = sess.UpdateConfig()
if err != nil {
sess.logger.Error(err, "failed to set stack config", "Stack.Name", sess.stack.Stack)
return errors.Wrap(err, "failed to set stack config")
}

project, err := workspace.LoadProject(filepath.Join(sess.workdir, "Pulumi.yaml"))
if err != nil {
return err
}
if err = sess.InstallProjectDependencies(project.Runtime); err != nil {
// Install project dependencies
if err = sess.InstallProjectDependencies(context.Background(), sess.autoStack.Workspace()); err != nil {
return errors.Wrap(err, "installing project dependencies")
}

return nil
}

func (sess *reconcileStackSession) InstallProjectDependencies(runtime workspace.ProjectRuntimeInfo) error {
switch runtime.Name() {
func (sess *reconcileStackSession) InstallProjectDependencies(ctx context.Context, workspace auto.Workspace) error {
project, err := workspace.ProjectSettings(ctx)
if err != nil {
return errors.Wrap(err, "unable to get project runtime")
}
switch project.Runtime.Name() {
case "nodejs":
npm, _ := exec.LookPath("npm")
if npm == "" {
Expand All @@ -567,7 +564,7 @@ func (sess *reconcileStackSession) InstallProjectDependencies(runtime workspace.
}
// TODO: Consider using `npm ci` instead if there is a `package-lock.json` or `npm-shrinkwrap.json` present
cmd := exec.Command(npm, "install")
_, _, err := sess.runCmd("NPM/Yarn", cmd)
_, _, err := sess.runCmd("NPM/Yarn", cmd, workspace)
return err
case "python":
python3, _ := exec.LookPath("python3")
Expand All @@ -579,8 +576,8 @@ func (sess *reconcileStackSession) InstallProjectDependencies(runtime workspace.
return errors.New("did not find 'pip3' on the PATH; can't install project dependencies")
}
venv := ""
if runtime.Options() != nil {
venv, _ = runtime.Options()["virtualenv"].(string)
if project.Runtime.Options() != nil {
venv, _ = project.Runtime.Options()["virtualenv"].(string)
}
if venv == "" {
// TODO[pulumi/pulumi-kubernetes-operator#79]
Expand All @@ -589,18 +586,18 @@ func (sess *reconcileStackSession) InstallProjectDependencies(runtime workspace.
// Emulate the same steps as the CLI does in https://github.com/pulumi/pulumi/blob/master/sdk/python/python.go#L97-L99.
// TODO[pulumi/pulumi#5164]: Ideally the CLI would automatically do these - since it already knows how.
cmd := exec.Command(python3, "-m", "venv", venv)
_, _, err := sess.runCmd("Pip Install", cmd)
_, _, err := sess.runCmd("Pip Install", cmd, workspace)
if err != nil {
return err
}
venvPython := filepath.Join(venv, "bin", "python")
cmd = exec.Command(venvPython, "-m", "pip", "install", "--upgrade", "pip", "setuptools", "wheel")
_, _, err = sess.runCmd("Pip Install", cmd)
_, _, err = sess.runCmd("Pip Install", cmd, workspace)
if err != nil {
return err
}
cmd = exec.Command(venvPython, "-m", "pip", "install", "-r", "requirements.txt")
_, _, err = sess.runCmd("Pip Install", cmd)
_, _, err = sess.runCmd("Pip Install", cmd, workspace)
if err != nil {
return err
}
Expand All @@ -610,7 +607,7 @@ func (sess *reconcileStackSession) InstallProjectDependencies(runtime workspace.
return nil
default:
// Allow unknown runtimes without any pre-processing, but print a message indicating runtime was unknown
sess.logger.Info(fmt.Sprintf("Handling unknown project runtime '%s'", runtime.Name()), "Stack.Name", sess.stack.Stack)
sess.logger.Info(fmt.Sprintf("Handling unknown project runtime '%s'", project.Runtime.Name()), "Stack.Name", sess.stack.Stack)
return nil
}
}
Expand Down

0 comments on commit 4ba8f90

Please sign in to comment.