LaunchFlow.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. import React, { Component } from "react";
  2. import styled from "styled-components";
  3. import _ from "lodash";
  4. import randomWords from "random-words";
  5. import { RouteComponentProps, withRouter } from "react-router";
  6. import api from "shared/api";
  7. import { Context } from "shared/Context";
  8. import hardcodedNames from "../hardcodedNameDict";
  9. import SourcePage from "./SourcePage";
  10. import SettingsPage from "./SettingsPage";
  11. import {
  12. PorterTemplate,
  13. ActionConfigType,
  14. ChoiceType,
  15. ClusterType,
  16. StorageType,
  17. } from "shared/types";
  18. type PropsType = RouteComponentProps & {
  19. currentTab?: string;
  20. currentTemplate: PorterTemplate;
  21. hideLaunchFlow: () => void;
  22. form: any;
  23. };
  24. type StateType = {
  25. currentPage: string;
  26. templateName: string;
  27. sourceType: string;
  28. valuesToOverride: any;
  29. imageUrl: string;
  30. imageTag: string;
  31. actionConfig: ActionConfigType;
  32. procfileProcess: string;
  33. branch: string;
  34. repoType: string;
  35. dockerfilePath: string | null;
  36. procfilePath: string | null;
  37. folderPath: string | null;
  38. selectedRegistry: any;
  39. selectedNamespace: string;
  40. saveValuesStatus: string;
  41. };
  42. const defaultActionConfig: ActionConfigType = {
  43. git_repo: "",
  44. image_repo_uri: "",
  45. branch: "",
  46. git_repo_id: 0,
  47. };
  48. class LaunchFlow extends Component<PropsType, StateType> {
  49. state = {
  50. currentPage: "source",
  51. templateName: "",
  52. saveValuesStatus: "",
  53. sourceType: "",
  54. selectedNamespace: "default",
  55. valuesToOverride: {} as any,
  56. imageUrl: "",
  57. imageTag: "",
  58. actionConfig: { ...defaultActionConfig },
  59. procfileProcess: "",
  60. branch: "",
  61. repoType: "",
  62. dockerfilePath: null as string | null,
  63. procfilePath: null as string | null,
  64. folderPath: null as string | null,
  65. selectedRegistry: null as any,
  66. };
  67. createGHAction = (chartName: string, chartNamespace: string, env?: any) => {
  68. let { currentProject, currentCluster, setCurrentError } = this.context;
  69. let {
  70. actionConfig,
  71. branch,
  72. selectedRegistry,
  73. dockerfilePath,
  74. folderPath,
  75. } = this.state;
  76. let imageRepoUri = `${selectedRegistry.url}/${chartName}-${chartNamespace}`;
  77. // DockerHub registry integration is per repo
  78. if (selectedRegistry.service === "dockerhub") {
  79. imageRepoUri = selectedRegistry.url;
  80. }
  81. api
  82. .createGHAction(
  83. "<token>",
  84. {
  85. git_repo: actionConfig.git_repo,
  86. git_branch: branch,
  87. registry_id: selectedRegistry.id,
  88. dockerfile_path: dockerfilePath,
  89. folder_path: folderPath,
  90. image_repo_uri: imageRepoUri,
  91. git_repo_id: actionConfig.git_repo_id,
  92. env: env,
  93. },
  94. {
  95. project_id: currentProject.id,
  96. CLUSTER_ID: currentCluster.id,
  97. RELEASE_NAME: chartName,
  98. RELEASE_NAMESPACE: chartNamespace,
  99. }
  100. )
  101. .then((res) => console.log(""))
  102. .catch((err) => {
  103. let parsedErr =
  104. err?.response?.data?.errors && err.response.data.errors[0];
  105. if (parsedErr) {
  106. err = parsedErr;
  107. }
  108. this.setState({
  109. saveValuesStatus: `Could not create GitHub Action: ${err}`,
  110. });
  111. setCurrentError(err);
  112. });
  113. };
  114. onSubmitAddon = (wildcard?: any) => {
  115. let { selectedNamespace } = this.state;
  116. let { currentCluster, currentProject, setCurrentError } = this.context;
  117. let name =
  118. this.state.templateName || randomWords({ exactly: 3, join: "-" });
  119. this.setState({ saveValuesStatus: "loading" });
  120. let values = {};
  121. for (let key in wildcard) {
  122. _.set(values, key, wildcard[key]);
  123. }
  124. api
  125. .deployTemplate(
  126. "<token>",
  127. {
  128. templateName: this.props.currentTemplate.name,
  129. storage: StorageType.Secret,
  130. formValues: values,
  131. namespace: selectedNamespace,
  132. name,
  133. },
  134. {
  135. id: currentProject.id,
  136. cluster_id: currentCluster.id,
  137. name: this.props.currentTemplate.name.toLowerCase().trim(),
  138. version: this.props.currentTemplate?.currentVersion || "latest",
  139. repo_url: process.env.ADDON_CHART_REPO_URL,
  140. }
  141. )
  142. .then((_) => {
  143. // this.props.setCurrentView('cluster-dashboard');
  144. this.setState({ saveValuesStatus: "successful" }, () => {
  145. // redirect to dashboard
  146. let dst =
  147. this.props.currentTemplate.name === "job" ? "jobs" : "applications";
  148. setTimeout(() => {
  149. this.props.history.push(dst);
  150. }, 500);
  151. window.analytics.track("Deployed Add-on", {
  152. name: this.props.currentTemplate.name,
  153. namespace: selectedNamespace,
  154. values: values,
  155. });
  156. });
  157. })
  158. .catch((err) => {
  159. let parsedErr =
  160. err?.response?.data?.errors && err.response.data.errors[0];
  161. if (parsedErr) {
  162. err = parsedErr;
  163. }
  164. this.setState({
  165. saveValuesStatus: parsedErr,
  166. });
  167. setCurrentError(err.response.data.errors[0]);
  168. window.analytics.track("Failed to Deploy Add-on", {
  169. name: this.props.currentTemplate.name,
  170. namespace: selectedNamespace,
  171. values: values,
  172. error: err,
  173. });
  174. });
  175. };
  176. onSubmit = async (rawValues: any) => {
  177. let { currentCluster, currentProject, setCurrentError } = this.context;
  178. let {
  179. selectedNamespace,
  180. templateName,
  181. imageUrl,
  182. imageTag,
  183. sourceType,
  184. } = this.state;
  185. let name = templateName || randomWords({ exactly: 3, join: "-" });
  186. this.setState({ saveValuesStatus: "loading" });
  187. // Convert dotted keys to nested objects
  188. let values: any = {};
  189. for (let key in rawValues) {
  190. _.set(values, key, rawValues[key]);
  191. }
  192. let tag = imageTag;
  193. if (imageUrl.includes(":")) {
  194. let splits = imageUrl.split(":");
  195. imageUrl = splits[0];
  196. tag = splits[1];
  197. } else if (!tag) {
  198. tag = "latest";
  199. }
  200. if (sourceType === "repo") {
  201. if (this.props.currentTemplate?.name == "job") {
  202. imageUrl = "public.ecr.aws/o1j4x7p4/hello-porter-job";
  203. tag = "latest";
  204. } else {
  205. imageUrl = "public.ecr.aws/o1j4x7p4/hello-porter";
  206. tag = "latest";
  207. }
  208. }
  209. let provider;
  210. switch (currentCluster.service) {
  211. case "eks":
  212. provider = "aws";
  213. break;
  214. case "gke":
  215. provider = "gcp";
  216. break;
  217. case "doks":
  218. provider = "digitalocean";
  219. break;
  220. default:
  221. provider = "";
  222. }
  223. // don't overwrite for templates that already have a source (i.e. non-Docker templates)
  224. if (imageUrl && tag) {
  225. _.set(values, "image.repository", imageUrl);
  226. _.set(values, "image.tag", tag);
  227. }
  228. _.set(values, "ingress.provider", provider);
  229. var url: string;
  230. // check if template is docker and create external domain if necessary
  231. if (this.props.currentTemplate.name == "web") {
  232. if (values?.ingress?.enabled && !values?.ingress?.custom_domain) {
  233. url = await new Promise((resolve, reject) => {
  234. api
  235. .createSubdomain(
  236. "<token>",
  237. {
  238. release_name: name,
  239. },
  240. {
  241. id: currentProject.id,
  242. cluster_id: currentCluster.id,
  243. }
  244. )
  245. .then((res) => {
  246. resolve(res.data?.external_url);
  247. })
  248. .catch((err) => {
  249. let parsedErr =
  250. err?.response?.data?.errors && err.response.data.errors[0];
  251. if (parsedErr) {
  252. err = parsedErr;
  253. }
  254. this.setState({
  255. saveValuesStatus: `Could not create subdomain: ${err}`,
  256. });
  257. setCurrentError(err);
  258. });
  259. });
  260. values.ingress.porter_hosts = [url];
  261. }
  262. }
  263. api
  264. .deployTemplate(
  265. "<token>",
  266. {
  267. templateName: this.props.currentTemplate.name,
  268. imageURL: imageUrl,
  269. storage: StorageType.Secret,
  270. formValues: values,
  271. namespace: selectedNamespace,
  272. name,
  273. },
  274. {
  275. id: currentProject.id,
  276. cluster_id: currentCluster.id,
  277. name: this.props.currentTemplate.name.toLowerCase().trim(),
  278. version: this.props.currentTemplate?.currentVersion || "latest",
  279. repo_url: process.env.APPLICATION_CHART_REPO_URL,
  280. }
  281. )
  282. .then((res: any) => {
  283. if (sourceType === "repo") {
  284. let env = rawValues["container.env.normal"];
  285. console.log(env);
  286. this.createGHAction(name, selectedNamespace, env);
  287. }
  288. // this.props.setCurrentView('cluster-dashboard');
  289. this.setState({ saveValuesStatus: "successful" }, () => {
  290. // redirect to dashboard with namespace
  291. setTimeout(() => {
  292. let dst =
  293. this.props.currentTemplate.name === "job"
  294. ? "jobs"
  295. : "applications";
  296. this.props.history.push(dst);
  297. }, 1000);
  298. });
  299. })
  300. .catch((err: any) => {
  301. let parsedErr =
  302. err?.response?.data?.errors && err.response.data.errors[0];
  303. console.log(parsedErr);
  304. if (parsedErr) {
  305. err = parsedErr;
  306. }
  307. this.setState({
  308. saveValuesStatus: `Could not deploy template: ${err}`,
  309. });
  310. setCurrentError(err);
  311. });
  312. };
  313. renderCurrentPage = () => {
  314. let { form, currentTab } = this.props;
  315. let {
  316. currentPage,
  317. valuesToOverride,
  318. templateName,
  319. imageUrl,
  320. imageTag,
  321. actionConfig,
  322. branch,
  323. repoType,
  324. dockerfilePath,
  325. procfileProcess,
  326. procfilePath,
  327. folderPath,
  328. selectedNamespace,
  329. selectedRegistry,
  330. saveValuesStatus,
  331. sourceType,
  332. } = this.state;
  333. if (currentPage === "source" && currentTab === "porter") {
  334. return (
  335. <SourcePage
  336. sourceType={sourceType}
  337. setSourceType={(x: string) => this.setState({ sourceType: x })}
  338. templateName={templateName}
  339. setPage={(x: string) => {
  340. this.setState({ currentPage: x });
  341. }}
  342. setTemplateName={(x: string) => this.setState({ templateName: x })}
  343. setValuesToOverride={(x: any) =>
  344. this.setState({ valuesToOverride: x })
  345. }
  346. imageUrl={imageUrl}
  347. setImageUrl={(x: string) => this.setState({ imageUrl: x })}
  348. imageTag={imageTag}
  349. setImageTag={(x: string) => this.setState({ imageTag: x })}
  350. actionConfig={actionConfig}
  351. setActionConfig={(x: ActionConfigType) =>
  352. this.setState({ actionConfig: x })
  353. }
  354. branch={branch}
  355. setBranch={(x: string) => this.setState({ branch: x })}
  356. procfileProcess={procfileProcess}
  357. setProcfileProcess={(x: string) =>
  358. this.setState({ procfileProcess: x })
  359. }
  360. repoType={repoType}
  361. setRepoType={(x: string) => this.setState({ repoType: x })}
  362. dockerfilePath={dockerfilePath}
  363. setDockerfilePath={(x: string) =>
  364. this.setState({ dockerfilePath: x })
  365. }
  366. folderPath={folderPath}
  367. setFolderPath={(x: string) => this.setState({ folderPath: x })}
  368. procfilePath={procfilePath}
  369. setProcfilePath={(x: string) => this.setState({ procfilePath: x })}
  370. selectedRegistry={selectedRegistry}
  371. setSelectedRegistry={(x: string) =>
  372. this.setState({ selectedRegistry: x })
  373. }
  374. />
  375. );
  376. }
  377. // Display main (non-source) settings page
  378. return (
  379. <SettingsPage
  380. onSubmit={currentTab === "porter" ? this.onSubmit : this.onSubmitAddon}
  381. saveValuesStatus={saveValuesStatus}
  382. selectedNamespace={selectedNamespace}
  383. setSelectedNamespace={(x: string) =>
  384. this.setState({ selectedNamespace: x })
  385. }
  386. templateName={templateName}
  387. setTemplateName={(x: string) => this.setState({ templateName: x })}
  388. hasSource={currentTab === "porter"}
  389. setPage={(x: string) => this.setState({ currentPage: x })}
  390. form={form}
  391. valuesToOverride={valuesToOverride}
  392. clearValuesToOverride={() => this.setState({ valuesToOverride: null })}
  393. />
  394. );
  395. };
  396. renderIcon = () => {
  397. let icon = this.props.currentTemplate?.icon;
  398. if (icon) {
  399. return <Icon src={icon} />;
  400. }
  401. return (
  402. <Polymer>
  403. <i className="material-icons">layers</i>
  404. </Polymer>
  405. );
  406. };
  407. render() {
  408. let { currentTab } = this.props;
  409. let { name } = this.props.currentTemplate;
  410. if (hardcodedNames[name]) {
  411. name = hardcodedNames[name];
  412. }
  413. return (
  414. <StyledLaunchFlow>
  415. <TitleSection>
  416. <i className="material-icons" onClick={this.props.hideLaunchFlow}>
  417. keyboard_backspace
  418. </i>
  419. {this.renderIcon()}
  420. <Title>
  421. New {name} {currentTab === "porter" ? null : "Instance"}
  422. </Title>
  423. </TitleSection>
  424. {this.renderCurrentPage()}
  425. <Br />
  426. </StyledLaunchFlow>
  427. );
  428. }
  429. }
  430. LaunchFlow.contextType = Context;
  431. export default withRouter(LaunchFlow);
  432. const Br = styled.div`
  433. width: 100%;
  434. height: 120px;
  435. `;
  436. const Icon = styled.img`
  437. width: 40px;
  438. margin-right: 14px;
  439. opacity: 0;
  440. animation: floatIn 0.5s 0.2s;
  441. animation-fill-mode: forwards;
  442. @keyframes floatIn {
  443. from {
  444. opacity: 0;
  445. transform: translateY(10px);
  446. }
  447. to {
  448. opacity: 1;
  449. transform: translateY(0px);
  450. }
  451. }
  452. `;
  453. const Polymer = styled.div`
  454. margin-bottom: -3px;
  455. > i {
  456. color: ${(props) => props.theme.containerIcon};
  457. font-size: 24px;
  458. margin-left: 12px;
  459. margin-right: 3px;
  460. }
  461. `;
  462. const Title = styled.div`
  463. font-size: 24px;
  464. font-weight: 600;
  465. font-family: "Work Sans", sans-serif;
  466. color: #ffffff;
  467. white-space: nowrap;
  468. overflow: hidden;
  469. text-overflow: ellipsis;
  470. `;
  471. const TitleSection = styled.div`
  472. margin-bottom: 20px;
  473. display: flex;
  474. flex-direction: row;
  475. align-items: center;
  476. > i {
  477. cursor: pointer;
  478. font-size 24px;
  479. color: #969Fbbaa;
  480. margin-right: 10px;
  481. padding: 3px;
  482. margin-left: 0px;
  483. border-radius: 100px;
  484. :hover {
  485. background: #ffffff11;
  486. }
  487. }
  488. > a {
  489. > i {
  490. display: flex;
  491. align-items: center;
  492. margin-bottom: -2px;
  493. font-size: 18px;
  494. margin-left: 18px;
  495. color: #858faaaa;
  496. cursor: pointer;
  497. :hover {
  498. color: #aaaabb;
  499. }
  500. }
  501. }
  502. `;
  503. const StyledLaunchFlow = styled.div`
  504. width: calc(90% - 130px);
  505. min-width: 300px;
  506. padding-top: 20px;
  507. margin-top: calc(50vh - 340px);
  508. `;