apply.go 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. package cmd
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/url"
  8. "os"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. "github.com/cli/cli/git"
  13. "github.com/fatih/color"
  14. "github.com/mitchellh/mapstructure"
  15. api "github.com/porter-dev/porter/api/client"
  16. "github.com/porter-dev/porter/api/types"
  17. "github.com/porter-dev/porter/cli/cmd/config"
  18. "github.com/porter-dev/porter/cli/cmd/deploy"
  19. "github.com/porter-dev/porter/cli/cmd/deploy/wait"
  20. "github.com/porter-dev/porter/cli/cmd/preview"
  21. previewInt "github.com/porter-dev/porter/internal/integrations/preview"
  22. "github.com/porter-dev/porter/internal/templater/utils"
  23. "github.com/porter-dev/switchboard/pkg/drivers"
  24. "github.com/porter-dev/switchboard/pkg/models"
  25. "github.com/porter-dev/switchboard/pkg/parser"
  26. switchboardTypes "github.com/porter-dev/switchboard/pkg/types"
  27. switchboardWorker "github.com/porter-dev/switchboard/pkg/worker"
  28. "github.com/rs/zerolog"
  29. "github.com/spf13/cobra"
  30. )
  31. // applyCmd represents the "porter apply" base command when called
  32. // with a porter.yaml file as an argument
  33. var applyCmd = &cobra.Command{
  34. Use: "apply",
  35. Short: "Applies a configuration to an application",
  36. Long: fmt.Sprintf(`
  37. %s
  38. Applies a configuration to an application by either creating a new one or updating an existing
  39. one. For example:
  40. %s
  41. This command will apply the configuration contained in porter.yaml to the requested project and
  42. cluster either provided inside the porter.yaml file or through environment variables. Note that
  43. environment variables will always take precendence over values specified in the porter.yaml file.
  44. By default, this command expects to be run from a local git repository.
  45. The following are the environment variables that can be used to set certain values while
  46. applying a configuration:
  47. PORTER_CLUSTER Cluster ID that contains the project
  48. PORTER_PROJECT Project ID that contains the application
  49. PORTER_NAMESPACE The Kubernetes namespace that the application belongs to
  50. PORTER_SOURCE_NAME Name of the source Helm chart
  51. PORTER_SOURCE_REPO The URL of the Helm charts registry
  52. PORTER_SOURCE_VERSION The version of the Helm chart to use
  53. PORTER_TAG The Docker image tag to use (like the git commit hash)
  54. `,
  55. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter apply\":"),
  56. color.New(color.FgGreen, color.Bold).Sprintf("porter apply -f porter.yaml"),
  57. ),
  58. Run: func(cmd *cobra.Command, args []string) {
  59. err := checkLoginAndRun(args, apply)
  60. if err != nil {
  61. os.Exit(1)
  62. }
  63. },
  64. }
  65. var porterYAML string
  66. func init() {
  67. rootCmd.AddCommand(applyCmd)
  68. applyCmd.Flags().StringVarP(&porterYAML, "file", "f", "", "path to porter.yaml")
  69. applyCmd.MarkFlagRequired("file")
  70. }
  71. func apply(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
  72. fileBytes, err := ioutil.ReadFile(porterYAML)
  73. if err != nil {
  74. return fmt.Errorf("error reading porter.yaml: %w", err)
  75. }
  76. validationErrors := previewInt.Validate(string(fileBytes))
  77. if len(validationErrors) > 0 {
  78. errString := "the following error(s) were found while validating the porter.yaml file:"
  79. for _, err := range validationErrors {
  80. errString += "\n- " + strings.ReplaceAll(err.Error(), "\n\n*", "\n *")
  81. }
  82. return fmt.Errorf(errString)
  83. }
  84. resGroup, err := parser.ParseRawBytes(fileBytes)
  85. if err != nil {
  86. return fmt.Errorf("error parsing porter.yaml: %w", err)
  87. }
  88. basePath, err := os.Getwd()
  89. if err != nil {
  90. return fmt.Errorf("error getting working directory: %w", err)
  91. }
  92. worker := switchboardWorker.NewWorker()
  93. worker.RegisterDriver("deploy", NewDeployDriver)
  94. worker.RegisterDriver("build-image", preview.NewBuildDriver)
  95. worker.RegisterDriver("push-image", preview.NewPushDriver)
  96. worker.RegisterDriver("update-config", preview.NewUpdateConfigDriver)
  97. worker.RegisterDriver("random-string", preview.NewRandomStringDriver)
  98. worker.RegisterDriver("env-group", preview.NewEnvGroupDriver)
  99. worker.RegisterDriver("os-env", preview.NewOSEnvDriver)
  100. worker.SetDefaultDriver("deploy")
  101. if hasDeploymentHookEnvVars() {
  102. deplNamespace := os.Getenv("PORTER_NAMESPACE")
  103. if deplNamespace == "" {
  104. return fmt.Errorf("namespace must be set by PORTER_NAMESPACE")
  105. }
  106. deploymentHook, err := NewDeploymentHook(client, resGroup, deplNamespace)
  107. if err != nil {
  108. return fmt.Errorf("error creating deployment hook: %w", err)
  109. }
  110. worker.RegisterHook("deployment", deploymentHook)
  111. }
  112. cloneEnvGroupHook := NewCloneEnvGroupHook(client, resGroup)
  113. worker.RegisterHook("cloneenvgroup", cloneEnvGroupHook)
  114. return worker.Apply(resGroup, &switchboardTypes.ApplyOpts{
  115. BasePath: basePath,
  116. })
  117. }
  118. func hasDeploymentHookEnvVars() bool {
  119. if ghIDStr := os.Getenv("PORTER_GIT_INSTALLATION_ID"); ghIDStr == "" {
  120. return false
  121. }
  122. if prIDStr := os.Getenv("PORTER_PULL_REQUEST_ID"); prIDStr == "" {
  123. return false
  124. }
  125. if branchFrom := os.Getenv("PORTER_BRANCH_FROM"); branchFrom == "" {
  126. return false
  127. }
  128. if branchInto := os.Getenv("PORTER_BRANCH_INTO"); branchInto == "" {
  129. return false
  130. }
  131. if actionIDStr := os.Getenv("PORTER_ACTION_ID"); actionIDStr == "" {
  132. return false
  133. }
  134. if repoName := os.Getenv("PORTER_REPO_NAME"); repoName == "" {
  135. return false
  136. }
  137. if repoOwner := os.Getenv("PORTER_REPO_OWNER"); repoOwner == "" {
  138. return false
  139. }
  140. if prName := os.Getenv("PORTER_PR_NAME"); prName == "" {
  141. return false
  142. }
  143. return true
  144. }
  145. type DeployDriver struct {
  146. source *previewInt.Source
  147. target *previewInt.Target
  148. output map[string]interface{}
  149. lookupTable *map[string]drivers.Driver
  150. logger *zerolog.Logger
  151. }
  152. func NewDeployDriver(resource *models.Resource, opts *drivers.SharedDriverOpts) (drivers.Driver, error) {
  153. driver := &DeployDriver{
  154. lookupTable: opts.DriverLookupTable,
  155. logger: opts.Logger,
  156. output: make(map[string]interface{}),
  157. }
  158. source, err := preview.GetSource(resource.Name, resource.Source)
  159. if err != nil {
  160. return nil, err
  161. }
  162. driver.source = source
  163. target, err := preview.GetTarget(resource.Name, resource.Target)
  164. if err != nil {
  165. return nil, err
  166. }
  167. driver.target = target
  168. return driver, nil
  169. }
  170. func (d *DeployDriver) ShouldApply(_ *models.Resource) bool {
  171. return true
  172. }
  173. func (d *DeployDriver) Apply(resource *models.Resource) (*models.Resource, error) {
  174. client := config.GetAPIClient()
  175. name := resource.Name
  176. if name == "" {
  177. return nil, fmt.Errorf("empty resource name")
  178. }
  179. _, err := client.GetRelease(
  180. context.Background(),
  181. d.target.Project,
  182. d.target.Cluster,
  183. d.target.Namespace,
  184. resource.Name,
  185. )
  186. shouldCreate := err != nil
  187. if err != nil {
  188. color.New(color.FgYellow).Printf("Could not read release %s/%s (%s): attempting creation\n", d.target.Namespace, resource.Name, err.Error())
  189. }
  190. if d.source.IsApplication {
  191. return d.applyApplication(resource, client, shouldCreate)
  192. }
  193. return d.applyAddon(resource, client, shouldCreate)
  194. }
  195. // Simple apply for addons
  196. func (d *DeployDriver) applyAddon(resource *models.Resource, client *api.Client, shouldCreate bool) (*models.Resource, error) {
  197. addonConfig, err := d.getAddonConfig(resource)
  198. if err != nil {
  199. return nil, fmt.Errorf("error getting addon config for resource %s: %w", resource.Name, err)
  200. }
  201. if shouldCreate {
  202. err := client.DeployAddon(
  203. context.Background(),
  204. d.target.Project,
  205. d.target.Cluster,
  206. d.target.Namespace,
  207. &types.CreateAddonRequest{
  208. CreateReleaseBaseRequest: &types.CreateReleaseBaseRequest{
  209. RepoURL: d.source.Repo,
  210. TemplateName: d.source.Name,
  211. TemplateVersion: d.source.Version,
  212. Values: addonConfig,
  213. Name: resource.Name,
  214. },
  215. },
  216. )
  217. if err != nil {
  218. return nil, fmt.Errorf("error creating addon from resource %s: %w", resource.Name, err)
  219. }
  220. } else {
  221. bytes, err := json.Marshal(addonConfig)
  222. if err != nil {
  223. return nil, fmt.Errorf("error marshalling addon config from resource %s: %w", resource.Name, err)
  224. }
  225. err = client.UpgradeRelease(
  226. context.Background(),
  227. d.target.Project,
  228. d.target.Cluster,
  229. d.target.Namespace,
  230. resource.Name,
  231. &types.UpgradeReleaseRequest{
  232. Values: string(bytes),
  233. },
  234. )
  235. if err != nil {
  236. return nil, fmt.Errorf("error updating addon from resource %s: %w", resource.Name, err)
  237. }
  238. }
  239. if err = d.assignOutput(resource, client); err != nil {
  240. return nil, err
  241. }
  242. return resource, nil
  243. }
  244. func (d *DeployDriver) applyApplication(resource *models.Resource, client *api.Client, shouldCreate bool) (*models.Resource, error) {
  245. if resource == nil {
  246. return nil, fmt.Errorf("nil resource")
  247. }
  248. resourceName := resource.Name
  249. appConfig, err := d.getApplicationConfig(resource)
  250. if err != nil {
  251. return nil, err
  252. }
  253. method := appConfig.Build.Method
  254. if method != "pack" && method != "docker" && method != "registry" {
  255. return nil, fmt.Errorf("for resource %s, config.build.method should either be \"docker\", \"pack\" or \"registry\"",
  256. resourceName)
  257. }
  258. fullPath, err := filepath.Abs(appConfig.Build.Context)
  259. if err != nil {
  260. return nil, fmt.Errorf("for resource %s, error getting absolute path for config.build.context: %w", resourceName,
  261. err)
  262. }
  263. tag := os.Getenv("PORTER_TAG")
  264. if tag == "" {
  265. color.New(color.FgYellow).Printf("for resource %s, since PORTER_TAG is not set, the Docker image tag will default to"+
  266. " the git repo SHA", resourceName)
  267. commit, err := git.LastCommit()
  268. if err != nil {
  269. return nil, fmt.Errorf("for resource %s, error getting last git commit: %w", resourceName, err)
  270. }
  271. tag = commit.Sha[:7]
  272. color.New(color.FgYellow).Printf("for resource %s, using tag %s\n", resourceName, tag)
  273. }
  274. // if the method is registry and a tag is defined, we use the provided tag
  275. if appConfig.Build.Method == "registry" {
  276. imageSpl := strings.Split(appConfig.Build.Image, ":")
  277. if len(imageSpl) == 2 {
  278. tag = imageSpl[1]
  279. }
  280. if tag == "" {
  281. tag = "latest"
  282. }
  283. }
  284. sharedOpts := &deploy.SharedOpts{
  285. ProjectID: d.target.Project,
  286. ClusterID: d.target.Cluster,
  287. Namespace: d.target.Namespace,
  288. LocalPath: fullPath,
  289. LocalDockerfile: appConfig.Build.Dockerfile,
  290. OverrideTag: tag,
  291. Method: deploy.DeployBuildType(method),
  292. EnvGroups: appConfig.EnvGroups,
  293. UseCache: appConfig.Build.UseCache,
  294. }
  295. if appConfig.Build.UseCache {
  296. // set the docker config so that pack caching can use the repo credentials
  297. err := config.SetDockerConfig(client)
  298. if err != nil {
  299. return nil, err
  300. }
  301. }
  302. if shouldCreate {
  303. resource, err = d.createApplication(resource, client, sharedOpts, appConfig)
  304. if err != nil {
  305. return nil, fmt.Errorf("error creating app from resource %s: %w", resourceName, err)
  306. }
  307. } else if !appConfig.OnlyCreate {
  308. resource, err = d.updateApplication(resource, client, sharedOpts, appConfig)
  309. if err != nil {
  310. return nil, fmt.Errorf("error updating application from resource %s: %w", resourceName, err)
  311. }
  312. } else {
  313. color.New(color.FgYellow).Printf("Skipping creation for resource %s as onlyCreate is set to true\n", resourceName)
  314. }
  315. if err = d.assignOutput(resource, client); err != nil {
  316. return nil, err
  317. }
  318. if d.source.Name == "job" && appConfig.WaitForJob && (shouldCreate || !appConfig.OnlyCreate) {
  319. color.New(color.FgYellow).Printf("Waiting for job '%s' to finish\n", resourceName)
  320. err = wait.WaitForJob(client, &wait.WaitOpts{
  321. ProjectID: d.target.Project,
  322. ClusterID: d.target.Cluster,
  323. Namespace: d.target.Namespace,
  324. Name: resourceName,
  325. })
  326. if err != nil && appConfig.OnlyCreate {
  327. deleteJobErr := client.DeleteRelease(
  328. context.Background(),
  329. d.target.Project,
  330. d.target.Cluster,
  331. d.target.Namespace,
  332. resourceName,
  333. )
  334. if deleteJobErr != nil {
  335. return nil, fmt.Errorf("error deleting job %s with waitForJob and onlyCreate set to true: %w",
  336. resourceName, deleteJobErr)
  337. }
  338. } else if err != nil {
  339. return nil, fmt.Errorf("error waiting for job %s: %w", resourceName, err)
  340. }
  341. }
  342. return resource, err
  343. }
  344. func (d *DeployDriver) createApplication(resource *models.Resource, client *api.Client, sharedOpts *deploy.SharedOpts, appConf *previewInt.ApplicationConfig) (*models.Resource, error) {
  345. // create new release
  346. color.New(color.FgGreen).Printf("Creating %s release: %s\n", d.source.Name, resource.Name)
  347. regList, err := client.ListRegistries(context.Background(), d.target.Project)
  348. if err != nil {
  349. return nil, fmt.Errorf("for resource %s, error listing registries: %w", resource.Name, err)
  350. }
  351. var registryURL string
  352. if len(*regList) == 0 {
  353. return nil, fmt.Errorf("no registry found")
  354. } else {
  355. registryURL = (*regList)[0].URL
  356. }
  357. color.New(color.FgBlue).Printf("for resource %s, using registry %s\n", resource.Name, registryURL)
  358. // attempt to get repo suffix from environment variables
  359. var repoSuffix string
  360. if repoName := os.Getenv("PORTER_REPO_NAME"); repoName != "" {
  361. if repoOwner := os.Getenv("PORTER_REPO_OWNER"); repoOwner != "" {
  362. repoSuffix = strings.ToLower(strings.ReplaceAll(fmt.Sprintf("%s-%s", repoOwner, repoName), "_", "-"))
  363. }
  364. }
  365. createAgent := &deploy.CreateAgent{
  366. Client: client,
  367. CreateOpts: &deploy.CreateOpts{
  368. SharedOpts: sharedOpts,
  369. Kind: d.source.Name,
  370. ReleaseName: resource.Name,
  371. RegistryURL: registryURL,
  372. RepoSuffix: repoSuffix,
  373. },
  374. }
  375. var buildConfig *types.BuildConfig
  376. if appConf.Build.Builder != "" {
  377. buildConfig = &types.BuildConfig{
  378. Builder: appConf.Build.Builder,
  379. Buildpacks: appConf.Build.Buildpacks,
  380. }
  381. }
  382. var subdomain string
  383. if appConf.Build.Method == "registry" {
  384. subdomain, err = createAgent.CreateFromRegistry(appConf.Build.Image, appConf.Values)
  385. } else {
  386. // if useCache is set, create the image repository first
  387. if appConf.Build.UseCache {
  388. regID, imageURL, err := createAgent.GetImageRepoURL(resource.Name, sharedOpts.Namespace)
  389. if err != nil {
  390. return nil, err
  391. }
  392. err = client.CreateRepository(
  393. context.Background(),
  394. sharedOpts.ProjectID,
  395. regID,
  396. &types.CreateRegistryRepositoryRequest{
  397. ImageRepoURI: imageURL,
  398. },
  399. )
  400. if err != nil {
  401. return nil, err
  402. }
  403. }
  404. subdomain, err = createAgent.CreateFromDocker(appConf.Values, sharedOpts.OverrideTag, buildConfig)
  405. }
  406. if err != nil {
  407. return nil, err
  408. }
  409. return resource, handleSubdomainCreate(subdomain, err)
  410. }
  411. func (d *DeployDriver) updateApplication(resource *models.Resource, client *api.Client, sharedOpts *deploy.SharedOpts, appConf *previewInt.ApplicationConfig) (*models.Resource, error) {
  412. color.New(color.FgGreen).Println("Updating existing release:", resource.Name)
  413. if len(appConf.Build.Env) > 0 {
  414. sharedOpts.AdditionalEnv = appConf.Build.Env
  415. }
  416. updateAgent, err := deploy.NewDeployAgent(client, resource.Name, &deploy.DeployOpts{
  417. SharedOpts: sharedOpts,
  418. Local: appConf.Build.Method != "registry",
  419. })
  420. if err != nil {
  421. return nil, err
  422. }
  423. // if the build method is registry, we do not trigger a build
  424. if appConf.Build.Method != "registry" {
  425. buildEnv, err := updateAgent.GetBuildEnv(&deploy.GetBuildEnvOpts{
  426. UseNewConfig: true,
  427. NewConfig: appConf.Values,
  428. })
  429. if err != nil {
  430. return nil, err
  431. }
  432. err = updateAgent.SetBuildEnv(buildEnv)
  433. if err != nil {
  434. return nil, err
  435. }
  436. var buildConfig *types.BuildConfig
  437. if appConf.Build.Builder != "" {
  438. buildConfig = &types.BuildConfig{
  439. Builder: appConf.Build.Builder,
  440. Buildpacks: appConf.Build.Buildpacks,
  441. }
  442. }
  443. err = updateAgent.Build(buildConfig)
  444. if err != nil {
  445. return nil, err
  446. }
  447. if !appConf.Build.UseCache {
  448. err = updateAgent.Push()
  449. if err != nil {
  450. return nil, err
  451. }
  452. }
  453. }
  454. err = updateAgent.UpdateImageAndValues(appConf.Values)
  455. if err != nil {
  456. return nil, err
  457. }
  458. return resource, nil
  459. }
  460. func (d *DeployDriver) assignOutput(resource *models.Resource, client *api.Client) error {
  461. release, err := client.GetRelease(
  462. context.Background(),
  463. d.target.Project,
  464. d.target.Cluster,
  465. d.target.Namespace,
  466. resource.Name,
  467. )
  468. if err != nil {
  469. return err
  470. }
  471. d.output = utils.CoalesceValues(d.source.SourceValues, release.Config)
  472. return nil
  473. }
  474. func (d *DeployDriver) Output() (map[string]interface{}, error) {
  475. return d.output, nil
  476. }
  477. func (d *DeployDriver) getApplicationConfig(resource *models.Resource) (*previewInt.ApplicationConfig, error) {
  478. populatedConf, err := drivers.ConstructConfig(&drivers.ConstructConfigOpts{
  479. RawConf: resource.Config,
  480. LookupTable: *d.lookupTable,
  481. Dependencies: resource.Dependencies,
  482. })
  483. if err != nil {
  484. return nil, err
  485. }
  486. appConf := &previewInt.ApplicationConfig{}
  487. err = mapstructure.Decode(populatedConf, appConf)
  488. if err != nil {
  489. return nil, err
  490. }
  491. if _, ok := resource.Config["waitForJob"]; !ok && d.source.Name == "job" {
  492. // default to true and wait for the job to finish
  493. appConf.WaitForJob = true
  494. }
  495. return appConf, nil
  496. }
  497. func (d *DeployDriver) getAddonConfig(resource *models.Resource) (map[string]interface{}, error) {
  498. return drivers.ConstructConfig(&drivers.ConstructConfigOpts{
  499. RawConf: resource.Config,
  500. LookupTable: *d.lookupTable,
  501. Dependencies: resource.Dependencies,
  502. })
  503. }
  504. type DeploymentHook struct {
  505. client *api.Client
  506. resourceGroup *switchboardTypes.ResourceGroup
  507. gitInstallationID, projectID, clusterID, prID, actionID, envID uint
  508. branchFrom, branchInto, namespace, repoName, repoOwner, prName, commitSHA string
  509. }
  510. func NewDeploymentHook(client *api.Client, resourceGroup *switchboardTypes.ResourceGroup, namespace string) (*DeploymentHook, error) {
  511. res := &DeploymentHook{
  512. client: client,
  513. resourceGroup: resourceGroup,
  514. namespace: namespace,
  515. }
  516. ghIDStr := os.Getenv("PORTER_GIT_INSTALLATION_ID")
  517. ghID, err := strconv.Atoi(ghIDStr)
  518. if err != nil {
  519. return nil, err
  520. }
  521. res.gitInstallationID = uint(ghID)
  522. prIDStr := os.Getenv("PORTER_PULL_REQUEST_ID")
  523. prID, err := strconv.Atoi(prIDStr)
  524. if err != nil {
  525. return nil, err
  526. }
  527. res.prID = uint(prID)
  528. res.projectID = cliConf.Project
  529. if res.projectID == 0 {
  530. return nil, fmt.Errorf("project id must be set")
  531. }
  532. res.clusterID = cliConf.Cluster
  533. if res.clusterID == 0 {
  534. return nil, fmt.Errorf("cluster id must be set")
  535. }
  536. branchFrom := os.Getenv("PORTER_BRANCH_FROM")
  537. res.branchFrom = branchFrom
  538. branchInto := os.Getenv("PORTER_BRANCH_INTO")
  539. res.branchInto = branchInto
  540. actionIDStr := os.Getenv("PORTER_ACTION_ID")
  541. actionID, err := strconv.Atoi(actionIDStr)
  542. if err != nil {
  543. return nil, err
  544. }
  545. res.actionID = uint(actionID)
  546. repoName := os.Getenv("PORTER_REPO_NAME")
  547. res.repoName = repoName
  548. repoOwner := os.Getenv("PORTER_REPO_OWNER")
  549. res.repoOwner = repoOwner
  550. prName := os.Getenv("PORTER_PR_NAME")
  551. res.prName = prName
  552. commit, err := git.LastCommit()
  553. if err != nil {
  554. return nil, fmt.Errorf(err.Error())
  555. }
  556. res.commitSHA = commit.Sha[:7]
  557. return res, nil
  558. }
  559. func (t *DeploymentHook) PreApply() error {
  560. envList, err := t.client.ListEnvironments(
  561. context.Background(), t.projectID, t.clusterID,
  562. )
  563. if err != nil {
  564. return err
  565. }
  566. envs := *envList
  567. for _, env := range envs {
  568. if env.GitRepoOwner == t.repoOwner && env.GitRepoName == t.repoName && env.GitInstallationID == t.gitInstallationID {
  569. t.envID = env.ID
  570. break
  571. }
  572. }
  573. if t.envID == 0 {
  574. return fmt.Errorf("could not find environment for deployment")
  575. }
  576. // attempt to read the deployment -- if it doesn't exist, create it
  577. _, err = t.client.GetDeployment(
  578. context.Background(),
  579. t.projectID, t.clusterID, t.envID,
  580. &types.GetDeploymentRequest{
  581. Namespace: t.namespace,
  582. },
  583. )
  584. if err != nil && strings.Contains(err.Error(), "not found") {
  585. // in this case, create the deployment
  586. _, err = t.client.CreateDeployment(
  587. context.Background(),
  588. t.projectID, t.gitInstallationID, t.clusterID,
  589. t.repoOwner, t.repoName,
  590. &types.CreateDeploymentRequest{
  591. Namespace: t.namespace,
  592. PullRequestID: t.prID,
  593. CreateGHDeploymentRequest: &types.CreateGHDeploymentRequest{
  594. ActionID: t.actionID,
  595. },
  596. GitHubMetadata: &types.GitHubMetadata{
  597. PRName: t.prName,
  598. RepoName: t.repoName,
  599. RepoOwner: t.repoOwner,
  600. CommitSHA: t.commitSHA,
  601. PRBranchFrom: t.branchFrom,
  602. PRBranchInto: t.branchInto,
  603. },
  604. },
  605. )
  606. } else if err == nil {
  607. _, err = t.client.UpdateDeployment(
  608. context.Background(),
  609. t.projectID, t.gitInstallationID, t.clusterID,
  610. t.repoOwner, t.repoName,
  611. &types.UpdateDeploymentRequest{
  612. Namespace: t.namespace,
  613. CreateGHDeploymentRequest: &types.CreateGHDeploymentRequest{
  614. ActionID: t.actionID,
  615. },
  616. PRBranchFrom: t.branchFrom,
  617. CommitSHA: t.commitSHA,
  618. },
  619. )
  620. }
  621. return err
  622. }
  623. func (t *DeploymentHook) DataQueries() map[string]interface{} {
  624. res := make(map[string]interface{})
  625. // use the resource group to find all web applications that can have an exposed subdomain
  626. // that we can query for
  627. for _, resource := range t.resourceGroup.Resources {
  628. isWeb := false
  629. if sourceNameInter, exists := resource.Source["name"]; exists {
  630. if sourceName, ok := sourceNameInter.(string); ok {
  631. if sourceName == "web" {
  632. isWeb = true
  633. }
  634. }
  635. }
  636. if isWeb {
  637. // determine if we should query for porter_hosts or just hosts
  638. isCustomDomain := false
  639. ingressMap, err := deploy.GetNestedMap(resource.Config, "values", "ingress")
  640. if err == nil {
  641. enabledVal, enabledExists := ingressMap["enabled"]
  642. customDomVal, customDomExists := ingressMap["custom_domain"]
  643. if enabledExists && customDomExists {
  644. enabled, eOK := enabledVal.(bool)
  645. customDomain, cOK := customDomVal.(bool)
  646. if eOK && cOK && enabled {
  647. if customDomain {
  648. // return the first custom domain when one exists
  649. hostsArr, hostsExists := ingressMap["hosts"]
  650. if hostsExists {
  651. hostsArrVal, hostsArrOk := hostsArr.([]interface{})
  652. if hostsArrOk && len(hostsArrVal) > 0 {
  653. if _, ok := hostsArrVal[0].(string); ok {
  654. res[resource.Name] = fmt.Sprintf("{ .%s.ingress.hosts[0] }", resource.Name)
  655. isCustomDomain = true
  656. }
  657. }
  658. }
  659. }
  660. }
  661. }
  662. }
  663. if !isCustomDomain {
  664. res[resource.Name] = fmt.Sprintf("{ .%s.ingress.porter_hosts[0] }", resource.Name)
  665. }
  666. }
  667. }
  668. return res
  669. }
  670. func (t *DeploymentHook) PostApply(populatedData map[string]interface{}) error {
  671. subdomains := make([]string, 0)
  672. for _, data := range populatedData {
  673. domain, ok := data.(string)
  674. if !ok {
  675. continue
  676. }
  677. if _, err := url.Parse("https://" + domain); err == nil {
  678. subdomains = append(subdomains, "https://"+domain)
  679. }
  680. }
  681. req := &types.FinalizeDeploymentRequest{
  682. Namespace: t.namespace,
  683. Subdomain: strings.Join(subdomains, ", "),
  684. }
  685. for _, res := range t.resourceGroup.Resources {
  686. releaseType := getReleaseType(res)
  687. releaseName := getReleaseName(res)
  688. if releaseType != "" && releaseName != "" {
  689. req.SuccessfulResources = append(req.SuccessfulResources, &types.SuccessfullyDeployedResource{
  690. ReleaseName: releaseName,
  691. ReleaseType: releaseType,
  692. })
  693. }
  694. }
  695. // finalize the deployment
  696. _, err := t.client.FinalizeDeployment(
  697. context.Background(),
  698. t.projectID, t.gitInstallationID, t.clusterID,
  699. t.repoOwner, t.repoName, req,
  700. )
  701. return err
  702. }
  703. func (t *DeploymentHook) OnError(err error) {
  704. // if the deployment exists, throw an error for that deployment
  705. _, getDeplErr := t.client.GetDeployment(
  706. context.Background(),
  707. t.projectID, t.clusterID, t.envID,
  708. &types.GetDeploymentRequest{
  709. Namespace: t.namespace,
  710. },
  711. )
  712. if getDeplErr == nil {
  713. _, err = t.client.UpdateDeploymentStatus(
  714. context.Background(),
  715. t.projectID, t.gitInstallationID, t.clusterID,
  716. t.repoOwner, t.repoName,
  717. &types.UpdateDeploymentStatusRequest{
  718. Namespace: t.namespace,
  719. CreateGHDeploymentRequest: &types.CreateGHDeploymentRequest{
  720. ActionID: t.actionID,
  721. },
  722. PRBranchFrom: t.branchFrom,
  723. Status: string(types.DeploymentStatusFailed),
  724. },
  725. )
  726. }
  727. }
  728. func (t *DeploymentHook) OnConsolidatedErrors(allErrors map[string]error) {
  729. // if the deployment exists, throw an error for that deployment
  730. _, getDeplErr := t.client.GetDeployment(
  731. context.Background(),
  732. t.projectID, t.clusterID, t.envID,
  733. &types.GetDeploymentRequest{
  734. Namespace: t.namespace,
  735. },
  736. )
  737. if getDeplErr == nil {
  738. req := &types.FinalizeDeploymentWithErrorsRequest{
  739. Namespace: t.namespace,
  740. Errors: make(map[string]string),
  741. }
  742. for _, res := range t.resourceGroup.Resources {
  743. if _, ok := allErrors[res.Name]; !ok {
  744. req.SuccessfulResources = append(req.SuccessfulResources, &types.SuccessfullyDeployedResource{
  745. ReleaseName: getReleaseName(res),
  746. ReleaseType: getReleaseType(res),
  747. })
  748. }
  749. }
  750. for res, err := range allErrors {
  751. req.Errors[res] = err.Error()
  752. }
  753. // FIXME: handle the error
  754. t.client.FinalizeDeploymentWithErrors(
  755. context.Background(),
  756. t.projectID, t.gitInstallationID, t.clusterID,
  757. t.repoOwner, t.repoName,
  758. req,
  759. )
  760. }
  761. }
  762. type CloneEnvGroupHook struct {
  763. client *api.Client
  764. resGroup *switchboardTypes.ResourceGroup
  765. }
  766. func NewCloneEnvGroupHook(client *api.Client, resourceGroup *switchboardTypes.ResourceGroup) *CloneEnvGroupHook {
  767. return &CloneEnvGroupHook{
  768. client: client,
  769. resGroup: resourceGroup,
  770. }
  771. }
  772. func (t *CloneEnvGroupHook) PreApply() error {
  773. for _, res := range t.resGroup.Resources {
  774. if res.Driver == "env-group" {
  775. continue
  776. }
  777. appConf := &previewInt.ApplicationConfig{}
  778. err := mapstructure.Decode(res.Config, &appConf)
  779. if err != nil {
  780. continue
  781. }
  782. if appConf != nil && len(appConf.EnvGroups) > 0 {
  783. target, err := preview.GetTarget(res.Name, res.Target)
  784. if err != nil {
  785. return err
  786. }
  787. for _, group := range appConf.EnvGroups {
  788. if group.Name == "" {
  789. return fmt.Errorf("env group name cannot be empty")
  790. }
  791. _, err := t.client.GetEnvGroup(
  792. context.Background(),
  793. target.Project,
  794. target.Cluster,
  795. target.Namespace,
  796. &types.GetEnvGroupRequest{
  797. Name: group.Name,
  798. Version: group.Version,
  799. },
  800. )
  801. if err != nil && err.Error() == "env group not found" {
  802. if group.Namespace == "" {
  803. return fmt.Errorf("env group namespace cannot be empty")
  804. }
  805. color.New(color.FgBlue, color.Bold).
  806. Printf("Env group '%s' does not exist in the target namespace '%s'\n", group.Name, target.Namespace)
  807. color.New(color.FgBlue, color.Bold).
  808. Printf("Cloning env group '%s' from namespace '%s' to target namespace '%s'\n",
  809. group.Name, group.Namespace, target.Namespace)
  810. _, err = t.client.CloneEnvGroup(
  811. context.Background(), target.Project, target.Cluster, group.Namespace,
  812. &types.CloneEnvGroupRequest{
  813. Name: group.Name,
  814. Namespace: target.Namespace,
  815. },
  816. )
  817. if err != nil {
  818. return err
  819. }
  820. } else if err != nil {
  821. return err
  822. }
  823. }
  824. }
  825. }
  826. return nil
  827. }
  828. func (t *CloneEnvGroupHook) DataQueries() map[string]interface{} {
  829. return nil
  830. }
  831. func (t *CloneEnvGroupHook) PostApply(map[string]interface{}) error {
  832. return nil
  833. }
  834. func (t *CloneEnvGroupHook) OnError(error) {}
  835. func (t *CloneEnvGroupHook) OnConsolidatedErrors(map[string]error) {}
  836. func getReleaseName(res *switchboardTypes.Resource) string {
  837. // can ignore the error because this method is called once
  838. // GetTarget has alrealy been called and validated previously
  839. target, _ := preview.GetTarget(res.Name, res.Target)
  840. if target.AppName != "" {
  841. return target.AppName
  842. }
  843. return res.Name
  844. }
  845. func getReleaseType(res *switchboardTypes.Resource) string {
  846. // can ignore the error because this method is called once
  847. // GetSource has alrealy been called and validated previously
  848. source, _ := preview.GetSource(res.Name, res.Source)
  849. if source != nil && source.Name != "" {
  850. return source.Name
  851. }
  852. return ""
  853. }