apply.go 28 KB

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