apply.go 27 KB

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