瀏覽代碼

Merge branch 'simplified-view' of github.com:porter-dev/porter into simplified-view

Feroze Mohideen 3 年之前
父節點
當前提交
be97966334

+ 0 - 1
api/server/handlers/stacks/create.go

@@ -32,7 +32,6 @@ func NewCreateStackHandler(
 }
 
 func (c *CreateStackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
-	fmt.Println("welcome to create stack handler")
 	ctx := r.Context()
 	cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
 

+ 8 - 4
api/server/handlers/stacks/create_porter_app.go

@@ -8,6 +8,8 @@ import (
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"
 	"github.com/porter-dev/porter/api/server/shared/config"
+	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/internal/models"
 )
 
 type CreatePorterAppHandler struct {
@@ -22,14 +24,16 @@ func NewCreatePorterAppHandler(
 ) *CreatePorterAppHandler {
 	return &CreatePorterAppHandler{
 		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
-		KubernetesAgentGetter:   authz.NewOutOfClusterAgentGetter(config),
 	}
 }
 
 func (c *CreatePorterAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
-	// ctx := r.Context()
-	// cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
-	fmt.Println("congrats on making it!")
+	ctx := r.Context()
+	project, _ := ctx.Value(types.ProjectScope).(*models.Project)
+	cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
+	fmt.Println("congrats on making it!", cluster.ID, project.ID)
+
+	// _, err := c.Repo().PorterApp().CreatePorterApp(project.ID, cluster.ID)
 
 	w.WriteHeader(http.StatusCreated)
 }

+ 107 - 105
dashboard/src/main/home/app-dashboard/new-app-flow/GithubActionModal.tsx

@@ -12,121 +12,123 @@ import api from "shared/api";
 import { Context } from "shared/Context";
 
 interface GithubActionModalProps {
-    closeModal: () => void;
-    githubAppInstallationID?: number;
-    githubRepoOwner?: string;
-    githubRepoName?: string;
-    branch?: string;
-    stackName?: string;
-    projectId?: number;
-    clusterId?: number;
+  closeModal: () => void;
+  githubAppInstallationID?: number;
+  githubRepoOwner?: string;
+  githubRepoName?: string;
+  branch?: string;
+  stackName?: string;
+  projectId?: number;
+  clusterId?: number;
+  deployPorterApp: () => void;
 }
 
 type Choice = "open_pr" | "copy";
 
 const GithubActionModal: React.FC<GithubActionModalProps> = ({
-    closeModal,
-    githubAppInstallationID,
-    githubRepoOwner,
-    githubRepoName,
-    branch,
-    stackName,
-    projectId,
-    clusterId,
+  closeModal,
+  githubAppInstallationID,
+  githubRepoOwner,
+  githubRepoName,
+  branch,
+  stackName,
+  projectId,
+  clusterId,
+  deployPorterApp,
 }) => {
-    const [choice, setChoice] = React.useState<Choice>("open_pr");
-    const [loading, setLoading] = React.useState<boolean>(false);
-    const { currentProject, currentCluster } = useContext(Context);
+  const [choice, setChoice] = React.useState<Choice>("open_pr");
+  const [loading, setLoading] = React.useState<boolean>(false);
+  const { currentProject, currentCluster } = useContext(Context);
 
-    const submit = async () => {
-        if (githubAppInstallationID && githubRepoOwner && githubRepoName && branch && stackName) {
-            try {
-                setLoading(true)
-                const res = await api.createSecretAndOpenGitHubPullRequest(
-                    "<token>",
-                    {
-                        github_app_installation_id: githubAppInstallationID,
-                        github_repo_owner: githubRepoOwner,
-                        github_repo_name: githubRepoName,
-                        branch,
-                        open_pr: choice === "open_pr",
-                    },
-                    {
-                        project_id: projectId,
-                        cluster_id: clusterId,
-                        stack_name: stackName,
-                    }
-                );
-                if (res?.data?.url) {
-                    window.open(res.data.url, "_blank", "noreferrer")
-                }
-            } catch (error) {
-                console.log(error)
-            } finally {
-                setLoading(false)
-            }
-        } else {
-            console.log("missing information")
+  const submit = async () => {
+    if (githubAppInstallationID && githubRepoOwner && githubRepoName && branch && stackName) {
+      try {
+        setLoading(true)
+        const res = await api.createSecretAndOpenGitHubPullRequest(
+          "<token>",
+          {
+            github_app_installation_id: githubAppInstallationID,
+            github_repo_owner: githubRepoOwner,
+            github_repo_name: githubRepoName,
+            branch,
+            open_pr: choice === "open_pr",
+          },
+          {
+            project_id: projectId,
+            cluster_id: clusterId,
+            stack_name: stackName,
+          }
+        );
+        if (res?.data?.url) {
+            window.open(res.data.url, "_blank", "noreferrer")
         }
+      } catch (error) {
+        console.log(error)
+      } finally {
+        setLoading(false)
+      }
+    } else {
+      console.log("missing information");
     }
-    return (
-        <Modal closeModal={closeModal}>
-            <Text size={16}>
-                Continuous Integration (CI) with GitHub Actions
-            </Text>
+  }
+  return (
+    <Modal closeModal={closeModal}>
+      <Text size={16}>
+        Continuous Integration (CI) with GitHub Actions
+      </Text>
+      <Spacer height="15px" />
+      <Text color="helper">
+        In order to automatically update your services every time new code is pushed to your GitHub branch, the following file must exist in your Github repository:
+      </Text>
+      <Spacer y={1} />
+      <ExpandableSection
+        noWrapper
+        expandText="[+] Show code"
+        collapseText="[-] Hide code"
+        Header={
+          <ModalHeader>./github/workflows/porter_deploy.yml</ModalHeader>
+        }
+        isInitiallyExpanded={true}
+        ExpandedSection={
+          <>
             <Spacer height="15px" />
-            <Text color="helper">
-                In order to automatically update your services every time new code is pushed to your GitHub branch, the following file must exist in your Github repository:
-            </Text>
-            <Spacer y={1} />
-            <ExpandableSection
-                noWrapper
-                expandText="[+] Show code"
-                collapseText="[-] Hide code"
-                Header={
-                    <ModalHeader>./github/workflows/porter_deploy.yml</ModalHeader>
-                }
-                isInitiallyExpanded={true}
-                ExpandedSection={
-                    <>
-                        <Spacer height="15px" />
-                        <Fieldset background="#1b1d2688">
-                            • Amazon Elastic Kubernetes Service (EKS) = $73/mo
-                            <Spacer height="15px" />
-                            • Amazon EC2:
-                            <Spacer height="15px" />
-                            <Tab />+ System workloads: t3.medium instance (2) = $60.74/mo
-                            <Spacer height="15px" />
-                            <Tab />+ Monitoring workloads: t3.large instance (1) = $60.74/mo
-                            <Spacer height="15px" />
-                            <Tab />+ Application workloads: t3.xlarge instance (1) = $121.47/mo
-                        </Fieldset>
-                    </>
-                }
-            />
-            <Spacer y={1} />
-            <Text color="helper">
-                Porter can open a PR for you to approve and merge this file into your repository, or you can add it yourself. If you allow Porter to open a PR, you will be redirected to the PR in a new tab after hitting Complete below.
-            </Text>
-            <Spacer y={1} />
-            <Select
-                options={[
-                    { label: "I authorize Porter to open a PR on my behalf", value: "open_pr" },
-                    { label: "I will copy the file into my repository myself", value: "copy" },
-                ]}
-                setValue={(x: Choice) => setChoice(x)}
-                width="100%"
-            />
-            <Button
-                onClick={submit}
-                width={"100%"}
-                status={loading ? "loading" : undefined}
-                loadingText="Opening PR..."
-            >
-                Complete
-            </Button>
-        </Modal>
-    )
+            <Fieldset background="#1b1d2688">
+              • Amazon Elastic Kubernetes Service (EKS) = $73/mo
+              <Spacer height="15px" />
+              • Amazon EC2:
+              <Spacer height="15px" />
+              <Tab />+ System workloads: t3.medium instance (2) = $60.74/mo
+              <Spacer height="15px" />
+              <Tab />+ Monitoring workloads: t3.large instance (1) = $60.74/mo
+              <Spacer height="15px" />
+              <Tab />+ Application workloads: t3.xlarge instance (1) = $121.47/mo
+            </Fieldset>
+          </>
+        }
+      />
+      <Spacer y={1} />
+      <Text color="helper">
+        Porter can open a PR for you to approve and merge this file into your repository, or you can add it yourself. If you allow Porter to open a PR, you will be redirected to the PR in a new tab after hitting Complete below.
+      </Text>
+      <Spacer y={1} />
+      <Select
+        options={[
+          { label: "I authorize Porter to open a PR on my behalf", value: "open_pr" },
+          { label: "I will copy the file into my repository myself", value: "copy" },
+        ]}
+        setValue={(x: Choice) => setChoice(x)}
+        width="100%"
+      />
+      <Button
+        onClick={submit}
+        width={"100%"}
+        status={loading ? "loading" : undefined}
+        loadingText="Opening PR..."
+      >
+        Complete
+      </Button>
+    </Modal>
+  )
 }
 
 export default GithubActionModal;

+ 11 - 62
dashboard/src/main/home/app-dashboard/new-app-flow/NewAppFlow.tsx

@@ -109,79 +109,27 @@ const NewAppFlow: React.FC<Props> = ({ ...props }) => {
   };
   const [showGHAModal, setShowGHAModal] = useState<boolean>(false);
 
+  // Deploys a Helm chart and writes build settings to the DB
   const deployPorterApp = async () => {
-    const dummyPorterAppConfig = {
-      "daft-web": {
-        image: {
-          repository: "public.ecr.aws/o1j4x7p4/hello-porter",
-          tag: "latest",
-        },
-        ingress: {
-          enabled: false,
-        },
-      },
-      "daft-worker-1": {
-        image: {
-          repository: "public.ecr.aws/o1j4x7p4/hello-porter",
-          tag: "latest",
-        },
-      },
-      "daft-worker-2": {
-        image: {
-          repository: "public.ecr.aws/o1j4x7p4/hello-porter",
-          tag: "latest",
-        },
-      },
-      "daft-release": {
-        image: {
-          repository: "public.ecr.aws/o1j4x7p4/hello-porter",
-          tag: "latest",
-        },
-      },
-    };
-
     try {
-      const res = await api.updatePorterStack(
+
+      // Write build settings to the DB
+      const res = await api.createPorterApp(
         "<token>",
         {
-          stack_name: formState.applicationName,
-          values: dummyPorterAppConfig,
-          dependencies: [
-            {
-              name: "web",
-              alias: "daft-web",
-              version: "0.88",
-              repository: "https://charts.getporter.dev",
-            },
-            {
-              name: "worker",
-              alias: "daft-worker-1",
-              version: "0.38",
-              repository: "https://charts.getporter.dev",
-            },
-            {
-              name: "worker",
-              alias: "daft-worker-2",
-              version: "0.38",
-              repository: "https://charts.getporter.dev",
-            },
-            {
-              name: "job",
-              alias: "daft-release",
-              version: "0.37",
-              repository: "https://charts.getporter.dev",
-            },
-          ],
+          name: formState.applicationName,
         },
         {
           cluster_id: currentCluster.id,
           project_id: currentProject.id,
         }
       );
-      console.log(res);
-    } catch (error) {
-      console.log(error);
+      alert("ok")
+    } catch (err) {
+      console.log(err);
     }
+
+    // TODO: update Porter stack
   };
 
   return (
@@ -336,6 +284,7 @@ const NewAppFlow: React.FC<Props> = ({ ...props }) => {
           stackName={formState.applicationName}
           projectId={currentProject.id}
           clusterId={currentCluster.id}
+          deployPorterApp={deployPorterApp}
         />
       )}
     </CenterWrapper>

+ 55 - 0
internal/repository/gorm/porter_app.go

@@ -0,0 +1,55 @@
+package gorm
+
+import (
+	"github.com/porter-dev/porter/internal/models"
+	"github.com/porter-dev/porter/internal/repository"
+	"gorm.io/gorm"
+)
+
+// PorterAppRepository uses gorm.DB for querying the database
+type PorterAppRepository struct {
+	db *gorm.DB
+}
+
+// NewPorterAppRepository returns a PorterAppRepository which uses
+// gorm.DB for querying the database
+func NewPorterAppRepository(db *gorm.DB) repository.PorterAppRepository {
+	return &PorterAppRepository{db}
+}
+
+func (repo *PorterAppRepository) CreatePorterApp(a *models.PorterApp) (*models.PorterApp, error) {
+	if err := repo.db.Create(a).Error; err != nil {
+		return nil, err
+	}
+	return a, nil
+}
+
+func (repo *PorterAppRepository) ListPorterAppByClusterID(clusterID uint) ([]*models.PorterApp, error) {
+	apps := []*models.PorterApp{}
+
+	/*
+		if err := repo.db.Where("project_id = ? AND NOT revoked", projectID).Find(&tokens).Error; err != nil {
+			return nil, err
+		}
+	*/
+
+	return apps, nil
+}
+
+func (repo *PorterAppRepository) ReadPorterApp(clusterID uint, name string) (*models.PorterApp, error) {
+	app := &models.PorterApp{}
+
+	if err := repo.db.Where("cluster_id = ? AND name = ?", clusterID, name).First(&app).Error; err != nil {
+		return nil, err
+	}
+
+	return app, nil
+}
+
+func (repo *PorterAppRepository) UpdatePorterApp(app *models.PorterApp) (*models.PorterApp, error) {
+	if err := repo.db.Save(app).Error; err != nil {
+		return nil, err
+	}
+
+	return app, nil
+}

+ 13 - 0
internal/repository/porter_app.go

@@ -0,0 +1,13 @@
+package repository
+
+import (
+	"github.com/porter-dev/porter/internal/models"
+)
+
+// PorterAppRepository represents the set of queries on the PorterApp model
+type PorterAppRepository interface {
+	CreatePorterApp(token *models.PorterApp) (*models.PorterApp, error)
+	ListPorterAppByClusterID(clusterID uint) ([]*models.PorterApp, error)
+	ReadPorterApp(clusterID uint, name string) (*models.PorterApp, error)
+	UpdatePorterApp(app *models.PorterApp) (*models.PorterApp, error)
+}

+ 1 - 0
internal/repository/repository.go

@@ -45,4 +45,5 @@ type Repository interface {
 	MonitorTestResult() MonitorTestResultRepository
 	APIContractRevisioner() APIContractRevisioner
 	AWSAssumeRoleChainer() AWSAssumeRoleChainer
+	PorterApp() PorterAppRepository
 }