apply.go 28 KB

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